Skip to content

Commit

Permalink
fix: fix rate computation and formatting in summary
Browse files Browse the repository at this point in the history
Signed-off-by: Jérôme Benoit <[email protected]>
  • Loading branch information
jerome-benoit committed Apr 14, 2024
1 parent 69cca77 commit 4041d4e
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 11 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]

### Fixed

- Fix rate computation and formatting in summary.

## [0.3.2] - 2024-04-13

### Changed
Expand Down
6 changes: 6 additions & 0 deletions src/reporter/fmt.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,9 @@ export function errorMargin(rmoe) {
.toLocaleString(locale)
.replaceAll(',', "'")} %`;
}

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

export function size(names) {
let size = 9;
Expand Down Expand Up @@ -134,16 +134,11 @@ export function summary(benchmarks, { colors = true }) {
}`}\n ${clr.bold(colors, clr.cyan(colors, baseline.name))}${benchmarks
.filter(benchmark => benchmark !== baseline)
.map(benchmark => {
const diff = Number(
((1 / baseline.stats.avg) * benchmark.stats.avg).toFixed(2),
);
const invDiff = Number(
((1 / benchmark.stats.avg) * baseline.stats.avg).toFixed(2),
);
return `\n ${clr[1 > diff ? 'red' : 'green'](
const rate = (1 / baseline.stats.avg) * benchmark.stats.avg;
return `\n ${clr[1 > rate ? 'red' : 'green'](
colors,
1 <= diff ? diff : invDiff,
)}x ${1 > diff ? 'slower' : 'faster'} than ${clr.bold(
1 > rate ? speedRate(1 / rate) : speedRate(rate),
)}x ${1 > rate ? 'slower' : 'faster'} than ${clr.bold(
colors,
clr.cyan(colors, benchmark.name),
)}`;
Expand Down
2 changes: 1 addition & 1 deletion tests/formatting.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { bench, group, run } from '../src/cli.mjs';

group({ summary: false }, () => {
group({ summary: true }, () => {
bench('noop', () => {});
bench('new Array(2 ** 0)', () => new Array(2 ** 0));
bench('new Array(2 ** 1)', () => new Array(2 ** 1));
Expand Down

0 comments on commit 4041d4e

Please sign in to comment.