Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/Buffer/SwapMicroBenchmark.java
41149 views
1
/*
2
* Copyright (c) 2007, 2016, 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
* To exercise swap, run with filter=LITTLE_ENDIAN on sparc,
27
* filter=BIG_ENDIAN on x86.
28
*
29
* I have run this as follows:
30
*
31
* for f in -client -server; do mergeBench dolphin . jr -dsa -da $f SwapMicroBenchmark.java filter=LITTLE_ENDIAN; done
32
*
33
* @author Martin Buchholz
34
*/
35
36
import java.util.*;
37
import java.nio.*;
38
import java.util.regex.Pattern;
39
40
public class SwapMicroBenchmark {
41
abstract static class Job {
42
private final String name;
43
public Job(String name) { this.name = name; }
44
public String name() { return name; }
45
public abstract void work() throws Throwable;
46
}
47
48
private static void collectAllGarbage() {
49
final java.util.concurrent.CountDownLatch drained
50
= new java.util.concurrent.CountDownLatch(1);
51
try {
52
System.gc(); // enqueue finalizable objects
53
new Object() { protected void finalize() {
54
drained.countDown(); }};
55
System.gc(); // enqueue detector
56
drained.await(); // wait for finalizer queue to drain
57
System.gc(); // cleanup finalized objects
58
} catch (InterruptedException e) { throw new Error(e); }
59
}
60
61
/**
62
* Runs each job for long enough that all the runtime compilers
63
* have had plenty of time to warm up, i.e. get around to
64
* compiling everything worth compiling.
65
* Returns array of average times per job per run.
66
*/
67
private static long[] time0(Job ... jobs) throws Throwable {
68
final long warmupNanos = 10L * 1000L * 1000L * 1000L;
69
long[] nanoss = new long[jobs.length];
70
for (int i = 0; i < jobs.length; i++) {
71
collectAllGarbage();
72
long t0 = System.nanoTime();
73
long t;
74
int j = 0;
75
do { jobs[i].work(); j++; }
76
while ((t = System.nanoTime() - t0) < warmupNanos);
77
nanoss[i] = t/j;
78
}
79
return nanoss;
80
}
81
82
private static void time(Job ... jobs) throws Throwable {
83
84
long[] warmup = time0(jobs); // Warm up run
85
long[] nanoss = time0(jobs); // Real timing run
86
long[] milliss = new long[jobs.length];
87
double[] ratios = new double[jobs.length];
88
89
final String nameHeader = "Method";
90
final String millisHeader = "Millis";
91
final String ratioHeader = "Ratio";
92
93
int nameWidth = nameHeader.length();
94
int millisWidth = millisHeader.length();
95
int ratioWidth = ratioHeader.length();
96
97
for (int i = 0; i < jobs.length; i++) {
98
nameWidth = Math.max(nameWidth, jobs[i].name().length());
99
100
milliss[i] = nanoss[i]/(1000L * 1000L);
101
millisWidth = Math.max(millisWidth,
102
String.format("%d", milliss[i]).length());
103
104
ratios[i] = (double) nanoss[i] / (double) nanoss[0];
105
ratioWidth = Math.max(ratioWidth,
106
String.format("%.3f", ratios[i]).length());
107
}
108
109
String format = String.format("%%-%ds %%%dd %%%d.3f%%n",
110
nameWidth, millisWidth, ratioWidth);
111
String headerFormat = String.format("%%-%ds %%%ds %%%ds%%n",
112
nameWidth, millisWidth, ratioWidth);
113
System.out.printf(headerFormat, "Method", "Millis", "Ratio");
114
115
// Print out absolute and relative times, calibrated against first job
116
for (int i = 0; i < jobs.length; i++)
117
System.out.printf(format, jobs[i].name(), milliss[i], ratios[i]);
118
}
119
120
private static String keywordValue(String[] args, String keyword) {
121
for (String arg : args)
122
if (arg.startsWith(keyword))
123
return arg.substring(keyword.length() + 1);
124
return null;
125
}
126
127
private static int intArg(String[] args, String keyword, int defaultValue) {
128
String val = keywordValue(args, keyword);
129
return val == null ? defaultValue : Integer.parseInt(val);
130
}
131
132
private static Pattern patternArg(String[] args, String keyword) {
133
String val = keywordValue(args, keyword);
134
return val == null ? null : Pattern.compile(val);
135
}
136
137
private static Job[] filter(Pattern filter, Job[] jobs) {
138
if (filter == null) return jobs;
139
Job[] newJobs = new Job[jobs.length];
140
int n = 0;
141
for (Job job : jobs)
142
if (filter.matcher(job.name()).find())
143
newJobs[n++] = job;
144
// Arrays.copyOf not available in JDK 5
145
Job[] ret = new Job[n];
146
System.arraycopy(newJobs, 0, ret, 0, n);
147
return ret;
148
}
149
150
private static void deoptimize(int sum) {
151
if (sum == 42)
152
System.out.println("the answer");
153
}
154
155
/**
156
* Usage: [iterations=N] [size=N] [filter=REGEXP]
157
*/
158
public static void main(String[] args) throws Throwable {
159
final int iterations = intArg(args, "iterations", 10000);
160
final int size = intArg(args, "size", 1024);
161
final Pattern filter = patternArg(args, "filter");
162
163
final Random rnd = new Random();
164
165
final ByteBuffer b = ByteBuffer.allocateDirect(8*size);
166
for (int i = 0; i < b.limit(); i++)
167
b.put(i, (byte) rnd.nextInt());
168
169
Job[] jobs = {
170
new Job("swap char BIG_ENDIAN") {
171
public void work() throws Throwable {
172
b.order(ByteOrder.BIG_ENDIAN);
173
CharBuffer x = b.asCharBuffer();
174
for (int i = 0; i < iterations; i++) {
175
int sum = 0;
176
for (int j = 0, end = x.limit(); j < end; j++)
177
sum += x.get(j);
178
deoptimize(sum);}}},
179
new Job("swap char LITTLE_ENDIAN") {
180
public void work() throws Throwable {
181
b.order(ByteOrder.LITTLE_ENDIAN);
182
CharBuffer x = b.asCharBuffer();
183
for (int i = 0; i < iterations; i++) {
184
int sum = 0;
185
for (int j = 0, end = x.limit(); j < end; j++)
186
sum += x.get(j);
187
deoptimize(sum);}}},
188
new Job("swap short BIG_ENDIAN") {
189
public void work() throws Throwable {
190
b.order(ByteOrder.BIG_ENDIAN);
191
ShortBuffer x = b.asShortBuffer();
192
for (int i = 0; i < iterations; i++) {
193
int sum = 0;
194
for (int j = 0, end = x.limit(); j < end; j++)
195
sum += x.get(j);
196
deoptimize(sum);}}},
197
new Job("swap short LITTLE_ENDIAN") {
198
public void work() throws Throwable {
199
b.order(ByteOrder.LITTLE_ENDIAN);
200
ShortBuffer x = b.asShortBuffer();
201
for (int i = 0; i < iterations; i++) {
202
int sum = 0;
203
for (int j = 0, end = x.limit(); j < end; j++)
204
sum += x.get(j);
205
deoptimize(sum);}}},
206
new Job("swap int BIG_ENDIAN") {
207
public void work() throws Throwable {
208
b.order(ByteOrder.BIG_ENDIAN);
209
IntBuffer x = b.asIntBuffer();
210
for (int i = 0; i < iterations; i++) {
211
int sum = 0;
212
for (int j = 0, end = x.limit(); j < end; j++)
213
sum += x.get(j);
214
deoptimize(sum);}}},
215
new Job("swap int LITTLE_ENDIAN") {
216
public void work() throws Throwable {
217
b.order(ByteOrder.LITTLE_ENDIAN);
218
IntBuffer x = b.asIntBuffer();
219
for (int i = 0; i < iterations; i++) {
220
int sum = 0;
221
for (int j = 0, end = x.limit(); j < end; j++)
222
sum += x.get(j);
223
deoptimize(sum);}}},
224
new Job("swap long BIG_ENDIAN") {
225
public void work() throws Throwable {
226
b.order(ByteOrder.BIG_ENDIAN);
227
LongBuffer x = b.asLongBuffer();
228
for (int i = 0; i < iterations; i++) {
229
int sum = 0;
230
for (int j = 0, end = x.limit(); j < end; j++)
231
sum += x.get(j);
232
deoptimize(sum);}}},
233
new Job("swap long LITTLE_ENDIAN") {
234
public void work() throws Throwable {
235
b.order(ByteOrder.LITTLE_ENDIAN);
236
LongBuffer x = b.asLongBuffer();
237
for (int i = 0; i < iterations; i++) {
238
int sum = 0;
239
for (int j = 0, end = x.limit(); j < end; j++)
240
sum += x.get(j);
241
deoptimize(sum);}}}
242
};
243
244
time(filter(filter, jobs));
245
}
246
}
247
248