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 |
|
|
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 |
jsr166 |
1.2 |
volatile Check check = new Check(); |
221 |
jsr166 |
1.1 |
|
222 |
|
|
public static void main(String[] args) throws Throwable { |
223 |
|
|
new IteratorMicroBenchmark().run(args); |
224 |
|
|
} |
225 |
|
|
|
226 |
|
|
void run(String[] args) throws Throwable { |
227 |
jsr166 |
1.2 |
iterations = intArg(args, "iterations", 10_000); |
228 |
jsr166 |
1.1 |
size = intArg(args, "size", 1000); |
229 |
jsr166 |
1.2 |
warmupSeconds = doubleArg(args, "warmup", 5); |
230 |
jsr166 |
1.1 |
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 |
jsr166 |
1.3 |
for (int i = 0; i < size; i++) |
240 |
jsr166 |
1.1 |
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 |
|
|
String desc = klazz + " Collection interface"; |
274 |
|
|
return List.of( |
275 |
|
|
new Job(desc + " iterate for loop") { |
276 |
|
|
public void work() throws Throwable { |
277 |
|
|
for (int i = 0; i < iterations; i++) { |
278 |
|
|
int sum = 0; |
279 |
|
|
for (Integer n : x) |
280 |
|
|
sum += n; |
281 |
|
|
check.sum(sum);}}}, |
282 |
|
|
new Job(desc + " .iterator().forEachRemaining()") { |
283 |
|
|
public void work() throws Throwable { |
284 |
|
|
int[] sum = new int[1]; |
285 |
|
|
for (int i = 0; i < iterations; i++) { |
286 |
|
|
sum[0] = 0; |
287 |
|
|
x.iterator().forEachRemaining(n -> sum[0] += n); |
288 |
|
|
check.sum(sum[0]);}}}, |
289 |
|
|
new Job(desc + " .spliterator().tryAdvance()") { |
290 |
|
|
public void work() throws Throwable { |
291 |
|
|
int[] sum = new int[1]; |
292 |
|
|
for (int i = 0; i < iterations; i++) { |
293 |
|
|
sum[0] = 0; |
294 |
|
|
Spliterator<Integer> spliterator = x.spliterator(); |
295 |
|
|
do {} while (spliterator.tryAdvance(n -> sum[0] += n)); |
296 |
|
|
check.sum(sum[0]);}}}, |
297 |
|
|
new Job(desc + " .spliterator().forEachRemaining()") { |
298 |
|
|
public void work() throws Throwable { |
299 |
|
|
int[] sum = new int[1]; |
300 |
|
|
for (int i = 0; i < iterations; i++) { |
301 |
|
|
sum[0] = 0; |
302 |
|
|
x.spliterator().forEachRemaining(n -> sum[0] += n); |
303 |
|
|
check.sum(sum[0]);}}}, |
304 |
|
|
new Job(desc + " .removeIf") { |
305 |
|
|
public void work() throws Throwable { |
306 |
|
|
int[] sum = new int[1]; |
307 |
|
|
for (int i = 0; i < iterations; i++) { |
308 |
|
|
sum[0] = 0; |
309 |
|
|
x.removeIf(n -> { sum[0] += n; return false; }); |
310 |
|
|
check.sum(sum[0]);}}}, |
311 |
|
|
new Job(desc + " .foreach") { |
312 |
|
|
public void work() throws Throwable { |
313 |
|
|
int[] sum = new int[1]; |
314 |
|
|
for (int i = 0; i < iterations; i++) { |
315 |
|
|
sum[0] = 0; |
316 |
|
|
x.forEach(n -> sum[0] += n); |
317 |
|
|
check.sum(sum[0]);}}}); |
318 |
|
|
} |
319 |
|
|
|
320 |
|
|
List<Job> dequeJobs(Deque<Integer> x) { |
321 |
|
|
String klazz = x.getClass().getSimpleName(); |
322 |
|
|
String desc = klazz + " Deque interface"; |
323 |
|
|
return List.of( |
324 |
|
|
new Job(desc + " .descendingIterator() loop") { |
325 |
|
|
public void work() throws Throwable { |
326 |
|
|
for (int i = 0; i < iterations; i++) { |
327 |
|
|
int sum = 0; |
328 |
|
|
Iterator<Integer> it = x.descendingIterator(); |
329 |
|
|
while (it.hasNext()) |
330 |
|
|
sum += it.next(); |
331 |
|
|
check.sum(sum);}}}, |
332 |
|
|
new Job(desc + " .descendingIterator().forEachRemaining()") { |
333 |
|
|
public void work() throws Throwable { |
334 |
|
|
int[] sum = new int[1]; |
335 |
|
|
for (int i = 0; i < iterations; i++) { |
336 |
|
|
sum[0] = 0; |
337 |
|
|
x.descendingIterator().forEachRemaining(n -> sum[0] += n); |
338 |
|
|
check.sum(sum[0]);}}}); |
339 |
|
|
} |
340 |
|
|
} |