-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
49 lines (45 loc) · 1.22 KB
/
index.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
'use strict';
const { PerformanceObserver } = require('perf_hooks');
class GCObserver {
startTime;
lastTime;
total;
current;
observer;
start() {
if (!this.observer) {
this.lastTime = this.startTime = Date.now();
this.total = this.current = 0;
this.observer = new PerformanceObserver((items) => {
items.getEntries().forEach((item) => {
this.current += item.duration;
this.total += item.duration;
});
});
this.observer.observe({ entryTypes: ['gc'] });
}
}
stop() {
if (this.observer) {
this.observer.disconnect();
this.observer = null;
}
}
load() {
if (!this.observer) {
throw new Error('GCLoad have been closed');
}
const now = Date.now();
const interval = now - this.lastTime;
const load = this.current / interval * 100;
this.lastTime = now;
this.current = 0;
return {
lastLoad: load,
totalLoad: now - this.startTime && this.total / (now - this.startTime) * 100,
};
}
}
module.exports = {
GCObserver
};