Skip to content

Commit

Permalink
fix(benchmark): print table on non TTY output
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Apr 4, 2024
1 parent d400388 commit 48f762a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
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

0 comments on commit 48f762a

Please sign in to comment.