-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo-lag.js
39 lines (30 loc) · 893 Bytes
/
demo-lag.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
const EventEmitter = require('events').EventEmitter;
const lagger = new EventEmitter();
const NS_PER_SEC = 1e9;
const countTarget = process.argv[2] || 10000;
const interval = process.argv[3] || 200;
const count = () => {
for (var i = 0; i < countTarget; i++) {
if (i % Math.floor(countTarget / 10) === 0) {
process.stdout.write(`${i} `);
}
}
console.log('Done counting');
}
const next = () => {
const now = process.hrtime();
setTimeout(() => {
lagger.emit('measure-lag', interval * (NS_PER_SEC / 1000), now);
}, interval);
count();
};
lagger.on('measure-lag', (nanoseconds, previous) => {
const time = process.hrtime(previous);
const took = time[0] * NS_PER_SEC + time[1];
console.log(
`Scheduled log at ${nanoseconds} nanoseconds took ${took} nanoseconds`
)
console.log(`${took - nanoseconds} nanoseconds late`);
next();
});
next();