timer.js
762 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* 计时类
* @example
* let timer = new Timer();
* timer.put('profile');
* timer.put('proflie'); // console output: 12.14
*
* @author: hbomb<qiqi.zhou@yoho.cn>
* @date: 2016/05/07
*/
'use strict';
class Timer {
constructor() {
this.timers = {};
}
/**
* 打点计时
*/
put(label) {
const labelTime = this.timers[label];
if (labelTime) {
const duration = process.hrtime(labelTime);
return this._round(duration[0], duration[1]);
} else {
this.timers[label] = process.hrtime();
}
}
/**
* 格式化成毫秒
* @param {Number} value 纳秒
*/
_round(seconds, nanoseconds) {
return Math.round((seconds * 1e9 + nanoseconds) / 10000) / 100;
}
}
module.exports = Timer;