-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathperf.js
51 lines (46 loc) · 1.2 KB
/
perf.js
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
41
42
43
44
45
46
47
48
49
50
51
class PerfLogger {
constructor() {
this.totalTimes = {}
this.calls = {}
this.totalCalls = 0
this.active = {}
this.firstLog = null
this.lastLog = null
this.enabled = false
}
start(ref) {
this.active[ref] = (new Date()).getTime()
if (this.lastLog === null) {
this.firstLog = this.active[ref]
this.lastLog = this.active[ref]
}
}
stop(ref) {
var stop = (new Date()).getTime()
var start = this.active[ref]
delete this.active[ref]
var time = (stop - start)
if (this.totalTimes[ref] === undefined) {
this.totalTimes[ref] = 0
this.calls[ref] = 0
}
this.totalTimes[ref] += time
this.calls[ref] += 1
this.totalCalls += 1
if (stop - this.lastLog > 5000) {
this.log()
}
}
log() {
this.lastLog = (new Date()).getTime()
if (this.enabled) {
console.log('Total time: ' + (this.lastLog - this.firstLog) / 1000)
console.log('Calls: ')
Object.entries(this.totalTimes).sort((a, b) => b[1] - a[1]).forEach((entry) => {
console.log(entry[0] + ': ' + (entry[1] / 1000) + ' (' + this.calls[entry[0]] + ')')
})
console.log('')
}
}
}
module.exports = { PerfLogger }