Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(benchmark): fix benchmark summary of single bench suite #5489

Merged
merged 3 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/vitest/src/node/reporters/base.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Comment on lines +6 to +7
Copy link
Contributor Author

@hi-ogawa hi-ogawa Apr 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this test might be flaky since "good" one becomes really slow depending on CI workload

I'll tweak the assertion if this CI failure is so frequent.


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
Original file line number Diff line number Diff line change
@@ -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()
})
Loading