00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
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
00057
00058 (void)ptr;
00059 (void)data;
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
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
00138 if(prtsep) {
00139 printf("-------------------------------------------------\n");
00140 }
00141
00142 if(prttime) {
00143 time_t t = time(NULL);
00144 printf("Localtime: %s", ctime(&t));
00145 }
00146
00147
00148 curl_global_init(CURL_GLOBAL_ALL);
00149
00150
00151 curl_handle = curl_easy_init();
00152
00153
00154 curl_easy_setopt(curl_handle, CURLOPT_URL, url);
00155
00156
00157 curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteCallback);
00158
00159
00160
00161 curl_easy_setopt(curl_handle, CURLOPT_USERAGENT,
00162 "libcurl-speedchecker/" CHKSPEED_VERSION);
00163
00164
00165 res = curl_easy_perform(curl_handle);
00166
00167 if(CURLE_OK == res) {
00168 double val;
00169
00170
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
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
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
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
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
00203 curl_easy_cleanup(curl_handle);
00204
00205
00206 curl_global_cleanup();
00207
00208 return 0;
00209 }