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): table reporter for non TTY output #5484

Merged
merged 7 commits into from Apr 6, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 23 additions & 1 deletion packages/vitest/src/node/reporters/benchmark/table/index.ts
@@ -1,7 +1,10 @@
import c from 'picocolors'
import type { TaskResultPack } from '@vitest/runner'
import type { UserConsoleLog } from '../../../../types/general'
import { BaseReporter } from '../../base'
import { type TableRendererOptions, createTableRenderer } from './tableRender'
import { getFullName } from '../../../../utils'
import { getStateSymbol } from '../../renderers/utils'
import { type TableRendererOptions, createTableRenderer, renderTree } from './tableRender'

export class TableReporter extends BaseReporter {
renderer?: ReturnType<typeof createTableRenderer>
Expand Down Expand Up @@ -30,6 +33,25 @@ export class TableReporter extends BaseReporter {
}
}

onTaskUpdate(packs: TaskResultPack[]) {
if (this.isTTY)
return
for (const pack of packs) {
const task = this.ctx.state.idMap.get(pack[0])
if (task && task.type === 'suite' && task.result?.state && task.result?.state !== 'run') {
// render static table when all benches inside single suite are finished
const benches = task.tasks.filter(t => t.meta.benchmark)
if (benches.length > 0 && benches.every(t => t.result?.state !== 'run')) {
let title = ` ${getStateSymbol(task)} ${getFullName(task, c.dim(' > '))}`
if (task.result.duration != null && task.result.duration > this.ctx.config.slowTestThreshold)
title += c.yellow(` ${Math.round(task.result.duration)}${c.dim('ms')}`)
this.ctx.logger.log(title)
this.ctx.logger.log(renderTree(benches, this.rendererOptions, 1, true))
}
}
}
}

async onFinished(files = this.ctx.state.getFiles(), errors = this.ctx.state.getUnhandledErrors()) {
await this.stopListRender()
this.ctx.logger.log()
Expand Down
Expand Up @@ -100,7 +100,7 @@ function renderBenchmark(task: Benchmark, tasks: Task[]): string {
].join(' ')
}

function renderTree(tasks: Task[], options: TableRendererOptions, level = 0): string {
export function renderTree(tasks: Task[], options: TableRendererOptions, level = 0, shallow = false): string {
const output: string[] = []

let idx = 0
Expand Down Expand Up @@ -151,7 +151,7 @@ function renderTree(tasks: Task[], options: TableRendererOptions, level = 0): st
}
}

if (task.type === 'suite' && task.tasks.length > 0) {
if (!shallow && task.type === 'suite' && task.tasks.length > 0) {
if (task.result?.state)
output.push(renderTree(task.tasks, options, level + 1))
}
Expand Down
14 changes: 11 additions & 3 deletions test/benchmark/fixtures/basic/base.bench.ts
Expand Up @@ -38,22 +38,30 @@ describe('timeout', () => {
teardown() {

},
...benchOptions
})

bench('timeout75', async () => {
await timeout(75)
})
}, benchOptions)

bench('timeout50', async () => {
await timeout(50)
})
}, benchOptions)

bench('timeout25', async () => {
await timeout(25)
})
}, benchOptions)

// TODO: move to failed tests
// test('reduce', () => {
// expect(1 - 1).toBe(2)
// })
})

const benchOptions = {
time: 0,
iterations: 3,
warmupIterations: 0,
warmupTime: 0,
}
19 changes: 19 additions & 0 deletions test/benchmark/test/reporter.test.ts
Expand Up @@ -6,5 +6,24 @@
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()

Check failure on line 9 in test/benchmark/test/reporter.test.ts

View workflow job for this annotation

GitHub Actions / test (windows-latest, 20)

test/reporter.test.ts > summary

Error: Snapshot `summary 1` mismatched - Expected + Received @@ -1,9 +1,9 @@ " - good - summary.bench.ts > suite-a - ?.??x faster than bad + bad - summary.bench.ts > suite-a + ?.??x faster than good good - summary.bench.ts > suite-b good - summary.bench.ts > suite-b > suite-b-nested ❯ test/reporter.test.ts:9:75
})

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 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)))
})