2019-07-19 15:15:05 +02:00

49 lines
1.0 KiB
JavaScript

export class BenchmarkSuite {
constructor() {
this.reset()
}
reset() {
this.cases = []
this.stats = {}
}
setBenchmarkType(benchmarktype) {
this.benchmarktype = benchmarktype
}
setCases(cases) {
this.stats = {}
this.cases = cases
for (let c of this.cases) {
this.stats[c.name] = []
}
}
onCycle(i, count, stats) {}
async run() {
let mean
let i = 0
const count = this.cases.length
this.onCycle(i, count, this.stats)
for (const benchCase of this.cases) {
this.benchmarktype.setCase(benchCase)
mean = await this.benchmarktype.run()
this.stats[benchCase.name] = [...this.stats[benchCase.name], mean]
i++
this.onCycle(i, count, this.stats)
await freeEventLoop()
}
}
}
/**
* Promise that uses setTimeout to resolve.
* This is a hack to free the eventloop from microtasks.
* Without this rendering blocks during benchmarks.
*/
async function freeEventLoop() {
return new Promise(resolve => setTimeout(resolve, 0))
}