41 lines
881 B
JavaScript
41 lines
881 B
JavaScript
import { Benchmark as BenchmarkJS } from './initBenchmarkJS.js'
|
|
import { BenchmarkType } from './Benchmark.js'
|
|
|
|
export class BenchmarkJSBenchmark extends BenchmarkType {
|
|
async measure() {
|
|
this.constructSuite()
|
|
await this.runSuite()
|
|
return this.mean
|
|
}
|
|
|
|
constructSuite() {
|
|
this.suite = new BenchmarkJS.Suite()
|
|
this.suite.add(this.case.name, {
|
|
defer: true,
|
|
fn: this.resolvingFn.bind(this)
|
|
})
|
|
this.suite.on('cycle', this.onCycle.bind(this))
|
|
}
|
|
|
|
async resolvingFn(deferred) {
|
|
await this.case.fn()
|
|
deferred.resolve()
|
|
}
|
|
|
|
onCycle(ev) {
|
|
this.mean = ev.target.stats.mean * 1000
|
|
}
|
|
|
|
async runSuite() {
|
|
const suitePromise = this.promisifySuite()
|
|
this.suite.run({ async: true })
|
|
await suitePromise
|
|
}
|
|
|
|
promisifySuite() {
|
|
return new Promise(resolve => {
|
|
this.suite.on('complete', resolve)
|
|
})
|
|
}
|
|
}
|