fix: introduce and use BenchLike interface to decouple task from Benc… · tinylibs/tinybench@c5a0729

1-

import type { Bench } from './bench'

21

import type {

32

AddEventListenerOptionsArgument,

3+

BenchLike,

44

EventListener,

55

EventListenerObject,

66

Fn,

@@ -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

*/

112112

readonly #signal: AbortSignal | undefined

113113114-

constructor (bench: Bench, name: string, fn: Fn, fnOpts: FnOptions = {}) {

114+

constructor (bench: BenchLike, name: string, fn: Fn, fnOpts: FnOptions = {}) {

115115

super()

116116

this.#bench = bench

117117

this.#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) {

148148

this.#onAbort()

149149

} else {

150-

this.#bench.opts.signal.addEventListener(

150+

this.#bench.signal.addEventListener(

151151

'abort',

152152

this.#onAbort.bind(this),

153153

{ once: true }

@@ -179,13 +179,13 @@ export class Task extends EventTarget {

179179

}

180180

this.#result = { state: 'started' }

181181

this.dispatchEvent(new BenchEvent('start', this))

182-

await this.#bench.opts.setup(this, 'run')

182+

await this.#bench.setup(this, 'run')

183183

const { 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')

189189190190

this.#processRunResult({ error, latencySamples })

191191

@@ -209,19 +209,19 @@ export class Task extends EventTarget {

209209

this.#result = startedTaskResult

210210

this.dispatchEvent(new BenchEvent('start', this))

211211212-

const setupResult = this.#bench.opts.setup(this, 'run')

212+

const setupResult = this.#bench.setup(this, 'run')

213213

invariant(

214214

!isPromiseLike(setupResult),

215215

'`setup` function must be sync when using `runSync()`'

216216

)

217217218218

const { 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')

225225

invariant(

226226

!isPromiseLike(teardownResult),

227227

'`teardown` function must be sync when using `runSync()`'

@@ -241,13 +241,13 @@ export class Task extends EventTarget {

241241

return

242242

}

243243

this.dispatchEvent(new BenchEvent('warmup', this))

244-

await this.#bench.opts.setup(this, 'warmup')

244+

await this.#bench.setup(this, 'warmup')

245245

const { 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')

251251252252

this.#postWarmup(error)

253253

}

@@ -263,19 +263,19 @@ export class Task extends EventTarget {

263263264264

this.dispatchEvent(new BenchEvent('warmup', this))

265265266-

const setupResult = this.#bench.opts.setup(this, 'warmup')

266+

const setupResult = this.#bench.setup(this, 'warmup')

267267

invariant(

268268

!isPromiseLike(setupResult),

269269

'`setup` function must be sync when using `runSync()`'

270270

)

271271272272

const { 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')

279279

invariant(

280280

!isPromiseLike(teardownResult),

281281

'`teardown` function must be sync when using `runSync()`'

@@ -330,8 +330,8 @@ export class Task extends EventTarget {

330330

fn: benchmarkTask,

331331

iterations,

332332

limit: 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

}

445445446446

async #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

449449

const fnResult = await this.#fn.call(this)

450-

let taskTime = this.#bench.opts.now() - taskStart

450+

let taskTime = this.#bench.now() - taskStart

451451452452

const overriddenDuration = getOverriddenDurationFromFnResult(fnResult)

453453

if (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

462462

const fnResult = this.#fn.call(this)

463-

let taskTime = this.#bench.opts.now() - taskStart

463+

let taskTime = this.#bench.now() - taskStart

464464465465

invariant(

466466

!isPromiseLike(fnResult),

@@ -493,7 +493,7 @@ export class Task extends EventTarget {

493493

const ev = new BenchEvent('error', this, error)

494494

this.dispatchEvent(ev)

495495

this.#bench.dispatchEvent(ev)

496-

if (this.#bench.opts.throws) {

496+

if (this.#bench.throws) {

497497

throw error

498498

}

499499

}

@@ -553,7 +553,7 @@ export class Task extends EventTarget {

553553

const ev = new BenchEvent('error', this, error)

554554

this.dispatchEvent(ev)

555555

this.#bench.dispatchEvent(ev)

556-

if (this.#bench.opts.throws) {

556+

if (this.#bench.throws) {

557557

throw error

558558

}

559559

}