Skip to content

Commit 66cd966

Browse files
committed
chore: tweak benchmark suite
1 parent 98a5cfa commit 66cd966

15 files changed

+328
-510
lines changed

benchmarks/benchmark-template.mjs

-97
This file was deleted.

benchmarks/benchmark.mjs

+169
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
import * as assert from 'node:assert/strict';
2+
import * as fs from 'node:fs/promises';
3+
import * as path from 'node:path';
4+
import * as process from 'node:process';
5+
import { fileURLToPath } from 'node:url';
6+
import { parseArgs } from 'node:util';
7+
8+
import Benchmark from 'benchmark';
9+
import yaml from 'js-yaml';
10+
import jp from 'jsonpath';
11+
import * as JSONPath from 'jsonpath-plus';
12+
13+
import scenarios from './scenarios.mjs';
14+
15+
const options = {
16+
scenario: {
17+
type: 'string',
18+
},
19+
document: {
20+
type: 'string',
21+
},
22+
};
23+
24+
const { values } = parseArgs({ options });
25+
26+
const scenario = scenarios.find(({ name }) => name === values.scenario);
27+
28+
assert.ok(scenario);
29+
30+
const { expressions } = scenario;
31+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
32+
33+
const document = await loadDocument(scenario, values.document);
34+
35+
let results = [];
36+
const callbacksWithResults = Object.fromEntries(
37+
expressions.map(p => [
38+
p,
39+
r => {
40+
results.push(r.value);
41+
},
42+
]),
43+
);
44+
45+
const suite = new Benchmark.Suite();
46+
47+
for (const [version, { cold, hot }] of Object.entries(
48+
await loadNimma(scenario),
49+
)) {
50+
suite.add(`Nimma@${version} (cold)`, function () {
51+
cold(expressions, document, callbacksWithResults);
52+
});
53+
54+
suite.add(`Nimma@${version} (hot)`, function () {
55+
hot(document, callbacksWithResults);
56+
});
57+
}
58+
59+
suite.add(
60+
'JSONPath-Plus (resultType=value)',
61+
expressions.length > 1
62+
? function () {
63+
for (const path of expressions) {
64+
JSONPath.JSONPath({
65+
json: document,
66+
path,
67+
resultType: 'value',
68+
});
69+
}
70+
}
71+
: function () {
72+
JSONPath.JSONPath({
73+
json: document,
74+
path: expressions[0],
75+
resultType: 'value',
76+
});
77+
},
78+
);
79+
80+
suite.add(
81+
'JSONPath-Plus (resultType=all)',
82+
expressions.length > 1
83+
? function () {
84+
for (const path of expressions) {
85+
JSONPath.JSONPath({
86+
json: document,
87+
path,
88+
resultType: 'all',
89+
});
90+
}
91+
}
92+
: function () {
93+
JSONPath.JSONPath({
94+
json: document,
95+
path: expressions[0],
96+
resultType: 'all',
97+
});
98+
},
99+
);
100+
101+
suite.add(
102+
'JSONPath',
103+
expressions.length > 1
104+
? function () {
105+
for (const path of expressions) {
106+
jp.query(document, path);
107+
}
108+
}
109+
: function () {
110+
jp.query(document, expressions[0]);
111+
},
112+
);
113+
114+
suite.on('cycle', function (event) {
115+
process.stdout.write(String(event.target));
116+
process.stdout.write('\n');
117+
results = [];
118+
});
119+
suite.on('error', function (event) {
120+
process.stderr.write(event.target.error.message);
121+
});
122+
suite.on('complete', function () {
123+
process.stdout.write('Fastest is ' + this.filter('fastest').map('name'));
124+
});
125+
suite.run();
126+
127+
async function loadNimma(scenario) {
128+
const dirname = path.join(__dirname, `.gen/nimma/${scenario.name}`);
129+
const files = await fs.readdir(dirname);
130+
const versionRegex = /local|\d+\.\d+\.\d+/;
131+
132+
const instances = {};
133+
for (const file of files) {
134+
const [version] = file.match(versionRegex);
135+
instances[version] ??= {};
136+
if (file.includes('cold')) {
137+
instances[version].cold = (
138+
await import(path.join(dirname, file))
139+
).default;
140+
} else {
141+
instances[version].hot = (await import(path.join(dirname, file))).default;
142+
}
143+
}
144+
145+
return instances;
146+
}
147+
148+
async function loadDocument(scenario, document) {
149+
if (document === '') {
150+
assert.ok(scenario.defaultDocument);
151+
return JSON.parse(
152+
await fs.readFile(
153+
path.join(
154+
__dirname,
155+
`.gen/documents/${path.basename(scenario.defaultDocument)}`,
156+
),
157+
'utf8',
158+
),
159+
);
160+
}
161+
162+
if (document.startsWith('https://')) {
163+
return yaml.load(await (await fetch(document)).text());
164+
}
165+
166+
return yaml.load(
167+
await fs.readFile(path.join(process.cwd(), document), 'utf8'),
168+
);
169+
}

benchmarks/fixtures/address-large.json

-55
This file was deleted.

benchmarks/fixtures/goessner-huge.json

-66
This file was deleted.

0 commit comments

Comments
 (0)