Skip to content

Commit bea234b

Browse files
authoredApr 6, 2024··
fix(benchmark): table reporter for non TTY output (#5484)
1 parent 6797b04 commit bea234b

File tree

4 files changed

+55
-6
lines changed

4 files changed

+55
-6
lines changed
 

‎packages/vitest/src/node/reporters/benchmark/table/index.ts

+23-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import c from 'picocolors'
2+
import type { TaskResultPack } from '@vitest/runner'
23
import type { UserConsoleLog } from '../../../../types/general'
34
import { BaseReporter } from '../../base'
4-
import { type TableRendererOptions, createTableRenderer } from './tableRender'
5+
import { getFullName } from '../../../../utils'
6+
import { getStateSymbol } from '../../renderers/utils'
7+
import { type TableRendererOptions, createTableRenderer, renderTree } from './tableRender'
58

69
export class TableReporter extends BaseReporter {
710
renderer?: ReturnType<typeof createTableRenderer>
@@ -30,6 +33,25 @@ export class TableReporter extends BaseReporter {
3033
}
3134
}
3235

36+
onTaskUpdate(packs: TaskResultPack[]) {
37+
if (this.isTTY)
38+
return
39+
for (const pack of packs) {
40+
const task = this.ctx.state.idMap.get(pack[0])
41+
if (task && task.type === 'suite' && task.result?.state && task.result?.state !== 'run') {
42+
// render static table when all benches inside single suite are finished
43+
const benches = task.tasks.filter(t => t.meta.benchmark)
44+
if (benches.length > 0 && benches.every(t => t.result?.state !== 'run')) {
45+
let title = ` ${getStateSymbol(task)} ${getFullName(task, c.dim(' > '))}`
46+
if (task.result.duration != null && task.result.duration > this.ctx.config.slowTestThreshold)
47+
title += c.yellow(` ${Math.round(task.result.duration)}${c.dim('ms')}`)
48+
this.ctx.logger.log(title)
49+
this.ctx.logger.log(renderTree(benches, this.rendererOptions, 1, true))
50+
}
51+
}
52+
}
53+
}
54+
3355
async onFinished(files = this.ctx.state.getFiles(), errors = this.ctx.state.getUnhandledErrors()) {
3456
await this.stopListRender()
3557
this.ctx.logger.log()

‎packages/vitest/src/node/reporters/benchmark/table/tableRender.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ function renderBenchmark(task: Benchmark, tasks: Task[]): string {
100100
].join(' ')
101101
}
102102

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

106106
let idx = 0
@@ -151,7 +151,7 @@ function renderTree(tasks: Task[], options: TableRendererOptions, level = 0): st
151151
}
152152
}
153153

154-
if (task.type === 'suite' && task.tasks.length > 0) {
154+
if (!shallow && task.type === 'suite' && task.tasks.length > 0) {
155155
if (task.result?.state)
156156
output.push(renderTree(task.tasks, options, level + 1))
157157
}

‎test/benchmark/fixtures/basic/base.bench.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,30 @@ describe('timeout', () => {
3838
teardown() {
3939

4040
},
41+
...benchOptions
4142
})
4243

4344
bench('timeout75', async () => {
4445
await timeout(75)
45-
})
46+
}, benchOptions)
4647

4748
bench('timeout50', async () => {
4849
await timeout(50)
49-
})
50+
}, benchOptions)
5051

5152
bench('timeout25', async () => {
5253
await timeout(25)
53-
})
54+
}, benchOptions)
5455

5556
// TODO: move to failed tests
5657
// test('reduce', () => {
5758
// expect(1 - 1).toBe(2)
5859
// })
5960
})
61+
62+
const benchOptions = {
63+
time: 0,
64+
iterations: 3,
65+
warmupIterations: 0,
66+
warmupTime: 0,
67+
}

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

+19
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,22 @@ it('summary', async () => {
88
expect(result.stdout).not.toContain('NaNx')
99
expect(result.stdout.split('BENCH Summary')[1].replaceAll(/\d/g, '?')).toMatchSnapshot()
1010
})
11+
12+
it('non-tty', async () => {
13+
const root = pathe.join(import.meta.dirname, '../fixtures/basic')
14+
const result = await runVitest({ root }, ['base.bench.ts'], 'benchmark')
15+
const lines = result.stdout.split('\n').slice(3).slice(0, 10)
16+
const expected = `\
17+
✓ base.bench.ts > sort
18+
name
19+
· normal
20+
· reverse
21+
✓ base.bench.ts > timeout
22+
name
23+
· timeout100
24+
· timeout75
25+
· timeout50
26+
· timeout25
27+
`
28+
expect(lines).toMatchObject(expected.trim().split('\n').map(s => expect.stringContaining(s)))
29+
})

0 commit comments

Comments
 (0)
Please sign in to comment.