2017-12-19 19 views
0

libcurl 라이브러리를 사용하여 네트워크 업로드 다운로드 속도를 계산했습니다. 다음 코드를 사용하고 있습니다. 그러나 업로드 및 다운로드 속도는 원래 네트워크 속도와 비교하여 정확하지 않습니다. (내 출력은 약 1.3KBps, 업로드의 경우 원래 500KBps와 비교하여 370KBps, 다운로드의 경우 1MBps 임)libcurl : 네트워크 업로드 다운로드 속도가 정확하지 않습니다.

누군가가이 이유를 알 수 있고 적절한 업로드 다운로드 속도를 얻으려면 어떤 수정을해야하는지 매우 유용합니다. . 요금을 계산하는 새로운 절차도 환영합니다.

#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; 
} 

답변

0

문제에 대한 완벽한 해결책은 없습니다. Wikipedia와의 이미지 전송을 통해 공급자의 네트워크 속도를 정확하게 측정 할 수 없습니다. Wikipedia의 자체 네트워크와 Wikipedia 간의 인터넷 백본이 포함되기 때문입니다. 당신은 인터넷의 유일한 사용자가 아니므로 귀하의 대역폭은 다른 많은 사람들과 공유됩니다. 따라서 Wikipedia에서 이미지를 전송할 때 속도는 당신과 Wikipedia 간의 네트워크 상태를 즉각적으로 측정하는 것입니다. 그리고 1 분 후에 반복하면 다른 결과를 얻을 수 있습니다.

액세스하는 PNG는별로 크지 않습니다. 1 메가 바이트/초에서 전송하는 데는 1 초당 5 분이 걸립니다. 그러나 실제 다운로드가 시작되기 전에 컴퓨터가 upload.wikimedia.org에 대한 DNS 조회를 수행 한 다음 Wikipedia의 서버에 TCP 연결을 만들고 TLS를 사용하여 암호화를 설정하고 이미지 요청을 보낸 다음에 만 실제 이미지 시작. 연결 설정에 소요되는 시간 중 일부는 Curl이 측정 한 전송 속도와 비교하여 계산됩니다. 더 큰 파일을 사용하면 더 나은 결과를 얻을 수 있지만,이 속도 테스트를 자주 실행하려면 Wikipedia가 대역폭 낭비를 좋아하지 않을 수 있음을 알아야합니다.

제공 업체의 속도를 측정 할 수있는 전용 사이트가 있습니다. "http 속도 테스트"를 검색하면 몇 가지를 찾을 수 있습니다.