Skip to content

Commit

Permalink
perf(reporters): update summary only when needed (#7291)
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio authored Jan 20, 2025
1 parent 99732d9 commit 7f36b6f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
3 changes: 3 additions & 0 deletions packages/vitest/src/node/reporters/dot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,13 @@ export class DotReporter extends BaseReporter {
return
}
this.tests.set(test.id, test.result().state || 'run')
this.renderer?.schedule()
}

onTestCaseResult(test: TestCase) {
this.finishedTests.add(test.id)
this.tests.set(test.id, test.result().state || 'skipped')
this.renderer?.schedule()
}

onTestModuleEnd() {
Expand Down Expand Up @@ -104,6 +106,7 @@ export class DotReporter extends BaseReporter {
}

this.ctx.logger.log(formatTests(states))
this.renderer?.schedule()
}

private createSummary() {
Expand Down
21 changes: 18 additions & 3 deletions packages/vitest/src/node/reporters/renderers/windowedRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Writable } from 'node:stream'
import type { Vitest } from '../../core'
import { stripVTControlCharacters } from 'node:util'

const DEFAULT_RENDER_INTERVAL = 16
const DEFAULT_RENDER_INTERVAL_MS = 1_000

const ESC = '\x1B['
const CLEAR_LINE = `${ESC}K`
Expand All @@ -27,14 +27,15 @@ export class WindowRenderer {
private streams!: Record<StreamType, Vitest['logger']['outputStream' | 'errorStream']['write']>
private buffer: { type: StreamType; message: string }[] = []
private renderInterval: NodeJS.Timeout | undefined = undefined
private renderScheduled = false

private windowHeight = 0
private finished = false
private cleanups: (() => void)[] = []

constructor(options: Options) {
this.options = {
interval: DEFAULT_RENDER_INTERVAL,
interval: DEFAULT_RENDER_INTERVAL_MS,
...options,
}

Expand All @@ -59,7 +60,7 @@ export class WindowRenderer {

start() {
this.finished = false
this.renderInterval = setInterval(() => this.flushBuffer(), this.options.interval).unref()
this.renderInterval = setInterval(() => this.schedule(), this.options.interval).unref()
}

stop() {
Expand All @@ -77,6 +78,20 @@ export class WindowRenderer {
clearInterval(this.renderInterval)
}

/**
* Queue new render update
*/
schedule() {
if (!this.renderScheduled) {
this.renderScheduled = true
this.flushBuffer()

setTimeout(() => {
this.renderScheduled = false
}, 100).unref()
}
}

private flushBuffer() {
if (this.buffer.length === 0) {
return this.render()
Expand Down
6 changes: 6 additions & 0 deletions packages/vitest/src/node/reporters/summary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ export class SummaryReporter implements Reporter {
}

this.runningModules.set(module.id, initializeStats(module))
this.renderer.schedule()
}

onTestModuleCollected(module: TestModule) {
Expand All @@ -124,6 +125,7 @@ export class SummaryReporter implements Reporter {
stats.total = total

this.maxParallelTests = Math.max(this.maxParallelTests, this.runningModules.size)
this.renderer.schedule()
}

onHookStart(options: ReportedHookContext) {
Expand Down Expand Up @@ -213,6 +215,8 @@ export class SummaryReporter implements Reporter {
else if (!result?.state || result?.state === 'skipped') {
this.tests.skipped++
}

this.renderer.schedule()
}

onTestModuleEnd(module: TestModule) {
Expand Down Expand Up @@ -247,6 +251,8 @@ export class SummaryReporter implements Reporter {
// Remove finished test immediatelly.
this.removeTestModule(module.id)
}

this.renderer.schedule()
}

private getHookStats({ entity }: ReportedHookContext) {
Expand Down

0 comments on commit 7f36b6f

Please sign in to comment.