libcurl:网络上传下载速度输出不准确

问题描述 投票:0回答:1

我使用libcurl库来计算网络上传下载速度。我使用以下代码。但与原始网络速度相比,上传和下载的速度并不准确。 (我的输出大约是1.3 KBps,相比原始500KBps上传370KBps,下载1MBps)

如果有人能说出这个的原因以及我应该做些什么修改来获得正确的上传下载速度将会非常有用。我们也欢迎新的计算费率的程序。

#include <stdio.h>
#include <curl/curl.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>

size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) 

{
size_t written = fwrite(ptr, size, nmemb, stream);
return written;
}

int main(void) {
 CURL *curl;
 CURL *curl1;
 FILE *fp;
 CURLcode res;
 CURLcode res1;
 char *url = 
  "https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency
  _demonstration_1.png";           
 char outfilename[FILENAME_MAX] = "aaa.jpg";
 curl = curl_easy_init();
 curl1 = curl_easy_init();
 struct stat file_info;
 double speed_upload, total_time1;
 FILE *fd;

 fd = fopen("aaa.jpg", "rb"); 
if(!fd)
  return 1; 

if(fstat(fileno(fd), &file_info) != 0)
  return 1;  

if(curl1) {
  curl_easy_setopt(curl1, CURLOPT_URL,
                  "https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png");            
curl_easy_setopt(curl1, CURLOPT_UPLOAD, 1L);

curl_easy_setopt(curl1, CURLOPT_READDATA, fd);

curl_easy_setopt(curl1, CURLOPT_INFILESIZE_LARGE,(curl_off_t)file_info.st_size);

curl_easy_setopt(curl1, CURLOPT_VERBOSE, 1L);

res1 = curl_easy_perform(curl1);
/* Check for errors */ 
if(res1 != CURLE_OK) {
  fprintf(stderr, "curl_easy_perform() failed: %s\n",
          curl_easy_strerror(res1));

}
else {
  curl_easy_getinfo(curl1, CURLINFO_SPEED_UPLOAD, &speed_upload);
  curl_easy_getinfo(curl1, CURLINFO_TOTAL_TIME, &total_time1);

  fprintf(stderr, "Speed: %.3f bytes/sec during %.3f seconds\n",
          speed_upload, total_time1);

}
curl_easy_cleanup(curl1);
fclose(fd);
}
if (curl) {
    fp = fopen(outfilename,"wb");
    curl_easy_setopt(curl, CURLOPT_URL, url);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
    res = curl_easy_perform(curl);

if (CURLE_OK == res) {
    double val;

    res = curl_easy_getinfo(curl, CURLINFO_SIZE_DOWNLOAD, &val);
    if ((CURLE_OK == res) && (val>0))
        printf("Data downloaded: %0.0f bytes.\n", val);

    res = curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &val);
    if ((CURLE_OK == res) && (val>0))
        printf("Total download time: %0.3f sec.\n", val);

    res = curl_easy_getinfo(curl, CURLINFO_SPEED_DOWNLOAD, &val);
    if ((CURLE_OK == res) && (val>0))
        printf("Average download speed: %0.3f kbyte/sec.\n", val / 1024);

}
else {
    fprintf(stderr, "Error while fetching '%s' : %s\n",
        url, curl_easy_strerror(res));
}
    curl_easy_cleanup(curl);
    fclose(fp);
}
return 0;
}
c++ c libcurl bandwidth
1个回答
0
投票

你的问题没有完美的解决方案。您无法通过向/从维基百科传输图像来准确衡量提供商的网络速度,因为这也涉及维基百科自己的网络,以及您与维基百科之间的互联网骨干网。您不是Internet的唯一用户,因此您的带宽与许多其他用户共享。因此,当您将图像传输到维基百科或从维基百科传输图像时,您获得的速度只是对您和维基百科之间的网络状况的一种实时测量,如果您在一分钟后重复它,您可能会得到不同的结果。

你正在访问的PNG不是很大,1兆字节/秒只需要五分之一秒的时间来传输。但是,在实际下载开始之前,您的计算机必须为upload.wikimedia.org执行DNS查找,然后创建与维基百科服务器的TCP连接,使用TLS设置加密,发送图像请求,然后才会实际传输图像开始。设置连接所花费的时间也可能与Curl测量的传输速度相对应。通过使用更大的文件,您将获得更好的结果,但如果您要经常运行此速度测试,请注意维基百科可能不喜欢您浪费带宽。

有专门的网站可以帮助您衡量提供商的速度。只需搜索“http速度测试”,你会发现几个。

© www.soinside.com 2019 - 2024. All rights reserved.