Git init
[external/curl.git] / docs / examples / chkspeed.c
1 /*****************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  *
9  * Example source code to show how the callback function can be used to
10  * download data into a chunk of memory instead of storing it in a file.
11  * After successful download we use curl_easy_getinfo() calls to get the
12  * amount of downloaded bytes, the time used for the whole download, and
13  * the average download speed.
14  * On Linux you can create the download test files with:
15  * dd if=/dev/urandom of=file_1M.bin bs=1M count=1
16  *
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <time.h>
23
24 #include <curl/curl.h>
25 #include <curl/types.h>
26 #include <curl/easy.h>
27
28 #define URL_BASE "http://speedtest.your.domain/"
29 #define URL_1M   URL_BASE "file_1M.bin"
30 #define URL_2M   URL_BASE "file_2M.bin"
31 #define URL_5M   URL_BASE "file_5M.bin"
32 #define URL_10M  URL_BASE "file_10M.bin"
33 #define URL_20M  URL_BASE "file_20M.bin"
34 #define URL_50M  URL_BASE "file_50M.bin"
35 #define URL_100M URL_BASE "file_100M.bin"
36
37 #define CHKSPEED_VERSION "1.0"
38
39 static size_t WriteCallback(void *ptr, size_t size, size_t nmemb, void *data)
40 {
41   /* we are not interested in the downloaded bytes itself,
42      so we only return the size we would have saved ... */
43   (void)ptr;  /* unused */
44   (void)data; /* unused */
45   return (size_t)(size * nmemb);
46 }
47
48 int main(int argc, char *argv[])
49 {
50   CURL *curl_handle;
51   CURLcode res;
52   int prtsep = 0, prttime = 0;
53   const char *url = URL_1M;
54   char *appname = argv[0];
55
56   if (argc > 1) {
57     /* parse input parameters */
58     for (argc--, argv++; *argv; argc--, argv++) {
59       if (strncasecmp(*argv, "-", 1) == 0) {
60         if (strncasecmp(*argv, "-H", 2) == 0) {
61           fprintf(stderr,
62                   "\rUsage: %s [-m=1|2|5|10|20|50|100] [-t] [-x] [url]\n",
63                   appname);
64           exit(1);
65         } else if (strncasecmp(*argv, "-V", 2) == 0) {
66           fprintf(stderr, "\r%s %s - %s\n",
67                   appname, CHKSPEED_VERSION, curl_version());
68           exit(1);
69         } else if (strncasecmp(*argv, "-X", 2) == 0) {
70           prtsep = 1;
71         } else if (strncasecmp(*argv, "-T", 2) == 0) {
72           prttime = 1;
73         } else if (strncasecmp(*argv, "-M=", 3) == 0) {
74           long m = strtol(argv+3, NULL, 10);
75           switch(m) {
76             case   1: url = URL_1M;
77                       break;
78             case   2: url = URL_2M;
79                       break;
80             case   5: url = URL_5M;
81                       break;
82             case  10: url = URL_10M;
83                       break;
84             case  20: url = URL_20M;
85                       break;
86             case  50: url = URL_50M;
87                       break;
88             case 100: url = URL_100M;
89                       break;
90             default:  fprintf(stderr, "\r%s: invalid parameter %s\n",
91                               appname, *argv + 3);
92                       exit(1);
93           }
94         } else {
95           fprintf(stderr, "\r%s: invalid or unknown option %s\n",
96                   appname, *argv);
97           exit(1);
98         }
99       } else {
100         url = *argv;
101       }
102     }
103   }
104
105   /* print separator line */
106   if (prtsep) {
107     printf("-------------------------------------------------\n");
108   }
109   /* print localtime */
110   if (prttime) {
111     time_t t = time(NULL);
112     printf("Localtime: %s", ctime(&t));
113   }
114
115   /* init libcurl */
116   curl_global_init(CURL_GLOBAL_ALL);
117
118   /* init the curl session */
119   curl_handle = curl_easy_init();
120
121   /* specify URL to get */
122   curl_easy_setopt(curl_handle, CURLOPT_URL, url);
123
124   /* send all data to this function  */
125   curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteCallback);
126
127   /* some servers don't like requests that are made without a user-agent
128      field, so we provide one */
129   curl_easy_setopt(curl_handle, CURLOPT_USERAGENT,
130                    "libcurl-speedchecker/" CHKSPEED_VERSION);
131
132   /* get it! */
133   res = curl_easy_perform(curl_handle);
134
135   if(CURLE_OK == res) {
136     double val;
137
138     /* check for bytes downloaded */
139     res = curl_easy_getinfo(curl_handle, CURLINFO_SIZE_DOWNLOAD, &val);
140     if((CURLE_OK == res) && (val>0))
141       printf("Data downloaded: %0.0f bytes.\n", val);
142
143     /* check for total download time */
144     res = curl_easy_getinfo(curl_handle, CURLINFO_TOTAL_TIME, &val);
145     if((CURLE_OK == res) && (val>0))
146       printf("Total download time: %0.3f sec.\n", val);
147
148     /* check for average download speed */
149     res = curl_easy_getinfo(curl_handle, CURLINFO_SPEED_DOWNLOAD, &val);
150     if((CURLE_OK == res) && (val>0))
151       printf("Average download speed: %0.3f kbyte/sec.\n", val / 1024);
152
153   } else {
154     fprintf(stderr, "Error while fetching '%s' : %s\n",
155             url, curl_easy_strerror(res));
156   }
157
158   /* cleanup curl stuff */
159   curl_easy_cleanup(curl_handle);
160
161   /* we're done with libcurl, so clean it up */
162   curl_global_cleanup();
163
164   return 0;
165 }