2
0

chkspeed.c 6.5 KB

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