诸暨麻将添加redis
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

257 lines
6.2 KiB

  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2013, Linus Nielsen Feltzing, <linus@haxx.se>
  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. #include "test.h"
  23. #include "testutil.h"
  24. #include "warnless.h"
  25. #include "memdebug.h"
  26. #define TEST_HANG_TIMEOUT 60 * 1000
  27. #define MAX_URLS 200
  28. #define MAX_BLACKLIST 20
  29. int urltime[MAX_URLS];
  30. char *urlstring[MAX_URLS];
  31. CURL *handles[MAX_URLS];
  32. char *site_blacklist[MAX_BLACKLIST];
  33. char *server_blacklist[MAX_BLACKLIST];
  34. int num_handles;
  35. int blacklist_num_servers;
  36. int blacklist_num_sites;
  37. int parse_url_file(const char *filename);
  38. void free_urls(void);
  39. int create_handles(void);
  40. void setup_handle(char *base_url, CURLM *m, int handlenum);
  41. void remove_handles(void);
  42. static size_t
  43. write_callback(void *contents, size_t size, size_t nmemb, void *userp)
  44. {
  45. size_t realsize = size * nmemb;
  46. (void)contents;
  47. (void)userp;
  48. return realsize;
  49. }
  50. int parse_url_file(const char *filename)
  51. {
  52. FILE *f;
  53. int filetime;
  54. char buf[200];
  55. num_handles = 0;
  56. blacklist_num_sites = 0;
  57. blacklist_num_servers = 0;
  58. f = fopen(filename, "rb");
  59. if(!f)
  60. return 0;
  61. while(!feof(f)) {
  62. if(fscanf(f, "%d %s\n", &filetime, buf)) {
  63. urltime[num_handles] = filetime;
  64. urlstring[num_handles] = strdup(buf);
  65. num_handles++;
  66. continue;
  67. }
  68. if(fscanf(f, "blacklist_site %s\n", buf)) {
  69. site_blacklist[blacklist_num_sites] = strdup(buf);
  70. blacklist_num_sites++;
  71. continue;
  72. }
  73. break;
  74. }
  75. fclose(f);
  76. site_blacklist[blacklist_num_sites] = NULL;
  77. server_blacklist[blacklist_num_servers] = NULL;
  78. return num_handles;
  79. }
  80. void free_urls(void)
  81. {
  82. int i;
  83. for(i = 0;i < num_handles;i++) {
  84. free(urlstring[i]);
  85. }
  86. for(i = 0;i < blacklist_num_servers;i++) {
  87. free(server_blacklist[i]);
  88. }
  89. for(i = 0;i < blacklist_num_sites;i++) {
  90. free(site_blacklist[i]);
  91. }
  92. }
  93. int create_handles(void)
  94. {
  95. int i;
  96. for(i = 0;i < num_handles;i++) {
  97. handles[i] = curl_easy_init();
  98. }
  99. return 0;
  100. }
  101. void setup_handle(char *base_url, CURLM *m, int handlenum)
  102. {
  103. char urlbuf[256];
  104. sprintf(urlbuf, "%s%s", base_url, urlstring[handlenum]);
  105. curl_easy_setopt(handles[handlenum], CURLOPT_URL, urlbuf);
  106. curl_easy_setopt(handles[handlenum], CURLOPT_VERBOSE, 1L);
  107. curl_easy_setopt(handles[handlenum], CURLOPT_FAILONERROR, 1L);
  108. curl_easy_setopt(handles[handlenum], CURLOPT_WRITEFUNCTION, write_callback);
  109. curl_easy_setopt(handles[handlenum], CURLOPT_WRITEDATA, NULL);
  110. curl_multi_add_handle(m, handles[handlenum]);
  111. }
  112. void remove_handles(void)
  113. {
  114. int i;
  115. for(i = 0;i < num_handles;i++) {
  116. if(handles[i])
  117. curl_easy_cleanup(handles[i]);
  118. }
  119. }
  120. int test(char *URL)
  121. {
  122. int res = 0;
  123. CURLM *m = NULL;
  124. CURLMsg *msg; /* for picking up messages with the transfer status */
  125. int msgs_left; /* how many messages are left */
  126. int running;
  127. int handlenum = 0;
  128. struct timeval last_handle_add;
  129. if(parse_url_file("log/urls.txt") <= 0)
  130. goto test_cleanup;
  131. start_test_timing();
  132. curl_global_init(CURL_GLOBAL_ALL);
  133. m = curl_multi_init();
  134. create_handles();
  135. multi_setopt(m, CURLMOPT_PIPELINING, 1L);
  136. multi_setopt(m, CURLMOPT_MAX_HOST_CONNECTIONS, 2L);
  137. multi_setopt(m, CURLMOPT_MAX_PIPELINE_LENGTH, 3L);
  138. multi_setopt(m, CURLMOPT_CONTENT_LENGTH_PENALTY_SIZE, 15000L);
  139. multi_setopt(m, CURLMOPT_CHUNK_LENGTH_PENALTY_SIZE, 10000L);
  140. multi_setopt(m, CURLMOPT_PIPELINING_SITE_BL, site_blacklist);
  141. multi_setopt(m, CURLMOPT_PIPELINING_SERVER_BL, server_blacklist);
  142. last_handle_add = tutil_tvnow();
  143. for(;;) {
  144. struct timeval interval;
  145. struct timeval now;
  146. long int msnow, mslast;
  147. fd_set rd, wr, exc;
  148. int maxfd = -99;
  149. long timeout;
  150. interval.tv_sec = 1;
  151. interval.tv_usec = 0;
  152. if(handlenum < num_handles) {
  153. now = tutil_tvnow();
  154. msnow = now.tv_sec * 1000 + now.tv_usec / 1000;
  155. mslast = last_handle_add.tv_sec * 1000 + last_handle_add.tv_usec / 1000;
  156. if(msnow - mslast >= urltime[handlenum] && handlenum < num_handles) {
  157. fprintf(stdout, "Adding handle %d\n", handlenum);
  158. setup_handle(URL, m, handlenum);
  159. last_handle_add = now;
  160. handlenum++;
  161. }
  162. }
  163. curl_multi_perform(m, &running);
  164. abort_on_test_timeout();
  165. /* See how the transfers went */
  166. while ((msg = curl_multi_info_read(m, &msgs_left))) {
  167. if (msg->msg == CURLMSG_DONE) {
  168. int i, found = 0;
  169. /* Find out which handle this message is about */
  170. for (i = 0; i < num_handles; i++) {
  171. found = (msg->easy_handle == handles[i]);
  172. if(found)
  173. break;
  174. }
  175. printf("Handle %d Completed with status %d\n", i, msg->data.result);
  176. curl_multi_remove_handle(m, handles[i]);
  177. }
  178. }
  179. if(handlenum == num_handles && !running) {
  180. break; /* done */
  181. }
  182. FD_ZERO(&rd);
  183. FD_ZERO(&wr);
  184. FD_ZERO(&exc);
  185. curl_multi_fdset(m, &rd, &wr, &exc, &maxfd);
  186. /* At this point, maxfd is guaranteed to be greater or equal than -1. */
  187. curl_multi_timeout(m, &timeout);
  188. if(timeout < 0)
  189. timeout = 1;
  190. interval.tv_sec = timeout / 1000;
  191. interval.tv_usec = (timeout % 1000) * 1000;
  192. interval.tv_sec = 0;
  193. interval.tv_usec = 1000;
  194. select_test(maxfd+1, &rd, &wr, &exc, &interval);
  195. abort_on_test_timeout();
  196. }
  197. test_cleanup:
  198. remove_handles();
  199. /* undocumented cleanup sequence - type UB */
  200. curl_multi_cleanup(m);
  201. curl_global_cleanup();
  202. free_urls();
  203. return res;
  204. }