Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DEV-6326 Allow custom functions to be benchmarked (#239)
DEV-6326 Allow custom functions to be benchmarked
- Loading branch information
1 parent
46e6cf3
commit dc1a59e
Showing
19 changed files
with
713 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import Benchmark from 'benchmark'; | ||
|
||
type testFunction = () => void; | ||
type setupFunction = () => void | Promise<void>; | ||
type teardownFunction = () => void | Promise<void>; | ||
|
||
export default abstract class BenchmarkCollection { | ||
protected readonly _benchmarks: { | ||
benchmark: Benchmark; | ||
setup?: setupFunction; | ||
teardown?: teardownFunction; | ||
}[] = []; | ||
|
||
protected readonly _comparisons: { | ||
benchmarks: Benchmark[]; | ||
name: string; | ||
setup?: setupFunction; | ||
teardown?: teardownFunction; | ||
}[] = []; | ||
|
||
public addBenchmark( | ||
name: string, | ||
test: testFunction, | ||
setup?: setupFunction, | ||
teardown?: teardownFunction | ||
): void { | ||
this._benchmarks.push({ | ||
benchmark: new Benchmark(name, test), | ||
// We do not use the setup and teardown which is offered within the API of benchmarkjs | ||
// as several attempts to get this working did not yield any successful results. | ||
setup, | ||
teardown, | ||
}); | ||
} | ||
|
||
public compareBenchmarks( | ||
name: string, | ||
setup?: setupFunction, | ||
teardown?: teardownFunction, | ||
...benchmarks: { | ||
name: string; | ||
test: testFunction; | ||
}[] | ||
): void { | ||
// We do not use the setup and teardown which is offered within the API of benchmarkjs | ||
// as several attempts to get this working did not yield any successful results. We also | ||
// allow only 1 setup and teardown as all functions which compare with one another should | ||
// use the same data to test with. | ||
const comparison = { name, benchmarks: [], setup, teardown }; | ||
for (const benchmark of benchmarks) { | ||
comparison.benchmarks.push(new Benchmark(benchmark.name, benchmark.test)); | ||
} | ||
this._comparisons.push(comparison); | ||
} | ||
} |
Oops, something went wrong.