私はネットワークアップロードのダウンロード速度を計算するために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;
}