forked from BurntSushi/rebar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
359 lines (335 loc) · 10.3 KB
/
main.js
File metadata and controls
359 lines (335 loc) · 10.3 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
const buffer = require('buffer')
const fs = require('fs')
function main() {
// Beware, do not use 'process.stdin.fd'!
// See: https://github.com/nodejs/node/issues/7439
//
// I was using it, but was getting *transient* errors:
//
// Error: EAGAIN: resource temporarily unavailable, read
//
// And it *is* part of the public API and mentions
// nothing about this failure mode:
// https://nodejs.org/api/process.html#process_process_stdin_fd
//
// 💩💩💩¯\_(ツ)_/¯💩💩💩
const config = parseConfig(fs.readFileSync(0));
let samples = [];
if (config.model == "compile") {
samples = modelCompile(config);
} else if (config.model == "count") {
samples = modelCount(config);
} else if (config.model == "count-spans") {
samples = modelCountSpans(config);
} else if (config.model == "count-captures") {
samples = modelCountCaptures(config);
} else if (config.model == "grep") {
samples = modelGrep(config);
} else if (config.model == "grep-captures") {
samples = modelGrepCaptures(config);
} else if (config.model == "regex-redux") {
samples = modelRegexRedux(config);
} else {
throw new Error(`unrecognized benchmark model '${config.model}'`);
}
for (const s of samples) {
process.stdout.write(`${s.duration},${s.count}\n`);
}
}
function modelCompile(config) {
return runAndCount(
config,
re => regexCount(re, config.haystack),
() => compileRegex(config),
);
}
function modelCount(config) {
const re = compileRegex(config);
return run(config, () => regexCount(re, config.haystack));
}
function modelCountSpans(config) {
const re = compileRegex(config);
return run(config, () => {
let sum = 0;
let last = 0;
let m;
while ((m = re.exec(config.haystack)) != null) {
sum += m[0].length;
// Oh my goodness, the whole lastIndex business
// doesn't account for zero-width matches.
if (last == re.lastIndex) {
re.lastIndex++;
}
last = re.lastIndex;
}
return sum;
});
}
function modelCountCaptures(config) {
const re = compileRegex(config);
return run(config, () => {
let count = 0;
let last = 0;
let m;
while ((m = re.exec(config.haystack)) != null) {
for (const group of m) {
if (typeof group != 'undefined') {
count++;
}
}
// Oh my goodness, the whole lastIndex business
// doesn't account for zero-width matches.
if (last == re.lastIndex) {
re.lastIndex++;
}
last = re.lastIndex;
}
return count;
});
}
function modelGrep(config) {
const re = compileRegex(config);
return run(config, () => {
let count = 0;
const lines = config.haystack.split('\n');
for (let line of lines) {
if (line.endsWith('\r')) {
line = line.slice(0, line.length - 1);
}
re.lastIndex = 0;
if (re.test(line)) {
count++;
}
}
return count;
});
}
function modelGrepCaptures(config) {
const re = compileRegex(config);
return run(config, () => {
let count = 0;
const lines = config.haystack.split('\n');
for (let line of lines) {
if (line.endsWith('\r')) {
line = line.slice(0, line.length - 1);
}
let m;
while ((m = re.exec(line)) != null) {
for (const group of m) {
if (typeof group != 'undefined') {
count++;
}
}
// N.B. The grep benchmark models
// permit assuming that the regex
// will never match the empty string.
}
}
return count;
});
}
function modelRegexRedux(config) {
return run(config, () => {
const 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
`
const result = [];
let seq = config.haystack;
const ilen = seq.length;
seq = seq.replaceAll(compilePattern(config, '>[^\n]*\n|\n'), '');
const clen = seq.length;
const variants = [
"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 (const variant of variants) {
const re = compilePattern(config, variant);
const count = regexCount(re, seq);
result.push(`${variant} ${count}`);
}
seq = seq.replaceAll(compilePattern(config, 'tHa[Nt]'), '<4>');
seq = seq.replaceAll(compilePattern(config, 'aND|caN|Ha[DS]|WaS'), '<3>');
seq = seq.replaceAll(compilePattern(config, 'a[NSt]|BY'), '<2>');
seq = seq.replaceAll(compilePattern(config, '<[^>]*>'), '|');
seq = seq.replaceAll(compilePattern(config, '\\|[^|][^|]*\\|'), '-');
result.push('');
result.push(`${ilen}`);
result.push(`${clen}`);
result.push(`${seq.length}`);
if (expected.trim() != result.join('\n').trim()) {
throw new Error(`result did not match what was expected`);
}
return seq.length;
});
}
// Repeatedly runs the given 'bench' function according to the
// 'config' object given. The 'bench' function should return a count
// corresponding to the verification number for this benchmark.
function run(config, bench) {
return runAndCount(config, n => n, bench);
}
// Repeatedly runs the given 'bench' function according to the 'config'
// object given. The result of 'bench' is given to 'count', which
// should return a verification number for the given benchmark. Only
// the 'bench' function is measured.
function runAndCount(config, count, bench) {
const warmupStart = process.hrtime.bigint();
for (let i = 0; i < config.maxWarmupIters; i++) {
count(bench());
if ((process.hrtime.bigint() - warmupStart) >= config.maxWarmupTime) {
break;
}
}
const samples = [];
const runStart = process.hrtime.bigint();
for (let i = 0; i < config.maxIters; i++) {
const benchStart = process.hrtime.bigint();
const result = bench();
const elapsed = process.hrtime.bigint() - benchStart;
const n = count(result);
samples.push({duration: elapsed, count: n});
if ((process.hrtime.bigint() - runStart) >= config.maxTime) {
break;
}
}
return samples;
}
// Parses a sequence of KLV items into a single config object.
// 'raw' should be a Buffer corresponding to the benchmark KLV
// data.
function parseConfig(raw) {
const config = {
name: null,
model: null,
pattern: null,
caseInsensitive: false,
unicode: false,
haystack: null,
maxIters: 0,
maxWarmupIters: 0,
maxTime: 0,
maxWarmupTime: 0,
};
while (raw.length > 0) {
const klv = parseOneKLV(raw);
raw = raw.subarray(klv.length);
if (klv.key == "name") {
config.name = klv.value;
} else if (klv.key == "model") {
config.model = klv.value;
} else if (klv.key == "pattern") {
if (config.pattern != null) {
throw new Error(`only one pattern is supported`);
}
config.pattern = klv.value;
} else if (klv.key == "case-insensitive") {
config.caseInsensitive = klv.value == "true";
} else if (klv.key == "unicode") {
config.unicode = klv.value == "true";
} else if (klv.key == "haystack") {
config.haystack = klv.value;
} else if (klv.key == "max-iters") {
config.maxIters = parseInt(klv.value, 10);
} else if (klv.key == "max-warmup-iters") {
config.maxWarmupIters = parseInt(klv.value, 10);
} else if (klv.key == "max-time") {
config.maxTime = BigInt(klv.value);
} else if (klv.key == "max-warmup-time") {
config.maxWarmupTime = BigInt(klv.value);
} else {
throw new Error(`unrecognized KLV key '${klv.key}'`);
}
}
return config;
}
// Parses a single KLV item, and returns an object with the following
// keys: 'key', 'value' and 'length'. 'length' is the number of *bytes*
// read from the given 'raw' Buffer in order to parse the KLV item.
function parseOneKLV(raw) {
const klv = {length: 0};
const keyEnd = raw.indexOf(':');
if (keyEnd == -1) {
throw new Error(`invalid KLV item: could not find first ':'`);
}
klv.key = decode(raw.subarray(0, keyEnd));
klv.length += keyEnd + 1;
raw = raw.subarray(keyEnd + 1);
const valueLenEnd = raw.indexOf(':');
if (valueLenEnd == -1) {
throw new Error(`invalid KLV item: could not find second ':'`);
}
const valueLen = parseInt(decode(raw.subarray(0, valueLenEnd)), 10);
klv.length += valueLenEnd + 1;
raw = raw.subarray(valueLenEnd + 1);
if (raw[valueLen] != 0x0A) {
throw new Error(`invalid KLV item: no line terminator`);
}
klv.value = decode(raw.subarray(0, valueLen));
klv.length += valueLen + 1;
return klv;
}
// Compiles the pattern in the given config object.
// (Where the config object is given by 'parseConfig'.)
function compileRegex(config) {
return compilePattern(config, config.pattern);
}
// Compiles the given pattern according to the given config
// object. (Where the config object is given by 'parseConfig'.)
function compilePattern(config, pattern) {
let flags = "g";
if (config.caseInsensitive) {
flags += "i";
}
if (config.unicode) {
flags += "u";
}
return new RegExp(pattern, flags);
}
// Counts the total number of times 're' matches 'haystack'.
function regexCount(re, haystack) {
let count = 0;
let last = 0;
while (re.test(haystack)) {
count++;
// Oh my goodness, the whole lastIndex business
// doesn't account for zero-width matches.
if (last == re.lastIndex) {
re.lastIndex++;
}
last = re.lastIndex;
}
return count;
}
// Decodes the given 'Buffer' into a string, assuming that
// the buffer given is UTF-8 encoded bytes. If it's invalid
// UTF-8, then this throws an exception.
function decode(buf) {
// All of the transcoding APIs seem to do lossy decoding,
// but we want to actually fail if we get invalid UTF-8.
// Otherwise, we might end up searching a subtly different
// haystack than what other regex engines do for the same
// benchmark definition.
if (!buffer.isUtf8(buf)) {
throw new Error(`buffer contains invalid UTF-8: ${buf}`)
}
return buf.toString();
}
main()