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 |
|
|
import java.lang.ref.WeakReference; |
31 |
|
|
import java.util.ArrayDeque; |
32 |
|
|
import java.util.Arrays; |
33 |
|
|
import java.util.ArrayList; |
34 |
|
|
import java.util.Collection; |
35 |
|
|
import java.util.Deque; |
36 |
|
|
import java.util.Enumeration; |
37 |
|
|
import java.util.Iterator; |
38 |
|
|
import java.util.List; |
39 |
|
|
import java.util.ListIterator; |
40 |
|
|
import java.util.Map; |
41 |
jsr166 |
1.8 |
import java.util.PriorityQueue; |
42 |
jsr166 |
1.1 |
import java.util.Spliterator; |
43 |
|
|
import java.util.Vector; |
44 |
|
|
import java.util.concurrent.ArrayBlockingQueue; |
45 |
|
|
import java.util.concurrent.ConcurrentLinkedDeque; |
46 |
|
|
import java.util.concurrent.ConcurrentLinkedQueue; |
47 |
|
|
import java.util.concurrent.LinkedBlockingDeque; |
48 |
|
|
import java.util.concurrent.LinkedBlockingQueue; |
49 |
|
|
import java.util.concurrent.LinkedTransferQueue; |
50 |
jsr166 |
1.8 |
import java.util.concurrent.PriorityBlockingQueue; |
51 |
jsr166 |
1.1 |
import java.util.concurrent.ConcurrentSkipListMap; |
52 |
|
|
import java.util.concurrent.CountDownLatch; |
53 |
|
|
import java.util.concurrent.ThreadLocalRandom; |
54 |
|
|
import java.util.concurrent.TimeUnit; |
55 |
|
|
import java.util.regex.Pattern; |
56 |
|
|
|
57 |
|
|
/** |
58 |
|
|
* Usage: [iterations=N] [size=N] [filter=REGEXP] [warmup=SECONDS] |
59 |
|
|
* |
60 |
|
|
* To run this in micro-benchmark mode, simply run as a normal java program. |
61 |
|
|
* Be patient; this program runs for a very long time. |
62 |
|
|
* For faster runs, restrict execution using command line args. |
63 |
|
|
* |
64 |
|
|
* This is an interface based version of ArrayList/IteratorMicroBenchmark |
65 |
|
|
* |
66 |
|
|
* @author Martin Buchholz |
67 |
|
|
*/ |
68 |
|
|
public class IteratorMicroBenchmark { |
69 |
|
|
abstract static class Job { |
70 |
|
|
private final String name; |
71 |
|
|
public Job(String name) { this.name = name; } |
72 |
|
|
public String name() { return name; } |
73 |
|
|
public abstract void work() throws Throwable; |
74 |
|
|
} |
75 |
|
|
|
76 |
|
|
int iterations; |
77 |
|
|
int size; |
78 |
|
|
double warmupSeconds; |
79 |
|
|
Pattern filter; |
80 |
|
|
|
81 |
|
|
// --------------- GC finalization infrastructure --------------- |
82 |
|
|
|
83 |
|
|
/** No guarantees, but effective in practice. */ |
84 |
|
|
static void forceFullGc() { |
85 |
|
|
CountDownLatch finalizeDone = new CountDownLatch(1); |
86 |
|
|
WeakReference<?> ref = new WeakReference<Object>(new Object() { |
87 |
|
|
protected void finalize() { finalizeDone.countDown(); }}); |
88 |
|
|
try { |
89 |
|
|
for (int i = 0; i < 10; i++) { |
90 |
|
|
System.gc(); |
91 |
|
|
if (finalizeDone.await(1L, TimeUnit.SECONDS) && ref.get() == null) { |
92 |
|
|
System.runFinalization(); // try to pick up stragglers |
93 |
|
|
return; |
94 |
|
|
} |
95 |
|
|
} |
96 |
|
|
} catch (InterruptedException unexpected) { |
97 |
|
|
throw new AssertionError("unexpected InterruptedException"); |
98 |
|
|
} |
99 |
|
|
throw new AssertionError("failed to do a \"full\" gc"); |
100 |
|
|
} |
101 |
|
|
|
102 |
|
|
/** |
103 |
|
|
* Runs each job for long enough that all the runtime compilers |
104 |
|
|
* have had plenty of time to warm up, i.e. get around to |
105 |
|
|
* compiling everything worth compiling. |
106 |
|
|
* Returns array of average times per job per run. |
107 |
|
|
*/ |
108 |
|
|
long[] time0(List<Job> jobs) throws Throwable { |
109 |
|
|
final long warmupNanos = (long) (warmupSeconds * 1000L * 1000L * 1000L); |
110 |
|
|
final int size = jobs.size(); |
111 |
|
|
long[] nanoss = new long[size]; |
112 |
|
|
for (int i = 0; i < size; i++) { |
113 |
|
|
if (warmupNanos > 0) forceFullGc(); |
114 |
|
|
long t0 = System.nanoTime(); |
115 |
|
|
long t; |
116 |
|
|
int j = 0; |
117 |
|
|
do { jobs.get(i).work(); j++; } |
118 |
|
|
while ((t = System.nanoTime() - t0) < warmupNanos); |
119 |
|
|
nanoss[i] = t/j; |
120 |
|
|
} |
121 |
|
|
return nanoss; |
122 |
|
|
} |
123 |
|
|
|
124 |
|
|
void time(List<Job> jobs) throws Throwable { |
125 |
|
|
if (warmupSeconds > 0) time0(jobs); // Warm up run |
126 |
|
|
final int size = jobs.size(); |
127 |
|
|
final long[] nanoss = time0(jobs); // Real timing run |
128 |
|
|
final long[] milliss = new long[size]; |
129 |
|
|
final double[] ratios = new double[size]; |
130 |
|
|
|
131 |
|
|
final String nameHeader = "Method"; |
132 |
|
|
final String millisHeader = "Millis"; |
133 |
|
|
final String ratioHeader = "Ratio"; |
134 |
|
|
|
135 |
|
|
int nameWidth = nameHeader.length(); |
136 |
|
|
int millisWidth = millisHeader.length(); |
137 |
|
|
int ratioWidth = ratioHeader.length(); |
138 |
|
|
|
139 |
|
|
for (int i = 0; i < size; i++) { |
140 |
|
|
nameWidth = Math.max(nameWidth, jobs.get(i).name().length()); |
141 |
|
|
|
142 |
|
|
milliss[i] = nanoss[i]/(1000L * 1000L); |
143 |
|
|
millisWidth = Math.max(millisWidth, |
144 |
|
|
String.format("%d", milliss[i]).length()); |
145 |
|
|
|
146 |
|
|
ratios[i] = (double) nanoss[i] / (double) nanoss[0]; |
147 |
|
|
ratioWidth = Math.max(ratioWidth, |
148 |
|
|
String.format("%.3f", ratios[i]).length()); |
149 |
|
|
} |
150 |
|
|
|
151 |
|
|
String format = String.format("%%-%ds %%%dd %%%d.3f%%n", |
152 |
|
|
nameWidth, millisWidth, ratioWidth); |
153 |
|
|
String headerFormat = String.format("%%-%ds %%%ds %%%ds%%n", |
154 |
|
|
nameWidth, millisWidth, ratioWidth); |
155 |
|
|
System.out.printf(headerFormat, "Method", "Millis", "Ratio"); |
156 |
|
|
|
157 |
|
|
// Print out absolute and relative times, calibrated against first job |
158 |
|
|
for (int i = 0; i < size; i++) |
159 |
|
|
System.out.printf(format, jobs.get(i).name(), milliss[i], ratios[i]); |
160 |
|
|
} |
161 |
|
|
|
162 |
|
|
private static String keywordValue(String[] args, String keyword) { |
163 |
|
|
for (String arg : args) |
164 |
|
|
if (arg.startsWith(keyword)) |
165 |
|
|
return arg.substring(keyword.length() + 1); |
166 |
|
|
return null; |
167 |
|
|
} |
168 |
|
|
|
169 |
|
|
private static int intArg(String[] args, String keyword, int defaultValue) { |
170 |
|
|
String val = keywordValue(args, keyword); |
171 |
|
|
return (val == null) ? defaultValue : Integer.parseInt(val); |
172 |
|
|
} |
173 |
|
|
|
174 |
|
|
private static double doubleArg(String[] args, String keyword, double defaultValue) { |
175 |
|
|
String val = keywordValue(args, keyword); |
176 |
|
|
return (val == null) ? defaultValue : Double.parseDouble(val); |
177 |
|
|
} |
178 |
|
|
|
179 |
|
|
private static Pattern patternArg(String[] args, String keyword) { |
180 |
|
|
String val = keywordValue(args, keyword); |
181 |
|
|
return (val == null) ? null : Pattern.compile(val); |
182 |
|
|
} |
183 |
|
|
|
184 |
|
|
private static List<Job> filter(Pattern filter, List<Job> jobs) { |
185 |
|
|
if (filter == null) return jobs; |
186 |
|
|
ArrayList<Job> newJobs = new ArrayList<>(); |
187 |
|
|
for (Job job : jobs) |
188 |
|
|
if (filter.matcher(job.name()).find()) |
189 |
|
|
newJobs.add(job); |
190 |
|
|
return newJobs; |
191 |
|
|
} |
192 |
|
|
|
193 |
|
|
private static void deoptimize(int sum) { |
194 |
|
|
if (sum == 42) |
195 |
|
|
System.out.println("the answer"); |
196 |
|
|
} |
197 |
|
|
|
198 |
|
|
private static <T> List<T> asSubList(List<T> list) { |
199 |
|
|
return list.subList(0, list.size()); |
200 |
|
|
} |
201 |
|
|
|
202 |
|
|
private static <T> Iterable<T> backwards(final List<T> list) { |
203 |
|
|
return new Iterable<T>() { |
204 |
|
|
public Iterator<T> iterator() { |
205 |
|
|
return new Iterator<T>() { |
206 |
|
|
final ListIterator<T> it = list.listIterator(list.size()); |
207 |
|
|
public boolean hasNext() { return it.hasPrevious(); } |
208 |
|
|
public T next() { return it.previous(); } |
209 |
|
|
public void remove() { it.remove(); }};}}; |
210 |
|
|
} |
211 |
|
|
|
212 |
|
|
// Checks for correctness *and* prevents loop optimizations |
213 |
|
|
class Check { |
214 |
|
|
private int sum; |
215 |
|
|
public void sum(int sum) { |
216 |
|
|
if (this.sum == 0) |
217 |
|
|
this.sum = sum; |
218 |
|
|
if (this.sum != sum) |
219 |
|
|
throw new AssertionError("Sum mismatch"); |
220 |
|
|
} |
221 |
|
|
} |
222 |
jsr166 |
1.2 |
volatile Check check = new Check(); |
223 |
jsr166 |
1.1 |
|
224 |
|
|
public static void main(String[] args) throws Throwable { |
225 |
|
|
new IteratorMicroBenchmark().run(args); |
226 |
|
|
} |
227 |
|
|
|
228 |
|
|
void run(String[] args) throws Throwable { |
229 |
jsr166 |
1.2 |
iterations = intArg(args, "iterations", 10_000); |
230 |
jsr166 |
1.1 |
size = intArg(args, "size", 1000); |
231 |
jsr166 |
1.2 |
warmupSeconds = doubleArg(args, "warmup", 5); |
232 |
jsr166 |
1.1 |
filter = patternArg(args, "filter"); |
233 |
|
|
// System.out.printf( |
234 |
|
|
// "iterations=%d size=%d, warmup=%1g, filter=\"%s\"%n", |
235 |
|
|
// iterations, size, warmupSeconds, filter); |
236 |
|
|
|
237 |
|
|
final ArrayList<Integer> al = new ArrayList<Integer>(size); |
238 |
|
|
|
239 |
|
|
// Populate collections with random data |
240 |
|
|
final ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
241 |
jsr166 |
1.3 |
for (int i = 0; i < size; i++) |
242 |
jsr166 |
1.1 |
al.add(rnd.nextInt(size)); |
243 |
|
|
|
244 |
|
|
final ArrayDeque<Integer> ad = new ArrayDeque<>(al); |
245 |
|
|
final ArrayBlockingQueue<Integer> abq = new ArrayBlockingQueue<>(al.size()); |
246 |
|
|
abq.addAll(al); |
247 |
|
|
|
248 |
|
|
// shuffle circular array elements so they wrap |
249 |
|
|
for (int i = 0, n = rnd.nextInt(size); i < n; i++) { |
250 |
|
|
ad.addLast(ad.removeFirst()); |
251 |
|
|
abq.add(abq.remove()); |
252 |
|
|
} |
253 |
|
|
|
254 |
|
|
ArrayList<Job> jobs = new ArrayList<>(Arrays.asList()); |
255 |
|
|
|
256 |
|
|
List.of(al, ad, abq, |
257 |
jsr166 |
1.8 |
new PriorityQueue<>(al), |
258 |
jsr166 |
1.1 |
new Vector<>(al), |
259 |
|
|
new ConcurrentLinkedQueue<>(al), |
260 |
|
|
new ConcurrentLinkedDeque<>(al), |
261 |
|
|
new LinkedBlockingQueue<>(al), |
262 |
|
|
new LinkedBlockingDeque<>(al), |
263 |
jsr166 |
1.8 |
new LinkedTransferQueue<>(al), |
264 |
|
|
new PriorityBlockingQueue<>(al)) |
265 |
jsr166 |
1.1 |
.stream() |
266 |
|
|
.forEach(x -> { |
267 |
|
|
jobs.addAll(collectionJobs(x)); |
268 |
|
|
if (x instanceof Deque) |
269 |
|
|
jobs.addAll(dequeJobs((Deque<Integer>)x)); |
270 |
|
|
}); |
271 |
|
|
|
272 |
|
|
time(filter(filter, jobs)); |
273 |
|
|
} |
274 |
|
|
|
275 |
|
|
List<Job> collectionJobs(Collection<Integer> x) { |
276 |
|
|
String klazz = x.getClass().getSimpleName(); |
277 |
|
|
return List.of( |
278 |
jsr166 |
1.5 |
new Job(klazz + " iterate for loop") { |
279 |
jsr166 |
1.1 |
public void work() throws Throwable { |
280 |
|
|
for (int i = 0; i < iterations; i++) { |
281 |
|
|
int sum = 0; |
282 |
|
|
for (Integer n : x) |
283 |
|
|
sum += n; |
284 |
|
|
check.sum(sum);}}}, |
285 |
jsr166 |
1.5 |
new Job(klazz + " .iterator().forEachRemaining()") { |
286 |
jsr166 |
1.1 |
public void work() throws Throwable { |
287 |
|
|
int[] sum = new int[1]; |
288 |
|
|
for (int i = 0; i < iterations; i++) { |
289 |
|
|
sum[0] = 0; |
290 |
|
|
x.iterator().forEachRemaining(n -> sum[0] += n); |
291 |
|
|
check.sum(sum[0]);}}}, |
292 |
jsr166 |
1.5 |
new Job(klazz + " .spliterator().tryAdvance()") { |
293 |
jsr166 |
1.1 |
public void work() throws Throwable { |
294 |
|
|
int[] sum = new int[1]; |
295 |
|
|
for (int i = 0; i < iterations; i++) { |
296 |
|
|
sum[0] = 0; |
297 |
|
|
Spliterator<Integer> spliterator = x.spliterator(); |
298 |
|
|
do {} while (spliterator.tryAdvance(n -> sum[0] += n)); |
299 |
|
|
check.sum(sum[0]);}}}, |
300 |
jsr166 |
1.5 |
new Job(klazz + " .spliterator().forEachRemaining()") { |
301 |
jsr166 |
1.1 |
public void work() throws Throwable { |
302 |
|
|
int[] sum = new int[1]; |
303 |
|
|
for (int i = 0; i < iterations; i++) { |
304 |
|
|
sum[0] = 0; |
305 |
|
|
x.spliterator().forEachRemaining(n -> sum[0] += n); |
306 |
|
|
check.sum(sum[0]);}}}, |
307 |
jsr166 |
1.5 |
new Job(klazz + " .removeIf") { |
308 |
jsr166 |
1.1 |
public void work() throws Throwable { |
309 |
|
|
int[] sum = new int[1]; |
310 |
|
|
for (int i = 0; i < iterations; i++) { |
311 |
|
|
sum[0] = 0; |
312 |
|
|
x.removeIf(n -> { sum[0] += n; return false; }); |
313 |
|
|
check.sum(sum[0]);}}}, |
314 |
jsr166 |
1.5 |
new Job(klazz + " .forEach") { |
315 |
jsr166 |
1.1 |
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.forEach(n -> sum[0] += n); |
320 |
jsr166 |
1.6 |
check.sum(sum[0]);}}}, |
321 |
|
|
new Job(klazz + " .toArray()") { |
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 |
|
|
for (Object o : x.toArray()) |
327 |
|
|
sum[0] += (Integer) o; |
328 |
|
|
check.sum(sum[0]);}}}, |
329 |
|
|
new Job(klazz + " .toArray(a)") { |
330 |
|
|
public void work() throws Throwable { |
331 |
|
|
Integer[] a = new Integer[x.size()]; |
332 |
|
|
int[] sum = new int[1]; |
333 |
|
|
for (int i = 0; i < iterations; i++) { |
334 |
|
|
sum[0] = 0; |
335 |
|
|
x.toArray(a); |
336 |
|
|
for (Object o : a) |
337 |
|
|
sum[0] += (Integer) o; |
338 |
jsr166 |
1.7 |
check.sum(sum[0]);}}}, |
339 |
|
|
new Job(klazz + " .toArray(empty)") { |
340 |
|
|
public void work() throws Throwable { |
341 |
|
|
Integer[] empty = new Integer[0]; |
342 |
|
|
int[] sum = new int[1]; |
343 |
|
|
for (int i = 0; i < iterations; i++) { |
344 |
|
|
sum[0] = 0; |
345 |
|
|
for (Integer o : x.toArray(empty)) |
346 |
|
|
sum[0] += o; |
347 |
jsr166 |
1.1 |
check.sum(sum[0]);}}}); |
348 |
|
|
} |
349 |
|
|
|
350 |
|
|
List<Job> dequeJobs(Deque<Integer> x) { |
351 |
|
|
String klazz = x.getClass().getSimpleName(); |
352 |
|
|
return List.of( |
353 |
jsr166 |
1.5 |
new Job(klazz + " .descendingIterator() loop") { |
354 |
jsr166 |
1.1 |
public void work() throws Throwable { |
355 |
|
|
for (int i = 0; i < iterations; i++) { |
356 |
|
|
int sum = 0; |
357 |
|
|
Iterator<Integer> it = x.descendingIterator(); |
358 |
|
|
while (it.hasNext()) |
359 |
|
|
sum += it.next(); |
360 |
|
|
check.sum(sum);}}}, |
361 |
jsr166 |
1.5 |
new Job(klazz + " .descendingIterator().forEachRemaining()") { |
362 |
jsr166 |
1.1 |
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.descendingIterator().forEachRemaining(n -> sum[0] += n); |
367 |
|
|
check.sum(sum[0]);}}}); |
368 |
|
|
} |
369 |
|
|
} |