|
|
|
@ -3,9 +3,7 @@ |
|
|
|
|
|
|
|
import fs from 'node:fs' |
|
|
|
import util from 'node:util' |
|
|
|
import Benchmark from 'benchmark' |
|
|
|
import ansi from 'ansi' |
|
|
|
const cursor = ansi(process.stdout) |
|
|
|
import { Bench } from 'tinybench' |
|
|
|
|
|
|
|
const IMPLS = [] |
|
|
|
|
|
|
|
@ -27,34 +25,34 @@ fs.readdirSync(new URL('./samples', import.meta.url)).sort().forEach(sample => { |
|
|
|
|
|
|
|
const title = `(${content.string.length} bytes)` |
|
|
|
|
|
|
|
function onComplete () { cursor.write('\n') } |
|
|
|
|
|
|
|
const suite = new Benchmark.Suite( |
|
|
|
title, |
|
|
|
{ |
|
|
|
onStart: () => { console.log('\nSample: %s %s', sample, title) }, |
|
|
|
onComplete |
|
|
|
} |
|
|
|
) |
|
|
|
const bench = new Bench({ name: title }) |
|
|
|
|
|
|
|
IMPLS.forEach(function (impl) { |
|
|
|
suite.add( |
|
|
|
impl.name, |
|
|
|
{ |
|
|
|
onCycle: function onCycle (event) { |
|
|
|
cursor.horizontalAbsolute() |
|
|
|
cursor.eraseLine() |
|
|
|
cursor.write(' > ' + event.target) |
|
|
|
}, |
|
|
|
onComplete, |
|
|
|
fn: function () { impl.code.run(content.string) } |
|
|
|
} |
|
|
|
) |
|
|
|
bench.add(impl.name, function () { impl.code.run(content.string) }) |
|
|
|
}) |
|
|
|
|
|
|
|
SAMPLES.push({ name: sample.split('.')[0], title, content, suite }) |
|
|
|
SAMPLES.push({ name: sample.split('.')[0], filename: sample, title, content, bench }) |
|
|
|
}) |
|
|
|
|
|
|
|
function formatNumber (num) { |
|
|
|
return num.toLocaleString('en-US', { maximumFractionDigits: 0 }) |
|
|
|
} |
|
|
|
|
|
|
|
function formatTask (task) { |
|
|
|
const result = task.result |
|
|
|
|
|
|
|
if (result.state !== 'completed') { |
|
|
|
return `${task.name}: ${result.state}` |
|
|
|
} |
|
|
|
|
|
|
|
return [ |
|
|
|
task.name, |
|
|
|
`${formatNumber(result.throughput.mean)} ops/sec`, |
|
|
|
`+/-${result.throughput.rme.toFixed(2)}%`, |
|
|
|
`${result.throughput.samplesCount} samples` |
|
|
|
].join(' ') |
|
|
|
} |
|
|
|
|
|
|
|
function select (patterns) { |
|
|
|
const result = [] |
|
|
|
|
|
|
|
@ -77,7 +75,7 @@ function select (patterns) { |
|
|
|
return result |
|
|
|
} |
|
|
|
|
|
|
|
function run (files) { |
|
|
|
async function run (files) { |
|
|
|
const selected = select(files) |
|
|
|
|
|
|
|
if (selected.length > 0) { |
|
|
|
@ -89,9 +87,15 @@ function run (files) { |
|
|
|
console.log('There isn\'t any sample matches any of these patterns: %s', util.inspect(files)) |
|
|
|
} |
|
|
|
|
|
|
|
selected.forEach(function (sample) { |
|
|
|
sample.suite.run() |
|
|
|
}) |
|
|
|
for (const sample of selected) { |
|
|
|
console.log('\n\nSample: %s %s', sample.filename, sample.title) |
|
|
|
|
|
|
|
sample.bench.addEventListener('cycle', function (event) { |
|
|
|
console.log(' > %s', formatTask(event.task)) |
|
|
|
}) |
|
|
|
|
|
|
|
await sample.bench.run() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
run(process.argv.slice(2).map(source => new RegExp(source, 'i'))) |
|
|
|
await run(process.argv.slice(2).map(source => new RegExp(source, 'i'))) |
|
|
|
|