tags:

views:

81

answers:

1

I'd like to know how to check out the speed of a file being uploaded in real time using the curl library in C++. This is what I have written:

curl_easy_getinfo(curl,CURLINFO_SPEED_UPLOAD,&c);

But the manual says that it shows average speed, but even this doesn't seem to work with me, because I can only see a 0.

There is one more thing: How to set an upload limit that works, because if I write this:

curl_easy_setopt(curl, CURLOPT_MAX_SEND_SPEED_LARGE, 100);

I get an error 502 message.

+2  A: 

From the curl_easy_getinfo docs:

CURLINFO_SPEED_UPLOAD

Pass a pointer to a double to receive the average upload speed that curl measured for the complete [emphasis added] upload. Measured in bytes/second.

CURLINFO_SPEED_UPLOAD only works once the updload has completed. Instead, write a progress callback and set CURLOPT_PROGRESSFUNCTION. Have your progress function calculate the exponential moving average for the current speed.

progress(curl, fd, len):
    now = time()
    speed = len/(now-then) * weight + speed * (1-weight)
    update progress display
    then=now

As always, network speed is an approximation.

outis