Calculate download speed with Recv?

Started by
2 comments, last by SimonForsman 11 years, 11 months ago
How do you calculate the download speed when recieving data with recv?
:)
Advertisement

How do you calculate the download speed when recieving data with recv?


recv returns the amount of data read in bytes, add up the total amount of data recieved across multiple calls and divide by the time it took to recieve that data.
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
how do you get the time it took?
:)

how do you get the time it took?


it depends on your OS, recv itself doesn't take any time at all.

Basically you do something like this:
startime = getCurrentTimeFromOS();
currenttime = starttime;
lastintervalTime = starttime;
totalBytesdownloaded = 0;
byteslastSecond =0;
while we're not done with the download {
bytesThisRecv = recv(....);
totalBytesdownloaded+=bytesThisRecv;
byteslastSecond+=bytesThisRecv;
currentTime = getCurrentTimeFromOS();
if (currentTime > lastIntervalTime + 1 second) {
downloadSpeedLastSecond = bytesLastSecond; //this is the average speed across the last second
bytesLastSecond = 0;
lastIntervalTime+= 1 second;
}
averageSpeed = totalBytes / (currenttime - startime); //this is the total average speed for the download
}

on Windows you can use timeGetTime to get the current time in milliseconds (~10 ms accuracy normally which should be good enough for measuring download speed) or queryPerformanceCounter for a very high accuracy timer, on Linux you use clock_gettime
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

This topic is closed to new replies.

Advertisement