Skip to content

Commit

Permalink
refactor(benchmark): move sampleCount and median to `BenchmarkRes…
Browse files Browse the repository at this point in the history
…ult`
  • Loading branch information
hi-ogawa committed Sep 21, 2024
1 parent 2a50464 commit 637fe11
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 20 deletions.
17 changes: 2 additions & 15 deletions packages/vitest/src/node/reporters/benchmark/table/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,8 @@ interface FormattedBenchmarkGroup {
benchmarks: FormattedBenchmarkResult[]
}

export type FormattedBenchmarkResult = Omit<BenchmarkResult, 'samples'> & {
export type FormattedBenchmarkResult = BenchmarkResult & {
id: string
sampleCount: number
median: number
}

function createFormattedBenchmarkReport(files: File[]) {
Expand All @@ -183,18 +181,7 @@ function createFormattedBenchmarkReport(files: File[]) {
for (const t of task.tasks) {
const benchmark = t.meta.benchmark && t.result?.benchmark
if (benchmark) {
const { samples, ...rest } = benchmark
benchmarks.push({
id: t.id,
sampleCount: samples.length,
median:
samples.length % 2
? samples[Math.floor(samples.length / 2)]
: (samples[samples.length / 2]
+ samples[samples.length / 2 - 1])
/ 2,
...rest,
})
benchmarks.push({ id: t.id, ...benchmark, samples: [] })
}
}
if (benchmarks.length) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function renderBenchmarkItems(result: BenchmarkResult) {
formatNumber(result.p995 || 0),
formatNumber(result.p999 || 0),
${(result.rme || 0).toFixed(2)}%`,
result.samples.length.toString(),
(result.sampleCount || 0).toString(),
]
}

Expand Down Expand Up @@ -124,10 +124,7 @@ export function renderTree(
}
const baseline = options.compare?.[t.id]
if (baseline) {
benchMap[t.id].baseline = {
...baseline,
samples: Array.from({ length: baseline.sampleCount }),
}
benchMap[t.id].baseline = baseline
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions packages/vitest/src/runtime/runners/benchmark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,14 @@ async function runBenchmarkSuite(suite: Suite, runner: NodeBenchmarkRunner) {
if (benchmark) {
const result = benchmark.result!.benchmark!
result.rank = Number(idx) + 1

const samples = result.samples
result.sampleCount = samples.length
result.median = samples.length % 2
? samples[Math.floor(samples.length / 2)]
: (samples[samples.length / 2] + samples[samples.length / 2 - 1]) / 2
// TODO: config to clear samples before sending results
// result.samples = []
updateTask(benchmark)
}
})
Expand Down
2 changes: 2 additions & 0 deletions packages/vitest/src/runtime/types/benchmark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export interface Benchmark extends Custom {
export interface BenchmarkResult extends TinybenchResult {
name: string
rank: number
sampleCount: number
median: number
}

export type BenchFunction = (this: BenchFactory) => Promise<void> | void
Expand Down

0 comments on commit 637fe11

Please sign in to comment.