| xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame] | 1 | /*************************************************************************** | 
|  | 2 | *                                  _   _ ____  _ | 
|  | 3 | *  Project                     ___| | | |  _ \| | | 
|  | 4 | *                             / __| | | | |_) | | | 
|  | 5 | *                            | (__| |_| |  _ <| |___ | 
|  | 6 | *                             \___|\___/|_| \_\_____| | 
|  | 7 | * | 
|  | 8 | * Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al. | 
|  | 9 | * | 
|  | 10 | * This software is licensed as described in the file COPYING, which | 
|  | 11 | * you should have received as part of this distribution. The terms | 
|  | 12 | * are also available at https://curl.se/docs/copyright.html. | 
|  | 13 | * | 
|  | 14 | * You may opt to use, copy, modify, merge, publish, distribute and/or sell | 
|  | 15 | * copies of the Software, and permit persons to whom the Software is | 
|  | 16 | * furnished to do so, under the terms of the COPYING file. | 
|  | 17 | * | 
|  | 18 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | 
|  | 19 | * KIND, either express or implied. | 
|  | 20 | * | 
|  | 21 | * SPDX-License-Identifier: curl | 
|  | 22 | * | 
|  | 23 | ***************************************************************************/ | 
|  | 24 | /* <DESC> | 
|  | 25 | * Show transfer timing info after download completes. | 
|  | 26 | * </DESC> | 
|  | 27 | */ | 
|  | 28 | /* Example source code to show how the callback function can be used to | 
|  | 29 | * download data into a chunk of memory instead of storing it in a file. | 
|  | 30 | * After successful download we use curl_easy_getinfo() calls to get the | 
|  | 31 | * amount of downloaded bytes, the time used for the whole download, and | 
|  | 32 | * the average download speed. | 
|  | 33 | * On Linux you can create the download test files with: | 
|  | 34 | * dd if=/dev/urandom of=file_1M.bin bs=1M count=1 | 
|  | 35 | * | 
|  | 36 | */ | 
|  | 37 |  | 
|  | 38 | #include <stdio.h> | 
|  | 39 | #include <stdlib.h> | 
|  | 40 | #include <time.h> | 
|  | 41 |  | 
|  | 42 | #include <curl/curl.h> | 
|  | 43 |  | 
|  | 44 | #define URL_BASE "http://speedtest.your.domain/" | 
|  | 45 | #define URL_1M   URL_BASE "file_1M.bin" | 
|  | 46 | #define URL_2M   URL_BASE "file_2M.bin" | 
|  | 47 | #define URL_5M   URL_BASE "file_5M.bin" | 
|  | 48 | #define URL_10M  URL_BASE "file_10M.bin" | 
|  | 49 | #define URL_20M  URL_BASE "file_20M.bin" | 
|  | 50 | #define URL_50M  URL_BASE "file_50M.bin" | 
|  | 51 | #define URL_100M URL_BASE "file_100M.bin" | 
|  | 52 |  | 
|  | 53 | #define CHKSPEED_VERSION "1.0" | 
|  | 54 |  | 
|  | 55 | static size_t WriteCallback(void *ptr, size_t size, size_t nmemb, void *data) | 
|  | 56 | { | 
|  | 57 | /* we are not interested in the downloaded bytes itself, | 
|  | 58 | so we only return the size we would have saved ... */ | 
|  | 59 | (void)ptr;  /* unused */ | 
|  | 60 | (void)data; /* unused */ | 
|  | 61 | return (size_t)(size * nmemb); | 
|  | 62 | } | 
|  | 63 |  | 
|  | 64 | int main(int argc, char *argv[]) | 
|  | 65 | { | 
|  | 66 | CURL *curl_handle; | 
|  | 67 | CURLcode res; | 
|  | 68 | int prtall = 0, prtsep = 0, prttime = 0; | 
|  | 69 | const char *url = URL_1M; | 
|  | 70 | char *appname = argv[0]; | 
|  | 71 |  | 
|  | 72 | if(argc > 1) { | 
|  | 73 | /* parse input parameters */ | 
|  | 74 | for(argc--, argv++; *argv; argc--, argv++) { | 
|  | 75 | if(argv[0][0] == '-') { | 
|  | 76 | switch(argv[0][1]) { | 
|  | 77 | case 'h': | 
|  | 78 | case 'H': | 
|  | 79 | fprintf(stderr, | 
|  | 80 | "\rUsage: %s [-m=1|2|5|10|20|50|100] [-t] [-x] [url]\n", | 
|  | 81 | appname); | 
|  | 82 | exit(1); | 
|  | 83 | case 'v': | 
|  | 84 | case 'V': | 
|  | 85 | fprintf(stderr, "\r%s %s - %s\n", | 
|  | 86 | appname, CHKSPEED_VERSION, curl_version()); | 
|  | 87 | exit(1); | 
|  | 88 | case 'a': | 
|  | 89 | case 'A': | 
|  | 90 | prtall = 1; | 
|  | 91 | break; | 
|  | 92 | case 'x': | 
|  | 93 | case 'X': | 
|  | 94 | prtsep = 1; | 
|  | 95 | break; | 
|  | 96 | case 't': | 
|  | 97 | case 'T': | 
|  | 98 | prttime = 1; | 
|  | 99 | break; | 
|  | 100 | case 'm': | 
|  | 101 | case 'M': | 
|  | 102 | if(argv[0][2] == '=') { | 
|  | 103 | long m = strtol((*argv) + 3, NULL, 10); | 
|  | 104 | switch(m) { | 
|  | 105 | case 1: | 
|  | 106 | url = URL_1M; | 
|  | 107 | break; | 
|  | 108 | case 2: | 
|  | 109 | url = URL_2M; | 
|  | 110 | break; | 
|  | 111 | case 5: | 
|  | 112 | url = URL_5M; | 
|  | 113 | break; | 
|  | 114 | case 10: | 
|  | 115 | url = URL_10M; | 
|  | 116 | break; | 
|  | 117 | case 20: | 
|  | 118 | url = URL_20M; | 
|  | 119 | break; | 
|  | 120 | case 50: | 
|  | 121 | url = URL_50M; | 
|  | 122 | break; | 
|  | 123 | case 100: | 
|  | 124 | url = URL_100M; | 
|  | 125 | break; | 
|  | 126 | default: | 
|  | 127 | fprintf(stderr, "\r%s: invalid parameter %s\n", | 
|  | 128 | appname, *argv + 3); | 
|  | 129 | exit(1); | 
|  | 130 | } | 
|  | 131 | break; | 
|  | 132 | } | 
|  | 133 | /* FALLTHROUGH */ | 
|  | 134 | default: | 
|  | 135 | fprintf(stderr, "\r%s: invalid or unknown option %s\n", | 
|  | 136 | appname, *argv); | 
|  | 137 | exit(1); | 
|  | 138 | } | 
|  | 139 | } | 
|  | 140 | else { | 
|  | 141 | url = *argv; | 
|  | 142 | } | 
|  | 143 | } | 
|  | 144 | } | 
|  | 145 |  | 
|  | 146 | /* print separator line */ | 
|  | 147 | if(prtsep) { | 
|  | 148 | printf("-------------------------------------------------\n"); | 
|  | 149 | } | 
|  | 150 | /* print localtime */ | 
|  | 151 | if(prttime) { | 
|  | 152 | time_t t = time(NULL); | 
|  | 153 | printf("Localtime: %s", ctime(&t)); | 
|  | 154 | } | 
|  | 155 |  | 
|  | 156 | /* init libcurl */ | 
|  | 157 | curl_global_init(CURL_GLOBAL_ALL); | 
|  | 158 |  | 
|  | 159 | /* init the curl session */ | 
|  | 160 | curl_handle = curl_easy_init(); | 
|  | 161 |  | 
|  | 162 | /* specify URL to get */ | 
|  | 163 | curl_easy_setopt(curl_handle, CURLOPT_URL, url); | 
|  | 164 |  | 
|  | 165 | /* send all data to this function  */ | 
|  | 166 | curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteCallback); | 
|  | 167 |  | 
|  | 168 | /* some servers do not like requests that are made without a user-agent | 
|  | 169 | field, so we provide one */ | 
|  | 170 | curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, | 
|  | 171 | "libcurl-speedchecker/" CHKSPEED_VERSION); | 
|  | 172 |  | 
|  | 173 | /* get it! */ | 
|  | 174 | res = curl_easy_perform(curl_handle); | 
|  | 175 |  | 
|  | 176 | if(CURLE_OK == res) { | 
|  | 177 | curl_off_t val; | 
|  | 178 |  | 
|  | 179 | /* check for bytes downloaded */ | 
|  | 180 | res = curl_easy_getinfo(curl_handle, CURLINFO_SIZE_DOWNLOAD_T, &val); | 
|  | 181 | if((CURLE_OK == res) && (val>0)) | 
|  | 182 | printf("Data downloaded: %lu bytes.\n", (unsigned long)val); | 
|  | 183 |  | 
|  | 184 | /* check for total download time */ | 
|  | 185 | res = curl_easy_getinfo(curl_handle, CURLINFO_TOTAL_TIME_T, &val); | 
|  | 186 | if((CURLE_OK == res) && (val>0)) | 
|  | 187 | printf("Total download time: %lu.%06lu sec.\n", | 
|  | 188 | (unsigned long)(val / 1000000), (unsigned long)(val % 1000000)); | 
|  | 189 |  | 
|  | 190 | /* check for average download speed */ | 
|  | 191 | res = curl_easy_getinfo(curl_handle, CURLINFO_SPEED_DOWNLOAD_T, &val); | 
|  | 192 | if((CURLE_OK == res) && (val>0)) | 
|  | 193 | printf("Average download speed: %lu kbyte/sec.\n", | 
|  | 194 | (unsigned long)(val / 1024)); | 
|  | 195 |  | 
|  | 196 | if(prtall) { | 
|  | 197 | /* check for name resolution time */ | 
|  | 198 | res = curl_easy_getinfo(curl_handle, CURLINFO_NAMELOOKUP_TIME_T, &val); | 
|  | 199 | if((CURLE_OK == res) && (val>0)) | 
|  | 200 | printf("Name lookup time: %lu.%06lu sec.\n", | 
|  | 201 | (unsigned long)(val / 1000000), (unsigned long)(val % 1000000)); | 
|  | 202 |  | 
|  | 203 | /* check for connect time */ | 
|  | 204 | res = curl_easy_getinfo(curl_handle, CURLINFO_CONNECT_TIME_T, &val); | 
|  | 205 | if((CURLE_OK == res) && (val>0)) | 
|  | 206 | printf("Connect time: %lu.%06lu sec.\n", | 
|  | 207 | (unsigned long)(val / 1000000), (unsigned long)(val % 1000000)); | 
|  | 208 | } | 
|  | 209 | } | 
|  | 210 | else { | 
|  | 211 | fprintf(stderr, "Error while fetching '%s' : %s\n", | 
|  | 212 | url, curl_easy_strerror(res)); | 
|  | 213 | } | 
|  | 214 |  | 
|  | 215 | /* cleanup curl stuff */ | 
|  | 216 | curl_easy_cleanup(curl_handle); | 
|  | 217 |  | 
|  | 218 | /* we are done with libcurl, so clean it up */ | 
|  | 219 | curl_global_cleanup(); | 
|  | 220 |  | 
|  | 221 | return 0; | 
|  | 222 | } |