chkspeed.c 6.1 KB

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