ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/jtreg/util/Collection/IteratorMicroBenchmark.java
Revision: 1.6
Committed: Mon Nov 21 01:02:40 2016 UTC (7 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.5: +18 -0 lines
Log Message:
add toArray benchmarks/tests

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