chkspeed.c 4.9 KB

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