|
| 1 | +import type { Task, TaskResultPack } from '@vitest/runner' |
| 2 | +import type { Vitest } from '../../core' |
| 3 | +import fs from 'node:fs' |
| 4 | +import { getFullName } from '@vitest/runner/utils' |
| 5 | +import * as pathe from 'pathe' |
| 6 | +import c from 'tinyrainbow' |
| 7 | +import { DefaultReporter } from '../default' |
| 8 | +import { formatProjectName, getStateSymbol } from '../renderers/utils' |
| 9 | +import { createBenchmarkJsonReport, flattenFormattedBenchmarkReport } from './json-formatter' |
| 10 | +import { renderTable } from './tableRender' |
| 11 | + |
| 12 | +export class BenchmarkReporter extends DefaultReporter { |
| 13 | + compare?: Parameters<typeof renderTable>[0]['compare'] |
| 14 | + |
| 15 | + async onInit(ctx: Vitest) { |
| 16 | + super.onInit(ctx) |
| 17 | + |
| 18 | + if (this.ctx.config.benchmark?.compare) { |
| 19 | + const compareFile = pathe.resolve( |
| 20 | + this.ctx.config.root, |
| 21 | + this.ctx.config.benchmark?.compare, |
| 22 | + ) |
| 23 | + try { |
| 24 | + this.compare = flattenFormattedBenchmarkReport( |
| 25 | + JSON.parse(await fs.promises.readFile(compareFile, 'utf-8')), |
| 26 | + ) |
| 27 | + } |
| 28 | + catch (e) { |
| 29 | + this.error(`Failed to read '${compareFile}'`, e) |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + onTaskUpdate(packs: TaskResultPack[]): void { |
| 35 | + for (const pack of packs) { |
| 36 | + const task = this.ctx.state.idMap.get(pack[0]) |
| 37 | + |
| 38 | + if (task?.type === 'suite' && task.result?.state !== 'run') { |
| 39 | + task.tasks.filter(task => task.result?.benchmark) |
| 40 | + .sort((benchA, benchB) => benchA.result!.benchmark!.mean - benchB.result!.benchmark!.mean) |
| 41 | + .forEach((bench, idx) => { |
| 42 | + bench.result!.benchmark!.rank = Number(idx) + 1 |
| 43 | + }) |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + super.onTaskUpdate(packs) |
| 48 | + } |
| 49 | + |
| 50 | + printTask(task: Task) { |
| 51 | + if (task?.type !== 'suite' || !task.result?.state || task.result?.state === 'run' || task.result?.state === 'queued') { |
| 52 | + return |
| 53 | + } |
| 54 | + |
| 55 | + const benches = task.tasks.filter(t => t.meta.benchmark) |
| 56 | + const duration = task.result.duration |
| 57 | + |
| 58 | + if (benches.length > 0 && benches.every(t => t.result?.state !== 'run' && t.result?.state !== 'queued')) { |
| 59 | + let title = `\n ${getStateSymbol(task)} ${formatProjectName(task.file.projectName)}${getFullName(task, c.dim(' > '))}` |
| 60 | + |
| 61 | + if (duration != null && duration > this.ctx.config.slowTestThreshold) { |
| 62 | + title += c.yellow(` ${Math.round(duration)}${c.dim('ms')}`) |
| 63 | + } |
| 64 | + |
| 65 | + this.log(title) |
| 66 | + this.log(renderTable({ |
| 67 | + tasks: benches, |
| 68 | + level: 1, |
| 69 | + shallow: true, |
| 70 | + columns: this.ctx.logger.getColumns(), |
| 71 | + compare: this.compare, |
| 72 | + showHeap: this.ctx.config.logHeapUsage, |
| 73 | + slowTestThreshold: this.ctx.config.slowTestThreshold, |
| 74 | + })) |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + async onFinished(files = this.ctx.state.getFiles(), errors = this.ctx.state.getUnhandledErrors()) { |
| 79 | + super.onFinished(files, errors) |
| 80 | + |
| 81 | + // write output for future comparison |
| 82 | + let outputFile = this.ctx.config.benchmark?.outputJson |
| 83 | + |
| 84 | + if (outputFile) { |
| 85 | + outputFile = pathe.resolve(this.ctx.config.root, outputFile) |
| 86 | + const outputDirectory = pathe.dirname(outputFile) |
| 87 | + |
| 88 | + if (!fs.existsSync(outputDirectory)) { |
| 89 | + await fs.promises.mkdir(outputDirectory, { recursive: true }) |
| 90 | + } |
| 91 | + |
| 92 | + const output = createBenchmarkJsonReport(files) |
| 93 | + await fs.promises.writeFile(outputFile, JSON.stringify(output, null, 2)) |
| 94 | + this.log(`Benchmark report written to ${outputFile}`) |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments