kruonis
Version:
A tool to perform benchmarks on TS
112 lines (111 loc) • 3.19 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Benchmark = void 0;
const internal_1 = require("./internal");
/**
* The main class.
* A Benchmark consists of a group of tests that are run and compared regarding the time performances.
*/
class Benchmark {
/**
* Benchmark constructor
*
* @param properties This Benchmark properties used to create a properties object
*/
constructor(properties) {
/**
* This Benchmark properties
*/
this.properties = null;
/**
* The tests added by the user, to be run
*/
this.tests = [];
/**
* The results obtained in the previous run of this benchmark.
* Consists of a list of pairs test name - test stats
*/
this.results = [];
/**
* Function to run on the beginning of this benchmark
*/
this.onBegin = (benchmark) => { };
/**
* Function to run on the end of the benchmark (on the end of all tests)
*/
this.onTestBegin = (benchmark, test) => { };
/**
* Function to run on the end of the benchmark (on the end of all tests)
*/
this.onTestEnd = (benchmark, test) => { };
/**
* Function to run on the end of the benchmark (on the end of all tests)
*/
this.onEnd = (benchmark) => { };
this.properties = new internal_1.BenchmarkProperties(properties);
}
/**
* Get the @BenchmarkProperties used in this Benchmark, as an object
*/
getProperties() {
return this.properties;
}
/**
* Get the tests that perform on this Benchmark
*/
getTests() {
return this.tests;
}
/**
* Get the results previously obtained on this Benchmark
* @return list of pairs test name - test stats
*/
getResults() {
return this.results;
}
/**
* Add a new test to this Benchmark
*
* @param testName the test name
* @param fn The function to run on this test
* @return the created @Test
*/
add(test) {
this.tests.push(test);
return this;
}
;
/**
* Add an event to this Benchmark. Possibilities:
* - 'onBegin'
* - 'onTestBegin'
* - 'onTestEnd'
* - 'onEnd'
*
* @param eventName The name of the event to be altered
* @param fn The function that will run when the event is called
*/
on(eventName, fn) {
if (eventName.substr(0, 2) == "on" && this.hasOwnProperty(eventName))
this[eventName] = fn;
return this;
}
/**
* Run this Benchmark list of @Test
*
* @return results obtained as a list of pairs test name - test stats
*/
run() {
this.onBegin(this);
for (let test of this.tests) {
this.onTestBegin(this, test);
test.run(this.getProperties());
this.results.push([test.name, test.getStats()]);
this.onTestEnd(this, test);
}
this.onEnd(this);
return this.results;
}
;
}
exports.Benchmark = Benchmark;