aboutsummaryrefslogtreecommitdiff
path: root/src/testcurl/test_post_loop.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/testcurl/test_post_loop.c')
-rw-r--r--src/testcurl/test_post_loop.c515
1 files changed, 515 insertions, 0 deletions
diff --git a/src/testcurl/test_post_loop.c b/src/testcurl/test_post_loop.c
new file mode 100644
index 00000000..e19941df
--- /dev/null
+++ b/src/testcurl/test_post_loop.c
@@ -0,0 +1,515 @@
1/*
2 This file is part of libmicrohttpd
3 (C) 2007 Christian Grothoff
4
5 libmicrohttpd is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
9
10 libmicrohttpd is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with libmicrohttpd; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19*/
20
21/**
22 * @file daemontest_post_loop.c
23 * @brief Testcase for libmicrohttpd POST operations using URL-encoding
24 * @author Christian Grothoff (inspired by bug report #1296)
25 */
26
27#include "MHD_config.h"
28#include "platform.h"
29#include <curl/curl.h>
30#include <microhttpd.h>
31#include <stdlib.h>
32#include <string.h>
33#include <time.h>
34#include <gauger.h>
35
36#ifndef WINDOWS
37#include <unistd.h>
38#endif
39
40#define POST_DATA "<?xml version='1.0' ?>\n<xml>\n<data-id>1</data-id>\n</xml>\n"
41
42#define LOOPCOUNT 1000
43
44static int oneone;
45
46struct CBC
47{
48 char *buf;
49 size_t pos;
50 size_t size;
51};
52
53static size_t
54copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx)
55{
56 struct CBC *cbc = ctx;
57
58 if (cbc->pos + size * nmemb > cbc->size)
59 return 0; /* overflow */
60 memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
61 cbc->pos += size * nmemb;
62 return size * nmemb;
63}
64
65static int
66ahc_echo (void *cls,
67 struct MHD_Connection *connection,
68 const char *url,
69 const char *method,
70 const char *version,
71 const char *upload_data, size_t *upload_data_size,
72 void **mptr)
73{
74 static int marker;
75 struct MHD_Response *response;
76 int ret;
77
78 if (0 != strcmp ("POST", method))
79 {
80 printf ("METHOD: %s\n", method);
81 return MHD_NO; /* unexpected method */
82 }
83 if ((*mptr != NULL) && (0 == *upload_data_size))
84 {
85 if (*mptr != &marker)
86 abort ();
87 response = MHD_create_response_from_buffer (2, "OK",
88 MHD_RESPMEM_PERSISTENT);
89 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
90 MHD_destroy_response (response);
91 *mptr = NULL;
92 return ret;
93 }
94 if (strlen (POST_DATA) != *upload_data_size)
95 return MHD_YES;
96 *upload_data_size = 0;
97 *mptr = &marker;
98 return MHD_YES;
99}
100
101
102static int
103testInternalPost ()
104{
105 struct MHD_Daemon *d;
106 CURL *c;
107 char buf[2048];
108 struct CBC cbc;
109 CURLcode errornum;
110 int i;
111 char url[1024];
112
113 cbc.buf = buf;
114 cbc.size = 2048;
115 d = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG,
116 1080, NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END);
117 if (d == NULL)
118 return 1;
119 for (i = 0; i < LOOPCOUNT; i++)
120 {
121 if (99 == i % 100)
122 fprintf (stderr, ".");
123 c = curl_easy_init ();
124 cbc.pos = 0;
125 buf[0] = '\0';
126 sprintf (url, "http://127.0.0.1:1080/hw%d", i);
127 curl_easy_setopt (c, CURLOPT_URL, url);
128 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
129 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
130 curl_easy_setopt (c, CURLOPT_POSTFIELDS, POST_DATA);
131 curl_easy_setopt (c, CURLOPT_POSTFIELDSIZE, strlen (POST_DATA));
132 curl_easy_setopt (c, CURLOPT_POST, 1L);
133 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
134 curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
135 if (oneone)
136 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
137 else
138 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
139 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L);
140 // NOTE: use of CONNECTTIMEOUT without also
141 // setting NOSIGNAL results in really weird
142 // crashes on my system!
143 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
144 if (CURLE_OK != (errornum = curl_easy_perform (c)))
145 {
146 fprintf (stderr,
147 "curl_easy_perform failed: `%s'\n",
148 curl_easy_strerror (errornum));
149 curl_easy_cleanup (c);
150 MHD_stop_daemon (d);
151 return 2;
152 }
153 curl_easy_cleanup (c);
154 if ((buf[0] != 'O') || (buf[1] != 'K'))
155 {
156 MHD_stop_daemon (d);
157 return 4;
158 }
159 }
160 MHD_stop_daemon (d);
161 if (LOOPCOUNT >= 99)
162 fprintf (stderr, "\n");
163 return 0;
164}
165
166static int
167testMultithreadedPost ()
168{
169 struct MHD_Daemon *d;
170 CURL *c;
171 char buf[2048];
172 struct CBC cbc;
173 CURLcode errornum;
174 int i;
175 char url[1024];
176
177 cbc.buf = buf;
178 cbc.size = 2048;
179 d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION | MHD_USE_DEBUG,
180 1081, NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END);
181 if (d == NULL)
182 return 16;
183 for (i = 0; i < LOOPCOUNT; i++)
184 {
185 if (99 == i % 100)
186 fprintf (stderr, ".");
187 c = curl_easy_init ();
188 cbc.pos = 0;
189 buf[0] = '\0';
190 sprintf (url, "http://127.0.0.1:1081/hw%d", i);
191 curl_easy_setopt (c, CURLOPT_URL, url);
192 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
193 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
194 curl_easy_setopt (c, CURLOPT_POSTFIELDS, POST_DATA);
195 curl_easy_setopt (c, CURLOPT_POSTFIELDSIZE, strlen (POST_DATA));
196 curl_easy_setopt (c, CURLOPT_POST, 1L);
197 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
198 curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
199 if (oneone)
200 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
201 else
202 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
203 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L);
204 // NOTE: use of CONNECTTIMEOUT without also
205 // setting NOSIGNAL results in really weird
206 // crashes on my system!
207 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
208 if (CURLE_OK != (errornum = curl_easy_perform (c)))
209 {
210 fprintf (stderr,
211 "curl_easy_perform failed: `%s'\n",
212 curl_easy_strerror (errornum));
213 curl_easy_cleanup (c);
214 MHD_stop_daemon (d);
215 return 32;
216 }
217 curl_easy_cleanup (c);
218 if ((buf[0] != 'O') || (buf[1] != 'K'))
219 {
220 MHD_stop_daemon (d);
221 return 64;
222 }
223 }
224 MHD_stop_daemon (d);
225 if (LOOPCOUNT >= 99)
226 fprintf (stderr, "\n");
227 return 0;
228}
229
230static int
231testMultithreadedPoolPost ()
232{
233 struct MHD_Daemon *d;
234 CURL *c;
235 char buf[2048];
236 struct CBC cbc;
237 CURLcode errornum;
238 int i;
239 char url[1024];
240
241 cbc.buf = buf;
242 cbc.size = 2048;
243 d = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG,
244 1081, NULL, NULL, &ahc_echo, NULL,
245 MHD_OPTION_THREAD_POOL_SIZE, 4, MHD_OPTION_END);
246 if (d == NULL)
247 return 16;
248 for (i = 0; i < LOOPCOUNT; i++)
249 {
250 if (99 == i % 100)
251 fprintf (stderr, ".");
252 c = curl_easy_init ();
253 cbc.pos = 0;
254 buf[0] = '\0';
255 sprintf (url, "http://127.0.0.1:1081/hw%d", i);
256 curl_easy_setopt (c, CURLOPT_URL, url);
257 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
258 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
259 curl_easy_setopt (c, CURLOPT_POSTFIELDS, POST_DATA);
260 curl_easy_setopt (c, CURLOPT_POSTFIELDSIZE, strlen (POST_DATA));
261 curl_easy_setopt (c, CURLOPT_POST, 1L);
262 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
263 curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
264 if (oneone)
265 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
266 else
267 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
268 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L);
269 // NOTE: use of CONNECTTIMEOUT without also
270 // setting NOSIGNAL results in really weird
271 // crashes on my system!
272 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
273 if (CURLE_OK != (errornum = curl_easy_perform (c)))
274 {
275 fprintf (stderr,
276 "curl_easy_perform failed: `%s'\n",
277 curl_easy_strerror (errornum));
278 curl_easy_cleanup (c);
279 MHD_stop_daemon (d);
280 return 32;
281 }
282 curl_easy_cleanup (c);
283 if ((buf[0] != 'O') || (buf[1] != 'K'))
284 {
285 MHD_stop_daemon (d);
286 return 64;
287 }
288 }
289 MHD_stop_daemon (d);
290 if (LOOPCOUNT >= 99)
291 fprintf (stderr, "\n");
292 return 0;
293}
294
295static int
296testExternalPost ()
297{
298 struct MHD_Daemon *d;
299 CURL *c;
300 char buf[2048];
301 struct CBC cbc;
302 CURLM *multi;
303 CURLMcode mret;
304 fd_set rs;
305 fd_set ws;
306 fd_set es;
307 int max;
308 int running;
309 struct CURLMsg *msg;
310 time_t start;
311 struct timeval tv;
312 int i;
313 unsigned long long timeout;
314 long ctimeout;
315 char url[1024];
316
317 multi = NULL;
318 cbc.buf = buf;
319 cbc.size = 2048;
320 cbc.pos = 0;
321 d = MHD_start_daemon (MHD_USE_DEBUG,
322 1082, NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END);
323 if (d == NULL)
324 return 256;
325 multi = curl_multi_init ();
326 if (multi == NULL)
327 {
328 MHD_stop_daemon (d);
329 return 512;
330 }
331 for (i = 0; i < LOOPCOUNT; i++)
332 {
333 if (99 == i % 100)
334 fprintf (stderr, ".");
335 c = curl_easy_init ();
336 cbc.pos = 0;
337 buf[0] = '\0';
338 sprintf (url, "http://127.0.0.1:1082/hw%d", i);
339 curl_easy_setopt (c, CURLOPT_URL, url);
340 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
341 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
342 curl_easy_setopt (c, CURLOPT_POSTFIELDS, POST_DATA);
343 curl_easy_setopt (c, CURLOPT_POSTFIELDSIZE, strlen (POST_DATA));
344 curl_easy_setopt (c, CURLOPT_POST, 1L);
345 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
346 curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
347 if (oneone)
348 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
349 else
350 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
351 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L);
352 // NOTE: use of CONNECTTIMEOUT without also
353 // setting NOSIGNAL results in really weird
354 // crashes on my system!
355 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
356 mret = curl_multi_add_handle (multi, c);
357 if (mret != CURLM_OK)
358 {
359 curl_multi_cleanup (multi);
360 curl_easy_cleanup (c);
361 MHD_stop_daemon (d);
362 return 1024;
363 }
364 start = time (NULL);
365 while ((time (NULL) - start < 5) && (multi != NULL))
366 {
367 max = 0;
368 FD_ZERO (&rs);
369 FD_ZERO (&ws);
370 FD_ZERO (&es);
371 while (CURLM_CALL_MULTI_PERFORM ==
372 curl_multi_perform (multi, &running));
373 mret = curl_multi_fdset (multi, &rs, &ws, &es, &max);
374 if (mret != CURLM_OK)
375 {
376 curl_multi_remove_handle (multi, c);
377 curl_multi_cleanup (multi);
378 curl_easy_cleanup (c);
379 MHD_stop_daemon (d);
380 return 2048;
381 }
382 if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &max))
383 {
384 curl_multi_remove_handle (multi, c);
385 curl_multi_cleanup (multi);
386 curl_easy_cleanup (c);
387 MHD_stop_daemon (d);
388 return 4096;
389 }
390 if (MHD_NO == MHD_get_timeout (d, &timeout))
391 timeout = 100; /* 100ms == INFTY -- CURL bug... */
392 if ((CURLM_OK == curl_multi_timeout (multi, &ctimeout)) &&
393 (ctimeout < timeout) && (ctimeout >= 0))
394 timeout = ctimeout;
395 if ( (c == NULL) || (running == 0) )
396 timeout = 0; /* terminate quickly... */
397 tv.tv_sec = timeout / 1000;
398 tv.tv_usec = (timeout % 1000) * 1000;
399 select (max + 1, &rs, &ws, &es, &tv);
400 while (CURLM_CALL_MULTI_PERFORM ==
401 curl_multi_perform (multi, &running));
402 if (running == 0)
403 {
404 msg = curl_multi_info_read (multi, &running);
405 if (msg == NULL)
406 break;
407 if (msg->msg == CURLMSG_DONE)
408 {
409 if (msg->data.result != CURLE_OK)
410 printf ("%s failed at %s:%d: `%s'\n",
411 "curl_multi_perform",
412 __FILE__,
413 __LINE__, curl_easy_strerror (msg->data.result));
414 curl_multi_remove_handle (multi, c);
415 curl_easy_cleanup (c);
416 c = NULL;
417 }
418 }
419 MHD_run (d);
420 }
421 if (c != NULL)
422 {
423 curl_multi_remove_handle (multi, c);
424 curl_easy_cleanup (c);
425 }
426 if ((buf[0] != 'O') || (buf[1] != 'K'))
427 {
428 curl_multi_cleanup (multi);
429 MHD_stop_daemon (d);
430 return 8192;
431 }
432 }
433 curl_multi_cleanup (multi);
434 MHD_stop_daemon (d);
435 if (LOOPCOUNT >= 99)
436 fprintf (stderr, "\n");
437 return 0;
438}
439
440
441/**
442 * Time this round was started.
443 */
444static unsigned long long start_time;
445
446
447/**
448 * Get the current timestamp
449 *
450 * @return current time in ms
451 */
452static unsigned long long
453now ()
454{
455 struct timeval tv;
456
457 GETTIMEOFDAY (&tv, NULL);
458 return (((unsigned long long) tv.tv_sec * 1000LL) +
459 ((unsigned long long) tv.tv_usec / 1000LL));
460}
461
462
463int
464main (int argc, char *const *argv)
465{
466 unsigned int errorCount = 0;
467
468 oneone = NULL != strstr (argv[0], "11");
469 if (0 != curl_global_init (CURL_GLOBAL_WIN32))
470 return 2;
471 start_time = now();
472 errorCount += testInternalPost ();
473 fprintf (stderr,
474 oneone ? "%s: Sequential POSTs (http/1.1) %f/s\n" : "%s: Sequential POSTs (http/1.0) %f/s\n",
475 "internal select",
476 (double) 1000 * LOOPCOUNT / (now() - start_time + 1.0));
477 GAUGER ("internal select",
478 oneone ? "Sequential POSTs (http/1.1)" : "Sequential POSTs (http/1.0)",
479 (double) 1000 * LOOPCOUNT / (now() - start_time + 1.0),
480 "requests/s");
481 start_time = now();
482 errorCount += testMultithreadedPost ();
483 fprintf (stderr,
484 oneone ? "%s: Sequential POSTs (http/1.1) %f/s\n" : "%s: Sequential POSTs (http/1.0) %f/s\n",
485 "multithreaded post",
486 (double) 1000 * LOOPCOUNT / (now() - start_time + 1.0));
487 GAUGER ("Multithreaded select",
488 oneone ? "Sequential POSTs (http/1.1)" : "Sequential POSTs (http/1.0)",
489 (double) 1000 * LOOPCOUNT / (now() - start_time + 1.0),
490 "requests/s");
491 start_time = now();
492 errorCount += testMultithreadedPoolPost ();
493 fprintf (stderr,
494 oneone ? "%s: Sequential POSTs (http/1.1) %f/s\n" : "%s: Sequential POSTs (http/1.0) %f/s\n",
495 "thread with pool",
496 (double) 1000 * LOOPCOUNT / (now() - start_time + 1.0));
497 GAUGER ("thread with pool",
498 oneone ? "Sequential POSTs (http/1.1)" : "Sequential POSTs (http/1.0)",
499 (double) 1000 * LOOPCOUNT / (now() - start_time + 1.0),
500 "requests/s");
501 start_time = now();
502 errorCount += testExternalPost ();
503 fprintf (stderr,
504 oneone ? "%s: Sequential POSTs (http/1.1) %f/s\n" : "%s: Sequential POSTs (http/1.0) %f/s\n",
505 "external select",
506 (double) 1000 * LOOPCOUNT / (now() - start_time + 1.0));
507 GAUGER ("external select",
508 oneone ? "Sequential POSTs (http/1.1)" : "Sequential POSTs (http/1.0)",
509 (double) 1000 * LOOPCOUNT / (now() - start_time + 1.0),
510 "requests/s");
511 if (errorCount != 0)
512 fprintf (stderr, "Error (code: %u)\n", errorCount);
513 curl_global_cleanup ();
514 return errorCount != 0; /* 0 == pass */
515}