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 1 commit
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
Next Next commit
fix(benchmark): print table on non TTY output
  • Loading branch information
hi-ogawa committed Apr 4, 2024
commit 48f762a9865ba6c4d5bd34cab405ddc61a1c8ab6
28 changes: 24 additions & 4 deletions packages/vitest/src/node/reporters/benchmark/table/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import c from 'picocolors'
import type { UserConsoleLog } from '../../../../types/general'
import { BaseReporter } from '../../base'
import { type TableRendererOptions, createTableRenderer } from './tableRender'
import { getFullName } from '../../../../utils'
import type { TaskResultPack } from '../../../../types'
import { getStateSymbol } from '../../renderers/utils'
import { type TableRendererOptions, createTableRenderer, renderTree } from './tableRender'

export class TableReporter extends BaseReporter {
renderer?: ReturnType<typeof createTableRenderer>
Expand All @@ -18,10 +21,11 @@ export class TableReporter extends BaseReporter {
}

onCollected() {
this.rendererOptions.logger = this.ctx.logger
this.rendererOptions.showHeap = this.ctx.config.logHeapUsage
this.rendererOptions.slowTestThreshold = this.ctx.config.slowTestThreshold
this.rendererOptions.recurse = this.isTTY
if (this.isTTY) {
this.rendererOptions.logger = this.ctx.logger
this.rendererOptions.showHeap = this.ctx.config.logHeapUsage
this.rendererOptions.slowTestThreshold = this.ctx.config.slowTestThreshold
const files = this.ctx.state.getFiles(this.watchFilters)
if (!this.renderer)
this.renderer = createTableRenderer(files, this.rendererOptions).start()
Expand All @@ -30,6 +34,22 @@ 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')) {
this.ctx.logger.log(` ${getStateSymbol(task)} ${getFullName(task, c.dim(' > '))}`)
this.ctx.logger.log(renderTree(benches, this.rendererOptions, 1))
}
}
}
}

async onFinished(files = this.ctx.state.getFiles(), errors = this.ctx.state.getUnhandledErrors()) {
await this.stopListRender()
this.ctx.logger.log()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface TableRendererOptions {
logger: Logger
showHeap: boolean
slowTestThreshold: number
recurse: boolean
}

const outputMap = new WeakMap<Task, string>()
Expand Down Expand Up @@ -100,7 +101,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): string {
const output: string[] = []

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

if (task.type === 'suite' && task.tasks.length > 0) {
if (options.recurse && task.type === 'suite' && task.tasks.length > 0) {
if (task.result?.state)
output.push(renderTree(task.tasks, options, level + 1))
}
Expand Down
Loading