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 |
* This is not a regression test, but a micro-benchmark. |
26 |
* |
27 |
* I have run this as follows: |
28 |
* |
29 |
* repeat 5 for f in -client -server; do mergeBench dolphin . jr -dsa -da $f RangeCheckMicroBenchmark.java; done |
30 |
* |
31 |
* |
32 |
* @author Martin Buchholz |
33 |
*/ |
34 |
|
35 |
import java.util.ArrayList; |
36 |
import java.util.Arrays; |
37 |
import java.util.List; |
38 |
import java.util.Random; |
39 |
import java.util.concurrent.CountDownLatch; |
40 |
import java.util.regex.Pattern; |
41 |
|
42 |
import static java.util.stream.Collectors.toList; |
43 |
|
44 |
public class RangeCheckMicroBenchmark { |
45 |
abstract static class Job { |
46 |
private final String name; |
47 |
Job(String name) { this.name = name; } |
48 |
String name() { return name; } |
49 |
abstract void work() throws Throwable; |
50 |
} |
51 |
|
52 |
private static void collectAllGarbage() { |
53 |
final CountDownLatch drained = new CountDownLatch(1); |
54 |
try { |
55 |
System.gc(); // enqueue finalizable objects |
56 |
new Object() { protected void finalize() { |
57 |
drained.countDown(); }}; |
58 |
System.gc(); // enqueue detector |
59 |
drained.await(); // wait for finalizer queue to drain |
60 |
System.gc(); // cleanup finalized objects |
61 |
} catch (InterruptedException e) { throw new Error(e); } |
62 |
} |
63 |
|
64 |
/** |
65 |
* Runs each job for long enough that all the runtime compilers |
66 |
* have had plenty of time to warm up, i.e. get around to |
67 |
* compiling everything worth compiling. |
68 |
* Returns array of average times per job per run. |
69 |
*/ |
70 |
private static long[] time0(Job ... jobs) throws Throwable { |
71 |
final long warmupNanos = 10L * 1000L * 1000L * 1000L; |
72 |
long[] nanoss = new long[jobs.length]; |
73 |
for (int i = 0; i < jobs.length; i++) { |
74 |
collectAllGarbage(); |
75 |
long t0 = System.nanoTime(); |
76 |
long t; |
77 |
int j = 0; |
78 |
do { jobs[i].work(); j++; } |
79 |
while ((t = System.nanoTime() - t0) < warmupNanos); |
80 |
nanoss[i] = t/j; |
81 |
} |
82 |
return nanoss; |
83 |
} |
84 |
|
85 |
private static void time(Job ... jobs) throws Throwable { |
86 |
|
87 |
long[] warmup = time0(jobs); // Warm up run |
88 |
long[] nanoss = time0(jobs); // Real timing run |
89 |
long[] milliss = new long[jobs.length]; |
90 |
double[] ratios = new double[jobs.length]; |
91 |
|
92 |
final String nameHeader = "Method"; |
93 |
final String millisHeader = "Millis"; |
94 |
final String ratioHeader = "Ratio"; |
95 |
|
96 |
int nameWidth = nameHeader.length(); |
97 |
int millisWidth = millisHeader.length(); |
98 |
int ratioWidth = ratioHeader.length(); |
99 |
|
100 |
for (int i = 0; i < jobs.length; i++) { |
101 |
nameWidth = Math.max(nameWidth, jobs[i].name().length()); |
102 |
|
103 |
milliss[i] = nanoss[i]/(1000L * 1000L); |
104 |
millisWidth = Math.max(millisWidth, |
105 |
String.format("%d", milliss[i]).length()); |
106 |
|
107 |
ratios[i] = (double) nanoss[i] / (double) nanoss[0]; |
108 |
ratioWidth = Math.max(ratioWidth, |
109 |
String.format("%.3f", ratios[i]).length()); |
110 |
} |
111 |
|
112 |
String format = String.format("%%-%ds %%%dd %%%d.3f%%n", |
113 |
nameWidth, millisWidth, ratioWidth); |
114 |
String headerFormat = String.format("%%-%ds %%%ds %%%ds%%n", |
115 |
nameWidth, millisWidth, ratioWidth); |
116 |
System.out.printf(headerFormat, "Method", "Millis", "Ratio"); |
117 |
|
118 |
// Print out absolute and relative times, calibrated against first job |
119 |
for (int i = 0; i < jobs.length; i++) |
120 |
System.out.printf(format, jobs[i].name(), milliss[i], ratios[i]); |
121 |
} |
122 |
|
123 |
private static String keywordValue(String[] args, String keyword) { |
124 |
for (String arg : args) |
125 |
if (arg.startsWith(keyword)) |
126 |
return arg.substring(keyword.length() + 1); |
127 |
return null; |
128 |
} |
129 |
|
130 |
private static int intArg(String[] args, String keyword, int defaultValue) { |
131 |
String val = keywordValue(args, keyword); |
132 |
return val == null ? defaultValue : Integer.parseInt(val); |
133 |
} |
134 |
|
135 |
private static Pattern patternArg(String[] args, String keyword) { |
136 |
String val = keywordValue(args, keyword); |
137 |
return val == null ? null : Pattern.compile(val); |
138 |
} |
139 |
|
140 |
private static Job[] filter(Pattern filter, Job[] jobs) { |
141 |
return (filter == null) ? jobs |
142 |
: Arrays.stream(jobs) |
143 |
.filter(job -> filter.matcher(job.name()).find()) |
144 |
.collect(toList()) |
145 |
.toArray(new Job[0]); |
146 |
} |
147 |
|
148 |
private static void deoptimize(ArrayList<Integer> list) { |
149 |
for (Integer x : list) |
150 |
if (x == null) |
151 |
throw new Error(); |
152 |
} |
153 |
|
154 |
/** |
155 |
* Usage: [iterations=N] [size=N] [filter=REGEXP] |
156 |
*/ |
157 |
public static void main(String[] args) throws Throwable { |
158 |
final int iterations = intArg(args, "iterations", 30000); |
159 |
final int size = intArg(args, "size", 1000); |
160 |
final Pattern filter = patternArg(args, "filter"); |
161 |
|
162 |
final ArrayList<Integer> list = new ArrayList<>(); |
163 |
final Random rnd = new Random(); |
164 |
for (int i = 0; i < size; i++) |
165 |
list.add(rnd.nextInt()); |
166 |
|
167 |
final Job[] jobs = { |
168 |
new Job("get") { void work() { |
169 |
for (int i = 0; i < iterations; i++) { |
170 |
for (int k = 0; k < size; k++) |
171 |
if (list.get(k) == 42) |
172 |
throw new Error(); |
173 |
} |
174 |
deoptimize(list);}}, |
175 |
new Job("set") { void work() { |
176 |
Integer[] xs = list.toArray(new Integer[size]); |
177 |
for (int i = 0; i < iterations; i++) { |
178 |
for (int k = 0; k < size; k++) |
179 |
list.set(k, xs[k]); |
180 |
} |
181 |
deoptimize(list);}}, |
182 |
new Job("get/set") { void work() { |
183 |
for (int i = 0; i < iterations; i++) { |
184 |
for (int k = 0; k < size; k++) |
185 |
list.set(k, list.get(size - k - 1)); |
186 |
} |
187 |
deoptimize(list);}}, |
188 |
new Job("add/remove at end") { void work() { |
189 |
Integer x = rnd.nextInt(); |
190 |
for (int i = 0; i < iterations; i++) { |
191 |
for (int k = 0; k < size - 1; k++) { |
192 |
list.add(size, x); |
193 |
list.remove(size); |
194 |
} |
195 |
} |
196 |
deoptimize(list);}}, |
197 |
new Job("subList get") { void work() { |
198 |
List<Integer> sublist = list.subList(0, list.size()); |
199 |
for (int i = 0; i < iterations; i++) { |
200 |
for (int k = 0; k < size; k++) |
201 |
if (sublist.get(k) == 42) |
202 |
throw new Error(); |
203 |
} |
204 |
deoptimize(list);}}, |
205 |
new Job("subList set") { void work() { |
206 |
List<Integer> sublist = list.subList(0, list.size()); |
207 |
Integer[] xs = sublist.toArray(new Integer[size]); |
208 |
for (int i = 0; i < iterations; i++) { |
209 |
for (int k = 0; k < size; k++) |
210 |
sublist.set(k, xs[k]); |
211 |
} |
212 |
deoptimize(list);}}, |
213 |
new Job("subList get/set") { void work() { |
214 |
List<Integer> sublist = list.subList(0, list.size()); |
215 |
for (int i = 0; i < iterations; i++) { |
216 |
for (int k = 0; k < size; k++) |
217 |
sublist.set(k, sublist.get(size - k - 1)); |
218 |
} |
219 |
deoptimize(list);}}, |
220 |
new Job("subList add/remove at end") { void work() { |
221 |
List<Integer> sublist = list.subList(0, list.size()); |
222 |
Integer x = rnd.nextInt(); |
223 |
for (int i = 0; i < iterations; i++) { |
224 |
for (int k = 0; k < size - 1; k++) { |
225 |
sublist.add(size, x); |
226 |
sublist.remove(size); |
227 |
} |
228 |
} |
229 |
deoptimize(list);}} |
230 |
}; |
231 |
|
232 |
time(filter(filter, jobs)); |
233 |
} |
234 |
} |