enhance benchmark output with thousands separators (#13512)

This commit is contained in:
lightmare 2021-06-28 11:57:15 +02:00 committed by GitHub
parent 90d5eb7f2f
commit e6068cd9a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 8 deletions

View File

@ -1,13 +1,17 @@
export function report(event) { export function report(event) {
const bench = event.target; const bench = event.target;
const factor = bench.hz < 100 ? 100 : 1;
const timeMs = bench.stats.mean * 1000; const timeMs = bench.stats.mean * 1000;
const time = const time =
timeMs < 10 timeMs < 10
? `${Math.round(timeMs * 1000) / 1000}ms` ? `${Math.round(timeMs * 1000) / 1000}ms`
: `${Math.round(timeMs)}ms`; : `${Math.round(timeMs)}ms`;
const msg = `${bench.name}: ${ const msg = `${bench.name}: ${formatNumber(bench.hz)} ops/sec ±${
Math.round(bench.hz * factor) / factor Math.round(bench.stats.rme * 100) / 100
} ops/sec ±${Math.round(bench.stats.rme * 100) / 100}% (${time})`; }% (${time})`;
console.log(msg); console.log(msg);
} }
function formatNumber(x) {
if (x < 100) return `${Math.round(x * 100) / 100}`;
return `${Math.round(x)}`.replace(/\d(?=(?:\d{3})+$)/g, "$&_");
}

View File

@ -1,13 +1,17 @@
export function report(event) { export function report(event) {
const bench = event.target; const bench = event.target;
const factor = bench.hz < 100 ? 100 : 1;
const timeMs = bench.stats.mean * 1000; const timeMs = bench.stats.mean * 1000;
const time = const time =
timeMs < 10 timeMs < 10
? `${Math.round(timeMs * 1000) / 1000}ms` ? `${Math.round(timeMs * 1000) / 1000}ms`
: `${Math.round(timeMs)}ms`; : `${Math.round(timeMs)}ms`;
const msg = `${bench.name}: ${ const msg = `${bench.name}: ${formatNumber(bench.hz)} ops/sec ±${
Math.round(bench.hz * factor) / factor Math.round(bench.stats.rme * 100) / 100
} ops/sec ±${Math.round(bench.stats.rme * 100) / 100}% (${time})`; }% (${time})`;
console.log(msg); console.log(msg);
} }
function formatNumber(x) {
if (x < 100) return `${Math.round(x * 100) / 100}`;
return `${Math.round(x)}`.replace(/\d(?=(?:\d{3})+$)/g, "$&_");
}