-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(benchmark): rewrite reporter without
log-update
(#7019)
- Loading branch information
1 parent
b700d26
commit 6d23f4b
Showing
29 changed files
with
452 additions
and
923 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
import { VerboseReporter } from '../verbose' | ||
import { TableReporter } from './table' | ||
import { BenchmarkReporter } from './reporter' | ||
import { VerboseBenchmarkReporter } from './verbose' | ||
|
||
export const BenchmarkReportsMap = { | ||
default: TableReporter, | ||
verbose: VerboseReporter, | ||
default: BenchmarkReporter, | ||
verbose: VerboseBenchmarkReporter, | ||
} | ||
|
||
export type BenchmarkBuiltinReporters = keyof typeof BenchmarkReportsMap |
69 changes: 69 additions & 0 deletions
69
packages/vitest/src/node/reporters/benchmark/json-formatter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import type { File } from '@vitest/runner' | ||
import type { BenchmarkResult } from '../../../runtime/types/benchmark' | ||
import { getFullName, getTasks } from '@vitest/runner/utils' | ||
|
||
interface Report { | ||
files: { | ||
filepath: string | ||
groups: Group[] | ||
}[] | ||
} | ||
|
||
interface Group { | ||
fullName: string | ||
benchmarks: FormattedBenchmarkResult[] | ||
} | ||
|
||
export type FormattedBenchmarkResult = BenchmarkResult & { | ||
id: string | ||
} | ||
|
||
export function createBenchmarkJsonReport(files: File[]) { | ||
const report: Report = { files: [] } | ||
|
||
for (const file of files) { | ||
const groups: Group[] = [] | ||
|
||
for (const task of getTasks(file)) { | ||
if (task?.type === 'suite') { | ||
const benchmarks: FormattedBenchmarkResult[] = [] | ||
|
||
for (const t of task.tasks) { | ||
const benchmark = t.meta.benchmark && t.result?.benchmark | ||
|
||
if (benchmark) { | ||
benchmarks.push({ id: t.id, ...benchmark, samples: [] }) | ||
} | ||
} | ||
|
||
if (benchmarks.length) { | ||
groups.push({ | ||
fullName: getFullName(task, ' > '), | ||
benchmarks, | ||
}) | ||
} | ||
} | ||
} | ||
|
||
report.files.push({ | ||
filepath: file.filepath, | ||
groups, | ||
}) | ||
} | ||
|
||
return report | ||
} | ||
|
||
export function flattenFormattedBenchmarkReport(report: Report) { | ||
const flat: Record<FormattedBenchmarkResult['id'], FormattedBenchmarkResult> = {} | ||
|
||
for (const file of report.files) { | ||
for (const group of file.groups) { | ||
for (const t of group.benchmarks) { | ||
flat[t.id] = t | ||
} | ||
} | ||
} | ||
|
||
return flat | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import type { Task, TaskResultPack } from '@vitest/runner' | ||
import type { Vitest } from '../../core' | ||
import fs from 'node:fs' | ||
import { getFullName } from '@vitest/runner/utils' | ||
import * as pathe from 'pathe' | ||
import c from 'tinyrainbow' | ||
import { DefaultReporter } from '../default' | ||
import { formatProjectName, getStateSymbol } from '../renderers/utils' | ||
import { createBenchmarkJsonReport, flattenFormattedBenchmarkReport } from './json-formatter' | ||
import { renderTable } from './tableRender' | ||
|
||
export class BenchmarkReporter extends DefaultReporter { | ||
compare?: Parameters<typeof renderTable>[0]['compare'] | ||
|
||
async onInit(ctx: Vitest) { | ||
super.onInit(ctx) | ||
|
||
if (this.ctx.config.benchmark?.compare) { | ||
const compareFile = pathe.resolve( | ||
this.ctx.config.root, | ||
this.ctx.config.benchmark?.compare, | ||
) | ||
try { | ||
this.compare = flattenFormattedBenchmarkReport( | ||
JSON.parse(await fs.promises.readFile(compareFile, 'utf-8')), | ||
) | ||
} | ||
catch (e) { | ||
this.error(`Failed to read '${compareFile}'`, e) | ||
} | ||
} | ||
} | ||
|
||
onTaskUpdate(packs: TaskResultPack[]): void { | ||
for (const pack of packs) { | ||
const task = this.ctx.state.idMap.get(pack[0]) | ||
|
||
if (task?.type === 'suite' && task.result?.state !== 'run') { | ||
task.tasks.filter(task => task.result?.benchmark) | ||
.sort((benchA, benchB) => benchA.result!.benchmark!.mean - benchB.result!.benchmark!.mean) | ||
.forEach((bench, idx) => { | ||
bench.result!.benchmark!.rank = Number(idx) + 1 | ||
}) | ||
} | ||
} | ||
|
||
super.onTaskUpdate(packs) | ||
} | ||
|
||
printTask(task: Task) { | ||
if (task?.type !== 'suite' || !task.result?.state || task.result?.state === 'run' || task.result?.state === 'queued') { | ||
return | ||
} | ||
|
||
const benches = task.tasks.filter(t => t.meta.benchmark) | ||
const duration = task.result.duration | ||
|
||
if (benches.length > 0 && benches.every(t => t.result?.state !== 'run' && t.result?.state !== 'queued')) { | ||
let title = `\n ${getStateSymbol(task)} ${formatProjectName(task.file.projectName)}${getFullName(task, c.dim(' > '))}` | ||
|
||
if (duration != null && duration > this.ctx.config.slowTestThreshold) { | ||
title += c.yellow(` ${Math.round(duration)}${c.dim('ms')}`) | ||
} | ||
|
||
this.log(title) | ||
this.log(renderTable({ | ||
tasks: benches, | ||
level: 1, | ||
shallow: true, | ||
columns: this.ctx.logger.getColumns(), | ||
compare: this.compare, | ||
showHeap: this.ctx.config.logHeapUsage, | ||
slowTestThreshold: this.ctx.config.slowTestThreshold, | ||
})) | ||
} | ||
} | ||
|
||
async onFinished(files = this.ctx.state.getFiles(), errors = this.ctx.state.getUnhandledErrors()) { | ||
super.onFinished(files, errors) | ||
|
||
// write output for future comparison | ||
let outputFile = this.ctx.config.benchmark?.outputJson | ||
|
||
if (outputFile) { | ||
outputFile = pathe.resolve(this.ctx.config.root, outputFile) | ||
const outputDirectory = pathe.dirname(outputFile) | ||
|
||
if (!fs.existsSync(outputDirectory)) { | ||
await fs.promises.mkdir(outputDirectory, { recursive: true }) | ||
} | ||
|
||
const output = createBenchmarkJsonReport(files) | ||
await fs.promises.writeFile(outputFile, JSON.stringify(output, null, 2)) | ||
this.log(`Benchmark report written to ${outputFile}`) | ||
} | ||
} | ||
} |
Oops, something went wrong.