forked from BurntSushi/rebar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
453 lines (421 loc) · 15.5 KB
/
Copy pathMain.java
File metadata and controls
453 lines (421 loc) · 15.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
import java.io.ByteArrayOutputStream;
import java.nio.ByteBuffer;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.StandardCharsets;
import java.nio.charset.CodingErrorAction;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
final class Config {
public String name;
public String model;
public String pattern;
public boolean caseInsensitive;
public boolean unicode;
// rebar benchmarks permit the haystack to be invalid UTF-8,
// but Java's regex engine can only search sequences of UTF-16
// code units (as far as I can tell). So there's no point in
// trying to represent the haystack as a byte[]. If this runner
// program is called with a haystack that contains invalid UTF-8,
// then it will throw an exception.
public String haystack;
public int maxIters;
public int maxWarmupIters;
public long maxTime;
public long maxWarmupTime;
public Pattern CompileRegex() {
return CompilePattern(this.pattern);
}
public Pattern CompilePattern(String pat) {
int flags = 0;
if (this.caseInsensitive) {
flags |= Pattern.CASE_INSENSITIVE;
}
if (this.unicode) {
flags |= Pattern.UNICODE_CASE;
flags |= Pattern.UNICODE_CHARACTER_CLASS;
}
return Pattern.compile(pat, flags);
}
}
// A single Key-Length-Value item.
final class OneKLV {
// The key name.
public String key;
// The value contents.
public String value;
// The length, in bytes, used up by this KLV item.
// This is useful for parsing a sequence of KLV items.
// This length says how much to skip ahead to start
// parsing the next KLV item.
public int length;
public OneKLV(CharsetDecoder decoder, List<Byte> raw) throws Exception {
int keyEnd = raw.indexOf((byte)':');
if (keyEnd == -1) {
throw new Exception("invalid KLV item: could not find first ':'");
}
this.key = decode(decoder, raw.subList(0, keyEnd));
this.length = keyEnd + 1;
raw = raw.subList(keyEnd + 1, raw.size());
int valueLenEnd = raw.indexOf((byte)':');
if (valueLenEnd == -1) {
throw new Exception("invalid KLV item: could not find second ':'");
}
String valueLenStr = decode(decoder, raw.subList(0, valueLenEnd));
this.length += valueLenEnd + 1;
raw = raw.subList(valueLenEnd + 1, raw.size());
int valueLen = Integer.parseInt(valueLenStr);
if (raw.get(valueLen) != (byte)'\n') {
throw new Exception("invalid KLV item: no line terminator");
}
this.length += valueLen + 1; // +1 for the line terminator
this.value = decode(decoder, raw.subList(0, valueLen));
}
// Decodes a list of Bytes into a string.
static String decode(
CharsetDecoder decoder,
List<Byte> list
) throws Exception {
return decoder.decode(toByteBuffer(list)).toString();
}
// Convert a list of bytes to a ByteBuffer. This is so we can decode the
// raw UTF-8 bytes. What a kludge.
static ByteBuffer toByteBuffer(List<Byte> list) {
byte[] bytes = new byte[list.size()];
for(int i = 0; i < list.size(); i++){
bytes[i] = list.get(i);
}
return ByteBuffer.wrap(bytes);
}
}
// A representation of the data we gather from a single
// benchmark execution. That is, the time it took to run
// and the count reported for verification.
final class Sample {
// The duration, in nanoseconds. This might not always
// have nanosecond resolution, but its units are always
// nanoseconds.
public long duration;
// The count reported by the benchmark. This is checked
// against what is expected in the benchmark definition
// by rebar.
public int count;
public Sample(long duration, int count) {
this.duration = duration;
this.count = count;
}
}
public final class Main {
public static void main(String... args) throws Exception {
if (args.length == 1 && args[0].equals("version")) {
String vmname = System.getProperty("java.vm.name");
String vmversion = System.getProperty("java.vm.version");
System.out.printf("%s %s\n", vmname, vmversion);
System.exit(0);
}
// We create a decoder that will specifically fail on invalid UTF-8.
// This prevents cases where we get an invalid UTF-8 haystack and
// silently lossily decode it. Then you're in a situation where the
// Java regex engine will run on a different haystack than other regex
// engines. Instead, we loudly fail, which simply means that the Java
// regex engine can't be used with haystacks that contain invalid
// UTF-8.
CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder()
.onMalformedInput(CodingErrorAction.REPORT)
.onUnmappableCharacter(CodingErrorAction.REPORT);
List<Byte> raw = readStdin();
Config config = new Config();
while (raw.size() > 0) {
OneKLV klv = new OneKLV(decoder, raw);
raw = raw.subList(klv.length, raw.size());
if (klv.key.equals("name")) {
config.name = klv.value;
} else if (klv.key.equals("model")) {
config.model = klv.value;
} else if (klv.key.equals("pattern")) {
config.pattern = klv.value;
} else if (klv.key.equals("case-insensitive")) {
config.caseInsensitive = klv.value.equals("true");
} else if (klv.key.equals("unicode")) {
config.unicode = klv.value.equals("true");
} else if (klv.key.equals("haystack")) {
config.haystack = klv.value;
} else if (klv.key.equals("max-iters")) {
config.maxIters = Integer.parseInt(klv.value);
} else if (klv.key.equals("max-warmup-iters")) {
config.maxWarmupIters = Integer.parseInt(klv.value);
} else if (klv.key.equals("max-time")) {
config.maxTime = Long.parseLong(klv.value);
} else if (klv.key.equals("max-warmup-time")) {
config.maxWarmupTime = Long.parseLong(klv.value);
} else {
throw new Exception(String.format(
"unrecognized KLV key '%s'",
klv.key
));
}
}
if (!config.model.equals("regex-redux") && config.pattern == null) {
throw new Exception("missing pattern, must be provided once");
}
// Run our selected model and print the samples.
List<Sample> samples;
if (config.model.equals("compile")) {
samples = ModelCompile(config);
} else if (config.model.equals("count")) {
samples = ModelCount(config);
} else if (config.model.equals("count-spans")) {
samples = ModelCountSpans(config);
} else if (config.model.equals("count-captures")) {
samples = ModelCountCaptures(config);
} else if (config.model.equals("grep")) {
samples = ModelGrep(config);
} else if (config.model.equals("grep-captures")) {
samples = ModelGrepCaptures(config);
} else if (config.model.equals("regex-redux")) {
samples = ModelRegexRedux(config);
} else {
throw new Exception(String.format(
"unrecognized benchmark model %s",
config.model
));
}
for (int i = 0; i < samples.size(); i++) {
Sample s = samples.get(i);
System.out.printf("%d,%d\n", s.duration, s.count);
}
}
static List<Sample> ModelCompile(Config config) throws Exception {
return RunAndCount(
config,
re -> {
int count = 0;
Matcher m = re.matcher(config.haystack);
while (m.find()) {
count++;
}
return count;
},
() -> config.CompileRegex()
);
}
static List<Sample> ModelCount(Config config) throws Exception {
Pattern re = config.CompileRegex();
return RunAndCount(
config,
n -> n,
() -> {
int count = 0;
Matcher m = re.matcher(config.haystack);
while (m.find()) {
count++;
}
return count;
}
);
}
static List<Sample> ModelCountSpans(Config config) throws Exception {
Pattern re = config.CompileRegex();
return RunAndCount(
config,
n -> n,
() -> {
int sum = 0;
Matcher m = re.matcher(config.haystack);
while (m.find()) {
sum += m.end() - m.start();
}
return sum;
}
);
}
static List<Sample> ModelCountCaptures(Config config) throws Exception {
Pattern re = config.CompileRegex();
return RunAndCount(
config,
n -> n,
() -> {
int count = 0;
Matcher m = re.matcher(config.haystack);
while (m.find()) {
for (int i = 0; i < m.groupCount() + 1; i++) {
String cap = m.group(i);
if (cap != null) {
count++;
}
}
}
return count;
}
);
}
static List<Sample> ModelGrep(Config config) throws Exception {
Pattern re = config.CompileRegex();
return RunAndCount(
config,
n -> n,
() -> {
// Oh my, Java doesn't support a way to mutate a captured
// variable directly, so we have to stuff the count inside
// an array of length 1.
int[] count = new int[]{0};
config.haystack.lines().forEach(line -> {
if (re.matcher(line).find()) {
count[0]++;
}
});
return count[0];
}
);
}
static List<Sample> ModelGrepCaptures(Config config) throws Exception {
Pattern re = config.CompileRegex();
return RunAndCount(
config,
n -> n,
() -> {
int[] count = new int[]{0};
config.haystack.lines().forEach(line -> {
Matcher m = re.matcher(line);
while (m.find()) {
for (int i = 0; i < m.groupCount() + 1; i++) {
String cap = m.group(i);
if (cap != null) {
count[0]++;
}
}
}
});
return count[0];
}
);
}
static List<Sample> ModelRegexRedux(Config config) throws Exception {
return RunAndCount(
config,
n -> n,
() -> {
String expected = """
agggtaaa|tttaccct 6
[cgt]gggtaaa|tttaccc[acg] 26
a[act]ggtaaa|tttacc[agt]t 86
ag[act]gtaaa|tttac[agt]ct 58
agg[act]taaa|ttta[agt]cct 113
aggg[acg]aaa|ttt[cgt]ccct 31
agggt[cgt]aa|tt[acg]accct 31
agggta[cgt]a|t[acg]taccct 32
agggtaa[cgt]|[acg]ttaccct 43
1016745
1000000
547899
""";
StringBuilder result = new StringBuilder();
String seq = config.haystack;
int ilen = seq.length();
seq = config
.CompilePattern(">[^\n]*\n|\n")
.matcher(seq)
.replaceAll("");
int clen = seq.length();
String[] variants = new String[]{
"agggtaaa|tttaccct",
"[cgt]gggtaaa|tttaccc[acg]",
"a[act]ggtaaa|tttacc[agt]t",
"ag[act]gtaaa|tttac[agt]ct",
"agg[act]taaa|ttta[agt]cct",
"aggg[acg]aaa|ttt[cgt]ccct",
"agggt[cgt]aa|tt[acg]accct",
"agggta[cgt]a|t[acg]taccct",
"agggtaa[cgt]|[acg]ttaccct",
};
for (int i = 0; i < variants.length; i++) {
String variant = variants[i];
Pattern re = config.CompilePattern(variant);
int count = 0;
Matcher m = re.matcher(seq);
while (m.find()) {
count++;
}
result.append(String.format("%s %d\n", variant, count));
}
seq = config
.CompilePattern("tHa[Nt]")
.matcher(seq)
.replaceAll("<4>");
seq = config
.CompilePattern("aND|caN|Ha[DS]|WaS")
.matcher(seq)
.replaceAll("<3>");
seq = config
.CompilePattern("a[NSt]|BY")
.matcher(seq)
.replaceAll("<2>");
seq = config
.CompilePattern("<[^>]*>")
.matcher(seq)
.replaceAll("|");
seq = config
.CompilePattern("\\|[^|][^|]*\\|")
.matcher(seq)
.replaceAll("-");
result.append(String.format(
"\n%d\n%d\n%d\n", ilen, clen, seq.length()
));
if (!result.toString().trim().equals(expected.trim())) {
throw new Exception("result did not match expected");
}
return seq.length();
}
);
}
// I guess Java does not have anonymous closure types either? (Same as
// .NET?) I couldn't find it in their sections on delegates or lambda
// expressions. Oh well.
interface Count<T> {
int call(T t) throws Exception;
}
interface Bench<T> {
T call() throws Exception;
}
static <T> List<Sample> RunAndCount(
Config config,
Count<T> count,
Bench<T> bench
) throws Exception {
long warmupStart = System.nanoTime();
for (int i = 0; i < config.maxWarmupIters; i++) {
T result = bench.call();
count.call(result);
if ((System.nanoTime() - warmupStart) >= config.maxWarmupTime) {
break;
}
}
List<Sample> samples = new ArrayList<>();
long runStart = System.nanoTime();
for (int i = 0; i < config.maxIters; i++) {
long benchStart = System.nanoTime();
T result = bench.call();
long elapsed = System.nanoTime() - benchStart;
int n = count.call(result);
samples.add(new Sample(elapsed, n));
if ((System.nanoTime() - runStart) >= config.maxTime) {
break;
}
}
return samples;
}
static List<Byte> readStdin() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int nread;
while ((nread = System.in.read(buf)) > 0) {
out.write(buf, 0, nread);
}
List<Byte> list = new ArrayList<>();
for (byte b : out.toByteArray()) {
list.add(b);
}
return list;
}
}