Skip to content

Commit

Permalink
chore: tweak benchmark suite
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip committed Jan 30, 2024
1 parent 98a5cfa commit 66cd966
Show file tree
Hide file tree
Showing 15 changed files with 328 additions and 510 deletions.
97 changes: 0 additions & 97 deletions benchmarks/benchmark-template.mjs

This file was deleted.

169 changes: 169 additions & 0 deletions benchmarks/benchmark.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
import * as assert from 'node:assert/strict';
import * as fs from 'node:fs/promises';
import * as path from 'node:path';
import * as process from 'node:process';
import { fileURLToPath } from 'node:url';
import { parseArgs } from 'node:util';

import Benchmark from 'benchmark';
import yaml from 'js-yaml';
import jp from 'jsonpath';
import * as JSONPath from 'jsonpath-plus';

import scenarios from './scenarios.mjs';

const options = {
scenario: {
type: 'string',
},
document: {
type: 'string',
},
};

const { values } = parseArgs({ options });

const scenario = scenarios.find(({ name }) => name === values.scenario);

assert.ok(scenario);

const { expressions } = scenario;
const __dirname = path.dirname(fileURLToPath(import.meta.url));

const document = await loadDocument(scenario, values.document);

let results = [];
const callbacksWithResults = Object.fromEntries(
expressions.map(p => [
p,
r => {
results.push(r.value);
},
]),
);

const suite = new Benchmark.Suite();

for (const [version, { cold, hot }] of Object.entries(
await loadNimma(scenario),
)) {
suite.add(`Nimma@${version} (cold)`, function () {
cold(expressions, document, callbacksWithResults);
});

suite.add(`Nimma@${version} (hot)`, function () {
hot(document, callbacksWithResults);
});
}

suite.add(
'JSONPath-Plus (resultType=value)',
expressions.length > 1
? function () {
for (const path of expressions) {
JSONPath.JSONPath({
json: document,
path,
resultType: 'value',
});
}
}
: function () {
JSONPath.JSONPath({
json: document,
path: expressions[0],
resultType: 'value',
});
},
);

suite.add(
'JSONPath-Plus (resultType=all)',
expressions.length > 1
? function () {
for (const path of expressions) {
JSONPath.JSONPath({
json: document,
path,
resultType: 'all',
});
}
}
: function () {
JSONPath.JSONPath({
json: document,
path: expressions[0],
resultType: 'all',
});
},
);

suite.add(
'JSONPath',
expressions.length > 1
? function () {
for (const path of expressions) {
jp.query(document, path);
}
}
: function () {
jp.query(document, expressions[0]);
},
);

suite.on('cycle', function (event) {
process.stdout.write(String(event.target));
process.stdout.write('\n');
results = [];
});
suite.on('error', function (event) {
process.stderr.write(event.target.error.message);
});
suite.on('complete', function () {
process.stdout.write('Fastest is ' + this.filter('fastest').map('name'));
});
suite.run();

async function loadNimma(scenario) {
const dirname = path.join(__dirname, `.gen/nimma/${scenario.name}`);
const files = await fs.readdir(dirname);
const versionRegex = /local|\d+\.\d+\.\d+/;

const instances = {};
for (const file of files) {
const [version] = file.match(versionRegex);
instances[version] ??= {};
if (file.includes('cold')) {
instances[version].cold = (
await import(path.join(dirname, file))
).default;
} else {
instances[version].hot = (await import(path.join(dirname, file))).default;
}
}

return instances;
}

async function loadDocument(scenario, document) {
if (document === '') {
assert.ok(scenario.defaultDocument);
return JSON.parse(
await fs.readFile(
path.join(
__dirname,
`.gen/documents/${path.basename(scenario.defaultDocument)}`,
),
'utf8',
),
);
}

if (document.startsWith('https://')) {
return yaml.load(await (await fetch(document)).text());
}

return yaml.load(
await fs.readFile(path.join(process.cwd(), document), 'utf8'),
);
}
55 changes: 0 additions & 55 deletions benchmarks/fixtures/address-large.json

This file was deleted.

66 changes: 0 additions & 66 deletions benchmarks/fixtures/goessner-huge.json

This file was deleted.

Loading

0 comments on commit 66cd966

Please sign in to comment.