How to monitor/calculate bandwidth usage of a NodeJS server?
Asked Answered
C

1

6

Ok so in some spare time i have developed a nodejs/socketio application that streams video but i would like to know what sort of stats its running at, i have my activity monitor running and currently with 2 users sending each other data streams and the stats are as follows:

%CPU: 6.0
Threads: 5
Real Memory: 59.6mb

How can i work out the total MB/GB of data sent on the server?

Chisholm answered 16/8, 2012 at 14:48 Comment(0)
P
3

My suggestion would be to add a counter in your app.

You should be able to add something like the following:

socket.on('message', function(message, callback) { myCounter += sizeof(message); })

The key here would be identifying the sizeof the message you are sending. If it's a buffer, then you can just count bytes. I'm not clear what type of packing they use when sending JSON.

Phytogeography answered 16/8, 2012 at 22:18 Comment(2)
to get the size of the string in bytes is easy: Buffer.from(string, 'utf8').byteLength So just add up all bytes and you will have how much bandwidth you use. Actually, that's what I use at trafikito.com while counting how much data is coming into the system.Thoroughpaced
This doesn't work if messages are compressed. You are reading the message already uncompressed, and that's not the real measurement if messages are compressed.Leary

© 2022 - 2024 — McMap. All rights reserved.