1 /*
2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 package org.openjdk.bench.java.util;
24 
25 import java.security.InvalidAlgorithmParameterException;
26 import java.security.InvalidKeyException;
27 import java.security.NoSuchAlgorithmException;
28 import java.security.spec.InvalidParameterSpecException;
29 import java.util.Arrays;
30 import java.util.concurrent.TimeUnit;
31 import javax.crypto.BadPaddingException;
32 import javax.crypto.IllegalBlockSizeException;
33 import javax.crypto.NoSuchPaddingException;
34 import org.openjdk.jmh.annotations.Benchmark;
35 import org.openjdk.jmh.annotations.BenchmarkMode;
36 import org.openjdk.jmh.annotations.Measurement;
37 import org.openjdk.jmh.annotations.Mode;
38 import org.openjdk.jmh.annotations.OutputTimeUnit;
39 import org.openjdk.jmh.annotations.Param;
40 import org.openjdk.jmh.annotations.Scope;
41 import org.openjdk.jmh.annotations.Setup;
42 import org.openjdk.jmh.annotations.State;
43 import org.openjdk.jmh.annotations.Warmup;
44 
45 @BenchmarkMode(Mode.AverageTime)
46 @OutputTimeUnit(TimeUnit.NANOSECONDS)
47 @State(Scope.Thread)
48 public abstract class ArraysMismatch {
49 
50     @Param({"90", "800"})
51     private static int size;
52 
53     static final byte fill = 99;
54     static final byte mismatch = -1;
55 
56     int leftStartRange;
57     int leftEndRange;
58     int rightStartRange;
59     int rightEndRange;
60 
61     @Setup
setup()62     public void setup() {
63         leftStartRange = size / 4;
64         leftEndRange = size - size / 4;
65         rightStartRange = size / 4 + 10;
66         rightEndRange = size - size / 4 + 10;
67         specificSetup();
68     }
69 
specificSetup()70     abstract void specificSetup();
71 
72     public static class Byte extends ArraysMismatch {
73 
74         byte[] left;
75         byte[] right_startMismatch;
76         byte[] right_midMismatch;
77         byte[] right_endMismatch;
78         byte[] right_matches;
79 
specificSetup()80         public void specificSetup() {
81             left = new byte[size];
82             Arrays.fill(left, (byte) fill);
83             right_startMismatch = Arrays.copyOf(left, left.length);
84             right_startMismatch[2] = (byte) mismatch;
85             right_midMismatch = Arrays.copyOf(left, left.length);
86             right_midMismatch[size / 2] = (byte) mismatch;
87             right_endMismatch = Arrays.copyOf(left, left.length);
88             right_endMismatch[size - 5] = (byte) mismatch;
89             right_matches = Arrays.copyOf(left, left.length);
90         }
91 
92         @Benchmark
93         @Warmup(iterations = 3)
94         @Measurement(iterations = 3)
matches()95         public int matches() {
96             return Arrays.mismatch(left, right_matches);
97         }
98 
99         @Benchmark
100         @Warmup(iterations = 3)
101         @Measurement(iterations = 3)
differentSubrangeMatches()102         public int differentSubrangeMatches() {
103             return Arrays.mismatch(left, leftStartRange, leftEndRange, right_matches, rightStartRange, rightEndRange);
104         }
105 
106         @Benchmark
107         @Warmup(iterations = 3)
108         @Measurement(iterations = 3)
mismatchEnd()109         public int mismatchEnd() {
110             return Arrays.mismatch(left, right_endMismatch);
111         }
112 
113         @Benchmark
114         @Warmup(iterations = 3)
115         @Measurement(iterations = 3)
mismatchMid()116         public int mismatchMid() {
117             return Arrays.mismatch(left, right_midMismatch);
118         }
119 
120         @Benchmark
121         @Warmup(iterations = 3)
122         @Measurement(iterations = 3)
mismatchStart()123         public int mismatchStart() {
124             return Arrays.mismatch(left, right_startMismatch);
125         }
126     }
127 
128     public static class Char extends ArraysMismatch {
129 
130         char[] left;
131         char[] right_startMismatch;
132         char[] right_midMismatch;
133         char[] right_endMismatch;
134         char[] right_matches;
135 
specificSetup()136         public void specificSetup() {
137             left = new char[size];
138             Arrays.fill(left, (char) fill);
139             right_startMismatch = Arrays.copyOf(left, left.length);
140             right_startMismatch[2] = (char) mismatch;
141             right_midMismatch = Arrays.copyOf(left, left.length);
142             right_midMismatch[size / 2] = (char) mismatch;
143             right_endMismatch = Arrays.copyOf(left, left.length);
144             right_endMismatch[size - 5] = (char) mismatch;
145             right_matches = Arrays.copyOf(left, left.length);
146         }
147 
148         @Benchmark
149         @Warmup(iterations = 3)
150         @Measurement(iterations = 3)
matches()151         public int matches() {
152             return Arrays.mismatch(left, right_matches);
153         }
154 
155         @Benchmark
156         @Warmup(iterations = 3)
157         @Measurement(iterations = 3)
differentSubrangeMatches()158         public int differentSubrangeMatches() {
159             return Arrays.mismatch(left, leftStartRange, leftEndRange, right_matches, rightStartRange, rightEndRange);
160         }
161 
162         @Benchmark
163         @Warmup(iterations = 3)
164         @Measurement(iterations = 3)
mismatchEnd()165         public int mismatchEnd() {
166             return Arrays.mismatch(left, right_endMismatch);
167         }
168 
169         @Benchmark
170         @Warmup(iterations = 3)
171         @Measurement(iterations = 3)
mismatchMid()172         public int mismatchMid() {
173             return Arrays.mismatch(left, right_midMismatch);
174         }
175 
176         @Benchmark
177         @Warmup(iterations = 3)
178         @Measurement(iterations = 3)
mismatchStart()179         public int mismatchStart() {
180             return Arrays.mismatch(left, right_startMismatch);
181         }
182     }
183 
184     public static class Short extends ArraysMismatch {
185 
186         short[] left;
187         short[] right_startMismatch;
188         short[] right_midMismatch;
189         short[] right_endMismatch;
190         short[] right_matches;
191 
specificSetup()192         public void specificSetup() {
193             left = new short[size];
194             Arrays.fill(left, (short) fill);
195             right_startMismatch = Arrays.copyOf(left, left.length);
196             right_startMismatch[2] = (short) mismatch;
197             right_midMismatch = Arrays.copyOf(left, left.length);
198             right_midMismatch[size / 2] = (short) mismatch;
199             right_endMismatch = Arrays.copyOf(left, left.length);
200             right_endMismatch[size - 5] = (short) mismatch;
201             right_matches = Arrays.copyOf(left, left.length);
202         }
203 
204         @Benchmark
205         @Warmup(iterations = 3)
206         @Measurement(iterations = 3)
matches()207         public int matches() {
208             return Arrays.mismatch(left, right_matches);
209         }
210 
211         @Benchmark
212         @Warmup(iterations = 3)
213         @Measurement(iterations = 3)
differentSubrangeMatches()214         public int differentSubrangeMatches() {
215             return Arrays.mismatch(left, leftStartRange, leftEndRange, right_matches, rightStartRange, rightEndRange);
216         }
217 
218         @Benchmark
219         @Warmup(iterations = 3)
220         @Measurement(iterations = 3)
mismatchEnd()221         public int mismatchEnd() {
222             return Arrays.mismatch(left, right_endMismatch);
223         }
224 
225         @Benchmark
226         @Warmup(iterations = 3)
227         @Measurement(iterations = 3)
mismatchMid()228         public int mismatchMid() {
229             return Arrays.mismatch(left, right_midMismatch);
230         }
231 
232         @Benchmark
233         @Warmup(iterations = 3)
234         @Measurement(iterations = 3)
mismatchStart()235         public int mismatchStart() {
236             return Arrays.mismatch(left, right_startMismatch);
237         }
238     }
239 
240     public static class Int extends ArraysMismatch {
241 
242         int[] left;
243         int[] right_startMismatch;
244         int[] right_midMismatch;
245         int[] right_endMismatch;
246         int[] right_matches;
247 
specificSetup()248         public void specificSetup() {
249             left = new int[size];
250             Arrays.fill(left, (int) fill);
251             right_startMismatch = Arrays.copyOf(left, left.length);
252             right_startMismatch[2] = (int) mismatch;
253             right_midMismatch = Arrays.copyOf(left, left.length);
254             right_midMismatch[size / 2] = (int) mismatch;
255             right_endMismatch = Arrays.copyOf(left, left.length);
256             right_endMismatch[size - 5] = (int) mismatch;
257             right_matches = Arrays.copyOf(left, left.length);
258         }
259 
260         @Benchmark
261         @Warmup(iterations = 3)
262         @Measurement(iterations = 3)
matches()263         public int matches() {
264             return Arrays.mismatch(left, right_matches);
265         }
266 
267         @Benchmark
268         @Warmup(iterations = 3)
269         @Measurement(iterations = 3)
differentSubrangeMatches()270         public int differentSubrangeMatches() {
271             return Arrays.mismatch(left, leftStartRange, leftEndRange, right_matches, rightStartRange, rightEndRange);
272         }
273 
274         @Benchmark
275         @Warmup(iterations = 3)
276         @Measurement(iterations = 3)
mismatchEnd()277         public int mismatchEnd() {
278             return Arrays.mismatch(left, right_endMismatch);
279         }
280 
281         @Benchmark
282         @Warmup(iterations = 3)
283         @Measurement(iterations = 3)
mismatchMid()284         public int mismatchMid() {
285             return Arrays.mismatch(left, right_midMismatch);
286         }
287 
288         @Benchmark
289         @Warmup(iterations = 3)
290         @Measurement(iterations = 3)
mismatchStart()291         public int mismatchStart() {
292             return Arrays.mismatch(left, right_startMismatch);
293         }
294     }
295 
296     public static class Long extends ArraysMismatch {
297 
298         long[] left;
299         long[] right_startMismatch;
300         long[] right_midMismatch;
301         long[] right_endMismatch;
302         long[] right_matches;
303 
specificSetup()304         public void specificSetup() {
305             left = new long[size];
306             Arrays.fill(left, (long) fill);
307             right_startMismatch = Arrays.copyOf(left, left.length);
308             right_startMismatch[2] = (long) mismatch;
309             right_midMismatch = Arrays.copyOf(left, left.length);
310             right_midMismatch[size / 2] = (long) mismatch;
311             right_endMismatch = Arrays.copyOf(left, left.length);
312             right_endMismatch[size - 5] = (long) mismatch;
313             right_matches = Arrays.copyOf(left, left.length);
314         }
315 
316         @Benchmark
317         @Warmup(iterations = 3)
318         @Measurement(iterations = 3)
matches()319         public int matches() {
320             return Arrays.mismatch(left, right_matches);
321         }
322 
323         @Benchmark
324         @Warmup(iterations = 3)
325         @Measurement(iterations = 3)
differentSubrangeMatches()326         public int differentSubrangeMatches() {
327             return Arrays.mismatch(left, leftStartRange, leftEndRange, right_matches, rightStartRange, rightEndRange);
328         }
329 
330         @Benchmark
331         @Warmup(iterations = 3)
332         @Measurement(iterations = 3)
mismatchEnd()333         public int mismatchEnd() {
334             return Arrays.mismatch(left, right_endMismatch);
335         }
336 
337         @Benchmark
338         @Warmup(iterations = 3)
339         @Measurement(iterations = 3)
mismatchMid()340         public int mismatchMid() {
341             return Arrays.mismatch(left, right_midMismatch);
342         }
343 
344         @Benchmark
345         @Warmup(iterations = 3)
346         @Measurement(iterations = 3)
mismatchStart()347         public int mismatchStart() {
348             return Arrays.mismatch(left, right_startMismatch);
349         }
350     }
351 
352     public static class Float extends ArraysMismatch {
353 
354         float[] left;
355         float[] right_startMismatch;
356         float[] right_midMismatch;
357         float[] right_endMismatch;
358         float[] right_matches;
359 
specificSetup()360         public void specificSetup() {
361             left = new float[size];
362             Arrays.fill(left, (float) fill);
363             right_startMismatch = Arrays.copyOf(left, left.length);
364             right_startMismatch[2] = (float) mismatch;
365             right_midMismatch = Arrays.copyOf(left, left.length);
366             right_midMismatch[size / 2] = (float) mismatch;
367             right_endMismatch = Arrays.copyOf(left, left.length);
368             right_endMismatch[size - 5] = (float) mismatch;
369             right_matches = Arrays.copyOf(left, left.length);
370         }
371 
372         @Benchmark
373         @Warmup(iterations = 3)
374         @Measurement(iterations = 3)
matches()375         public int matches() {
376             return Arrays.mismatch(left, right_matches);
377         }
378 
379         @Benchmark
380         @Warmup(iterations = 3)
381         @Measurement(iterations = 3)
differentSubrangeMatches()382         public int differentSubrangeMatches() {
383             return Arrays.mismatch(left, leftStartRange, leftEndRange, right_matches, rightStartRange, rightEndRange);
384         }
385 
386         @Benchmark
387         @Warmup(iterations = 3)
388         @Measurement(iterations = 3)
mismatchEnd()389         public int mismatchEnd() {
390             return Arrays.mismatch(left, right_endMismatch);
391         }
392 
393         @Benchmark
394         @Warmup(iterations = 3)
395         @Measurement(iterations = 3)
mismatchMid()396         public int mismatchMid() {
397             return Arrays.mismatch(left, right_midMismatch);
398         }
399 
400         @Benchmark
401         @Warmup(iterations = 3)
402         @Measurement(iterations = 3)
mismatchStart()403         public int mismatchStart() {
404             return Arrays.mismatch(left, right_startMismatch);
405         }
406     }
407 
408     public static class Double extends ArraysMismatch {
409 
410         double[] left;
411         double[] right_startMismatch;
412         double[] right_midMismatch;
413         double[] right_endMismatch;
414         double[] right_matches;
415 
specificSetup()416         public void specificSetup() {
417             left = new double[size];
418             Arrays.fill(left, (double) fill);
419             right_startMismatch = Arrays.copyOf(left, left.length);
420             right_startMismatch[2] = (double) mismatch;
421             right_midMismatch = Arrays.copyOf(left, left.length);
422             right_midMismatch[size / 2] = (double) mismatch;
423             right_endMismatch = Arrays.copyOf(left, left.length);
424             right_endMismatch[size - 5] = (double) mismatch;
425             right_matches = Arrays.copyOf(left, left.length);
426         }
427 
428         @Benchmark
429         @Warmup(iterations = 3)
430         @Measurement(iterations = 3)
matches()431         public int matches() {
432             return Arrays.mismatch(left, right_matches);
433         }
434 
435         @Benchmark
436         @Warmup(iterations = 3)
437         @Measurement(iterations = 3)
differentSubrangeMatches()438         public int differentSubrangeMatches() {
439             return Arrays.mismatch(left, leftStartRange, leftEndRange, right_matches, rightStartRange, rightEndRange);
440         }
441 
442         @Benchmark
443         @Warmup(iterations = 3)
444         @Measurement(iterations = 3)
mismatchEnd()445         public int mismatchEnd() {
446             return Arrays.mismatch(left, right_endMismatch);
447         }
448 
449         @Benchmark
450         @Warmup(iterations = 3)
451         @Measurement(iterations = 3)
mismatchMid()452         public int mismatchMid() {
453             return Arrays.mismatch(left, right_midMismatch);
454         }
455 
456         @Benchmark
457         @Warmup(iterations = 3)
458         @Measurement(iterations = 3)
mismatchStart()459         public int mismatchStart() {
460             return Arrays.mismatch(left, right_startMismatch);
461         }
462     }
463 
464 }
465