Calculate download speed with Recv?
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.
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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement