Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: bench had options #2295

Merged
merged 2 commits into from Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion packages/vitest/src/runtime/map.ts
@@ -1,8 +1,9 @@
import type { Awaitable, BenchFunction, Benchmark, Suite, SuiteHooks, Test } from '../types'
import type { Awaitable, BenchFunction, BenchOptions, Benchmark, Suite, SuiteHooks, Test } from '../types'

// use WeakMap here to make the Test and Suite object serializable
const fnMap = new WeakMap()
const hooksMap = new WeakMap()
const benchOptsMap = new WeakMap()

export function setFn(key: Test | Benchmark, fn: (() => Awaitable<void>) | BenchFunction) {
fnMap.set(key, fn)
Expand All @@ -23,3 +24,11 @@ export function getHooks(key: Suite): SuiteHooks {
export function isTest(task: Test | Benchmark): task is Test {
return task.type === 'test'
}

export function setBenchOptions(key: Benchmark, val: BenchOptions) {
benchOptsMap.set(key, val)
}

export function getBenchOptions(key: Benchmark): BenchOptions {
return benchOptsMap.get(key)
}
5 changes: 3 additions & 2 deletions packages/vitest/src/runtime/run.ts
Expand Up @@ -6,7 +6,7 @@ import { clearTimeout, createDefer, getFullName, getWorkerState, hasFailed, hasT
import { getState, setState } from '../integrations/chai/jest-expect'
import { GLOBAL_EXPECT } from '../integrations/chai/constants'
import { takeCoverageInsideWorker } from '../integrations/coverage'
import { getFn, getHooks } from './map'
import { getBenchOptions, getFn, getHooks } from './map'
import { rpc } from './rpc'
import { collectTests } from './collect'
import { processError } from './error'
Expand Down Expand Up @@ -366,7 +366,8 @@ async function runBenchmarkSuite(suite: Suite) {
}
updateTask(suite)
benchmarkGroup.forEach((benchmark, idx) => {
const benchmarkInstance = new Bench(benchmark.options)
const options = getBenchOptions(benchmark)
const benchmarkInstance = new Bench(options)

const benchmarkFn = getFn(benchmark)

Expand Down
4 changes: 2 additions & 2 deletions packages/vitest/src/runtime/suite.ts
Expand Up @@ -3,7 +3,7 @@ import type { BenchFunction, BenchOptions, Benchmark, BenchmarkAPI, File, RunMod
import { getWorkerState, isObject, isRunningInBenchmark, isRunningInTest, noop } from '../utils'
import { createChainable } from './chain'
import { collectTask, collectorContext, createTestContext, runWithSuite, withTimeout } from './context'
import { getHooks, setFn, setHooks } from './map'
import { getHooks, setBenchOptions, setFn, setHooks } from './map'

// apis
export const suite = createSuite()
Expand Down Expand Up @@ -106,11 +106,11 @@ function createSuiteCollector(name: string, factory: SuiteFactory = () => { }, m
id: '',
name,
mode,
options,
suite: undefined!,
}

setFn(benchmark, fn)
setBenchOptions(benchmark, options)
tasks.push(benchmark)
})

Expand Down
1 change: 0 additions & 1 deletion packages/vitest/src/types/benchmark.ts
Expand Up @@ -43,7 +43,6 @@ export interface Benchmark extends TaskBase {
result?: TaskResult
fails?: boolean
task?: BenchTask
options: BenchOptions
}

export interface BenchmarkResult extends TinybenchResult {
Expand Down
7 changes: 7 additions & 0 deletions test/benchmark/test/base.bench.ts
Expand Up @@ -31,6 +31,13 @@ function timeout(time: number) {
describe('timeout', () => {
bench('timeout100', async () => {
await timeout(100)
}, {
setup() {

},
teardown() {

},
})

bench('timeout75', async () => {
Expand Down