-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lib,test: add terminal chart reporter
- Loading branch information
Showing
7 changed files
with
199 additions
and
38 deletions.
There are no files selected for viewing
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
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,21 @@ | ||
const { Suite, chartReport } = require('../../lib'); | ||
const assert = require('node:assert'); | ||
|
||
const suite = new Suite({ | ||
reporter: chartReport, | ||
}); | ||
|
||
suite | ||
.add('single with matcher', function () { | ||
const pattern = /[123]/g | ||
const replacements = { 1: 'a', 2: 'b', 3: 'c' } | ||
const subject = '123123123123123123123123123123123123123123123123' | ||
const r = subject.replace(pattern, m => replacements[m]) | ||
assert.ok(r); | ||
}) | ||
.add('multiple replaces', function () { | ||
const subject = '123123123123123123123123123123123123123123123123' | ||
const r = subject.replace(/1/g, 'a').replace(/2/g, 'b').replace(/3/g, 'c') | ||
assert.ok(r); | ||
}) | ||
.run(); |
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
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 |
---|---|---|
@@ -1,38 +1,7 @@ | ||
const { timer } = require('./clock'); | ||
|
||
const formatter = Intl.NumberFormat(undefined, { | ||
notation: 'standard', | ||
maximumFractionDigits: 2, | ||
}); | ||
|
||
function reportConsoleBench(bench, result) { | ||
const opsSecReported = result.opsSec < 100 ? | ||
result.opsSec.toFixed(2) : | ||
result.opsSec.toFixed(0); | ||
|
||
process.stdout.write(bench.name); | ||
process.stdout.write(' x '); | ||
process.stdout.write(`${ formatter.format(opsSecReported) } ops/sec`); | ||
// TODO: produce confidence on stddev | ||
// process.stdout.write(result.histogram.stddev.toString()); | ||
process.stdout.write(` (${ result.histogram.samples.length } runs sampled) `); | ||
|
||
for (const p of bench.plugins) { | ||
if (typeof p.getReport === 'function') | ||
process.stdout.write(`${p.getReport()} `); | ||
} | ||
|
||
process.stdout.write('min..max=('); | ||
process.stdout.write(timer.format(result.histogram.min)); | ||
process.stdout.write(' ... '); | ||
process.stdout.write(timer.format(result.histogram.max)); | ||
process.stdout.write(') p75='); | ||
process.stdout.write(timer.format(result.histogram.percentile(75))); | ||
process.stdout.write(' p99='); | ||
process.stdout.write(timer.format(result.histogram.percentile(99))); | ||
process.stdout.write('\n'); | ||
} | ||
const { textReport } = require('./reporter/text'); | ||
const { chartReport } = require('./reporter/chart'); | ||
|
||
module.exports = { | ||
reportConsoleBench, | ||
chartReport, | ||
textReport, | ||
}; |
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,28 @@ | ||
const { platform, arch, cpus, totalmem } = require('node:os'); | ||
|
||
function drawBar(label, value, total, length = 30) { | ||
const percentage = value / total; | ||
const filledLength = Math.round(length * percentage); | ||
const bar = '█'.repeat(filledLength) + '-'.repeat(length - filledLength); | ||
|
||
process.stdout.write(`${label.padEnd(45)} | ${bar} | ${value.toFixed(2)} ops/sec\n`); | ||
} | ||
|
||
const environment = { | ||
platform: `${platform()} ${arch()}`, | ||
hardware: `${cpus().length} vCPUs | ${(totalmem() / (1024 ** 3)).toFixed(1)}GB Mem`, | ||
}; | ||
|
||
function chartReport(results) { | ||
const maxOpsSec = Math.max(...results.map(b => b.opsSec)); | ||
|
||
process.stdout.write(`Platform: ${environment.platform}\n` + | ||
`CPU Cores: ${environment.hardware}\n\n`); | ||
results.forEach(result => { | ||
drawBar(result.name, result.opsSec, maxOpsSec); | ||
}); | ||
} | ||
|
||
module.exports = { | ||
chartReport, | ||
}; |
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,41 @@ | ||
const { timer } = require('../clock'); | ||
|
||
const formatter = Intl.NumberFormat(undefined, { | ||
notation: 'standard', | ||
maximumFractionDigits: 2, | ||
}); | ||
|
||
function textReport(results) { | ||
for (const result of results) { | ||
const opsSecReported = result.opsSec < 100 ? | ||
result.opsSec.toFixed(2) : | ||
result.opsSec.toFixed(0); | ||
|
||
process.stdout.write(result.name); | ||
process.stdout.write(' x '); | ||
process.stdout.write(`${ formatter.format(opsSecReported) } ops/sec`); | ||
// TODO: produce confidence on stddev | ||
// process.stdout.write(result.histogram.stddev.toString()); | ||
process.stdout.write(` (${ result.histogram.samples.length } runs sampled) `); | ||
|
||
for (const p of result.plugins) { | ||
if (p.report) { | ||
process.stdout.write(`${p.report} `); | ||
} | ||
} | ||
|
||
process.stdout.write('min..max=('); | ||
process.stdout.write(timer.format(result.histogram.min)); | ||
process.stdout.write(' ... '); | ||
process.stdout.write(timer.format(result.histogram.max)); | ||
process.stdout.write(') p75='); | ||
process.stdout.write(timer.format(result.histogram.percentile(75))); | ||
process.stdout.write(' p99='); | ||
process.stdout.write(timer.format(result.histogram.percentile(99))); | ||
process.stdout.write('\n'); | ||
} | ||
} | ||
|
||
module.exports = { | ||
textReport, | ||
}; |
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,40 @@ | ||
const { describe, it, before } = require('node:test'); | ||
const assert = require('node:assert'); | ||
const { Suite, chartReport } = require('../lib'); | ||
|
||
describe('chartReport outputs benchmark results as a bar chart', async (t) => { | ||
let output = ''; | ||
|
||
before(async () => { | ||
process.stdout.write = function (data) { | ||
output += data; | ||
}; | ||
|
||
const suite = new Suite({ | ||
reporter: chartReport, | ||
}); | ||
|
||
suite | ||
.add('single with matcher', function () { | ||
const pattern = /[123]/g | ||
const replacements = { 1: 'a', 2: 'b', 3: 'c' } | ||
const subject = '123123123123123123123123123123123123123123123123' | ||
const r = subject.replace(pattern, m => replacements[m]) | ||
assert.ok(r); | ||
}) | ||
.add('multiple replaces', function () { | ||
const subject = '123123123123123123123123123123123123123123123123' | ||
const r = subject.replace(/1/g, 'a').replace(/2/g, 'b').replace(/3/g, 'c') | ||
assert.ok(r); | ||
}) | ||
await suite.run(); | ||
}); | ||
|
||
it('should include bar chart chars', () => { | ||
assert.ok(output.includes('█')); | ||
}); | ||
|
||
it('should include ops/sec', () => { | ||
assert.ok(output.includes('ops/sec')); | ||
}) | ||
}); |