Skip to content

Commit

Permalink
fix: ensure benchmarks summary is only displayed with two valid bench…
Browse files Browse the repository at this point in the history
…marks

Signed-off-by: Jérôme Benoit <[email protected]>
  • Loading branch information
jerome-benoit committed Jun 16, 2024
1 parent 4c21af5 commit 6b24e33
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 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

- Ensure benchmarks summary is only displayed with two valid benchmarks.

## [0.4.13] - 2024-05-25

### Changed
Expand Down
12 changes: 8 additions & 4 deletions src/benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,12 +306,14 @@ export async function run(opts = {}) {
log(table.br(opts));
}

const noGroupBenchmarks = benchmarks.filter(
let noGroupBenchmarks = benchmarks.filter(
benchmark => benchmark.group == null,
);
let once = await executeBenchmarks(noGroupBenchmarks, log, opts);

// TODO: ensure no erroed benchmarks are included in the summary
noGroupBenchmarks = noGroupBenchmarks.filter(
noGroupBenchmark => noGroupBenchmark.error == null,
);
if (!opts.json && noGroupBenchmarks.length > 1) {
log('');
log(table.summary(noGroupBenchmarks, opts));
Expand All @@ -325,7 +327,7 @@ export async function run(opts = {}) {
log(clr.gray(opts.colors, table.br(opts)));
}

const groupBenchmarks = benchmarks.filter(
let groupBenchmarks = benchmarks.filter(
benchmark => benchmark.group === group,
);

Expand All @@ -339,7 +341,9 @@ export async function run(opts = {}) {
? await groupOpts.after()
: groupOpts.after();

// TODO: ensure no erroed benchmarks are included in the summary
groupBenchmarks = groupBenchmarks.filter(
groupBenchmark => groupBenchmark.error == null,
);
if (
groupOpts.summary === true &&
!opts.json &&
Expand Down
8 changes: 6 additions & 2 deletions src/reporter/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,12 @@ export function benchmark(
* @param {Array} benchmarks - array of benchmarks
*/
export function summary(benchmarks, { colors = true }) {
// biome-ignore lint/style/noParameterAssign: <explanation>
benchmarks = benchmarks.filter(benchmark => benchmark.error == null);
if (benchmarks.every(benchmark => benchmark.error != null)) {
throw new Error('Cannot summarize benchmarks with error');
}
if (benchmarks.length < 2) {
throw new Error('Cannot summarize less than two benchmarks');
}
benchmarks.sort(
(benchmarkA, benchmarkB) => benchmarkA.stats.avg - benchmarkB.stats.avg,
);
Expand Down

0 comments on commit 6b24e33

Please sign in to comment.