Skip to content

Instantly share code, notes, and snippets.

@retorquere
Created November 8, 2019 16:22
Show Gist options
  • Save retorquere/79fb0ad7062a85a1d83e4b004d40985e to your computer and use it in GitHub Desktop.
Save retorquere/79fb0ad7062a85a1d83e4b004d40985e to your computer and use it in GitHub Desktop.
const Benchmark = require('benchmark');
const fs = require('fs')
const path = require('path')
let parsers = [
'current',
// 'nearley',
'idea',
'idea-reworked',
'astrocite',
'fiduswriter',
'zotero',
'bbt'
]
parsers = parsers.map(function(name) {
const parser = require(`../lib/${name}`)
parser.parse_async = async function(text) { await this.parse(text) }
if (!parser.init) parser.init = async function() {}
return { name, parser }
})
const dirPath = path.join(__dirname, 'files')
function run(method, filename) {
return new Promise(function(resolve, reject) {
const suite = new Benchmark.Suite()
suite.on("cycle", function(event) {
const bench = event.target;
if (bench.error) {
console.log(bench.toString());
} else {
console.log(`${bench.name} ${filename || ''}: ${1000 / bench.hz}ms \xb1 ${bench.stats.rme}% (${bench.stats.sample.length} run${bench.stats.sample.length === 1 ? '' : 's'} sampled)`)
}
});
suite.on("complete", function() {
console.log("Fastest is " + this.filter("fastest").map("name"));
resolve()
});
const text = filename ? fs.readFileSync(path.join(dirPath, filename), 'utf8') : null
for (const parser of parsers) {
(function(p) {
suite.add(`${p.name}.${method}`, {
defer: true,
fn: function(test) {
p.parser[method](text).then(() => test.resolve())
}
})
})(parser)
}
suite.run({ async: true })
})
}
async function main() {
await run('init')
for (let filename of fs.readdirSync(dirPath)) {
await run('parse_async', filename)
}
}
// const console = global.console
// global.console = {}
main()
.then(console.table)
.catch(console.error)
@larsgw
Copy link

larsgw commented Jan 3, 2020

I forgot to ask explicitly last time, can I add this to the repository?

@retorquere
Copy link
Author

Oh yeah, sure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment