ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/jtreg/util/Collection/IteratorMicroBenchmark.java
Revision: 1.10
Committed: Thu Nov 24 20:01:18 2016 UTC (7 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.9: +12 -0 lines
Log Message:
add shuffle feature

File Contents

# Content
1 /*
2 * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 /*
25 * @test
26 * @summary micro-benchmark correctness mode
27 * @run main IteratorMicroBenchmark iterations=1 size=8 warmup=0
28 */
29
30 import 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.Collections;
36 import java.util.Deque;
37 import java.util.Enumeration;
38 import java.util.Iterator;
39 import java.util.List;
40 import java.util.ListIterator;
41 import java.util.Map;
42 import java.util.PriorityQueue;
43 import java.util.Spliterator;
44 import java.util.Vector;
45 import java.util.concurrent.ArrayBlockingQueue;
46 import java.util.concurrent.ConcurrentLinkedDeque;
47 import java.util.concurrent.ConcurrentLinkedQueue;
48 import java.util.concurrent.LinkedBlockingDeque;
49 import java.util.concurrent.LinkedBlockingQueue;
50 import java.util.concurrent.LinkedTransferQueue;
51 import java.util.concurrent.PriorityBlockingQueue;
52 import java.util.concurrent.ConcurrentSkipListMap;
53 import java.util.concurrent.CountDownLatch;
54 import java.util.concurrent.ThreadLocalRandom;
55 import java.util.concurrent.TimeUnit;
56 import java.util.regex.Pattern;
57
58 /**
59 * Usage: [iterations=N] [size=N] [filter=REGEXP] [warmup=SECONDS]
60 *
61 * To run this in micro-benchmark mode, simply run as a normal java program.
62 * Be patient; this program runs for a very long time.
63 * For faster runs, restrict execution using command line args.
64 *
65 * This is an interface based version of ArrayList/IteratorMicroBenchmark
66 *
67 * @author Martin Buchholz
68 */
69 public class IteratorMicroBenchmark {
70 abstract static class Job {
71 private final String name;
72 public Job(String name) { this.name = name; }
73 public String name() { return name; }
74 public abstract void work() throws Throwable;
75 }
76
77 int iterations;
78 int size;
79 double warmupSeconds;
80 long warmupNanos;
81 Pattern filter;
82 boolean shuffle;
83
84 // --------------- GC finalization infrastructure ---------------
85
86 /** No guarantees, but effective in practice. */
87 static void forceFullGc() {
88 CountDownLatch finalizeDone = new CountDownLatch(1);
89 WeakReference<?> ref = new WeakReference<Object>(new Object() {
90 protected void finalize() { finalizeDone.countDown(); }});
91 try {
92 for (int i = 0; i < 10; i++) {
93 System.gc();
94 if (finalizeDone.await(1L, TimeUnit.SECONDS) && ref.get() == null) {
95 System.runFinalization(); // try to pick up stragglers
96 return;
97 }
98 }
99 } catch (InterruptedException unexpected) {
100 throw new AssertionError("unexpected InterruptedException");
101 }
102 throw new AssertionError("failed to do a \"full\" gc");
103 }
104
105 /**
106 * Runs each job for long enough that all the runtime compilers
107 * have had plenty of time to warm up, i.e. get around to
108 * compiling everything worth compiling.
109 * Returns array of average times per job per run.
110 */
111 long[] time0(List<Job> jobs) throws Throwable {
112 final int size = jobs.size();
113 long[] nanoss = new long[size];
114 for (int i = 0; i < size; i++) {
115 if (warmupNanos > 0) forceFullGc();
116 Job job = jobs.get(i);
117 long totalTime;
118 int runs = 0;
119 long startTime = System.nanoTime();
120 do { job.work(); runs++; }
121 while ((totalTime = System.nanoTime() - startTime) < warmupNanos);
122 nanoss[i] = totalTime/runs;
123 }
124 return nanoss;
125 }
126
127 void time(List<Job> jobs) throws Throwable {
128 if (warmupNanos > 0) time0(jobs); // Warm up run
129 final int size = jobs.size();
130 final long[] nanoss = time0(jobs); // Real timing run
131 final long[] milliss = new long[size];
132 final double[] ratios = new double[size];
133
134 final String nameHeader = "Method";
135 final String millisHeader = "Millis";
136 final String ratioHeader = "Ratio";
137
138 int nameWidth = nameHeader.length();
139 int millisWidth = millisHeader.length();
140 int ratioWidth = ratioHeader.length();
141
142 for (int i = 0; i < size; i++) {
143 nameWidth = Math.max(nameWidth, jobs.get(i).name().length());
144
145 milliss[i] = nanoss[i]/(1000L * 1000L);
146 millisWidth = Math.max(millisWidth,
147 String.format("%d", milliss[i]).length());
148
149 ratios[i] = (double) nanoss[i] / (double) nanoss[0];
150 ratioWidth = Math.max(ratioWidth,
151 String.format("%.3f", ratios[i]).length());
152 }
153
154 String format = String.format("%%-%ds %%%dd %%%d.3f%%n",
155 nameWidth, millisWidth, ratioWidth);
156 String headerFormat = String.format("%%-%ds %%%ds %%%ds%%n",
157 nameWidth, millisWidth, ratioWidth);
158 System.out.printf(headerFormat, "Method", "Millis", "Ratio");
159
160 // Print out absolute and relative times, calibrated against first job
161 for (int i = 0; i < size; i++)
162 System.out.printf(format, jobs.get(i).name(), milliss[i], ratios[i]);
163 }
164
165 private static String keywordValue(String[] args, String keyword) {
166 for (String arg : args)
167 if (arg.startsWith(keyword))
168 return arg.substring(keyword.length() + 1);
169 return null;
170 }
171
172 private static int intArg(String[] args, String keyword, int defaultValue) {
173 String val = keywordValue(args, keyword);
174 return (val == null) ? defaultValue : Integer.parseInt(val);
175 }
176
177 private static double doubleArg(String[] args, String keyword, double defaultValue) {
178 String val = keywordValue(args, keyword);
179 return (val == null) ? defaultValue : Double.parseDouble(val);
180 }
181
182 private static Pattern patternArg(String[] args, String keyword) {
183 String val = keywordValue(args, keyword);
184 return (val == null) ? null : Pattern.compile(val);
185 }
186
187 private static boolean booleanArg(String[] args, String keyword) {
188 String val = keywordValue(args, keyword);
189 if (val == null || val.equals("false")) return false;
190 if (val.equals("true")) return true;
191 throw new IllegalArgumentException(val);
192 }
193
194 private static List<Job> filter(Pattern filter, List<Job> jobs) {
195 if (filter == null) return jobs;
196 ArrayList<Job> newJobs = new ArrayList<>();
197 for (Job job : jobs)
198 if (filter.matcher(job.name()).find())
199 newJobs.add(job);
200 return newJobs;
201 }
202
203 private static void deoptimize(int sum) {
204 if (sum == 42)
205 System.out.println("the answer");
206 }
207
208 private static <T> List<T> asSubList(List<T> list) {
209 return list.subList(0, list.size());
210 }
211
212 private static <T> Iterable<T> backwards(final List<T> list) {
213 return new Iterable<T>() {
214 public Iterator<T> iterator() {
215 return new Iterator<T>() {
216 final ListIterator<T> it = list.listIterator(list.size());
217 public boolean hasNext() { return it.hasPrevious(); }
218 public T next() { return it.previous(); }
219 public void remove() { it.remove(); }};}};
220 }
221
222 // Checks for correctness *and* prevents loop optimizations
223 class Check {
224 private int sum;
225 public void sum(int sum) {
226 if (this.sum == 0)
227 this.sum = sum;
228 if (this.sum != sum)
229 throw new AssertionError("Sum mismatch");
230 }
231 }
232 volatile Check check = new Check();
233
234 public static void main(String[] args) throws Throwable {
235 new IteratorMicroBenchmark().run(args);
236 }
237
238 void run(String[] args) throws Throwable {
239 iterations = intArg(args, "iterations", 10_000);
240 size = intArg(args, "size", 1000);
241 warmupSeconds = doubleArg(args, "warmup", 7.0);
242 filter = patternArg(args, "filter");
243 shuffle = booleanArg(args, "shuffle");
244
245 warmupNanos = (long) (warmupSeconds * (1000L * 1000L * 1000L));
246
247 // System.out.printf(
248 // "iterations=%d size=%d, warmup=%1g, filter=\"%s\"%n",
249 // iterations, size, warmupSeconds, filter);
250
251 final ArrayList<Integer> al = new ArrayList<Integer>(size);
252
253 // Populate collections with random data
254 final ThreadLocalRandom rnd = ThreadLocalRandom.current();
255 for (int i = 0; i < size; i++)
256 al.add(rnd.nextInt(size));
257
258 final ArrayDeque<Integer> ad = new ArrayDeque<>(al);
259 final ArrayBlockingQueue<Integer> abq = new ArrayBlockingQueue<>(al.size());
260 abq.addAll(al);
261
262 // shuffle circular array elements so they wrap
263 for (int i = 0, n = rnd.nextInt(size); i < n; i++) {
264 ad.addLast(ad.removeFirst());
265 abq.add(abq.remove());
266 }
267
268 ArrayList<Job> jobs = new ArrayList<>(Arrays.asList());
269
270 List.of(al, ad, abq,
271 new PriorityQueue<>(al),
272 new Vector<>(al),
273 new ConcurrentLinkedQueue<>(al),
274 new ConcurrentLinkedDeque<>(al),
275 new LinkedBlockingQueue<>(al),
276 new LinkedBlockingDeque<>(al),
277 new LinkedTransferQueue<>(al),
278 new PriorityBlockingQueue<>(al))
279 .stream()
280 .forEach(x -> {
281 jobs.addAll(collectionJobs(x));
282 if (x instanceof Deque)
283 jobs.addAll(dequeJobs((Deque<Integer>)x));
284 });
285
286 if (shuffle) Collections.shuffle(jobs);
287
288 time(filter(filter, jobs));
289 }
290
291 List<Job> collectionJobs(Collection<Integer> x) {
292 String klazz = x.getClass().getSimpleName();
293 return List.of(
294 new Job(klazz + " iterate for loop") {
295 public void work() throws Throwable {
296 for (int i = 0; i < iterations; i++) {
297 int sum = 0;
298 for (Integer n : x)
299 sum += n;
300 check.sum(sum);}}},
301 new Job(klazz + " .iterator().forEachRemaining()") {
302 public void work() throws Throwable {
303 int[] sum = new int[1];
304 for (int i = 0; i < iterations; i++) {
305 sum[0] = 0;
306 x.iterator().forEachRemaining(n -> sum[0] += n);
307 check.sum(sum[0]);}}},
308 new Job(klazz + " .spliterator().tryAdvance()") {
309 public void work() throws Throwable {
310 int[] sum = new int[1];
311 for (int i = 0; i < iterations; i++) {
312 sum[0] = 0;
313 Spliterator<Integer> spliterator = x.spliterator();
314 do {} while (spliterator.tryAdvance(n -> sum[0] += n));
315 check.sum(sum[0]);}}},
316 new Job(klazz + " .spliterator().forEachRemaining()") {
317 public void work() throws Throwable {
318 int[] sum = new int[1];
319 for (int i = 0; i < iterations; i++) {
320 sum[0] = 0;
321 x.spliterator().forEachRemaining(n -> sum[0] += n);
322 check.sum(sum[0]);}}},
323 new Job(klazz + " .removeIf") {
324 public void work() throws Throwable {
325 int[] sum = new int[1];
326 for (int i = 0; i < iterations; i++) {
327 sum[0] = 0;
328 x.removeIf(n -> { sum[0] += n; return false; });
329 check.sum(sum[0]);}}},
330 new Job(klazz + " .forEach") {
331 public void work() throws Throwable {
332 int[] sum = new int[1];
333 for (int i = 0; i < iterations; i++) {
334 sum[0] = 0;
335 x.forEach(n -> sum[0] += n);
336 check.sum(sum[0]);}}},
337 new Job(klazz + " .toArray()") {
338 public void work() throws Throwable {
339 int[] sum = new int[1];
340 for (int i = 0; i < iterations; i++) {
341 sum[0] = 0;
342 for (Object o : x.toArray())
343 sum[0] += (Integer) o;
344 check.sum(sum[0]);}}},
345 new Job(klazz + " .toArray(a)") {
346 public void work() throws Throwable {
347 Integer[] a = new Integer[x.size()];
348 int[] sum = new int[1];
349 for (int i = 0; i < iterations; i++) {
350 sum[0] = 0;
351 x.toArray(a);
352 for (Object o : a)
353 sum[0] += (Integer) o;
354 check.sum(sum[0]);}}},
355 new Job(klazz + " .toArray(empty)") {
356 public void work() throws Throwable {
357 Integer[] empty = new Integer[0];
358 int[] sum = new int[1];
359 for (int i = 0; i < iterations; i++) {
360 sum[0] = 0;
361 for (Integer o : x.toArray(empty))
362 sum[0] += o;
363 check.sum(sum[0]);}}});
364 }
365
366 List<Job> dequeJobs(Deque<Integer> x) {
367 String klazz = x.getClass().getSimpleName();
368 return List.of(
369 new Job(klazz + " .descendingIterator() loop") {
370 public void work() throws Throwable {
371 for (int i = 0; i < iterations; i++) {
372 int sum = 0;
373 Iterator<Integer> it = x.descendingIterator();
374 while (it.hasNext())
375 sum += it.next();
376 check.sum(sum);}}},
377 new Job(klazz + " .descendingIterator().forEachRemaining()") {
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.descendingIterator().forEachRemaining(n -> sum[0] += n);
383 check.sum(sum[0]);}}});
384 }
385 }