ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/jtreg/util/Collection/IteratorMicroBenchmark.java
Revision: 1.48
Committed: Wed Jan 2 16:39:09 2019 UTC (5 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.47: +49 -25 lines
Log Message:
add support for immutable collection types; add List.of

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 List.of(al.toArray(new Integer[0])),
308 makeSubList(new Vector<>(al)),
309 new CopyOnWriteArrayList<>(al),
310 makeSubList(new CopyOnWriteArrayList<>(al)),
311 new ConcurrentLinkedQueue<>(al),
312 new ConcurrentLinkedDeque<>(al),
313 new LinkedBlockingQueue<>(al),
314 new LinkedBlockingDeque<>(al),
315 new LinkedTransferQueue<>(al),
316 new PriorityBlockingQueue<>(al))
317 .flatMap(x -> jobs(x))
318 .filter(job ->
319 nameFilter == null || nameFilter.matcher(job.name()).find())
320 .collect(toCollection(ArrayList::new));
321
322 if (reverse) Collections.reverse(jobs);
323 if (shuffle) Collections.shuffle(jobs);
324
325 time(jobs);
326 }
327
328 @SafeVarargs @SuppressWarnings("varargs")
329 private static <T> Stream<T> concatStreams(Stream<T> ... streams) {
330 return Stream.of(streams).flatMap(s -> s);
331 }
332
333 boolean isMutable(Collection<Integer> x) {
334 return !goodClassName(x.getClass()).equals("ListN");
335 }
336
337 Stream<Job> jobs(Collection<Integer> x) {
338 final String klazz = goodClassName(x.getClass());
339 return concatStreams(
340 collectionJobs(x),
341
342 (isMutable(x))
343 ? mutableCollectionJobs(x)
344 : Stream.empty(),
345
346 (x instanceof Deque)
347 ? dequeJobs((Deque<Integer>)x)
348 : Stream.empty(),
349
350 (x instanceof List)
351 ? listJobs((List<Integer>)x)
352 : Stream.empty(),
353
354 (x instanceof List && isMutable(x))
355 ? mutableListJobs((List<Integer>)x)
356 : Stream.empty());
357 }
358
359 Object sneakyAdder(int[] sneakySum) {
360 return new Object() {
361 public int hashCode() { throw new AssertionError(); }
362 public boolean equals(Object z) {
363 sneakySum[0] += (int) z; return false; }};
364 }
365
366 Stream<Job> collectionJobs(Collection<Integer> x) {
367 final String klazz = goodClassName(x.getClass());
368 return Stream.of(
369 new Job(klazz + " iterate for loop") {
370 public void work() throws Throwable {
371 for (int i = 0; i < iterations; i++) {
372 int sum = 0;
373 for (Integer n : x)
374 sum += n;
375 check.sum(sum);}}},
376 new Job(klazz + " iterator().forEachRemaining()") {
377 public void work() throws Throwable {
378 int[] sum = new int[1];
379 for (int i = 0; i < iterations; i++) {
380 sum[0] = 0;
381 x.iterator().forEachRemaining(n -> sum[0] += n);
382 check.sum(sum[0]);}}},
383 new Job(klazz + " spliterator().tryAdvance()") {
384 public void work() throws Throwable {
385 int[] sum = new int[1];
386 for (int i = 0; i < iterations; i++) {
387 sum[0] = 0;
388 Spliterator<Integer> spliterator = x.spliterator();
389 do {} while (spliterator.tryAdvance(n -> sum[0] += n));
390 check.sum(sum[0]);}}},
391 new Job(klazz + " spliterator().forEachRemaining()") {
392 public void work() throws Throwable {
393 int[] sum = new int[1];
394 for (int i = 0; i < iterations; i++) {
395 sum[0] = 0;
396 x.spliterator().forEachRemaining(n -> sum[0] += n);
397 check.sum(sum[0]);}}},
398 new Job(klazz + " contains") {
399 public void work() throws Throwable {
400 int[] sum = new int[1];
401 Object sneakyAdder = sneakyAdder(sum);
402 for (int i = 0; i < iterations; i++) {
403 sum[0] = 0;
404 if (x.contains(sneakyAdder)) throw new AssertionError();
405 check.sum(sum[0]);}}},
406 new Job(klazz + " containsAll") {
407 public void work() throws Throwable {
408 int[] sum = new int[1];
409 Collection<Object> sneakyAdderCollection =
410 Collections.singleton(sneakyAdder(sum));
411 for (int i = 0; i < iterations; i++) {
412 sum[0] = 0;
413 if (x.containsAll(sneakyAdderCollection))
414 throw new AssertionError();
415 check.sum(sum[0]);}}},
416 new Job(klazz + " forEach") {
417 public void work() throws Throwable {
418 int[] sum = new int[1];
419 for (int i = 0; i < iterations; i++) {
420 sum[0] = 0;
421 x.forEach(n -> sum[0] += n);
422 check.sum(sum[0]);}}},
423 new Job(klazz + " toArray()") {
424 public void work() throws Throwable {
425 int[] sum = new int[1];
426 for (int i = 0; i < iterations; i++) {
427 sum[0] = 0;
428 for (Object o : x.toArray())
429 sum[0] += (Integer) o;
430 check.sum(sum[0]);}}},
431 new Job(klazz + " toArray(a)") {
432 public void work() throws Throwable {
433 Integer[] a = new Integer[x.size()];
434 int[] sum = new int[1];
435 for (int i = 0; i < iterations; i++) {
436 sum[0] = 0;
437 x.toArray(a);
438 for (Object o : a)
439 sum[0] += (Integer) o;
440 check.sum(sum[0]);}}},
441 new Job(klazz + " toArray(empty)") {
442 public void work() throws Throwable {
443 Integer[] empty = new Integer[0];
444 int[] sum = new int[1];
445 for (int i = 0; i < iterations; i++) {
446 sum[0] = 0;
447 for (Integer o : x.toArray(empty))
448 sum[0] += o;
449 check.sum(sum[0]);}}},
450 new Job(klazz + " stream().forEach") {
451 public void work() throws Throwable {
452 int[] sum = new int[1];
453 for (int i = 0; i < iterations; i++) {
454 sum[0] = 0;
455 x.stream().forEach(n -> sum[0] += n);
456 check.sum(sum[0]);}}},
457 new Job(klazz + " stream().mapToInt") {
458 public void work() throws Throwable {
459 for (int i = 0; i < iterations; i++) {
460 check.sum(x.stream().mapToInt(e -> e).sum());}}},
461 new Job(klazz + " stream().collect") {
462 public void work() throws Throwable {
463 for (int i = 0; i < iterations; i++) {
464 check.sum(x.stream()
465 .collect(summingInt(e -> e)));}}},
466 new Job(klazz + " stream()::iterator") {
467 public void work() throws Throwable {
468 int[] sum = new int[1];
469 for (int i = 0; i < iterations; i++) {
470 sum[0] = 0;
471 for (Integer o : (Iterable<Integer>) x.stream()::iterator)
472 sum[0] += o;
473 check.sum(sum[0]);}}},
474 new Job(klazz + " parallelStream().forEach") {
475 public void work() throws Throwable {
476 for (int i = 0; i < iterations; i++) {
477 LongAdder sum = new LongAdder();
478 x.parallelStream().forEach(n -> sum.add(n));
479 check.sum((int) sum.sum());}}},
480 new Job(klazz + " parallelStream().mapToInt") {
481 public void work() throws Throwable {
482 for (int i = 0; i < iterations; i++) {
483 check.sum(x.parallelStream().mapToInt(e -> e).sum());}}},
484 new Job(klazz + " parallelStream().collect") {
485 public void work() throws Throwable {
486 for (int i = 0; i < iterations; i++) {
487 check.sum(x.parallelStream()
488 .collect(summingInt(e -> e)));}}},
489 new Job(klazz + " parallelStream()::iterator") {
490 public void work() throws Throwable {
491 int[] sum = new int[1];
492 for (int i = 0; i < iterations; i++) {
493 sum[0] = 0;
494 for (Integer o : (Iterable<Integer>) x.parallelStream()::iterator)
495 sum[0] += o;
496 check.sum(sum[0]);}}});
497 }
498
499 Stream<Job> mutableCollectionJobs(Collection<Integer> x) {
500 final String klazz = goodClassName(x.getClass());
501 return Stream.of(
502 new Job(klazz + " removeIf") {
503 public void work() throws Throwable {
504 int[] sum = new int[1];
505 for (int i = 0; i < iterations; i++) {
506 sum[0] = 0;
507 if (x.removeIf(n -> { sum[0] += n; return false; }))
508 throw new AssertionError();
509 check.sum(sum[0]);}}},
510 new Job(klazz + " remove(Object)") {
511 public void work() throws Throwable {
512 int[] sum = new int[1];
513 Object sneakyAdder = sneakyAdder(sum);
514 for (int i = 0; i < iterations; i++) {
515 sum[0] = 0;
516 if (x.remove(sneakyAdder)) throw new AssertionError();
517 check.sum(sum[0]);}}});
518 }
519
520 Stream<Job> dequeJobs(Deque<Integer> x) {
521 String klazz = goodClassName(x.getClass());
522 return Stream.of(
523 new Job(klazz + " descendingIterator() loop") {
524 public void work() throws Throwable {
525 for (int i = 0; i < iterations; i++) {
526 int sum = 0;
527 Iterator<Integer> it = x.descendingIterator();
528 while (it.hasNext())
529 sum += it.next();
530 check.sum(sum);}}},
531 new Job(klazz + " descendingIterator().forEachRemaining()") {
532 public void work() throws Throwable {
533 int[] sum = new int[1];
534 for (int i = 0; i < iterations; i++) {
535 sum[0] = 0;
536 x.descendingIterator().forEachRemaining(n -> sum[0] += n);
537 check.sum(sum[0]);}}});
538 }
539
540 Stream<Job> listJobs(List<Integer> x) {
541 final String klazz = goodClassName(x.getClass());
542 return Stream.of(
543 new Job(klazz + " listIterator forward loop") {
544 public void work() throws Throwable {
545 for (int i = 0; i < iterations; i++) {
546 int sum = 0;
547 ListIterator<Integer> it = x.listIterator();
548 while (it.hasNext())
549 sum += it.next();
550 check.sum(sum);}}},
551 new Job(klazz + " listIterator backward loop") {
552 public void work() throws Throwable {
553 for (int i = 0; i < iterations; i++) {
554 int sum = 0;
555 ListIterator<Integer> it = x.listIterator(x.size());
556 while (it.hasPrevious())
557 sum += it.previous();
558 check.sum(sum);}}},
559 new Job(klazz + " indexOf") {
560 public void work() throws Throwable {
561 int[] sum = new int[1];
562 Object sneakyAdder = sneakyAdder(sum);
563 for (int i = 0; i < iterations; i++) {
564 sum[0] = 0;
565 if (x.indexOf(sneakyAdder) != -1)
566 throw new AssertionError();
567 check.sum(sum[0]);}}},
568 new Job(klazz + " lastIndexOf") {
569 public void work() throws Throwable {
570 int[] sum = new int[1];
571 Object sneakyAdder = sneakyAdder(sum);
572 for (int i = 0; i < iterations; i++) {
573 sum[0] = 0;
574 if (x.lastIndexOf(sneakyAdder) != -1)
575 throw new AssertionError();
576 check.sum(sum[0]);}}},
577 new Job(klazz + " equals") {
578 public void work() throws Throwable {
579 ArrayList<Integer> copy = new ArrayList<>(x);
580 for (int i = 0; i < iterations; i++) {
581 if (!x.equals(copy))
582 throw new AssertionError();}}},
583 new Job(klazz + " hashCode") {
584 public void work() throws Throwable {
585 int hashCode = Arrays.hashCode(x.toArray());
586 for (int i = 0; i < iterations; i++) {
587 if (x.hashCode() != hashCode)
588 throw new AssertionError();}}});
589 }
590
591 Stream<Job> mutableListJobs(List<Integer> x) {
592 final String klazz = goodClassName(x.getClass());
593 return Stream.of(
594 new Job(klazz + " replaceAll") {
595 public void work() throws Throwable {
596 int[] sum = new int[1];
597 UnaryOperator<Integer> sneakyAdder =
598 x -> { sum[0] += x; return x; };
599 for (int i = 0; i < iterations; i++) {
600 sum[0] = 0;
601 x.replaceAll(sneakyAdder);
602 check.sum(sum[0]);}}});
603 }
604 }