1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
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.haxx.se/docs/copyright.html.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 * Show transfer timing info after download completes.
26 /* Example source code to show how the callback function can be used to
27 * download data into a chunk of memory instead of storing it in a file.
28 * After successful download we use curl_easy_getinfo() calls to get the
29 * amount of downloaded bytes, the time used for the whole download, and
30 * the average download speed.
31 * On Linux you can create the download test files with:
32 * dd if=/dev/urandom of=file_1M.bin bs=1M count=1
41 #include <curl/curl.h>
43 #define URL_BASE "http://speedtest.your.domain/"
44 #define URL_1M URL_BASE "file_1M.bin"
45 #define URL_2M URL_BASE "file_2M.bin"
46 #define URL_5M URL_BASE "file_5M.bin"
47 #define URL_10M URL_BASE "file_10M.bin"
48 #define URL_20M URL_BASE "file_20M.bin"
49 #define URL_50M URL_BASE "file_50M.bin"
50 #define URL_100M URL_BASE "file_100M.bin"
52 #define CHKSPEED_VERSION "1.0"
54 static size_t WriteCallback(void *ptr, size_t size, size_t nmemb, void *data)
56 /* we are not interested in the downloaded bytes itself,
57 so we only return the size we would have saved ... */
58 (void)ptr; /* unused */
59 (void)data; /* unused */
60 return (size_t)(size * nmemb);
63 int main(int argc, char *argv[])
67 int prtall = 0, prtsep = 0, prttime = 0;
68 const char *url = URL_1M;
69 char *appname = argv[0];
72 /* parse input parameters */
73 for(argc--, argv++; *argv; argc--, argv++) {
74 if(strncasecmp(*argv, "-", 1) == 0) {
75 if(strncasecmp(*argv, "-H", 2) == 0) {
77 "\rUsage: %s [-m=1|2|5|10|20|50|100] [-t] [-x] [url]\n",
81 else if(strncasecmp(*argv, "-V", 2) == 0) {
82 fprintf(stderr, "\r%s %s - %s\n",
83 appname, CHKSPEED_VERSION, curl_version());
86 else if(strncasecmp(*argv, "-A", 2) == 0) {
89 else if(strncasecmp(*argv, "-X", 2) == 0) {
92 else if(strncasecmp(*argv, "-T", 2) == 0) {
95 else if(strncasecmp(*argv, "-M=", 3) == 0) {
96 long m = strtol((*argv) + 3, NULL, 10);
120 fprintf(stderr, "\r%s: invalid parameter %s\n",
126 fprintf(stderr, "\r%s: invalid or unknown option %s\n",
137 /* print separator line */
139 printf("-------------------------------------------------\n");
141 /* print localtime */
143 time_t t = time(NULL);
144 printf("Localtime: %s", ctime(&t));
148 curl_global_init(CURL_GLOBAL_ALL);
150 /* init the curl session */
151 curl_handle = curl_easy_init();
153 /* specify URL to get */
154 curl_easy_setopt(curl_handle, CURLOPT_URL, url);
156 /* send all data to this function */
157 curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteCallback);
159 /* some servers don't like requests that are made without a user-agent
160 field, so we provide one */
161 curl_easy_setopt(curl_handle, CURLOPT_USERAGENT,
162 "libcurl-speedchecker/" CHKSPEED_VERSION);
165 res = curl_easy_perform(curl_handle);
167 if(CURLE_OK == res) {
170 /* check for bytes downloaded */
171 res = curl_easy_getinfo(curl_handle, CURLINFO_SIZE_DOWNLOAD, &val);
172 if((CURLE_OK == res) && (val>0))
173 printf("Data downloaded: %0.0f bytes.\n", val);
175 /* check for total download time */
176 res = curl_easy_getinfo(curl_handle, CURLINFO_TOTAL_TIME, &val);
177 if((CURLE_OK == res) && (val>0))
178 printf("Total download time: %0.3f sec.\n", val);
180 /* check for average download speed */
181 res = curl_easy_getinfo(curl_handle, CURLINFO_SPEED_DOWNLOAD, &val);
182 if((CURLE_OK == res) && (val>0))
183 printf("Average download speed: %0.3f kbyte/sec.\n", val / 1024);
186 /* check for name resolution time */
187 res = curl_easy_getinfo(curl_handle, CURLINFO_NAMELOOKUP_TIME, &val);
188 if((CURLE_OK == res) && (val>0))
189 printf("Name lookup time: %0.3f sec.\n", val);
191 /* check for connect time */
192 res = curl_easy_getinfo(curl_handle, CURLINFO_CONNECT_TIME, &val);
193 if((CURLE_OK == res) && (val>0))
194 printf("Connect time: %0.3f sec.\n", val);
198 fprintf(stderr, "Error while fetching '%s' : %s\n",
199 url, curl_easy_strerror(res));
202 /* cleanup curl stuff */
203 curl_easy_cleanup(curl_handle);
205 /* we're done with libcurl, so clean it up */
206 curl_global_cleanup();