fix: introduce and use BenchLike interface to decouple task from Benc… · tinylibs/tinybench@c5a0729
1-import type { Bench } from './bench'
21import type {
32AddEventListenerOptionsArgument,
3+BenchLike,
44EventListener,
55EventListenerObject,
66Fn,
@@ -79,7 +79,7 @@ export class Task extends EventTarget {
7979/**
8080 * The Bench instance reference
8181 */
82-readonly #bench: Bench
82+readonly #bench: BenchLike
83838484/**
8585 * The task function
@@ -111,7 +111,7 @@ export class Task extends EventTarget {
111111 */
112112readonly #signal: AbortSignal | undefined
113113114-constructor (bench: Bench, name: string, fn: Fn, fnOpts: FnOptions = {}) {
114+constructor (bench: BenchLike, name: string, fn: Fn, fnOpts: FnOptions = {}) {
115115super()
116116this.#bench = bench
117117this.#name = name
@@ -143,11 +143,11 @@ export class Task extends EventTarget {
143143}
144144}
145145146-if (this.#bench.opts.signal) {
147-if (this.#bench.opts.signal.aborted) {
146+if (this.#bench.signal) {
147+if (this.#bench.signal.aborted) {
148148this.#onAbort()
149149} else {
150-this.#bench.opts.signal.addEventListener(
150+this.#bench.signal.addEventListener(
151151'abort',
152152this.#onAbort.bind(this),
153153{ once: true }
@@ -179,13 +179,13 @@ export class Task extends EventTarget {
179179}
180180this.#result = { state: 'started' }
181181this.dispatchEvent(new BenchEvent('start', this))
182-await this.#bench.opts.setup(this, 'run')
182+await this.#bench.setup(this, 'run')
183183const { error, samples: latencySamples } = await this.#benchmark(
184184'run',
185-this.#bench.opts.time,
186-this.#bench.opts.iterations
185+this.#bench.time,
186+this.#bench.iterations
187187)
188-await this.#bench.opts.teardown(this, 'run')
188+await this.#bench.teardown(this, 'run')
189189190190this.#processRunResult({ error, latencySamples })
191191@@ -209,19 +209,19 @@ export class Task extends EventTarget {
209209this.#result = startedTaskResult
210210this.dispatchEvent(new BenchEvent('start', this))
211211212-const setupResult = this.#bench.opts.setup(this, 'run')
212+const setupResult = this.#bench.setup(this, 'run')
213213invariant(
214214!isPromiseLike(setupResult),
215215'`setup` function must be sync when using `runSync()`'
216216)
217217218218const { error, samples: latencySamples } = this.#benchmarkSync(
219219'run',
220-this.#bench.opts.time,
221-this.#bench.opts.iterations
220+this.#bench.time,
221+this.#bench.iterations
222222)
223223224-const teardownResult = this.#bench.opts.teardown(this, 'run')
224+const teardownResult = this.#bench.teardown(this, 'run')
225225invariant(
226226!isPromiseLike(teardownResult),
227227'`teardown` function must be sync when using `runSync()`'
@@ -241,13 +241,13 @@ export class Task extends EventTarget {
241241return
242242}
243243this.dispatchEvent(new BenchEvent('warmup', this))
244-await this.#bench.opts.setup(this, 'warmup')
244+await this.#bench.setup(this, 'warmup')
245245const { error } = (await this.#benchmark(
246246'warmup',
247-this.#bench.opts.warmupTime,
248-this.#bench.opts.warmupIterations
247+this.#bench.warmupTime,
248+this.#bench.warmupIterations
249249))
250-await this.#bench.opts.teardown(this, 'warmup')
250+await this.#bench.teardown(this, 'warmup')
251251252252this.#postWarmup(error)
253253}
@@ -263,19 +263,19 @@ export class Task extends EventTarget {
263263264264this.dispatchEvent(new BenchEvent('warmup', this))
265265266-const setupResult = this.#bench.opts.setup(this, 'warmup')
266+const setupResult = this.#bench.setup(this, 'warmup')
267267invariant(
268268!isPromiseLike(setupResult),
269269'`setup` function must be sync when using `runSync()`'
270270)
271271272272const { error } = this.#benchmarkSync(
273273'warmup',
274-this.#bench.opts.warmupTime,
275-this.#bench.opts.warmupIterations
274+this.#bench.warmupTime,
275+this.#bench.warmupIterations
276276)
277277278-const teardownResult = this.#bench.opts.teardown(this, 'warmup')
278+const teardownResult = this.#bench.teardown(this, 'warmup')
279279invariant(
280280!isPromiseLike(teardownResult),
281281'`teardown` function must be sync when using `runSync()`'
@@ -330,8 +330,8 @@ export class Task extends EventTarget {
330330fn: benchmarkTask,
331331 iterations,
332332limit: Math.max(1, Math.floor(this.#bench.threshold)),
333-now: this.#bench.opts.now,
334-signal: this.#signal ?? this.#bench.opts.signal,
333+now: this.#bench.now,
334+signal: this.#signal ?? this.#bench.signal,
335335 time,
336336})
337337} catch (error) {
@@ -444,10 +444,10 @@ export class Task extends EventTarget {
444444}
445445446446async #measureOnce (): Promise<{ fnResult: ReturnType<Fn>, taskTime: number }> {
447-const taskStart = this.#bench.opts.now()
447+const taskStart = this.#bench.now()
448448// eslint-disable-next-line no-useless-call
449449const fnResult = await this.#fn.call(this)
450-let taskTime = this.#bench.opts.now() - taskStart
450+let taskTime = this.#bench.now() - taskStart
451451452452const overriddenDuration = getOverriddenDurationFromFnResult(fnResult)
453453if (overriddenDuration !== undefined) {
@@ -457,10 +457,10 @@ export class Task extends EventTarget {
457457}
458458459459 #measureOnceSync (): { fnResult: ReturnType<Fn>, taskTime: number } {
460-const taskStart = this.#bench.opts.now()
460+const taskStart = this.#bench.now()
461461// eslint-disable-next-line no-useless-call
462462const fnResult = this.#fn.call(this)
463-let taskTime = this.#bench.opts.now() - taskStart
463+let taskTime = this.#bench.now() - taskStart
464464465465invariant(
466466!isPromiseLike(fnResult),
@@ -493,7 +493,7 @@ export class Task extends EventTarget {
493493const ev = new BenchEvent('error', this, error)
494494this.dispatchEvent(ev)
495495this.#bench.dispatchEvent(ev)
496-if (this.#bench.opts.throws) {
496+if (this.#bench.throws) {
497497throw error
498498}
499499}
@@ -553,7 +553,7 @@ export class Task extends EventTarget {
553553const ev = new BenchEvent('error', this, error)
554554this.dispatchEvent(ev)
555555this.#bench.dispatchEvent(ev)
556-if (this.#bench.opts.throws) {
556+if (this.#bench.throws) {
557557throw error
558558}
559559}