-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathstats.js
More file actions
132 lines (115 loc) · 3.89 KB
/
stats.js
File metadata and controls
132 lines (115 loc) · 3.89 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const { monitorEventLoopDelay } = require('perf_hooks');
const {Utilities} = require("./library/utilities");
const escape = require('escape-html');
class ServerStats {
started = false;
requestCount = 0;
requestTime = 0;
// Collect metrics every 10 minutes
intervalMs = 10 * 60 * 1000;
history = [];
requestCountSnapshot = 0;
startMem = 0;
startTime = Date.now();
timer;
cachingModules = [];
taskMap = new Map();
constructor() {
this.timer = setInterval(() => {
this.recordMetrics();
}, this.intervalMs);
}
recordMetrics() {
if (this.started) {
const now = Date.now();
const currentMem = process.memoryUsage().heapUsed;
const requestsDelta = this.requestCount - this.requestCountSnapshot;
const requestsTat = requestsDelta > 0 ? this.requestTime / requestsDelta : 0;
const minutesSinceStart = this.history.length > 1
? this.intervalMs / 60000
: (now - this.startTime) / 60000;
const requestsPerMin = minutesSinceStart > 0 ? requestsDelta / minutesSinceStart : 0;
const currentCpu = this.readSystemCpu();
const idleDelta = currentCpu.idle - this.lastUsage.idle;
const totalDelta = currentCpu.total - this.lastUsage.total;
const percent = totalDelta > 0 ? 100 * (1 - idleDelta / totalDelta) : 0;
const loopDelay = this.eventLoopMonitor.mean / 1e6;
let cacheCount = 0;
for (let m of this.cachingModules) {
cacheCount = cacheCount + m.cacheCount();
}
this.history.push({time: now, mem: currentMem - this.startMem, rpm: requestsPerMin, tat: requestsTat, cpu: percent, block: loopDelay, cache : cacheCount});
this.eventLoopMonitor.reset();
this.requestCountSnapshot = this.requestCount;
this.requestTime = 0;
this.lastTime = now;
this.lastUsage = currentCpu;
// Prune old data (keep 24 hours)
const cutoff = now - (24 * 60 * 60 * 1000); // 24 hours ago
this.history = this.history.filter(m => m.time > cutoff);
}
}
markStarted() {
this.started = true;
this.startMem = process.memoryUsage().heapUsed;
this.startTime = Date.now();
this.lastUsage = this.readSystemCpu();
this.lastTime = this.startTime;
this.eventLoopMonitor = monitorEventLoopDelay({ resolution: 20 });
this.eventLoopMonitor.enable();
this.recordMetrics();
}
countRequest(name, tat) {
// we ignore name for now, but we might split the tat tracking up by name
// at some stage
this.requestCount++;
this.requestTime = this.requestTime + tat;
}
addTask(name, frequency) {
let info = {};
this.taskMap.set(name, info);
info.frequency = frequency;
info.state = "Started";
}
task(name, state) {
let info = this.taskMap.get(name);
if (info) {
info.date = Date.now();
info.state = state;
}
}
taskDetails() {
if (this.taskMap.size == 0) {
return "";
}
let html = '<table class="grid"><tr style="background-color: #EEEEEE"><th colspan="4">Background Tasks</th></tr>';
html += "<tr><th>Task</th><th>Status</th><th>Frequency</th><th>Last Seen</th></tr>";
for (let m of this.taskMap.keys()) {
html += "<tr><td>";
html += escape(m);
html += "</td><td>";
html += escape(this.taskMap.get(m).state);
html += "</td><td>";
html += this.taskMap.get(m).frequency;
html += "</td><td>";
html += Utilities.formatDuration(this.taskMap.get(m).date, Date.now());
html += "</td></tr>";
}
html += "</table>";
return html;
}
finishStats() {
clearInterval(this.timer);
}
readSystemCpu() {
const os = require('os');
const cpus = os.cpus();
let idle = 0, total = 0;
for (const cpu of cpus) {
idle += cpu.times.idle;
total += cpu.times.user + cpu.times.nice + cpu.times.sys + cpu.times.idle + cpu.times.irq;
}
return { idle, total };
}
}
module.exports = ServerStats;