2
0

chkspeed.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 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, "-X", 2) == 0) {
  74. prtsep = 1;
  75. } else if (strncasecmp(*argv, "-T", 2) == 0) {
  76. prttime = 1;
  77. } else if (strncasecmp(*argv, "-M=", 3) == 0) {
  78. long m = strtol((*argv)+3, NULL, 10);
  79. switch(m) {
  80. case 1: url = URL_1M;
  81. break;
  82. case 2: url = URL_2M;
  83. break;
  84. case 5: url = URL_5M;
  85. break;
  86. case 10: url = URL_10M;
  87. break;
  88. case 20: url = URL_20M;
  89. break;
  90. case 50: url = URL_50M;
  91. break;
  92. case 100: url = URL_100M;
  93. break;
  94. default: fprintf(stderr, "\r%s: invalid parameter %s\n",
  95. appname, *argv + 3);
  96. exit(1);
  97. }
  98. } else {
  99. fprintf(stderr, "\r%s: invalid or unknown option %s\n",
  100. appname, *argv);
  101. exit(1);
  102. }
  103. } else {
  104. url = *argv;
  105. }
  106. }
  107. }
  108. /* print separator line */
  109. if (prtsep) {
  110. printf("-------------------------------------------------\n");
  111. }
  112. /* print localtime */
  113. if (prttime) {
  114. time_t t = time(NULL);
  115. printf("Localtime: %s", ctime(&t));
  116. }
  117. /* init libcurl */
  118. curl_global_init(CURL_GLOBAL_ALL);
  119. /* init the curl session */
  120. curl_handle = curl_easy_init();
  121. /* specify URL to get */
  122. curl_easy_setopt(curl_handle, CURLOPT_URL, url);
  123. /* send all data to this function */
  124. curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteCallback);
  125. /* some servers don't like requests that are made without a user-agent
  126. field, so we provide one */
  127. curl_easy_setopt(curl_handle, CURLOPT_USERAGENT,
  128. "libcurl-speedchecker/" CHKSPEED_VERSION);
  129. /* get it! */
  130. res = curl_easy_perform(curl_handle);
  131. if(CURLE_OK == res) {
  132. double val;
  133. /* check for bytes downloaded */
  134. res = curl_easy_getinfo(curl_handle, CURLINFO_SIZE_DOWNLOAD, &val);
  135. if((CURLE_OK == res) && (val>0))
  136. printf("Data downloaded: %0.0f bytes.\n", val);
  137. /* check for total download time */
  138. res = curl_easy_getinfo(curl_handle, CURLINFO_TOTAL_TIME, &val);
  139. if((CURLE_OK == res) && (val>0))
  140. printf("Total download time: %0.3f sec.\n", val);
  141. /* check for average download speed */
  142. res = curl_easy_getinfo(curl_handle, CURLINFO_SPEED_DOWNLOAD, &val);
  143. if((CURLE_OK == res) && (val>0))
  144. printf("Average download speed: %0.3f kbyte/sec.\n", val / 1024);
  145. } else {
  146. fprintf(stderr, "Error while fetching '%s' : %s\n",
  147. url, curl_easy_strerror(res));
  148. }
  149. /* cleanup curl stuff */
  150. curl_easy_cleanup(curl_handle);
  151. /* we're done with libcurl, so clean it up */
  152. curl_global_cleanup();
  153. return 0;
  154. }