Skip to content

Commit db98145

Browse files
authoredApr 5, 2024··
fix(benchmark): fix benchmark summary of single bench suite (#5489)
1 parent 10b8971 commit db98145

File tree

5 files changed

+64
-1
lines changed

5 files changed

+64
-1
lines changed
 

‎packages/vitest/src/node/reporters/base.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,12 @@ export abstract class BaseReporter implements Reporter {
328328
const groupName = getFullName(group, c.dim(' > '))
329329
logger.log(` ${bench.name}${c.dim(` - ${groupName}`)}`)
330330
const siblings = group.tasks
331-
.filter(i => i.result?.benchmark && i !== bench)
331+
.filter(i => i.meta.benchmark && i.result?.benchmark && i !== bench)
332332
.sort((a, b) => a.result!.benchmark!.rank - b.result!.benchmark!.rank)
333+
if (siblings.length === 0) {
334+
logger.log('')
335+
continue
336+
}
333337
for (const sibling of siblings) {
334338
const number = `${(sibling.result!.benchmark!.mean / bench.result!.benchmark!.mean).toFixed(2)}x`
335339
logger.log(` ${c.green(number)} ${c.gray('faster than')} ${sibling.name}`)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { bench, describe } from 'vitest'
2+
3+
describe('suite-a', () => {
4+
bench('good', async () => {
5+
await sleep(25)
6+
}, options)
7+
8+
bench('bad', async () => {
9+
await sleep(50)
10+
}, options)
11+
})
12+
13+
describe('suite-b', () => {
14+
bench('good', async () => {
15+
await sleep(25)
16+
}, options)
17+
18+
describe('suite-b-nested', () => {
19+
bench('good', async () => {
20+
await sleep(50)
21+
}, options)
22+
})
23+
})
24+
25+
const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));
26+
27+
const options = {
28+
time: 0,
29+
iterations: 3,
30+
warmupIterations: 0,
31+
warmupTime: 0,
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { defineConfig } from 'vitest/config'
2+
3+
export default defineConfig({})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`summary 1`] = `
4+
"
5+
6+
good - summary.bench.ts > suite-a
7+
?.??x faster than bad
8+
9+
good - summary.bench.ts > suite-b
10+
11+
good - summary.bench.ts > suite-b > suite-b-nested
12+
13+
"
14+
`;

‎test/benchmark/test/reporter.test.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { expect, it } from 'vitest'
2+
import * as pathe from 'pathe'
3+
import { runVitest } from '../../test-utils'
4+
5+
it('summary', async () => {
6+
const root = pathe.join(import.meta.dirname, '../fixtures/reporter')
7+
const result = await runVitest({ root }, ['summary.bench.ts'], 'benchmark')
8+
expect(result.stdout).not.toContain('NaNx')
9+
expect(result.stdout.split('BENCH Summary')[1].replaceAll(/\d/g, '?')).toMatchSnapshot()
10+
})

0 commit comments

Comments
 (0)
Please sign in to comment.