Skip to content

Commit

Permalink
fix: verbose benchmark reporter
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio committed Dec 11, 2024
1 parent 412c67a commit b1c700c
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 9 deletions.
4 changes: 2 additions & 2 deletions packages/vitest/src/node/reporters/benchmark/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { VerboseReporter } from '../verbose'
import { BenchmarkReporter } from './reporter'
import { VerboseBenchmarkReporter } from './verbose'

export const BenchmarkReportsMap = {
default: BenchmarkReporter,
verbose: VerboseReporter,
verbose: VerboseBenchmarkReporter,
}

export type BenchmarkBuiltinReporters = keyof typeof BenchmarkReportsMap
3 changes: 1 addition & 2 deletions packages/vitest/src/node/reporters/benchmark/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { createBenchmarkJsonReport, flattenFormattedBenchmarkReport } from './js
import { renderTable } from './tableRender'

export class BenchmarkReporter extends DefaultReporter {
protected verbose = true
compare?: Parameters<typeof renderTable>[0]['compare']

async onInit(ctx: Vitest) {
Expand Down Expand Up @@ -41,7 +40,7 @@ export class BenchmarkReporter extends DefaultReporter {
const duration = task.result.duration

if (benches.length > 0 && benches.every(t => t.result?.state !== 'run')) {
let title = ` ${getStateSymbol(task)} ${getFullName(task, c.dim(' > '))}`
let title = `\n ${getStateSymbol(task)} ${getFullName(task, c.dim(' > '))}`

if (duration != null && duration > this.ctx.config.slowTestThreshold) {
title += c.yellow(` ${Math.round(duration)}${c.dim('ms')}`)
Expand Down
5 changes: 5 additions & 0 deletions packages/vitest/src/node/reporters/benchmark/verbose.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { BenchmarkReporter } from './reporter'

export class VerboseBenchmarkReporter extends BenchmarkReporter {
protected verbose = true
}
2 changes: 1 addition & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions test/benchmark/test/compare.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ test('compare', { timeout: 60_000 }, async () => {
reporters: ['default'],
}, [], 'benchmark')
expect(result.exitCode).toBe(0)
const lines = result.stdout.split('\n').slice(3).slice(0, 6)
const lines = result.stdout.split('\n').slice(4).slice(0, 6)
const expected = `
✓ basic.bench.ts > suite
name
Expand All @@ -33,6 +33,9 @@ test('compare', { timeout: 60_000 }, async () => {
· sleep100
(baseline)
`
expect(lines).toMatchObject(expected.trim().split('\n').map(s => expect.stringContaining(s.trim())))

for (const [index, line] of expected.trim().split('\n').entries()) {
expect(lines[index]).toMatch(line.trim())
}
}
})
8 changes: 6 additions & 2 deletions test/benchmark/test/reporter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,24 @@ it('summary', async () => {
it('non-tty', async () => {
const root = pathe.join(import.meta.dirname, '../fixtures/basic')
const result = await runVitest({ root }, ['base.bench.ts'], 'benchmark')
const lines = result.stdout.split('\n').slice(3).slice(0, 10)
const lines = result.stdout.split('\n').slice(4).slice(0, 11)
const expected = `\
✓ base.bench.ts > sort
name
· normal
· reverse
✓ base.bench.ts > timeout
name
· timeout100
· timeout75
· timeout50
· timeout25
`
expect(lines).toMatchObject(expected.trim().split('\n').map(s => expect.stringContaining(s)))

for (const [index, line] of expected.trim().split('\n').entries()) {
expect(lines[index]).toMatch(line)
}
})

it.for([true, false])('includeSamples %s', async (includeSamples) => {
Expand Down

0 comments on commit b1c700c

Please sign in to comment.