1 |
jsr166 |
1.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 |
jsr166 |
1.44 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
31 |
jsr166 |
1.12 |
import static java.util.stream.Collectors.summingInt; |
32 |
jsr166 |
1.31 |
import static java.util.stream.Collectors.toCollection; |
33 |
jsr166 |
1.12 |
|
34 |
jsr166 |
1.44 |
import java.lang.ref.ReferenceQueue; |
35 |
jsr166 |
1.1 |
import java.lang.ref.WeakReference; |
36 |
|
|
import java.util.ArrayDeque; |
37 |
jsr166 |
1.45 |
import java.util.Arrays; |
38 |
jsr166 |
1.1 |
import java.util.ArrayList; |
39 |
|
|
import java.util.Collection; |
40 |
jsr166 |
1.10 |
import java.util.Collections; |
41 |
jsr166 |
1.1 |
import java.util.Deque; |
42 |
jsr166 |
1.36 |
import java.util.HashMap; |
43 |
jsr166 |
1.1 |
import java.util.Iterator; |
44 |
jsr166 |
1.13 |
import java.util.LinkedList; |
45 |
jsr166 |
1.1 |
import java.util.List; |
46 |
|
|
import java.util.ListIterator; |
47 |
jsr166 |
1.8 |
import java.util.PriorityQueue; |
48 |
jsr166 |
1.1 |
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 |
jsr166 |
1.30 |
import java.util.concurrent.CopyOnWriteArrayList; |
54 |
jsr166 |
1.1 |
import java.util.concurrent.LinkedBlockingDeque; |
55 |
|
|
import java.util.concurrent.LinkedBlockingQueue; |
56 |
|
|
import java.util.concurrent.LinkedTransferQueue; |
57 |
jsr166 |
1.8 |
import java.util.concurrent.PriorityBlockingQueue; |
58 |
jsr166 |
1.1 |
import java.util.concurrent.CountDownLatch; |
59 |
|
|
import java.util.concurrent.ThreadLocalRandom; |
60 |
jsr166 |
1.19 |
import java.util.concurrent.atomic.LongAdder; |
61 |
jsr166 |
1.42 |
import java.util.function.UnaryOperator; |
62 |
jsr166 |
1.1 |
import java.util.regex.Pattern; |
63 |
jsr166 |
1.30 |
import java.util.stream.Stream; |
64 |
jsr166 |
1.1 |
|
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 |
jsr166 |
1.38 |
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 |
jsr166 |
1.1 |
} |
90 |
|
|
|
91 |
jsr166 |
1.11 |
final int iterations; |
92 |
|
|
final int size; // number of elements in collections |
93 |
|
|
final double warmupSeconds; |
94 |
|
|
final long warmupNanos; |
95 |
jsr166 |
1.31 |
final Pattern nameFilter; // select subset of Jobs to run |
96 |
jsr166 |
1.11 |
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 |
jsr166 |
1.31 |
nameFilter = patternArg(args, "filter"); |
104 |
jsr166 |
1.11 |
reverse = booleanArg(args, "reverse"); |
105 |
|
|
shuffle = booleanArg(args, "shuffle"); |
106 |
|
|
|
107 |
|
|
warmupNanos = (long) (warmupSeconds * (1000L * 1000L * 1000L)); |
108 |
|
|
} |
109 |
jsr166 |
1.1 |
|
110 |
|
|
// --------------- GC finalization infrastructure --------------- |
111 |
|
|
|
112 |
|
|
/** No guarantees, but effective in practice. */ |
113 |
|
|
static void forceFullGc() { |
114 |
jsr166 |
1.44 |
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 |
jsr166 |
1.1 |
try { |
121 |
jsr166 |
1.44 |
for (int tries = 3; tries--> 0; ) { |
122 |
jsr166 |
1.1 |
System.gc(); |
123 |
jsr166 |
1.44 |
if (finalized.await(timeoutMillis, MILLISECONDS) |
124 |
|
|
&& queue.remove(timeoutMillis) != null |
125 |
|
|
&& ref.get() == null) { |
126 |
jsr166 |
1.1 |
System.runFinalization(); // try to pick up stragglers |
127 |
|
|
return; |
128 |
|
|
} |
129 |
jsr166 |
1.44 |
timeoutMillis *= 4; |
130 |
jsr166 |
1.1 |
} |
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 |
jsr166 |
1.38 |
long[] time0(List<Job> jobs) { |
144 |
jsr166 |
1.1 |
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 |
jsr166 |
1.9 |
Job job = jobs.get(i); |
149 |
|
|
long totalTime; |
150 |
|
|
int runs = 0; |
151 |
|
|
long startTime = System.nanoTime(); |
152 |
jsr166 |
1.38 |
do { job.run(); runs++; } |
153 |
jsr166 |
1.9 |
while ((totalTime = System.nanoTime() - startTime) < warmupNanos); |
154 |
|
|
nanoss[i] = totalTime/runs; |
155 |
jsr166 |
1.1 |
} |
156 |
|
|
return nanoss; |
157 |
|
|
} |
158 |
|
|
|
159 |
|
|
void time(List<Job> jobs) throws Throwable { |
160 |
jsr166 |
1.9 |
if (warmupNanos > 0) time0(jobs); // Warm up run |
161 |
jsr166 |
1.1 |
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 |
jsr166 |
1.10 |
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 |
jsr166 |
1.1 |
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 |
jsr166 |
1.25 |
static class Check { |
243 |
jsr166 |
1.1 |
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 |
jsr166 |
1.2 |
volatile Check check = new Check(); |
252 |
jsr166 |
1.1 |
|
253 |
|
|
public static void main(String[] args) throws Throwable { |
254 |
jsr166 |
1.11 |
new IteratorMicroBenchmark(args).run(); |
255 |
jsr166 |
1.1 |
} |
256 |
|
|
|
257 |
jsr166 |
1.36 |
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 |
jsr166 |
1.11 |
void run() throws Throwable { |
283 |
jsr166 |
1.17 |
final ArrayList<Integer> al = new ArrayList<>(size); |
284 |
jsr166 |
1.1 |
|
285 |
|
|
// Populate collections with random data |
286 |
|
|
final ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
287 |
jsr166 |
1.3 |
for (int i = 0; i < size; i++) |
288 |
jsr166 |
1.1 |
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 |
jsr166 |
1.31 |
ArrayList<Job> jobs = Stream.<Collection<Integer>>of( |
301 |
|
|
al, ad, abq, |
302 |
jsr166 |
1.36 |
makeSubList(new ArrayList<>(al)), |
303 |
jsr166 |
1.31 |
new LinkedList<>(al), |
304 |
jsr166 |
1.36 |
makeSubList(new LinkedList<>(al)), |
305 |
jsr166 |
1.31 |
new PriorityQueue<>(al), |
306 |
|
|
new Vector<>(al), |
307 |
jsr166 |
1.36 |
makeSubList(new Vector<>(al)), |
308 |
jsr166 |
1.31 |
new CopyOnWriteArrayList<>(al), |
309 |
jsr166 |
1.36 |
makeSubList(new CopyOnWriteArrayList<>(al)), |
310 |
jsr166 |
1.31 |
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 |
jsr166 |
1.33 |
.filter(job -> |
318 |
|
|
nameFilter == null || nameFilter.matcher(job.name()).find()) |
319 |
jsr166 |
1.31 |
.collect(toCollection(ArrayList::new)); |
320 |
jsr166 |
1.1 |
|
321 |
jsr166 |
1.11 |
if (reverse) Collections.reverse(jobs); |
322 |
jsr166 |
1.10 |
if (shuffle) Collections.shuffle(jobs); |
323 |
|
|
|
324 |
jsr166 |
1.31 |
time(jobs); |
325 |
|
|
} |
326 |
|
|
|
327 |
jsr166 |
1.32 |
@SafeVarargs @SuppressWarnings("varargs") |
328 |
|
|
private <T> Stream<T> concatStreams(Stream<T> ... streams) { |
329 |
|
|
return Stream.of(streams).flatMap(s -> s); |
330 |
|
|
} |
331 |
jsr166 |
1.34 |
|
332 |
jsr166 |
1.31 |
Stream<Job> jobs(Collection<Integer> x) { |
333 |
jsr166 |
1.32 |
return concatStreams( |
334 |
|
|
collectionJobs(x), |
335 |
jsr166 |
1.40 |
|
336 |
jsr166 |
1.32 |
(x instanceof Deque) |
337 |
|
|
? dequeJobs((Deque<Integer>)x) |
338 |
|
|
: Stream.empty(), |
339 |
jsr166 |
1.40 |
|
340 |
jsr166 |
1.32 |
(x instanceof List) |
341 |
|
|
? listJobs((List<Integer>)x) |
342 |
|
|
: Stream.empty()); |
343 |
jsr166 |
1.1 |
} |
344 |
|
|
|
345 |
jsr166 |
1.36 |
Object sneakyAdder(int[] sneakySum) { |
346 |
|
|
return new Object() { |
347 |
|
|
public int hashCode() { throw new AssertionError(); } |
348 |
|
|
public boolean equals(Object z) { |
349 |
|
|
sneakySum[0] += (int) z; return false; }}; |
350 |
|
|
} |
351 |
|
|
|
352 |
jsr166 |
1.31 |
Stream<Job> collectionJobs(Collection<Integer> x) { |
353 |
jsr166 |
1.36 |
final String klazz = goodClassName(x.getClass()); |
354 |
jsr166 |
1.31 |
return Stream.of( |
355 |
jsr166 |
1.5 |
new Job(klazz + " iterate for loop") { |
356 |
jsr166 |
1.1 |
public void work() throws Throwable { |
357 |
|
|
for (int i = 0; i < iterations; i++) { |
358 |
|
|
int sum = 0; |
359 |
|
|
for (Integer n : x) |
360 |
|
|
sum += n; |
361 |
|
|
check.sum(sum);}}}, |
362 |
jsr166 |
1.18 |
new Job(klazz + " iterator().forEachRemaining()") { |
363 |
jsr166 |
1.1 |
public void work() throws Throwable { |
364 |
|
|
int[] sum = new int[1]; |
365 |
|
|
for (int i = 0; i < iterations; i++) { |
366 |
|
|
sum[0] = 0; |
367 |
|
|
x.iterator().forEachRemaining(n -> sum[0] += n); |
368 |
|
|
check.sum(sum[0]);}}}, |
369 |
jsr166 |
1.18 |
new Job(klazz + " spliterator().tryAdvance()") { |
370 |
jsr166 |
1.1 |
public void work() throws Throwable { |
371 |
|
|
int[] sum = new int[1]; |
372 |
|
|
for (int i = 0; i < iterations; i++) { |
373 |
|
|
sum[0] = 0; |
374 |
|
|
Spliterator<Integer> spliterator = x.spliterator(); |
375 |
|
|
do {} while (spliterator.tryAdvance(n -> sum[0] += n)); |
376 |
|
|
check.sum(sum[0]);}}}, |
377 |
jsr166 |
1.18 |
new Job(klazz + " spliterator().forEachRemaining()") { |
378 |
jsr166 |
1.1 |
public void work() throws Throwable { |
379 |
|
|
int[] sum = new int[1]; |
380 |
|
|
for (int i = 0; i < iterations; i++) { |
381 |
|
|
sum[0] = 0; |
382 |
|
|
x.spliterator().forEachRemaining(n -> sum[0] += n); |
383 |
|
|
check.sum(sum[0]);}}}, |
384 |
jsr166 |
1.18 |
new Job(klazz + " removeIf") { |
385 |
jsr166 |
1.1 |
public void work() throws Throwable { |
386 |
|
|
int[] sum = new int[1]; |
387 |
|
|
for (int i = 0; i < iterations; i++) { |
388 |
|
|
sum[0] = 0; |
389 |
jsr166 |
1.16 |
if (x.removeIf(n -> { sum[0] += n; return false; })) |
390 |
|
|
throw new AssertionError(); |
391 |
jsr166 |
1.1 |
check.sum(sum[0]);}}}, |
392 |
jsr166 |
1.18 |
new Job(klazz + " contains") { |
393 |
jsr166 |
1.14 |
public void work() throws Throwable { |
394 |
|
|
int[] sum = new int[1]; |
395 |
jsr166 |
1.36 |
Object sneakyAdder = sneakyAdder(sum); |
396 |
jsr166 |
1.14 |
for (int i = 0; i < iterations; i++) { |
397 |
|
|
sum[0] = 0; |
398 |
jsr166 |
1.36 |
if (x.contains(sneakyAdder)) throw new AssertionError(); |
399 |
jsr166 |
1.16 |
check.sum(sum[0]);}}}, |
400 |
jsr166 |
1.43 |
new Job(klazz + " containsAll") { |
401 |
|
|
public void work() throws Throwable { |
402 |
|
|
int[] sum = new int[1]; |
403 |
|
|
Collection<Object> sneakyAdderCollection = |
404 |
|
|
Collections.singleton(sneakyAdder(sum)); |
405 |
|
|
for (int i = 0; i < iterations; i++) { |
406 |
|
|
sum[0] = 0; |
407 |
|
|
if (x.containsAll(sneakyAdderCollection)) |
408 |
|
|
throw new AssertionError(); |
409 |
|
|
check.sum(sum[0]);}}}, |
410 |
jsr166 |
1.18 |
new Job(klazz + " remove(Object)") { |
411 |
jsr166 |
1.16 |
public void work() throws Throwable { |
412 |
|
|
int[] sum = new int[1]; |
413 |
jsr166 |
1.36 |
Object sneakyAdder = sneakyAdder(sum); |
414 |
jsr166 |
1.16 |
for (int i = 0; i < iterations; i++) { |
415 |
|
|
sum[0] = 0; |
416 |
jsr166 |
1.36 |
if (x.remove(sneakyAdder)) throw new AssertionError(); |
417 |
jsr166 |
1.14 |
check.sum(sum[0]);}}}, |
418 |
jsr166 |
1.18 |
new Job(klazz + " forEach") { |
419 |
jsr166 |
1.1 |
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 |
jsr166 |
1.6 |
check.sum(sum[0]);}}}, |
425 |
jsr166 |
1.18 |
new Job(klazz + " toArray()") { |
426 |
jsr166 |
1.6 |
public void work() throws Throwable { |
427 |
|
|
int[] sum = new int[1]; |
428 |
|
|
for (int i = 0; i < iterations; i++) { |
429 |
|
|
sum[0] = 0; |
430 |
|
|
for (Object o : x.toArray()) |
431 |
|
|
sum[0] += (Integer) o; |
432 |
|
|
check.sum(sum[0]);}}}, |
433 |
jsr166 |
1.18 |
new Job(klazz + " toArray(a)") { |
434 |
jsr166 |
1.6 |
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 |
jsr166 |
1.7 |
check.sum(sum[0]);}}}, |
443 |
jsr166 |
1.18 |
new Job(klazz + " toArray(empty)") { |
444 |
jsr166 |
1.7 |
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 |
jsr166 |
1.12 |
check.sum(sum[0]);}}}, |
452 |
jsr166 |
1.19 |
new Job(klazz + " stream().forEach") { |
453 |
|
|
public void work() throws Throwable { |
454 |
|
|
int[] sum = new int[1]; |
455 |
|
|
for (int i = 0; i < iterations; i++) { |
456 |
|
|
sum[0] = 0; |
457 |
|
|
x.stream().forEach(n -> sum[0] += n); |
458 |
|
|
check.sum(sum[0]);}}}, |
459 |
|
|
new Job(klazz + " stream().mapToInt") { |
460 |
|
|
public void work() throws Throwable { |
461 |
|
|
for (int i = 0; i < iterations; i++) { |
462 |
|
|
check.sum(x.stream().mapToInt(e -> e).sum());}}}, |
463 |
jsr166 |
1.18 |
new Job(klazz + " stream().collect") { |
464 |
jsr166 |
1.12 |
public void work() throws Throwable { |
465 |
|
|
for (int i = 0; i < iterations; i++) { |
466 |
|
|
check.sum(x.stream() |
467 |
|
|
.collect(summingInt(e -> e)));}}}, |
468 |
jsr166 |
1.21 |
new Job(klazz + " stream()::iterator") { |
469 |
jsr166 |
1.20 |
public void work() throws Throwable { |
470 |
|
|
int[] sum = new int[1]; |
471 |
|
|
for (int i = 0; i < iterations; i++) { |
472 |
|
|
sum[0] = 0; |
473 |
|
|
for (Integer o : (Iterable<Integer>) x.stream()::iterator) |
474 |
|
|
sum[0] += o; |
475 |
|
|
check.sum(sum[0]);}}}, |
476 |
jsr166 |
1.19 |
new Job(klazz + " parallelStream().forEach") { |
477 |
|
|
public void work() throws Throwable { |
478 |
|
|
for (int i = 0; i < iterations; i++) { |
479 |
|
|
LongAdder sum = new LongAdder(); |
480 |
|
|
x.parallelStream().forEach(n -> sum.add(n)); |
481 |
|
|
check.sum((int) sum.sum());}}}, |
482 |
|
|
new Job(klazz + " parallelStream().mapToInt") { |
483 |
|
|
public void work() throws Throwable { |
484 |
|
|
for (int i = 0; i < iterations; i++) { |
485 |
|
|
check.sum(x.parallelStream().mapToInt(e -> e).sum());}}}, |
486 |
jsr166 |
1.18 |
new Job(klazz + " parallelStream().collect") { |
487 |
jsr166 |
1.12 |
public void work() throws Throwable { |
488 |
|
|
for (int i = 0; i < iterations; i++) { |
489 |
|
|
check.sum(x.parallelStream() |
490 |
jsr166 |
1.20 |
.collect(summingInt(e -> e)));}}}, |
491 |
jsr166 |
1.21 |
new Job(klazz + " parallelStream()::iterator") { |
492 |
jsr166 |
1.20 |
public void work() throws Throwable { |
493 |
|
|
int[] sum = new int[1]; |
494 |
|
|
for (int i = 0; i < iterations; i++) { |
495 |
|
|
sum[0] = 0; |
496 |
|
|
for (Integer o : (Iterable<Integer>) x.parallelStream()::iterator) |
497 |
|
|
sum[0] += o; |
498 |
|
|
check.sum(sum[0]);}}}); |
499 |
jsr166 |
1.1 |
} |
500 |
|
|
|
501 |
jsr166 |
1.31 |
Stream<Job> dequeJobs(Deque<Integer> x) { |
502 |
jsr166 |
1.36 |
String klazz = goodClassName(x.getClass()); |
503 |
jsr166 |
1.31 |
return Stream.of( |
504 |
jsr166 |
1.18 |
new Job(klazz + " descendingIterator() loop") { |
505 |
jsr166 |
1.1 |
public void work() throws Throwable { |
506 |
|
|
for (int i = 0; i < iterations; i++) { |
507 |
|
|
int sum = 0; |
508 |
|
|
Iterator<Integer> it = x.descendingIterator(); |
509 |
|
|
while (it.hasNext()) |
510 |
|
|
sum += it.next(); |
511 |
|
|
check.sum(sum);}}}, |
512 |
jsr166 |
1.18 |
new Job(klazz + " descendingIterator().forEachRemaining()") { |
513 |
jsr166 |
1.1 |
public void work() throws Throwable { |
514 |
|
|
int[] sum = new int[1]; |
515 |
|
|
for (int i = 0; i < iterations; i++) { |
516 |
|
|
sum[0] = 0; |
517 |
|
|
x.descendingIterator().forEachRemaining(n -> sum[0] += n); |
518 |
|
|
check.sum(sum[0]);}}}); |
519 |
|
|
} |
520 |
jsr166 |
1.30 |
|
521 |
jsr166 |
1.31 |
Stream<Job> listJobs(List<Integer> x) { |
522 |
jsr166 |
1.36 |
final String klazz = goodClassName(x.getClass()); |
523 |
jsr166 |
1.31 |
return Stream.of( |
524 |
jsr166 |
1.41 |
new Job(klazz + " listIterator forward loop") { |
525 |
|
|
public void work() throws Throwable { |
526 |
|
|
for (int i = 0; i < iterations; i++) { |
527 |
|
|
int sum = 0; |
528 |
|
|
ListIterator<Integer> it = x.listIterator(); |
529 |
|
|
while (it.hasNext()) |
530 |
|
|
sum += it.next(); |
531 |
|
|
check.sum(sum);}}}, |
532 |
|
|
new Job(klazz + " listIterator backward loop") { |
533 |
|
|
public void work() throws Throwable { |
534 |
|
|
for (int i = 0; i < iterations; i++) { |
535 |
|
|
int sum = 0; |
536 |
|
|
ListIterator<Integer> it = x.listIterator(x.size()); |
537 |
|
|
while (it.hasPrevious()) |
538 |
|
|
sum += it.previous(); |
539 |
|
|
check.sum(sum);}}}, |
540 |
jsr166 |
1.39 |
new Job(klazz + " indexOf") { |
541 |
|
|
public void work() throws Throwable { |
542 |
|
|
int[] sum = new int[1]; |
543 |
|
|
Object sneakyAdder = sneakyAdder(sum); |
544 |
|
|
for (int i = 0; i < iterations; i++) { |
545 |
|
|
sum[0] = 0; |
546 |
|
|
if (x.indexOf(sneakyAdder) != -1) |
547 |
|
|
throw new AssertionError(); |
548 |
|
|
check.sum(sum[0]);}}}, |
549 |
|
|
new Job(klazz + " lastIndexOf") { |
550 |
|
|
public void work() throws Throwable { |
551 |
|
|
int[] sum = new int[1]; |
552 |
|
|
Object sneakyAdder = sneakyAdder(sum); |
553 |
|
|
for (int i = 0; i < iterations; i++) { |
554 |
|
|
sum[0] = 0; |
555 |
|
|
if (x.lastIndexOf(sneakyAdder) != -1) |
556 |
|
|
throw new AssertionError(); |
557 |
jsr166 |
1.42 |
check.sum(sum[0]);}}}, |
558 |
|
|
new Job(klazz + " replaceAll") { |
559 |
|
|
public void work() throws Throwable { |
560 |
|
|
int[] sum = new int[1]; |
561 |
|
|
UnaryOperator<Integer> sneakyAdder = |
562 |
|
|
x -> { sum[0] += x; return x; }; |
563 |
|
|
for (int i = 0; i < iterations; i++) { |
564 |
|
|
sum[0] = 0; |
565 |
|
|
x.replaceAll(sneakyAdder); |
566 |
jsr166 |
1.45 |
check.sum(sum[0]);}}}, |
567 |
jsr166 |
1.46 |
new Job(klazz + " equals") { |
568 |
|
|
public void work() throws Throwable { |
569 |
|
|
ArrayList<Integer> copy = new ArrayList<>(x); |
570 |
|
|
for (int i = 0; i < iterations; i++) { |
571 |
|
|
if (!x.equals(copy)) |
572 |
|
|
throw new AssertionError();}}}, |
573 |
jsr166 |
1.45 |
new Job(klazz + " hashCode") { |
574 |
|
|
public void work() throws Throwable { |
575 |
|
|
int hashCode = Arrays.hashCode(x.toArray()); |
576 |
|
|
for (int i = 0; i < iterations; i++) { |
577 |
|
|
if (x.hashCode() != hashCode) |
578 |
|
|
throw new AssertionError();}}}); |
579 |
jsr166 |
1.30 |
} |
580 |
jsr166 |
1.1 |
} |