2018-03-11 15:44:32 +00:00
|
|
|
import * as process from 'process';
|
2017-08-16 12:29:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* easy high resolution time measurement
|
|
|
|
*/
|
|
|
|
export class HrtMeasurement {
|
2019-06-17 14:54:39 +00:00
|
|
|
public nanoSeconds: number = null;
|
|
|
|
public milliSeconds: number = null;
|
2018-03-11 15:44:32 +00:00
|
|
|
private _hrTimeStart = null;
|
|
|
|
private _hrTimeStopDiff = null;
|
|
|
|
private _started: boolean = false;
|
2017-08-16 12:29:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* start the measurement
|
|
|
|
*/
|
2019-06-17 14:54:39 +00:00
|
|
|
public start() {
|
2018-03-11 15:44:32 +00:00
|
|
|
this._started = true;
|
|
|
|
this._hrTimeStart = process.hrtime();
|
2017-08-16 12:29:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* stop the measurement
|
|
|
|
*/
|
2019-06-17 14:54:39 +00:00
|
|
|
public stop() {
|
2017-08-16 12:29:12 +00:00
|
|
|
if (this._started === false) {
|
2018-03-11 15:44:32 +00:00
|
|
|
console.log("Hasn't started yet");
|
|
|
|
return;
|
2017-08-16 12:29:12 +00:00
|
|
|
}
|
2018-03-11 15:44:32 +00:00
|
|
|
this._hrTimeStopDiff = process.hrtime(this._hrTimeStart);
|
|
|
|
this.nanoSeconds = this._hrTimeStopDiff[0] * 1e9 + this._hrTimeStopDiff[1];
|
|
|
|
this.milliSeconds = this.nanoSeconds / 1000000;
|
|
|
|
return this;
|
2017-08-16 12:29:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* reset the measurement
|
|
|
|
*/
|
2019-06-17 14:54:39 +00:00
|
|
|
public reset() {
|
2018-03-11 15:44:32 +00:00
|
|
|
this.nanoSeconds = null;
|
|
|
|
this.milliSeconds = null;
|
|
|
|
this._hrTimeStart = null;
|
|
|
|
this._hrTimeStopDiff = null;
|
|
|
|
this._started = false;
|
2017-08-16 12:29:12 +00:00
|
|
|
}
|
|
|
|
}
|