-
Notifications
You must be signed in to change notification settings - Fork 0
/
test5.js
41 lines (31 loc) · 1.17 KB
/
test5.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
// set activity monitory update frequency to 2 seconds
// divide process times by elapsed hrtime
var startTime = process.hrtime()
var startUsage = process.cpuUsage()
run();
function run() {
print(startTime, startUsage);
startTime = process.hrtime();
startUsage = process.cpuUsage();
var now = Date.now()
var randomLength = Math.random() * 750 + 750; // should be less than 2000 as max
var timeout = 2000 - randomLength;
while (Date.now() - now < randomLength);
setTimeout(run, timeout);
};
function print(startTime, startUsage) {
var elapTimeMS = hrtimeToMS(process.hrtime(startTime));
var elapUsageMS = usageToTotalUsageMS(process.cpuUsage(startUsage));
var cpuPercent = (100.0 * elapUsageMS / elapTimeMS).toFixed(1) + '%';
console.log('elapsed time ms: ', elapTimeMS)
console.log('elapsed usage ms: ', elapUsageMS)
console.log('cpu percent: ', cpuPercent, '\n')
}
function usageToTotalUsageMS(elapUsage) {
var elapUserMS = elapUsage.user / 1000.0; // microseconds to milliseconds
var elapSystMS = elapUsage.system / 1000.0;
return elapUserMS + elapSystMS;
}
function hrtimeToMS (hrtime) {
return hrtime[0] * 1000.0 + hrtime[1] / 1000000.0;
}