chkspeed.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 /* <DESC>
00023  * Show transfer timing info after download completes.
00024  * </DESC>
00025  */
00026 /* Example source code to show how the callback function can be used to
00027  * download data into a chunk of memory instead of storing it in a file.
00028  * After successful download we use curl_easy_getinfo() calls to get the
00029  * amount of downloaded bytes, the time used for the whole download, and
00030  * the average download speed.
00031  * On Linux you can create the download test files with:
00032  * dd if=/dev/urandom of=file_1M.bin bs=1M count=1
00033  *
00034  */
00035 
00036 #include <stdio.h>
00037 #include <stdlib.h>
00038 #include <string.h>
00039 #include <time.h>
00040 
00041 #include <curl/curl.h>
00042 
00043 #define URL_BASE "http://speedtest.your.domain/"
00044 #define URL_1M   URL_BASE "file_1M.bin"
00045 #define URL_2M   URL_BASE "file_2M.bin"
00046 #define URL_5M   URL_BASE "file_5M.bin"
00047 #define URL_10M  URL_BASE "file_10M.bin"
00048 #define URL_20M  URL_BASE "file_20M.bin"
00049 #define URL_50M  URL_BASE "file_50M.bin"
00050 #define URL_100M URL_BASE "file_100M.bin"
00051 
00052 #define CHKSPEED_VERSION "1.0"
00053 
00054 static size_t WriteCallback(void *ptr, size_t size, size_t nmemb, void *data)
00055 {
00056   /* we are not interested in the downloaded bytes itself,
00057      so we only return the size we would have saved ... */
00058   (void)ptr;  /* unused */
00059   (void)data; /* unused */
00060   return (size_t)(size * nmemb);
00061 }
00062 
00063 int main(int argc, char *argv[])
00064 {
00065   CURL *curl_handle;
00066   CURLcode res;
00067   int prtall = 0, prtsep = 0, prttime = 0;
00068   const char *url = URL_1M;
00069   char *appname = argv[0];
00070 
00071   if(argc > 1) {
00072     /* parse input parameters */
00073     for(argc--, argv++; *argv; argc--, argv++) {
00074       if(strncasecmp(*argv, "-", 1) == 0) {
00075         if(strncasecmp(*argv, "-H", 2) == 0) {
00076           fprintf(stderr,
00077                   "\rUsage: %s [-m=1|2|5|10|20|50|100] [-t] [-x] [url]\n",
00078                   appname);
00079           exit(1);
00080         }
00081         else if(strncasecmp(*argv, "-V", 2) == 0) {
00082           fprintf(stderr, "\r%s %s - %s\n",
00083                   appname, CHKSPEED_VERSION, curl_version());
00084           exit(1);
00085         }
00086         else if(strncasecmp(*argv, "-A", 2) == 0) {
00087           prtall = 1;
00088         }
00089         else if(strncasecmp(*argv, "-X", 2) == 0) {
00090           prtsep = 1;
00091         }
00092         else if(strncasecmp(*argv, "-T", 2) == 0) {
00093           prttime = 1;
00094         }
00095         else if(strncasecmp(*argv, "-M=", 3) == 0) {
00096           long m = strtol((*argv)+3, NULL, 10);
00097           switch(m) {
00098           case 1:
00099             url = URL_1M;
00100             break;
00101           case 2:
00102             url = URL_2M;
00103             break;
00104           case 5:
00105             url = URL_5M;
00106             break;
00107           case 10:
00108             url = URL_10M;
00109             break;
00110           case 20:
00111             url = URL_20M;
00112             break;
00113           case 50:
00114             url = URL_50M;
00115             break;
00116           case 100:
00117             url = URL_100M;
00118             break;
00119           default:
00120             fprintf(stderr, "\r%s: invalid parameter %s\n",
00121                     appname, *argv + 3);
00122             exit(1);
00123           }
00124         }
00125         else {
00126           fprintf(stderr, "\r%s: invalid or unknown option %s\n",
00127                   appname, *argv);
00128           exit(1);
00129         }
00130       }
00131       else {
00132         url = *argv;
00133       }
00134     }
00135   }
00136 
00137   /* print separator line */
00138   if(prtsep) {
00139     printf("-------------------------------------------------\n");
00140   }
00141   /* print localtime */
00142   if(prttime) {
00143     time_t t = time(NULL);
00144     printf("Localtime: %s", ctime(&t));
00145   }
00146 
00147   /* init libcurl */
00148   curl_global_init(CURL_GLOBAL_ALL);
00149 
00150   /* init the curl session */
00151   curl_handle = curl_easy_init();
00152 
00153   /* specify URL to get */
00154   curl_easy_setopt(curl_handle, CURLOPT_URL, url);
00155 
00156   /* send all data to this function  */
00157   curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteCallback);
00158 
00159   /* some servers don't like requests that are made without a user-agent
00160      field, so we provide one */
00161   curl_easy_setopt(curl_handle, CURLOPT_USERAGENT,
00162                    "libcurl-speedchecker/" CHKSPEED_VERSION);
00163 
00164   /* get it! */
00165   res = curl_easy_perform(curl_handle);
00166 
00167   if(CURLE_OK == res) {
00168     double val;
00169 
00170     /* check for bytes downloaded */
00171     res = curl_easy_getinfo(curl_handle, CURLINFO_SIZE_DOWNLOAD, &val);
00172     if((CURLE_OK == res) && (val>0))
00173       printf("Data downloaded: %0.0f bytes.\n", val);
00174 
00175     /* check for total download time */
00176     res = curl_easy_getinfo(curl_handle, CURLINFO_TOTAL_TIME, &val);
00177     if((CURLE_OK == res) && (val>0))
00178       printf("Total download time: %0.3f sec.\n", val);
00179 
00180     /* check for average download speed */
00181     res = curl_easy_getinfo(curl_handle, CURLINFO_SPEED_DOWNLOAD, &val);
00182     if((CURLE_OK == res) && (val>0))
00183       printf("Average download speed: %0.3f kbyte/sec.\n", val / 1024);
00184 
00185     if(prtall) {
00186       /* check for name resolution time */
00187       res = curl_easy_getinfo(curl_handle, CURLINFO_NAMELOOKUP_TIME, &val);
00188       if((CURLE_OK == res) && (val>0))
00189         printf("Name lookup time: %0.3f sec.\n", val);
00190 
00191       /* check for connect time */
00192       res = curl_easy_getinfo(curl_handle, CURLINFO_CONNECT_TIME, &val);
00193       if((CURLE_OK == res) && (val>0))
00194         printf("Connect time: %0.3f sec.\n", val);
00195     }
00196   }
00197   else {
00198     fprintf(stderr, "Error while fetching '%s' : %s\n",
00199             url, curl_easy_strerror(res));
00200   }
00201 
00202   /* cleanup curl stuff */
00203   curl_easy_cleanup(curl_handle);
00204 
00205   /* we're done with libcurl, so clean it up */
00206   curl_global_cleanup();
00207 
00208   return 0;
00209 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:02