-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
328 additions
and
510 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'), | ||
); | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.