Imported Upstream version 7.44.0
[platform/upstream/curl.git] / docs / examples / chkspeed.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2015, 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 http://curl.haxx.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  ***************************************************************************/
22 /* <DESC>
23  * Show transfer timing info after download completes.
24  * </DESC>
25  */
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
33  *
34  */
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <time.h>
40
41 #include <curl/curl.h>
42
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"
51
52 #define CHKSPEED_VERSION "1.0"
53
54 static size_t WriteCallback(void *ptr, size_t size, size_t nmemb, void *data)
55 {
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);
61 }
62
63 int main(int argc, char *argv[])
64 {
65   CURL *curl_handle;
66   CURLcode res;
67   int prtall = 0, prtsep = 0, prttime = 0;
68   const char *url = URL_1M;
69   char *appname = argv[0];
70
71   if (argc > 1) {
72     /* parse input parameters */
73     for (argc--, argv++; *argv; argc--, argv++) {
74       if (strncasecmp(*argv, "-", 1) == 0) {
75         if (strncasecmp(*argv, "-H", 2) == 0) {
76           fprintf(stderr,
77                   "\rUsage: %s [-m=1|2|5|10|20|50|100] [-t] [-x] [url]\n",
78                   appname);
79           exit(1);
80         } else if (strncasecmp(*argv, "-V", 2) == 0) {
81           fprintf(stderr, "\r%s %s - %s\n",
82                   appname, CHKSPEED_VERSION, curl_version());
83           exit(1);
84         } else if (strncasecmp(*argv, "-A", 2) == 0) {
85           prtall = 1;
86         } else if (strncasecmp(*argv, "-X", 2) == 0) {
87           prtsep = 1;
88         } else if (strncasecmp(*argv, "-T", 2) == 0) {
89           prttime = 1;
90         } else if (strncasecmp(*argv, "-M=", 3) == 0) {
91           long m = strtol((*argv)+3, NULL, 10);
92           switch(m) {
93             case   1: url = URL_1M;
94                       break;
95             case   2: url = URL_2M;
96                       break;
97             case   5: url = URL_5M;
98                       break;
99             case  10: url = URL_10M;
100                       break;
101             case  20: url = URL_20M;
102                       break;
103             case  50: url = URL_50M;
104                       break;
105             case 100: url = URL_100M;
106                       break;
107             default:  fprintf(stderr, "\r%s: invalid parameter %s\n",
108                               appname, *argv + 3);
109                       exit(1);
110           }
111         } else {
112           fprintf(stderr, "\r%s: invalid or unknown option %s\n",
113                   appname, *argv);
114           exit(1);
115         }
116       } else {
117         url = *argv;
118       }
119     }
120   }
121
122   /* print separator line */
123   if (prtsep) {
124     printf("-------------------------------------------------\n");
125   }
126   /* print localtime */
127   if (prttime) {
128     time_t t = time(NULL);
129     printf("Localtime: %s", ctime(&t));
130   }
131
132   /* init libcurl */
133   curl_global_init(CURL_GLOBAL_ALL);
134
135   /* init the curl session */
136   curl_handle = curl_easy_init();
137
138   /* specify URL to get */
139   curl_easy_setopt(curl_handle, CURLOPT_URL, url);
140
141   /* send all data to this function  */
142   curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteCallback);
143
144   /* some servers don't like requests that are made without a user-agent
145      field, so we provide one */
146   curl_easy_setopt(curl_handle, CURLOPT_USERAGENT,
147                    "libcurl-speedchecker/" CHKSPEED_VERSION);
148
149   /* get it! */
150   res = curl_easy_perform(curl_handle);
151
152   if(CURLE_OK == res) {
153     double val;
154
155     /* check for bytes downloaded */
156     res = curl_easy_getinfo(curl_handle, CURLINFO_SIZE_DOWNLOAD, &val);
157     if((CURLE_OK == res) && (val>0))
158       printf("Data downloaded: %0.0f bytes.\n", val);
159
160     /* check for total download time */
161     res = curl_easy_getinfo(curl_handle, CURLINFO_TOTAL_TIME, &val);
162     if((CURLE_OK == res) && (val>0))
163       printf("Total download time: %0.3f sec.\n", val);
164
165     /* check for average download speed */
166     res = curl_easy_getinfo(curl_handle, CURLINFO_SPEED_DOWNLOAD, &val);
167     if((CURLE_OK == res) && (val>0))
168       printf("Average download speed: %0.3f kbyte/sec.\n", val / 1024);
169
170     if (prtall) {
171       /* check for name resolution time */
172       res = curl_easy_getinfo(curl_handle, CURLINFO_NAMELOOKUP_TIME, &val);
173       if((CURLE_OK == res) && (val>0))
174         printf("Name lookup time: %0.3f sec.\n", val);
175
176       /* check for connect time */
177       res = curl_easy_getinfo(curl_handle, CURLINFO_CONNECT_TIME, &val);
178       if((CURLE_OK == res) && (val>0))
179         printf("Connect time: %0.3f sec.\n", val);
180     }
181
182   } else {
183     fprintf(stderr, "Error while fetching '%s' : %s\n",
184             url, curl_easy_strerror(res));
185   }
186
187   /* cleanup curl stuff */
188   curl_easy_cleanup(curl_handle);
189
190   /* we're done with libcurl, so clean it up */
191   curl_global_cleanup();
192
193   return 0;
194 }