Skip to content

Commit

Permalink
fix(benchmark): fix benchmark summary of single bench suite (#5489)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Apr 5, 2024
1 parent 10b8971 commit db98145
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/vitest/src/node/reporters/base.ts
Expand Up @@ -328,8 +328,12 @@ export abstract class BaseReporter implements Reporter {
const groupName = getFullName(group, c.dim(' > '))
logger.log(` ${bench.name}${c.dim(` - ${groupName}`)}`)
const siblings = group.tasks
.filter(i => i.result?.benchmark && i !== bench)
.filter(i => i.meta.benchmark && i.result?.benchmark && i !== bench)
.sort((a, b) => a.result!.benchmark!.rank - b.result!.benchmark!.rank)
if (siblings.length === 0) {
logger.log('')
continue
}
for (const sibling of siblings) {
const number = `${(sibling.result!.benchmark!.mean / bench.result!.benchmark!.mean).toFixed(2)}x`
logger.log(` ${c.green(number)} ${c.gray('faster than')} ${sibling.name}`)
Expand Down
32 changes: 32 additions & 0 deletions test/benchmark/fixtures/reporter/summary.bench.ts
@@ -0,0 +1,32 @@
import { bench, describe } from 'vitest'

describe('suite-a', () => {
bench('good', async () => {
await sleep(25)
}, options)

bench('bad', async () => {
await sleep(50)
}, options)
})

describe('suite-b', () => {
bench('good', async () => {
await sleep(25)
}, options)

describe('suite-b-nested', () => {
bench('good', async () => {
await sleep(50)
}, options)
})
})

const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));

const options = {
time: 0,
iterations: 3,
warmupIterations: 0,
warmupTime: 0,
}
3 changes: 3 additions & 0 deletions test/benchmark/fixtures/reporter/vitest.config.ts
@@ -0,0 +1,3 @@
import { defineConfig } from 'vitest/config'

export default defineConfig({})
14 changes: 14 additions & 0 deletions test/benchmark/test/__snapshots__/reporter.test.ts.snap
@@ -0,0 +1,14 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`summary 1`] = `
"
good - summary.bench.ts > suite-a
?.??x faster than bad
good - summary.bench.ts > suite-b
good - summary.bench.ts > suite-b > suite-b-nested
"
`;
10 changes: 10 additions & 0 deletions test/benchmark/test/reporter.test.ts
@@ -0,0 +1,10 @@
import { expect, it } from 'vitest'
import * as pathe from 'pathe'
import { runVitest } from '../../test-utils'

it('summary', async () => {
const root = pathe.join(import.meta.dirname, '../fixtures/reporter')
const result = await runVitest({ root }, ['summary.bench.ts'], 'benchmark')
expect(result.stdout).not.toContain('NaNx')
expect(result.stdout.split('BENCH Summary')[1].replaceAll(/\d/g, '?')).toMatchSnapshot()
})

0 comments on commit db98145

Please sign in to comment.