Skip to content

Instantly share code, notes, and snippets.

@misterdjules
Last active March 6, 2021 17:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save misterdjules/bed63224df1ddc147d27 to your computer and use it in GitHub Desktop.
Save misterdjules/bed63224df1ddc147d27 to your computer and use it in GitHub Desktop.
monitoring active libuv handles with node.js
$ curl localhost:8080/ &
[1] 70338
$ curl localhost:8080/ &
[2] 70347
$ curl localhost:8080/ &
[3] 70356
$ curl localhost:8080/ &
[4] 70365
$ curl localhost:8080/ &
[5] 70374
$ curl localhost:8080/ &
[6] 70383
$ hello world!
[1] 70338 done curl localhost:8080/
$ hello world!
[2] 70347 done curl localhost:8080/
$ hello world!
[3] 70356 done curl localhost:8080/
$ hello world!
[4] 70365 done curl localhost:8080/
$ hello world!
[5] - 70374 done curl localhost:8080/
$ hello world!
[6] + 70383 done curl localhost:8080/
$
$ node /tmp/event-loop-monitoring.js
server listening on port 8080...
Number of active handles: 3
Active handles: { Server: 1, WriteStream: 2 }
Number of active handles: 10
Active handles: { Server: 1, WriteStream: 2, Socket: 6, Timer: 1 }
Number of active handles: 3
Active handles: { Server: 1, WriteStream: 2 }
const http = require('http');
const PORT = 8080;
const server = http.createServer(function onReq(req, res) {
setTimeout(function() {
res.end('hello world!');
}, 10000);
});
server.listen(PORT, function onListen() {
console.log(`server listening on port ${PORT}...`);
});
process.on('SIGUSR1', function onSignal() {
const activeHandles = process._getActiveHandles();
const activeHandlesByConstructor = {};
console.log('Number of active handles:', activeHandles.length);
activeHandles.forEach(function(activeHandle) {
if (activeHandlesByConstructor[activeHandle.constructor.name] !== undefined)
++activeHandlesByConstructor[activeHandle.constructor.name];
else
activeHandlesByConstructor[activeHandle.constructor.name] = 1;
});
console.log('Active handles:', activeHandlesByConstructor);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment