Skip to content

Commit

Permalink
feat: add benchmark relative speed ratio error margin to summary
Browse files Browse the repository at this point in the history
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
  • Loading branch information
jerome-benoit committed May 17, 2024
1 parent 118a07c commit abf4592
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ and this project adheres to

## [Unreleased]

###

- Add benchmark relative speed ratio error margin to summary.

## [0.4.10] - 2024-05-16

### Fixed
Expand Down
4 changes: 2 additions & 2 deletions src/reporter/fmt.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ export function errorMargin(rmoe) {
.replaceAll(',', "'")} %`;
}

export function speedRatio(speed) {
return `${Number(speed.toFixed(2))
export function speedRatio(ratio) {
return `${Number(ratio.toFixed(2))
.toLocaleString(locale)
.replaceAll(',', "'")}`;
}
24 changes: 19 additions & 5 deletions src/reporter/table.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { tatamiNgGroup } from '../constants.js';
import { tTable, tatamiNgGroup } from '../constants.js';
import * as clr from './clr.js';
import { duration, errorMargin, iterPerSecond, speedRatio } from './fmt.js';

Expand Down Expand Up @@ -140,13 +140,27 @@ export function summary(benchmarks, { colors = true }) {
.filter(benchmark => benchmark !== baseline)
.map(benchmark => {
const ratio = benchmark.stats.avg / baseline.stats.avg;
// https://en.wikipedia.org/wiki/Propagation_of_uncertainty#Example_formulae
const ratioSd =
ratio *
Math.sqrt(
(baseline.stats.sd / baseline.stats.avg) ** 2 +
(benchmark.stats.sd / benchmark.stats.avg) ** 2,
);
const ratioSem =
ratioSd / Math.sqrt(baseline.stats.samples + benchmark.stats.samples);
const critical =
tTable[
(baseline.stats.samples + benchmark.stats.samples - 1 || 1).toString()
] || tTable.infinity;
const ratioMoe = ratioSem * critical;
const ratioRmoe = (ratioMoe / ratio) * 100;
return `\n ${clr[1 > ratio ? 'red' : 'green'](
colors,
1 > ratio ? speedRatio(1 / ratio) : speedRatio(ratio),
)}x ${1 > ratio ? 'slower' : 'faster'} than ${clr.bold(
colors,
clr.cyan(colors, benchmark.name),
)}`;
)} ± ${clr.yellow(colors, errorMargin(ratioRmoe))} times ${
1 > ratio ? 'slower' : 'faster'
} than ${clr.bold(colors, clr.cyan(colors, benchmark.name))}`;
})
.join('')}`;
}

0 comments on commit abf4592

Please sign in to comment.