libmicrohttpd

HTTP/1.x server C library (MHD 1.x, stable)
Log | Files | Refs | Submodules | README | LICENSE

perf_get_concurrent.c (15275B)


      1 /*
      2      This file is part of libmicrohttpd
      3      Copyright (C) 2007, 2009, 2011 Christian Grothoff
      4      Copyright (C) 2014-2022 Evgeny Grin (Karlson2k)
      5 
      6      libmicrohttpd is free software; you can redistribute it and/or modify
      7      it under the terms of the GNU General Public License as published
      8      by the Free Software Foundation; either version 2, or (at your
      9      option) any later version.
     10 
     11      libmicrohttpd is distributed in the hope that it will be useful, but
     12      WITHOUT ANY WARRANTY; without even the implied warranty of
     13      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     14      General Public License for more details.
     15 
     16      You should have received a copy of the GNU General Public License
     17      along with libmicrohttpd; see the file COPYING.  If not, write to the
     18      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     19      Boston, MA 02110-1301, USA.
     20 */
     21 
     22 /**
     23  * @file perf_get_concurrent.c
     24  * @brief benchmark concurrent GET operations
     25  *        Note that we run libcurl on the machine at the
     26  *        same time, so the execution time may be influenced
     27  *        by the concurrent activity; it is quite possible
     28  *        that more time is spend with libcurl than with MHD,
     29  *        so the performance scores calculated with this code
     30  *        should NOT be used to compare with other HTTP servers
     31  *        (since MHD is actually better); only the relative
     32  *        scores between MHD versions are meaningful.
     33  * @author Christian Grothoff
     34  * @author Karlson2k (Evgeny Grin)
     35  */
     36 
     37 #include "MHD_config.h"
     38 #include "platform.h"
     39 #include <curl/curl.h>
     40 #include <microhttpd.h>
     41 #include <stdlib.h>
     42 #include <string.h>
     43 #include <time.h>
     44 #include <pthread.h>
     45 #include <errno.h>
     46 #include "mhd_has_in_name.h"
     47 
     48 /* Turn any MHD_PANIC() or failing mhd_assert() reached from this
     49    test into a marked, classifiable test error (TESTING.md, P5). */
     50 #include "mhd_panic_tripwire.h"
     51 
     52 #if defined(MHD_CPU_COUNT) && (MHD_CPU_COUNT + 0) < 2
     53 #undef MHD_CPU_COUNT
     54 #endif
     55 #if ! defined(MHD_CPU_COUNT)
     56 #define MHD_CPU_COUNT 2
     57 #endif
     58 
     59 /**
     60  * How many rounds of operations do we do for each
     61  * test (total number of requests will be ROUNDS * PAR).
     62  * Ensure that free ports are not exhausted during test.
     63  */
     64 #if MHD_CPU_COUNT > 8
     65 #ifndef _WIN32
     66 #define ROUNDS (1 + (30000 / 12) / MHD_CPU_COUNT)
     67 #else /* _WIN32 */
     68 #define ROUNDS (1 + (3000 / 12) / MHD_CPU_COUNT)
     69 #endif /* _WIN32 */
     70 #else
     71 #define ROUNDS 500
     72 #endif
     73 
     74 /**
     75  * How many requests do we do in parallel?
     76  */
     77 #define PAR MHD_CPU_COUNT
     78 
     79 /**
     80  * Do we use HTTP 1.1?
     81  */
     82 static int oneone;
     83 
     84 /**
     85  * Response to return (re-used).
     86  */
     87 static struct MHD_Response *response;
     88 
     89 /**
     90  * Time this round was started.
     91  */
     92 static unsigned long long start_time;
     93 
     94 /**
     95  * Set to 1 if the worker threads are done.
     96  */
     97 static volatile int signal_done;
     98 
     99 
    100 /**
    101  * Get the current timestamp
    102  *
    103  * @return current time in ms
    104  */
    105 static unsigned long long
    106 now (void)
    107 {
    108   struct timeval tv;
    109 
    110   gettimeofday (&tv, NULL);
    111   return (((unsigned long long) tv.tv_sec * 1000LL)
    112           + ((unsigned long long) tv.tv_usec / 1000LL));
    113 }
    114 
    115 
    116 /**
    117  * Start the timer.
    118  */
    119 static void
    120 start_timer (void)
    121 {
    122   start_time = now ();
    123 }
    124 
    125 
    126 /**
    127  * Stop the timer and report performance
    128  *
    129  * @param desc description of the threading mode we used
    130  */
    131 static void
    132 stop (const char *desc)
    133 {
    134   double rps = ((double) (PAR * ROUNDS * 1000)) / ((double) (now ()
    135                                                              - start_time));
    136 
    137   fprintf (stderr,
    138            "Parallel GETs using %s: %f %s\n",
    139            desc,
    140            rps,
    141            "requests/s");
    142 }
    143 
    144 
    145 static size_t
    146 copyBuffer (void *ptr,
    147             size_t size, size_t nmemb,
    148             void *ctx)
    149 {
    150   (void) ptr; (void) ctx;          /* Unused. Silent compiler warning. */
    151   return size * nmemb;
    152 }
    153 
    154 
    155 static enum MHD_Result
    156 ahc_echo (void *cls,
    157           struct MHD_Connection *connection,
    158           const char *url,
    159           const char *method,
    160           const char *version,
    161           const char *upload_data, size_t *upload_data_size,
    162           void **req_cls)
    163 {
    164   static int ptr;
    165   enum MHD_Result ret;
    166   (void) cls;
    167   (void) url; (void) version;                      /* Unused. Silent compiler warning. */
    168   (void) upload_data; (void) upload_data_size;     /* Unused. Silent compiler warning. */
    169 
    170   if (0 != strcmp (MHD_HTTP_METHOD_GET, method))
    171     return MHD_NO;              /* unexpected method */
    172   if (&ptr != *req_cls)
    173   {
    174     *req_cls = &ptr;
    175     return MHD_YES;
    176   }
    177   *req_cls = NULL;
    178   ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
    179   if (ret == MHD_NO)
    180     abort ();
    181   return ret;
    182 }
    183 
    184 
    185 static void *
    186 thread_gets (void *param)
    187 {
    188   CURL *c;
    189   CURLcode errornum;
    190   unsigned int i;
    191   char *const url = (char *) param;
    192   static char curl_err_marker[] = "curl error";
    193 
    194   c = curl_easy_init ();
    195   curl_easy_setopt (c, CURLOPT_URL, url);
    196   curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
    197   curl_easy_setopt (c, CURLOPT_WRITEDATA, NULL);
    198   curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L);
    199   curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
    200   if (oneone)
    201     curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    202   else
    203     curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
    204   curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L);
    205   /* NOTE: use of CONNECTTIMEOUT without also
    206      setting NOSIGNAL results in really weird
    207      crashes on my system! */
    208   curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L);
    209   curl_easy_setopt (c, CURLOPT_PROXY, "");
    210   for (i = 0; i < ROUNDS; i++)
    211   {
    212     if (CURLE_OK != (errornum = curl_easy_perform (c)))
    213     {
    214       fprintf (stderr,
    215                "curl_easy_perform failed: `%s'\n",
    216                curl_easy_strerror (errornum));
    217       curl_easy_cleanup (c);
    218       return curl_err_marker;
    219     }
    220   }
    221   curl_easy_cleanup (c);
    222 
    223   return NULL;
    224 }
    225 
    226 
    227 static void *
    228 do_gets (void *param)
    229 {
    230   int j;
    231   pthread_t par[PAR];
    232   char url[64];
    233   uint16_t port = (uint16_t) (intptr_t) param;
    234   char *err = NULL;
    235   static char pthr_err_marker[] = "pthread_create error";
    236 
    237   snprintf (url,
    238             sizeof (url),
    239             "http://127.0.0.1:%u/hello_world",
    240             (unsigned int) port);
    241   for (j = 0; j < PAR; j++)
    242   {
    243     if (0 != pthread_create (&par[j], NULL, &thread_gets, (void *) url))
    244     {
    245       for (j--; j >= 0; j--)
    246         pthread_join (par[j], NULL);
    247       return pthr_err_marker;
    248     }
    249   }
    250   for (j = 0; j < PAR; j++)
    251   {
    252     char *ret_val;
    253     if ((0 != pthread_join (par[j], (void **) &ret_val)) ||
    254         (NULL != ret_val) )
    255       err = ret_val;
    256   }
    257   signal_done = 1;
    258   return err;
    259 }
    260 
    261 
    262 static unsigned int
    263 testInternalGet (uint16_t port, uint32_t poll_flag)
    264 {
    265   struct MHD_Daemon *d;
    266   const char *const test_desc = ((poll_flag & MHD_USE_AUTO) ?
    267                                  "internal thread with 'auto'" :
    268                                  (poll_flag & MHD_USE_POLL) ?
    269                                  "internal thread with poll()" :
    270                                  (poll_flag & MHD_USE_EPOLL) ?
    271                                  "internal thread with epoll" :
    272                                  "internal thread with select()");
    273   const char *ret_val;
    274 
    275   if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT))
    276     port = 0;
    277 
    278   signal_done = 0;
    279   d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG
    280                         | (enum MHD_FLAG) poll_flag,
    281                         port, NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END);
    282   if (d == NULL)
    283     return 1;
    284   if (0 == port)
    285   {
    286     const union MHD_DaemonInfo *dinfo;
    287     dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
    288     if ((NULL == dinfo) || (0 == dinfo->port) )
    289     {
    290       MHD_stop_daemon (d); return 32;
    291     }
    292     port = dinfo->port;
    293   }
    294   start_timer ();
    295   ret_val = do_gets ((void *) (intptr_t) port);
    296   if (! ret_val)
    297     stop (test_desc);
    298   MHD_stop_daemon (d);
    299   if (ret_val)
    300   {
    301     fprintf (stderr,
    302              "Error performing %s test: %s\n", test_desc, ret_val);
    303     return 4;
    304   }
    305   return 0;
    306 }
    307 
    308 
    309 static unsigned int
    310 testMultithreadedGet (uint16_t port, uint32_t poll_flag)
    311 {
    312   struct MHD_Daemon *d;
    313   const char *const test_desc = ((poll_flag & MHD_USE_AUTO) ?
    314                                  "internal thread with 'auto' and thread per connection"
    315                                  :
    316                                  (poll_flag & MHD_USE_POLL) ?
    317                                  "internal thread with poll() and thread per connection"
    318                                  :
    319                                  (poll_flag & MHD_USE_EPOLL) ?
    320                                  "internal thread with epoll and thread per connection"
    321                                  :
    322                                  "internal thread with select() and thread per connection");
    323   const char *ret_val;
    324 
    325   if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT))
    326     port = 0;
    327 
    328   signal_done = 0;
    329   d = MHD_start_daemon (MHD_USE_THREAD_PER_CONNECTION
    330                         | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG
    331                         | (enum MHD_FLAG) poll_flag,
    332                         port, NULL, NULL, &ahc_echo, NULL, MHD_OPTION_END);
    333   if (d == NULL)
    334     return 16;
    335   if (0 == port)
    336   {
    337     const union MHD_DaemonInfo *dinfo;
    338     dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
    339     if ((NULL == dinfo) || (0 == dinfo->port) )
    340     {
    341       MHD_stop_daemon (d); return 32;
    342     }
    343     port = dinfo->port;
    344   }
    345   start_timer ();
    346   ret_val = do_gets ((void *) (intptr_t) port);
    347   if (! ret_val)
    348     stop (test_desc);
    349   MHD_stop_daemon (d);
    350   if (ret_val)
    351   {
    352     fprintf (stderr,
    353              "Error performing %s test: %s\n", test_desc, ret_val);
    354     return 4;
    355   }
    356   return 0;
    357 }
    358 
    359 
    360 static unsigned int
    361 testMultithreadedPoolGet (uint16_t port, uint32_t poll_flag)
    362 {
    363   struct MHD_Daemon *d;
    364   const char *const test_desc = ((poll_flag & MHD_USE_AUTO) ?
    365                                  "internal thread pool with 'auto'" :
    366                                  (poll_flag & MHD_USE_POLL) ?
    367                                  "internal thread pool with poll()" :
    368                                  (poll_flag & MHD_USE_EPOLL) ?
    369                                  "internal thread poll with epoll" :
    370                                  "internal thread pool with select()");
    371   const char *ret_val;
    372 
    373   if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT))
    374     port = 0;
    375 
    376   signal_done = 0;
    377   d = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG
    378                         | (enum MHD_FLAG) poll_flag,
    379                         port, NULL, NULL, &ahc_echo, NULL,
    380                         MHD_OPTION_THREAD_POOL_SIZE, MHD_CPU_COUNT,
    381                         MHD_OPTION_END);
    382   if (d == NULL)
    383     return 16;
    384   if (0 == port)
    385   {
    386     const union MHD_DaemonInfo *dinfo;
    387     dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
    388     if ((NULL == dinfo) || (0 == dinfo->port) )
    389     {
    390       MHD_stop_daemon (d); return 32;
    391     }
    392     port = dinfo->port;
    393   }
    394   start_timer ();
    395   ret_val = do_gets ((void *) (intptr_t) port);
    396   if (! ret_val)
    397     stop (test_desc);
    398   MHD_stop_daemon (d);
    399   if (ret_val)
    400   {
    401     fprintf (stderr,
    402              "Error performing %s test: %s\n", test_desc, ret_val);
    403     return 4;
    404   }
    405   return 0;
    406 }
    407 
    408 
    409 static unsigned int
    410 testExternalGet (uint16_t port)
    411 {
    412   struct MHD_Daemon *d;
    413   pthread_t tid;
    414   fd_set rs;
    415   fd_set ws;
    416   fd_set es;
    417   MHD_socket max;
    418   struct timeval tv;
    419   uint64_t tt64;
    420   char *ret_val;
    421   int ret = 0;
    422 
    423   if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT))
    424     port = 0;
    425 
    426   signal_done = 0;
    427   d = MHD_start_daemon (MHD_USE_ERROR_LOG,
    428                         port, NULL, NULL, &ahc_echo, NULL,
    429                         MHD_OPTION_APP_FD_SETSIZE, (int) FD_SETSIZE,
    430                         MHD_OPTION_END);
    431   if (d == NULL)
    432     return 256;
    433   if (0 == port)
    434   {
    435     const union MHD_DaemonInfo *dinfo;
    436     dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
    437     if ((NULL == dinfo) || (0 == dinfo->port) )
    438     {
    439       MHD_stop_daemon (d); return 32;
    440     }
    441     port = dinfo->port;
    442   }
    443   if (0 != pthread_create (&tid, NULL,
    444                            &do_gets, (void *) (intptr_t) port))
    445   {
    446     MHD_stop_daemon (d);
    447     return 512;
    448   }
    449   start_timer ();
    450 
    451   while (0 == signal_done)
    452   {
    453     max = 0;
    454     FD_ZERO (&rs);
    455     FD_ZERO (&ws);
    456     FD_ZERO (&es);
    457     if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &max))
    458     {
    459       MHD_stop_daemon (d);
    460       return 4096;
    461     }
    462     if (MHD_NO == MHD_get_timeout64 (d, &tt64))
    463       tt64 = 1;
    464 #if ! defined(_WIN32) || defined(__CYGWIN__)
    465     tv.tv_sec = (time_t) (tt64 / 1000);
    466 #else  /* Native W32 */
    467     tv.tv_sec = (long) (tt64 / 1000);
    468 #endif /* Native W32 */
    469     tv.tv_usec = ((long) (tt64 % 1000)) * 1000;
    470     if (-1 == select (max + 1, &rs, &ws, &es, &tv))
    471     {
    472 #ifdef MHD_POSIX_SOCKETS
    473       if (EINTR != errno)
    474       {
    475         fprintf (stderr, "Unexpected select() error: %d. Line: %d\n",
    476                  (int) errno, __LINE__);
    477         fflush (stderr);
    478         exit (99);
    479       }
    480       ret |= 1024;
    481       break;
    482 #else
    483       if ((WSAEINVAL != WSAGetLastError ()) ||
    484           (0 != rs.fd_count) || (0 != ws.fd_count) || (0 != es.fd_count) )
    485       {
    486         fprintf (stderr, "Unexpected select() error: %d. Line: %d\n",
    487                  (int) WSAGetLastError (), __LINE__);
    488         fflush (stderr);
    489         exit (99);
    490       }
    491       Sleep (1);
    492 #endif
    493     }
    494     MHD_run_from_select (d, &rs, &ws, &es);
    495   }
    496 
    497   stop ("external select");
    498   MHD_stop_daemon (d);
    499   if ((0 != pthread_join (tid, (void **) &ret_val)) ||
    500       (NULL != ret_val) )
    501   {
    502     fprintf (stderr,
    503              "%s\n", ret_val);
    504     ret |= 8;
    505   }
    506   if (ret)
    507     fprintf (stderr, "Error performing test.\n");
    508   return 0;
    509 }
    510 
    511 
    512 int
    513 main (int argc, char *const *argv)
    514 {
    515   unsigned int errorCount = 0;
    516   uint16_t port = 1100;
    517   (void) argc;   /* Unused. Silent compiler warning. */
    518 
    519   if ((NULL == argv) || (0 == argv[0]))
    520     return 99;
    521   oneone = has_in_name (argv[0], "11");
    522   if (oneone)
    523     port += 15;
    524   if (0 != curl_global_init (CURL_GLOBAL_WIN32))
    525     return 2;
    526   response = MHD_create_response_from_buffer_copy (strlen ("/hello_world"),
    527                                                    "/hello_world");
    528   errorCount += testInternalGet (port++, 0);
    529   errorCount += testMultithreadedGet (port++, 0);
    530   errorCount += testMultithreadedPoolGet (port++, 0);
    531   errorCount += testExternalGet (port++);
    532   errorCount += testInternalGet (port++, MHD_USE_AUTO);
    533   errorCount += testMultithreadedGet (port++, MHD_USE_AUTO);
    534   errorCount += testMultithreadedPoolGet (port++, MHD_USE_AUTO);
    535   if (MHD_YES == MHD_is_feature_supported (MHD_FEATURE_POLL))
    536   {
    537     errorCount += testInternalGet (port++, MHD_USE_POLL);
    538     errorCount += testMultithreadedGet (port++, MHD_USE_POLL);
    539     errorCount += testMultithreadedPoolGet (port++, MHD_USE_POLL);
    540   }
    541   if (MHD_YES == MHD_is_feature_supported (MHD_FEATURE_EPOLL))
    542   {
    543     errorCount += testInternalGet (port++, MHD_USE_EPOLL);
    544     errorCount += testMultithreadedPoolGet (port++, MHD_USE_EPOLL);
    545   }
    546   MHD_destroy_response (response);
    547   if (errorCount != 0)
    548     fprintf (stderr, "Error (code: %u)\n", errorCount);
    549   curl_global_cleanup ();
    550   return errorCount != 0;       /* 0 == pass */
    551 }