Skip to content

Commit edad9b1

Browse files
authored
fix(benchmark): don't fail when running correct benchmarks (#3629)
1 parent d756ee2 commit edad9b1

File tree

5 files changed

+50
-36
lines changed

5 files changed

+50
-36
lines changed

packages/vitest/src/runtime/runners/benchmark.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ function createBenchmarkResult(name: string): BenchmarkResult {
2424
} as BenchmarkResult
2525
}
2626

27+
const benchmarkTasks = new WeakMap<Benchmark, import('tinybench').Task>()
28+
2729
async function runBenchmarkSuite(suite: Suite, runner: VitestRunner) {
2830
const { Task, Bench } = await importTinybench()
2931
const start = performance.now()
@@ -67,12 +69,13 @@ async function runBenchmarkSuite(suite: Suite, runner: VitestRunner) {
6769
benchmarkMap[id] = benchmark
6870

6971
const task = new Task(benchmarkInstance, id, benchmarkFn)
70-
benchmark.meta.task = task
72+
benchmarkTasks.set(benchmark, task)
7173
updateTask(benchmark)
7274
})
7375

7476
benchmarkGroup.forEach((benchmark) => {
75-
benchmark.meta.task!.addEventListener('complete', (e) => {
77+
const task = benchmarkTasks.get(benchmark)!
78+
task.addEventListener('complete', (e) => {
7679
const task = e.task
7780
const _benchmark = benchmarkMap[task.name || '']
7881
if (_benchmark) {
@@ -82,7 +85,7 @@ async function runBenchmarkSuite(suite: Suite, runner: VitestRunner) {
8285
updateTask(_benchmark)
8386
}
8487
})
85-
benchmark.meta.task!.addEventListener('error', (e) => {
88+
task.addEventListener('error', (e) => {
8689
const task = e.task
8790
const _benchmark = benchmarkMap[task.name || '']
8891
defer.reject(_benchmark ? task.result!.error : e)
@@ -91,10 +94,11 @@ async function runBenchmarkSuite(suite: Suite, runner: VitestRunner) {
9194

9295
const tasks: BenchTask[] = []
9396
for (const benchmark of benchmarkGroup) {
94-
await benchmark.meta.task!.warmup()
97+
const task = benchmarkTasks.get(benchmark)!
98+
await task.warmup()
9599
const { setTimeout } = getSafeTimers()
96100
tasks.push(await new Promise<BenchTask>(resolve => setTimeout(async () => {
97-
resolve(await benchmark.meta.task!.run())
101+
resolve(await task.run())
98102
})))
99103
}
100104

packages/vitest/src/types/benchmark.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ export interface BenchmarkUserOptions {
4242
export interface Benchmark extends TaskCustom {
4343
meta: {
4444
benchmark: true
45-
task?: BenchTask
4645
result?: BenchTaskResult
4746
}
4847
}

test/benchmark/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "@vitest/benchmark",
33
"private": true,
44
"scripts": {
5-
"test": "node test.mjs",
5+
"test": "node --test specs/ && echo '1'",
66
"bench:json": "vitest bench --reporter=json",
77
"bench": "vitest bench"
88
},

test/benchmark/specs/runner.test.mjs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { existsSync, rmSync } from 'node:fs'
2+
import test from 'node:test'
3+
import * as assert from 'node:assert'
4+
import { readFile } from 'node:fs/promises'
5+
import { startVitest } from 'vitest/node'
6+
7+
if (existsSync('./bench.json'))
8+
rmSync('./bench.json')
9+
10+
try {
11+
await startVitest('benchmark', ['base.bench', 'mode.bench', 'only.bench'], {
12+
watch: false,
13+
})
14+
}
15+
catch (error) {
16+
console.error(error)
17+
process.exit(1)
18+
}
19+
20+
const benchResult = await readFile('./bench.json', 'utf-8')
21+
const resultJson = JSON.parse(benchResult)
22+
23+
await test('benchmarks are actually running', async () => {
24+
assert.ok(resultJson.testResults.sort, 'sort is in results')
25+
assert.ok(resultJson.testResults.timeout, 'timeout is in results')
26+
assert.ok(resultJson.testResults.a0, 'a0 is in results')
27+
assert.ok(resultJson.testResults.c1, 'c1 is in results')
28+
assert.ok(resultJson.testResults.a2, 'a2 is in results')
29+
assert.ok(resultJson.testResults.b3, 'b3 is in results')
30+
assert.ok(resultJson.testResults.b4, 'b4 is in results')
31+
})
32+
33+
await test('doesn\'t have skipped tests', () => {
34+
assert.doesNotMatch(benchResult, /skip/, 'contains skipped benchmarks')
35+
36+
const skippedBenches = ['s0', 's1', 's2', 's3', 'sb4', 's4']
37+
const todoBenches = ['unimplemented suite', 'unimplemented test']
38+
39+
assert.ok(skippedBenches.concat(todoBenches).every(b => !benchResult.includes(b)), 'contains skipped benchmarks')
40+
})

test/benchmark/test.mjs

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)