ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/jtreg/util/Collection/IteratorMicroBenchmark.java
Revision: 1.44
Committed: Wed May 9 17:43:01 2018 UTC (6 years ago) by jsr166
Branch: MAIN
Changes since 1.43: +13 -7 lines
Log Message:
forceFullGc: avoid rare failures due to back-to-back GCs, seen only at Google

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