author | dl |
Tue, 22 May 2018 21:50:45 -0700 | |
changeset 50229 | 6b29ef846c5c |
parent 49564 | 260bf39376a4 |
child 50764 | 5637aca18f1d |
permissions | -rw-r--r-- |
42319 | 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 |
||
50229
6b29ef846c5c
8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents:
49564
diff
changeset
|
30 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
42319 | 31 |
import static java.util.stream.Collectors.summingInt; |
48686 | 32 |
import static java.util.stream.Collectors.toCollection; |
42319 | 33 |
|
50229
6b29ef846c5c
8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents:
49564
diff
changeset
|
34 |
import java.lang.ref.ReferenceQueue; |
42319 | 35 |
import java.lang.ref.WeakReference; |
36 |
import java.util.ArrayDeque; |
|
50229
6b29ef846c5c
8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents:
49564
diff
changeset
|
37 |
import java.util.Arrays; |
42319 | 38 |
import java.util.ArrayList; |
39 |
import java.util.Collection; |
|
40 |
import java.util.Collections; |
|
41 |
import java.util.Deque; |
|
49564 | 42 |
import java.util.HashMap; |
42319 | 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; |
|
48686 | 53 |
import java.util.concurrent.CopyOnWriteArrayList; |
42319 | 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; |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
60 |
import java.util.concurrent.atomic.LongAdder; |
49564 | 61 |
import java.util.function.UnaryOperator; |
42319 | 62 |
import java.util.regex.Pattern; |
48686 | 63 |
import java.util.stream.Stream; |
42319 | 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; |
|
49564 | 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 |
} |
|
42319 | 89 |
} |
90 |
||
91 |
final int iterations; |
|
92 |
final int size; // number of elements in collections |
|
93 |
final double warmupSeconds; |
|
94 |
final long warmupNanos; |
|
48686 | 95 |
final Pattern nameFilter; // select subset of Jobs to run |
42319 | 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); |
|
48686 | 103 |
nameFilter = patternArg(args, "filter"); |
42319 | 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() { |
|
50229
6b29ef846c5c
8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents:
49564
diff
changeset
|
114 |
long timeoutMillis = 1000L; |
6b29ef846c5c
8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents:
49564
diff
changeset
|
115 |
CountDownLatch finalized = new CountDownLatch(1); |
6b29ef846c5c
8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents:
49564
diff
changeset
|
116 |
ReferenceQueue<Object> queue = new ReferenceQueue<>(); |
6b29ef846c5c
8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents:
49564
diff
changeset
|
117 |
WeakReference<Object> ref = new WeakReference<>( |
6b29ef846c5c
8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents:
49564
diff
changeset
|
118 |
new Object() { protected void finalize() { finalized.countDown(); }}, |
6b29ef846c5c
8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents:
49564
diff
changeset
|
119 |
queue); |
42319 | 120 |
try { |
50229
6b29ef846c5c
8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents:
49564
diff
changeset
|
121 |
for (int tries = 3; tries--> 0; ) { |
42319 | 122 |
System.gc(); |
50229
6b29ef846c5c
8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents:
49564
diff
changeset
|
123 |
if (finalized.await(timeoutMillis, MILLISECONDS) |
6b29ef846c5c
8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents:
49564
diff
changeset
|
124 |
&& queue.remove(timeoutMillis) != null |
6b29ef846c5c
8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents:
49564
diff
changeset
|
125 |
&& ref.get() == null) { |
42319 | 126 |
System.runFinalization(); // try to pick up stragglers |
127 |
return; |
|
128 |
} |
|
50229
6b29ef846c5c
8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents:
49564
diff
changeset
|
129 |
timeoutMillis *= 4; |
42319 | 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 |
*/ |
|
49564 | 143 |
long[] time0(List<Job> jobs) { |
42319 | 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(); |
|
49564 | 152 |
do { job.run(); runs++; } |
42319 | 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 |
|
47730
c7b5b1ce8145
8189764: Miscellaneous changes imported from jsr166 CVS 2017-11
dl
parents:
47307
diff
changeset
|
242 |
static class Check { |
42319 | 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 |
||
49564 | 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 |
||
42319 | 282 |
void run() throws Throwable { |
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
283 |
final ArrayList<Integer> al = new ArrayList<>(size); |
42319 | 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 |
||
48686 | 300 |
ArrayList<Job> jobs = Stream.<Collection<Integer>>of( |
301 |
al, ad, abq, |
|
49564 | 302 |
makeSubList(new ArrayList<>(al)), |
48686 | 303 |
new LinkedList<>(al), |
49564 | 304 |
makeSubList(new LinkedList<>(al)), |
48686 | 305 |
new PriorityQueue<>(al), |
306 |
new Vector<>(al), |
|
49564 | 307 |
makeSubList(new Vector<>(al)), |
48686 | 308 |
new CopyOnWriteArrayList<>(al), |
49564 | 309 |
makeSubList(new CopyOnWriteArrayList<>(al)), |
48686 | 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)); |
|
42319 | 320 |
|
321 |
if (reverse) Collections.reverse(jobs); |
|
322 |
if (shuffle) Collections.shuffle(jobs); |
|
323 |
||
48686 | 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); |
|
42319 | 330 |
} |
331 |
||
48686 | 332 |
Stream<Job> jobs(Collection<Integer> x) { |
333 |
return concatStreams( |
|
334 |
collectionJobs(x), |
|
49564 | 335 |
|
48686 | 336 |
(x instanceof Deque) |
337 |
? dequeJobs((Deque<Integer>)x) |
|
338 |
: Stream.empty(), |
|
49564 | 339 |
|
48686 | 340 |
(x instanceof List) |
341 |
? listJobs((List<Integer>)x) |
|
342 |
: Stream.empty()); |
|
343 |
} |
|
344 |
||
49564 | 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 |
||
48686 | 352 |
Stream<Job> collectionJobs(Collection<Integer> x) { |
49564 | 353 |
final String klazz = goodClassName(x.getClass()); |
48686 | 354 |
return Stream.of( |
42319 | 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);}}}, |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
362 |
new Job(klazz + " iterator().forEachRemaining()") { |
42319 | 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]);}}}, |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
369 |
new Job(klazz + " spliterator().tryAdvance()") { |
42319 | 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]);}}}, |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
377 |
new Job(klazz + " spliterator().forEachRemaining()") { |
42319 | 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]);}}}, |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
384 |
new Job(klazz + " removeIf") { |
42319 | 385 |
public void work() throws Throwable { |
386 |
int[] sum = new int[1]; |
|
387 |
for (int i = 0; i < iterations; i++) { |
|
388 |
sum[0] = 0; |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
389 |
if (x.removeIf(n -> { sum[0] += n; return false; })) |
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
390 |
throw new AssertionError(); |
42319 | 391 |
check.sum(sum[0]);}}}, |
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
392 |
new Job(klazz + " contains") { |
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
393 |
public void work() throws Throwable { |
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
394 |
int[] sum = new int[1]; |
49564 | 395 |
Object sneakyAdder = sneakyAdder(sum); |
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
396 |
for (int i = 0; i < iterations; i++) { |
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
397 |
sum[0] = 0; |
49564 | 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(); |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
409 |
check.sum(sum[0]);}}}, |
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
410 |
new Job(klazz + " remove(Object)") { |
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
411 |
public void work() throws Throwable { |
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
412 |
int[] sum = new int[1]; |
49564 | 413 |
Object sneakyAdder = sneakyAdder(sum); |
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
414 |
for (int i = 0; i < iterations; i++) { |
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
415 |
sum[0] = 0; |
49564 | 416 |
if (x.remove(sneakyAdder)) throw new AssertionError(); |
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
417 |
check.sum(sum[0]);}}}, |
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
418 |
new Job(klazz + " forEach") { |
42319 | 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]);}}}, |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
425 |
new Job(klazz + " toArray()") { |
42319 | 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]);}}}, |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
433 |
new Job(klazz + " toArray(a)") { |
42319 | 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]);}}}, |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
443 |
new Job(klazz + " toArray(empty)") { |
42319 | 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]);}}}, |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
452 |
new Job(klazz + " stream().forEach") { |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
453 |
public void work() throws Throwable { |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
454 |
int[] sum = new int[1]; |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
455 |
for (int i = 0; i < iterations; i++) { |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
456 |
sum[0] = 0; |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
457 |
x.stream().forEach(n -> sum[0] += n); |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
458 |
check.sum(sum[0]);}}}, |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
459 |
new Job(klazz + " stream().mapToInt") { |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
460 |
public void work() throws Throwable { |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
461 |
for (int i = 0; i < iterations; i++) { |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
462 |
check.sum(x.stream().mapToInt(e -> e).sum());}}}, |
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
463 |
new Job(klazz + " stream().collect") { |
42319 | 464 |
public void work() throws Throwable { |
465 |
for (int i = 0; i < iterations; i++) { |
|
466 |
check.sum(x.stream() |
|
467 |
.collect(summingInt(e -> e)));}}}, |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
468 |
new Job(klazz + " stream()::iterator") { |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
469 |
public void work() throws Throwable { |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
470 |
int[] sum = new int[1]; |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
471 |
for (int i = 0; i < iterations; i++) { |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
472 |
sum[0] = 0; |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
473 |
for (Integer o : (Iterable<Integer>) x.stream()::iterator) |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
474 |
sum[0] += o; |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
475 |
check.sum(sum[0]);}}}, |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
476 |
new Job(klazz + " parallelStream().forEach") { |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
477 |
public void work() throws Throwable { |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
478 |
for (int i = 0; i < iterations; i++) { |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
479 |
LongAdder sum = new LongAdder(); |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
480 |
x.parallelStream().forEach(n -> sum.add(n)); |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
481 |
check.sum((int) sum.sum());}}}, |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
482 |
new Job(klazz + " parallelStream().mapToInt") { |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
483 |
public void work() throws Throwable { |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
484 |
for (int i = 0; i < iterations; i++) { |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
485 |
check.sum(x.parallelStream().mapToInt(e -> e).sum());}}}, |
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
486 |
new Job(klazz + " parallelStream().collect") { |
42319 | 487 |
public void work() throws Throwable { |
488 |
for (int i = 0; i < iterations; i++) { |
|
489 |
check.sum(x.parallelStream() |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
490 |
.collect(summingInt(e -> e)));}}}, |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
491 |
new Job(klazz + " parallelStream()::iterator") { |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
492 |
public void work() throws Throwable { |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
493 |
int[] sum = new int[1]; |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
494 |
for (int i = 0; i < iterations; i++) { |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
495 |
sum[0] = 0; |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
496 |
for (Integer o : (Iterable<Integer>) x.parallelStream()::iterator) |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
497 |
sum[0] += o; |
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
43522
diff
changeset
|
498 |
check.sum(sum[0]);}}}); |
42319 | 499 |
} |
500 |
||
48686 | 501 |
Stream<Job> dequeJobs(Deque<Integer> x) { |
49564 | 502 |
String klazz = goodClassName(x.getClass()); |
48686 | 503 |
return Stream.of( |
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
504 |
new Job(klazz + " descendingIterator() loop") { |
42319 | 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);}}}, |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42319
diff
changeset
|
512 |
new Job(klazz + " descendingIterator().forEachRemaining()") { |
42319 | 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 |
} |
|
48686 | 520 |
|
521 |
Stream<Job> listJobs(List<Integer> x) { |
|
49564 | 522 |
final String klazz = goodClassName(x.getClass()); |
48686 | 523 |
return Stream.of( |
49564 | 524 |
new Job(klazz + " listIterator forward loop") { |
48686 | 525 |
public void work() throws Throwable { |
526 |
for (int i = 0; i < iterations; i++) { |
|
49564 | 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") { |
|
48686 | 533 |
public void work() throws Throwable { |
49564 | 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); |
|
48686 | 544 |
for (int i = 0; i < iterations; i++) { |
49564 | 545 |
sum[0] = 0; |
546 |
if (x.indexOf(sneakyAdder) != -1) |
|
547 |
throw new AssertionError(); |
|
548 |
check.sum(sum[0]);}}}, |
|
549 |
new Job(klazz + " lastIndexOf") { |
|
48686 | 550 |
public void work() throws Throwable { |
49564 | 551 |
int[] sum = new int[1]; |
552 |
Object sneakyAdder = sneakyAdder(sum); |
|
48686 | 553 |
for (int i = 0; i < iterations; i++) { |
49564 | 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); |
|
50229
6b29ef846c5c
8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents:
49564
diff
changeset
|
566 |
check.sum(sum[0]);}}}, |
6b29ef846c5c
8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents:
49564
diff
changeset
|
567 |
new Job(klazz + " equals") { |
6b29ef846c5c
8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents:
49564
diff
changeset
|
568 |
public void work() throws Throwable { |
6b29ef846c5c
8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents:
49564
diff
changeset
|
569 |
ArrayList<Integer> copy = new ArrayList<>(x); |
6b29ef846c5c
8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents:
49564
diff
changeset
|
570 |
for (int i = 0; i < iterations; i++) { |
6b29ef846c5c
8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents:
49564
diff
changeset
|
571 |
if (!x.equals(copy)) |
6b29ef846c5c
8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents:
49564
diff
changeset
|
572 |
throw new AssertionError();}}}, |
6b29ef846c5c
8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents:
49564
diff
changeset
|
573 |
new Job(klazz + " hashCode") { |
6b29ef846c5c
8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents:
49564
diff
changeset
|
574 |
public void work() throws Throwable { |
6b29ef846c5c
8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents:
49564
diff
changeset
|
575 |
int hashCode = Arrays.hashCode(x.toArray()); |
6b29ef846c5c
8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents:
49564
diff
changeset
|
576 |
for (int i = 0; i < iterations; i++) { |
6b29ef846c5c
8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents:
49564
diff
changeset
|
577 |
if (x.hashCode() != hashCode) |
6b29ef846c5c
8201386: Miscellaneous changes imported from jsr166 CVS 2018-05
dl
parents:
49564
diff
changeset
|
578 |
throw new AssertionError();}}}); |
48686 | 579 |
} |
42319 | 580 |
} |