【问题标题】:libcurl : output of network upload download speed is not accuratelibcurl : 网络上传下载速度的输出不准确
【发布时间】:2017-12-19 13:30:40
【问题描述】:

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

如果有人能说出原因以及我应该做哪些修改以获得正确的上传下载速度,这将非常有帮助。计算费率的新程序也受到欢迎。

#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】:

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

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

    有专门的网站可以帮助您衡量供应商的速度。只要搜索“http speed test”,你会发现几个。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多