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