ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/jtreg/util/Collection/IteratorMicroBenchmark.java
Revision: 1.45
Committed: Fri May 11 18:19:10 2018 UTC (6 years ago) by jsr166
Branch: MAIN
Changes since 1.44: +8 -1 lines
Log Message:
add Job for hashCode

File Contents

# Content
1 /*
2 * Copyright (c) 2007, 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
24 /*
25 * @test
26 * @summary micro-benchmark correctness mode
27 * @run main IteratorMicroBenchmark iterations=1 size=8 warmup=0
28 */
29
30 import static java.util.concurrent.TimeUnit.MILLISECONDS;
31 import static java.util.stream.Collectors.summingInt;
32 import static java.util.stream.Collectors.toCollection;
33
34 import java.lang.ref.ReferenceQueue;
35 import java.lang.ref.WeakReference;
36 import java.util.ArrayDeque;
37 import java.util.Arrays;
38 import java.util.ArrayList;
39 import java.util.Collection;
40 import java.util.Collections;
41 import java.util.Deque;
42 import java.util.HashMap;
43 import java.util.Iterator;
44 import java.util.LinkedList;
45 import java.util.List;
46 import java.util.ListIterator;
47 import java.util.PriorityQueue;
48 import java.util.Spliterator;
49 import java.util.Vector;
50 import java.util.concurrent.ArrayBlockingQueue;
51 import java.util.concurrent.ConcurrentLinkedDeque;
52 import java.util.concurrent.ConcurrentLinkedQueue;
53 import java.util.concurrent.CopyOnWriteArrayList;
54 import java.util.concurrent.LinkedBlockingDeque;
55 import java.util.concurrent.LinkedBlockingQueue;
56 import java.util.concurrent.LinkedTransferQueue;
57 import java.util.concurrent.PriorityBlockingQueue;
58 import java.util.concurrent.CountDownLatch;
59 import java.util.concurrent.ThreadLocalRandom;
60 import java.util.concurrent.atomic.LongAdder;
61 import java.util.function.UnaryOperator;
62 import java.util.regex.Pattern;
63 import java.util.stream.Stream;
64
65 /**
66 * Usage: [iterations=N] [size=N] [filter=REGEXP] [warmup=SECONDS]
67 *
68 * To run this in micro-benchmark mode, simply run as a normal java program.
69 * Be patient; this program runs for a very long time.
70 * For faster runs, restrict execution using command line args.
71 *
72 * This is an interface based version of ArrayList/IteratorMicroBenchmark
73 *
74 * @author Martin Buchholz
75 */
76 public class IteratorMicroBenchmark {
77 abstract static class Job {
78 private final String name;
79 public Job(String name) { this.name = name; }
80 public String name() { return name; }
81 public abstract void work() throws Throwable;
82 public void run() {
83 try { work(); }
84 catch (Throwable ex) {
85 // current job cannot always be deduced from stacktrace.
86 throw new RuntimeException("Job failed: " + name(), ex);
87 }
88 }
89 }
90
91 final int iterations;
92 final int size; // number of elements in collections
93 final double warmupSeconds;
94 final long warmupNanos;
95 final Pattern nameFilter; // select subset of Jobs to run
96 final boolean reverse; // reverse order of Jobs
97 final boolean shuffle; // randomize order of Jobs
98
99 IteratorMicroBenchmark(String[] args) {
100 iterations = intArg(args, "iterations", 10_000);
101 size = intArg(args, "size", 1000);
102 warmupSeconds = doubleArg(args, "warmup", 7.0);
103 nameFilter = patternArg(args, "filter");
104 reverse = booleanArg(args, "reverse");
105 shuffle = booleanArg(args, "shuffle");
106
107 warmupNanos = (long) (warmupSeconds * (1000L * 1000L * 1000L));
108 }
109
110 // --------------- GC finalization infrastructure ---------------
111
112 /** No guarantees, but effective in practice. */
113 static void forceFullGc() {
114 long timeoutMillis = 1000L;
115 CountDownLatch finalized = new CountDownLatch(1);
116 ReferenceQueue<Object> queue = new ReferenceQueue<>();
117 WeakReference<Object> ref = new WeakReference<>(
118 new Object() { protected void finalize() { finalized.countDown(); }},
119 queue);
120 try {
121 for (int tries = 3; tries--> 0; ) {
122 System.gc();
123 if (finalized.await(timeoutMillis, MILLISECONDS)
124 && queue.remove(timeoutMillis) != null
125 && ref.get() == null) {
126 System.runFinalization(); // try to pick up stragglers
127 return;
128 }
129 timeoutMillis *= 4;
130 }
131 } catch (InterruptedException unexpected) {
132 throw new AssertionError("unexpected InterruptedException");
133 }
134 throw new AssertionError("failed to do a \"full\" gc");
135 }
136
137 /**
138 * Runs each job for long enough that all the runtime compilers
139 * have had plenty of time to warm up, i.e. get around to
140 * compiling everything worth compiling.
141 * Returns array of average times per job per run.
142 */
143 long[] time0(List<Job> jobs) {
144 final int size = jobs.size();
145 long[] nanoss = new long[size];
146 for (int i = 0; i < size; i++) {
147 if (warmupNanos > 0) forceFullGc();
148 Job job = jobs.get(i);
149 long totalTime;
150 int runs = 0;
151 long startTime = System.nanoTime();
152 do { job.run(); runs++; }
153 while ((totalTime = System.nanoTime() - startTime) < warmupNanos);
154 nanoss[i] = totalTime/runs;
155 }
156 return nanoss;
157 }
158
159 void time(List<Job> jobs) throws Throwable {
160 if (warmupNanos > 0) time0(jobs); // Warm up run
161 final int size = jobs.size();
162 final long[] nanoss = time0(jobs); // Real timing run
163 final long[] milliss = new long[size];
164 final double[] ratios = new double[size];
165
166 final String nameHeader = "Method";
167 final String millisHeader = "Millis";
168 final String ratioHeader = "Ratio";
169
170 int nameWidth = nameHeader.length();
171 int millisWidth = millisHeader.length();
172 int ratioWidth = ratioHeader.length();
173
174 for (int i = 0; i < size; i++) {
175 nameWidth = Math.max(nameWidth, jobs.get(i).name().length());
176
177 milliss[i] = nanoss[i]/(1000L * 1000L);
178 millisWidth = Math.max(millisWidth,
179 String.format("%d", milliss[i]).length());
180
181 ratios[i] = (double) nanoss[i] / (double) nanoss[0];
182 ratioWidth = Math.max(ratioWidth,
183 String.format("%.3f", ratios[i]).length());
184 }
185
186 String format = String.format("%%-%ds %%%dd %%%d.3f%%n",
187 nameWidth, millisWidth, ratioWidth);
188 String headerFormat = String.format("%%-%ds %%%ds %%%ds%%n",
189 nameWidth, millisWidth, ratioWidth);
190 System.out.printf(headerFormat, "Method", "Millis", "Ratio");
191
192 // Print out absolute and relative times, calibrated against first job
193 for (int i = 0; i < size; i++)
194 System.out.printf(format, jobs.get(i).name(), milliss[i], ratios[i]);
195 }
196
197 private static String keywordValue(String[] args, String keyword) {
198 for (String arg : args)
199 if (arg.startsWith(keyword))
200 return arg.substring(keyword.length() + 1);
201 return null;
202 }
203
204 private static int intArg(String[] args, String keyword, int defaultValue) {
205 String val = keywordValue(args, keyword);
206 return (val == null) ? defaultValue : Integer.parseInt(val);
207 }
208
209 private static double doubleArg(String[] args, String keyword, double defaultValue) {
210 String val = keywordValue(args, keyword);
211 return (val == null) ? defaultValue : Double.parseDouble(val);
212 }
213
214 private static Pattern patternArg(String[] args, String keyword) {
215 String val = keywordValue(args, keyword);
216 return (val == null) ? null : Pattern.compile(val);
217 }
218
219 private static boolean booleanArg(String[] args, String keyword) {
220 String val = keywordValue(args, keyword);
221 if (val == null || val.equals("false")) return false;
222 if (val.equals("true")) return true;
223 throw new IllegalArgumentException(val);
224 }
225
226 private static void deoptimize(int sum) {
227 if (sum == 42)
228 System.out.println("the answer");
229 }
230
231 private static <T> Iterable<T> backwards(final List<T> list) {
232 return new Iterable<T>() {
233 public Iterator<T> iterator() {
234 return new Iterator<T>() {
235 final ListIterator<T> it = list.listIterator(list.size());
236 public boolean hasNext() { return it.hasPrevious(); }
237 public T next() { return it.previous(); }
238 public void remove() { it.remove(); }};}};
239 }
240
241 // Checks for correctness *and* prevents loop optimizations
242 static class Check {
243 private int sum;
244 public void sum(int sum) {
245 if (this.sum == 0)
246 this.sum = sum;
247 if (this.sum != sum)
248 throw new AssertionError("Sum mismatch");
249 }
250 }
251 volatile Check check = new Check();
252
253 public static void main(String[] args) throws Throwable {
254 new IteratorMicroBenchmark(args).run();
255 }
256
257 HashMap<Class<?>, String> goodClassName = new HashMap<>();
258
259 String goodClassName(Class<?> klazz) {
260 return goodClassName.computeIfAbsent(
261 klazz,
262 k -> {
263 String simple = k.getSimpleName();
264 return (simple.equals("SubList")) // too simple!
265 ? k.getName().replaceFirst(".*\\.", "")
266 : simple;
267 });
268 }
269
270 static List<Integer> makeSubList(List<Integer> list) {
271 final ThreadLocalRandom rnd = ThreadLocalRandom.current();
272 int size = list.size();
273 if (size <= 2) return list.subList(0, size);
274 List<Integer> subList = list.subList(rnd.nextInt(0, 2),
275 size - rnd.nextInt(0, 2));
276 List<Integer> copy = new ArrayList<>(list);
277 subList.clear();
278 subList.addAll(copy);
279 return subList;
280 }
281
282 void run() throws Throwable {
283 final ArrayList<Integer> al = new ArrayList<>(size);
284
285 // Populate collections with random data
286 final ThreadLocalRandom rnd = ThreadLocalRandom.current();
287 for (int i = 0; i < size; i++)
288 al.add(rnd.nextInt(size));
289
290 final ArrayDeque<Integer> ad = new ArrayDeque<>(al);
291 final ArrayBlockingQueue<Integer> abq = new ArrayBlockingQueue<>(al.size());
292 abq.addAll(al);
293
294 // shuffle circular array elements so they wrap
295 for (int i = 0, n = rnd.nextInt(size); i < n; i++) {
296 ad.addLast(ad.removeFirst());
297 abq.add(abq.remove());
298 }
299
300 ArrayList<Job> jobs = Stream.<Collection<Integer>>of(
301 al, ad, abq,
302 makeSubList(new ArrayList<>(al)),
303 new LinkedList<>(al),
304 makeSubList(new LinkedList<>(al)),
305 new PriorityQueue<>(al),
306 new Vector<>(al),
307 makeSubList(new Vector<>(al)),
308 new CopyOnWriteArrayList<>(al),
309 makeSubList(new CopyOnWriteArrayList<>(al)),
310 new ConcurrentLinkedQueue<>(al),
311 new ConcurrentLinkedDeque<>(al),
312 new LinkedBlockingQueue<>(al),
313 new LinkedBlockingDeque<>(al),
314 new LinkedTransferQueue<>(al),
315 new PriorityBlockingQueue<>(al))
316 .flatMap(x -> jobs(x))
317 .filter(job ->
318 nameFilter == null || nameFilter.matcher(job.name()).find())
319 .collect(toCollection(ArrayList::new));
320
321 if (reverse) Collections.reverse(jobs);
322 if (shuffle) Collections.shuffle(jobs);
323
324 time(jobs);
325 }
326
327 @SafeVarargs @SuppressWarnings("varargs")
328 private <T> Stream<T> concatStreams(Stream<T> ... streams) {
329 return Stream.of(streams).flatMap(s -> s);
330 }
331
332 Stream<Job> jobs(Collection<Integer> x) {
333 return concatStreams(
334 collectionJobs(x),
335
336 (x instanceof Deque)
337 ? dequeJobs((Deque<Integer>)x)
338 : Stream.empty(),
339
340 (x instanceof List)
341 ? listJobs((List<Integer>)x)
342 : Stream.empty());
343 }
344
345 Object sneakyAdder(int[] sneakySum) {
346 return new Object() {
347 public int hashCode() { throw new AssertionError(); }
348 public boolean equals(Object z) {
349 sneakySum[0] += (int) z; return false; }};
350 }
351
352 Stream<Job> collectionJobs(Collection<Integer> x) {
353 final String klazz = goodClassName(x.getClass());
354 return Stream.of(
355 new Job(klazz + " iterate for loop") {
356 public void work() throws Throwable {
357 for (int i = 0; i < iterations; i++) {
358 int sum = 0;
359 for (Integer n : x)
360 sum += n;
361 check.sum(sum);}}},
362 new Job(klazz + " iterator().forEachRemaining()") {
363 public void work() throws Throwable {
364 int[] sum = new int[1];
365 for (int i = 0; i < iterations; i++) {
366 sum[0] = 0;
367 x.iterator().forEachRemaining(n -> sum[0] += n);
368 check.sum(sum[0]);}}},
369 new Job(klazz + " spliterator().tryAdvance()") {
370 public void work() throws Throwable {
371 int[] sum = new int[1];
372 for (int i = 0; i < iterations; i++) {
373 sum[0] = 0;
374 Spliterator<Integer> spliterator = x.spliterator();
375 do {} while (spliterator.tryAdvance(n -> sum[0] += n));
376 check.sum(sum[0]);}}},
377 new Job(klazz + " spliterator().forEachRemaining()") {
378 public void work() throws Throwable {
379 int[] sum = new int[1];
380 for (int i = 0; i < iterations; i++) {
381 sum[0] = 0;
382 x.spliterator().forEachRemaining(n -> sum[0] += n);
383 check.sum(sum[0]);}}},
384 new Job(klazz + " removeIf") {
385 public void work() throws Throwable {
386 int[] sum = new int[1];
387 for (int i = 0; i < iterations; i++) {
388 sum[0] = 0;
389 if (x.removeIf(n -> { sum[0] += n; return false; }))
390 throw new AssertionError();
391 check.sum(sum[0]);}}},
392 new Job(klazz + " contains") {
393 public void work() throws Throwable {
394 int[] sum = new int[1];
395 Object sneakyAdder = sneakyAdder(sum);
396 for (int i = 0; i < iterations; i++) {
397 sum[0] = 0;
398 if (x.contains(sneakyAdder)) throw new AssertionError();
399 check.sum(sum[0]);}}},
400 new Job(klazz + " containsAll") {
401 public void work() throws Throwable {
402 int[] sum = new int[1];
403 Collection<Object> sneakyAdderCollection =
404 Collections.singleton(sneakyAdder(sum));
405 for (int i = 0; i < iterations; i++) {
406 sum[0] = 0;
407 if (x.containsAll(sneakyAdderCollection))
408 throw new AssertionError();
409 check.sum(sum[0]);}}},
410 new Job(klazz + " remove(Object)") {
411 public void work() throws Throwable {
412 int[] sum = new int[1];
413 Object sneakyAdder = sneakyAdder(sum);
414 for (int i = 0; i < iterations; i++) {
415 sum[0] = 0;
416 if (x.remove(sneakyAdder)) throw new AssertionError();
417 check.sum(sum[0]);}}},
418 new Job(klazz + " forEach") {
419 public void work() throws Throwable {
420 int[] sum = new int[1];
421 for (int i = 0; i < iterations; i++) {
422 sum[0] = 0;
423 x.forEach(n -> sum[0] += n);
424 check.sum(sum[0]);}}},
425 new Job(klazz + " toArray()") {
426 public void work() throws Throwable {
427 int[] sum = new int[1];
428 for (int i = 0; i < iterations; i++) {
429 sum[0] = 0;
430 for (Object o : x.toArray())
431 sum[0] += (Integer) o;
432 check.sum(sum[0]);}}},
433 new Job(klazz + " toArray(a)") {
434 public void work() throws Throwable {
435 Integer[] a = new Integer[x.size()];
436 int[] sum = new int[1];
437 for (int i = 0; i < iterations; i++) {
438 sum[0] = 0;
439 x.toArray(a);
440 for (Object o : a)
441 sum[0] += (Integer) o;
442 check.sum(sum[0]);}}},
443 new Job(klazz + " toArray(empty)") {
444 public void work() throws Throwable {
445 Integer[] empty = new Integer[0];
446 int[] sum = new int[1];
447 for (int i = 0; i < iterations; i++) {
448 sum[0] = 0;
449 for (Integer o : x.toArray(empty))
450 sum[0] += o;
451 check.sum(sum[0]);}}},
452 new Job(klazz + " stream().forEach") {
453 public void work() throws Throwable {
454 int[] sum = new int[1];
455 for (int i = 0; i < iterations; i++) {
456 sum[0] = 0;
457 x.stream().forEach(n -> sum[0] += n);
458 check.sum(sum[0]);}}},
459 new Job(klazz + " stream().mapToInt") {
460 public void work() throws Throwable {
461 for (int i = 0; i < iterations; i++) {
462 check.sum(x.stream().mapToInt(e -> e).sum());}}},
463 new Job(klazz + " stream().collect") {
464 public void work() throws Throwable {
465 for (int i = 0; i < iterations; i++) {
466 check.sum(x.stream()
467 .collect(summingInt(e -> e)));}}},
468 new Job(klazz + " stream()::iterator") {
469 public void work() throws Throwable {
470 int[] sum = new int[1];
471 for (int i = 0; i < iterations; i++) {
472 sum[0] = 0;
473 for (Integer o : (Iterable<Integer>) x.stream()::iterator)
474 sum[0] += o;
475 check.sum(sum[0]);}}},
476 new Job(klazz + " parallelStream().forEach") {
477 public void work() throws Throwable {
478 for (int i = 0; i < iterations; i++) {
479 LongAdder sum = new LongAdder();
480 x.parallelStream().forEach(n -> sum.add(n));
481 check.sum((int) sum.sum());}}},
482 new Job(klazz + " parallelStream().mapToInt") {
483 public void work() throws Throwable {
484 for (int i = 0; i < iterations; i++) {
485 check.sum(x.parallelStream().mapToInt(e -> e).sum());}}},
486 new Job(klazz + " parallelStream().collect") {
487 public void work() throws Throwable {
488 for (int i = 0; i < iterations; i++) {
489 check.sum(x.parallelStream()
490 .collect(summingInt(e -> e)));}}},
491 new Job(klazz + " parallelStream()::iterator") {
492 public void work() throws Throwable {
493 int[] sum = new int[1];
494 for (int i = 0; i < iterations; i++) {
495 sum[0] = 0;
496 for (Integer o : (Iterable<Integer>) x.parallelStream()::iterator)
497 sum[0] += o;
498 check.sum(sum[0]);}}});
499 }
500
501 Stream<Job> dequeJobs(Deque<Integer> x) {
502 String klazz = goodClassName(x.getClass());
503 return Stream.of(
504 new Job(klazz + " descendingIterator() loop") {
505 public void work() throws Throwable {
506 for (int i = 0; i < iterations; i++) {
507 int sum = 0;
508 Iterator<Integer> it = x.descendingIterator();
509 while (it.hasNext())
510 sum += it.next();
511 check.sum(sum);}}},
512 new Job(klazz + " descendingIterator().forEachRemaining()") {
513 public void work() throws Throwable {
514 int[] sum = new int[1];
515 for (int i = 0; i < iterations; i++) {
516 sum[0] = 0;
517 x.descendingIterator().forEachRemaining(n -> sum[0] += n);
518 check.sum(sum[0]);}}});
519 }
520
521 Stream<Job> listJobs(List<Integer> x) {
522 final String klazz = goodClassName(x.getClass());
523 return Stream.of(
524 new Job(klazz + " listIterator forward loop") {
525 public void work() throws Throwable {
526 for (int i = 0; i < iterations; i++) {
527 int sum = 0;
528 ListIterator<Integer> it = x.listIterator();
529 while (it.hasNext())
530 sum += it.next();
531 check.sum(sum);}}},
532 new Job(klazz + " listIterator backward loop") {
533 public void work() throws Throwable {
534 for (int i = 0; i < iterations; i++) {
535 int sum = 0;
536 ListIterator<Integer> it = x.listIterator(x.size());
537 while (it.hasPrevious())
538 sum += it.previous();
539 check.sum(sum);}}},
540 new Job(klazz + " indexOf") {
541 public void work() throws Throwable {
542 int[] sum = new int[1];
543 Object sneakyAdder = sneakyAdder(sum);
544 for (int i = 0; i < iterations; i++) {
545 sum[0] = 0;
546 if (x.indexOf(sneakyAdder) != -1)
547 throw new AssertionError();
548 check.sum(sum[0]);}}},
549 new Job(klazz + " lastIndexOf") {
550 public void work() throws Throwable {
551 int[] sum = new int[1];
552 Object sneakyAdder = sneakyAdder(sum);
553 for (int i = 0; i < iterations; i++) {
554 sum[0] = 0;
555 if (x.lastIndexOf(sneakyAdder) != -1)
556 throw new AssertionError();
557 check.sum(sum[0]);}}},
558 new Job(klazz + " replaceAll") {
559 public void work() throws Throwable {
560 int[] sum = new int[1];
561 UnaryOperator<Integer> sneakyAdder =
562 x -> { sum[0] += x; return x; };
563 for (int i = 0; i < iterations; i++) {
564 sum[0] = 0;
565 x.replaceAll(sneakyAdder);
566 check.sum(sum[0]);}}},
567 new Job(klazz + " hashCode") {
568 public void work() throws Throwable {
569 int hashCode = Arrays.hashCode(x.toArray());
570 for (int i = 0; i < iterations; i++) {
571 if (x.hashCode() != hashCode)
572 throw new AssertionError();}}});
573 }
574 }