-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathbenchmark.mjs
More file actions
90 lines (76 loc) · 2.76 KB
/
benchmark.mjs
File metadata and controls
90 lines (76 loc) · 2.76 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
import { compileSchema } from './compile.mjs';
import { Blaze } from './index.mjs';
import { readFileSync, readdirSync, existsSync } from 'node:fs';
import { join, resolve, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
const MODULE_DIR = dirname(fileURLToPath(import.meta.url));
const PROJECT_ROOT = resolve(MODULE_DIR, '../..');
const BENCHMARK_DIR = join(PROJECT_ROOT, 'benchmark/e2e');
const MINIMUM_DURATION_NS = 1_000_000_000n;
const WARMUP_DURATION_NS = 500_000_000n;
function measure(callback) {
const warmupStart = process.hrtime.bigint();
while (process.hrtime.bigint() - warmupStart < WARMUP_DURATION_NS) {
callback();
}
let iterations = 0;
const start = process.hrtime.bigint();
while (true) {
callback();
iterations++;
const elapsed = process.hrtime.bigint() - start;
if (elapsed >= MINIMUM_DURATION_NS) {
return Number(elapsed) / iterations;
}
}
}
function loadCase(name) {
const directory = join(BENCHMARK_DIR, name);
const schemaPath = join(directory, 'schema.json');
const instancesPath = join(directory, 'instances.jsonl');
if (!existsSync(schemaPath) || !existsSync(instancesPath)) return null;
const evaluator = new Blaze(compileSchema(schemaPath, { mode: 'fast' }));
const lines = readFileSync(instancesPath, 'utf-8').trim().split('\n');
const instances = lines.map(line => JSON.parse(line));
return { evaluator, instances };
}
const dirs = readdirSync(BENCHMARK_DIR)
.filter(entry => !entry.endsWith('.cc'))
.sort();
const cases = {};
for (const directory of dirs) {
try {
const loaded = loadCase(directory);
if (loaded) {
cases[directory] = loaded;
}
} catch (error) {
process.stderr.write(`Skipping ${directory}: ${error.message.split('\n')[0]}\n`);
}
}
for (const [name, { evaluator, instances }] of Object.entries(cases)) {
for (const instance of instances) {
if (!evaluator.validate(instance)) {
throw new Error(`${name}: validation returned false`);
}
}
}
function formatNumber(value) {
return Math.round(value).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
}
const names = Object.keys(cases);
const longest = Math.max(...names.map(name => `E2E_Evaluator_${name}`.length));
const results = [];
process.stderr.write('\n');
for (const [name, { evaluator, instances }] of Object.entries(cases)) {
const label = `E2E_Evaluator_${name}`;
const nanoseconds = Math.round(measure(() => {
for (const instance of instances) {
evaluator.validate(instance);
}
}));
const padding = ' '.repeat(longest - label.length + 4);
process.stderr.write(`${label}${padding}${formatNumber(nanoseconds)} ns/iter\n`);
results.push({ name: label, unit: 'ns', value: nanoseconds });
}
process.stdout.write(JSON.stringify(results, null, 2) + '\n');