diff options
author | Christian Grothoff <christian@grothoff.org> | 2013-06-23 10:50:05 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2013-06-23 10:50:05 +0000 |
commit | 3fb6d911e938e5fb4e1be0a6494d181b3b6711a5 (patch) | |
tree | 395653926eaf15c23f9247ccf85a33599eff0564 | |
parent | efbf50c0d22b0c27088d05b707773433371cc9c1 (diff) | |
download | libmicrohttpd-3fb6d911e938e5fb4e1be0a6494d181b3b6711a5.tar.gz libmicrohttpd-3fb6d911e938e5fb4e1be0a6494d181b3b6711a5.zip |
-benchmark for latency measurements and epoll demonstration
-rw-r--r-- | src/examples/Makefile.am | 6 | ||||
-rw-r--r-- | src/examples/benchmark.c | 147 | ||||
-rw-r--r-- | src/examples/demo.c | 15 |
3 files changed, 165 insertions, 3 deletions
diff --git a/src/examples/Makefile.am b/src/examples/Makefile.am index 4a02d2dc..070ccf94 100644 --- a/src/examples/Makefile.am +++ b/src/examples/Makefile.am | |||
@@ -25,6 +25,7 @@ endif | |||
25 | 25 | ||
26 | # example programs | 26 | # example programs |
27 | noinst_PROGRAMS = \ | 27 | noinst_PROGRAMS = \ |
28 | benchmark \ | ||
28 | minimal_example \ | 29 | minimal_example \ |
29 | dual_stack_example \ | 30 | dual_stack_example \ |
30 | minimal_example_comet \ | 31 | minimal_example_comet \ |
@@ -73,6 +74,11 @@ demo_LDADD = \ | |||
73 | $(top_builddir)/src/microhttpd/libmicrohttpd.la \ | 74 | $(top_builddir)/src/microhttpd/libmicrohttpd.la \ |
74 | -lmagic | 75 | -lmagic |
75 | 76 | ||
77 | benchmark_SOURCES = \ | ||
78 | benchmark.c | ||
79 | benchmark_LDADD = \ | ||
80 | $(top_builddir)/src/microhttpd/libmicrohttpd.la | ||
81 | |||
76 | dual_stack_example_SOURCES = \ | 82 | dual_stack_example_SOURCES = \ |
77 | dual_stack_example.c | 83 | dual_stack_example.c |
78 | dual_stack_example_LDADD = \ | 84 | dual_stack_example_LDADD = \ |
diff --git a/src/examples/benchmark.c b/src/examples/benchmark.c new file mode 100644 index 00000000..711d118a --- /dev/null +++ b/src/examples/benchmark.c | |||
@@ -0,0 +1,147 @@ | |||
1 | /* | ||
2 | This file is part of libmicrohttpd | ||
3 | (C) 2007, 2013 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | This library is free software; you can redistribute it and/or | ||
6 | modify it under the terms of the GNU Lesser General Public | ||
7 | License as published by the Free Software Foundation; either | ||
8 | version 2.1 of the License, or (at your option) any later version. | ||
9 | |||
10 | This library is distributed in the hope that it will be useful, | ||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Lesser General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Lesser General Public | ||
16 | License along with this library; if not, write to the Free Software | ||
17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
18 | */ | ||
19 | /** | ||
20 | * @file benchmark.c | ||
21 | * @brief minimal code to benchmark MHD GET performance | ||
22 | * @author Christian Grothoff | ||
23 | */ | ||
24 | |||
25 | #include "platform.h" | ||
26 | #include <microhttpd.h> | ||
27 | |||
28 | #define PAGE "<html><head><title>libmicrohttpd demo</title></head><body>libmicrohttpd demo</body></html>" | ||
29 | |||
30 | |||
31 | #define SMALL (1024 * 128) | ||
32 | |||
33 | /** | ||
34 | * Number of threads to run in the thread pool. Should (roughly) match | ||
35 | * the number of cores on your system. | ||
36 | */ | ||
37 | #define NUMBER_OF_THREADS 4 | ||
38 | |||
39 | static unsigned int small_deltas[SMALL]; | ||
40 | |||
41 | static struct MHD_Response *response; | ||
42 | |||
43 | |||
44 | |||
45 | /** | ||
46 | * Signature of the callback used by MHD to notify the | ||
47 | * application about completed requests. | ||
48 | * | ||
49 | * @param cls client-defined closure | ||
50 | * @param connection connection handle | ||
51 | * @param con_cls value as set by the last call to | ||
52 | * the MHD_AccessHandlerCallback | ||
53 | * @param toe reason for request termination | ||
54 | * @see MHD_OPTION_NOTIFY_COMPLETED | ||
55 | */ | ||
56 | static void | ||
57 | completed_callback (void *cls, | ||
58 | struct MHD_Connection *connection, | ||
59 | void **con_cls, | ||
60 | enum MHD_RequestTerminationCode toe) | ||
61 | { | ||
62 | struct timeval *tv = *con_cls; | ||
63 | struct timeval tve; | ||
64 | uint64_t delta; | ||
65 | |||
66 | if (NULL == tv) | ||
67 | return; | ||
68 | gettimeofday (&tve, NULL); | ||
69 | |||
70 | delta = 0; | ||
71 | if (tve.tv_usec >= tv->tv_usec) | ||
72 | delta += (tve.tv_sec - tv->tv_sec) * 1000000LL | ||
73 | + (tve.tv_usec - tv->tv_usec); | ||
74 | else | ||
75 | delta += (tve.tv_sec - tv->tv_sec) * 1000000LL | ||
76 | - tv->tv_usec + tve.tv_usec; | ||
77 | if (delta < SMALL) | ||
78 | small_deltas[delta]++; | ||
79 | else | ||
80 | fprintf (stdout, "D: %llu 1\n", (unsigned long long) delta); | ||
81 | free (tv); | ||
82 | } | ||
83 | |||
84 | |||
85 | static void * | ||
86 | uri_logger_cb (void *cls, | ||
87 | const char *uri) | ||
88 | { | ||
89 | struct timeval *tv = malloc (sizeof (struct timeval)); | ||
90 | |||
91 | if (NULL != tv) | ||
92 | gettimeofday (tv, NULL); | ||
93 | return tv; | ||
94 | } | ||
95 | |||
96 | |||
97 | static int | ||
98 | ahc_echo (void *cls, | ||
99 | struct MHD_Connection *connection, | ||
100 | const char *url, | ||
101 | const char *method, | ||
102 | const char *version, | ||
103 | const char *upload_data, size_t *upload_data_size, void **ptr) | ||
104 | { | ||
105 | if (0 != strcmp (method, "GET")) | ||
106 | return MHD_NO; /* unexpected method */ | ||
107 | return MHD_queue_response (connection, MHD_HTTP_OK, response); | ||
108 | } | ||
109 | |||
110 | |||
111 | int | ||
112 | main (int argc, char *const *argv) | ||
113 | { | ||
114 | struct MHD_Daemon *d; | ||
115 | unsigned int i; | ||
116 | |||
117 | if (argc != 2) | ||
118 | { | ||
119 | printf ("%s PORT\n", argv[0]); | ||
120 | return 1; | ||
121 | } | ||
122 | response = MHD_create_response_from_buffer (strlen (PAGE), | ||
123 | (void *) PAGE, | ||
124 | MHD_RESPMEM_PERSISTENT); | ||
125 | |||
126 | d = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY | ||
127 | #if ! EPOLL_SUPPORT | ||
128 | | MHD_USE_EPOLL_LINUX_ONLY | ||
129 | #endif | ||
130 | , | ||
131 | atoi (argv[1]), | ||
132 | NULL, NULL, &ahc_echo, NULL, | ||
133 | MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 120, | ||
134 | MHD_OPTION_THREAD_POOL_SIZE, (unsigned int) NUMBER_OF_THREADS, | ||
135 | MHD_OPTION_URI_LOG_CALLBACK, &uri_logger_cb, NULL, | ||
136 | MHD_OPTION_NOTIFY_COMPLETED, &completed_callback, NULL, | ||
137 | MHD_OPTION_END); | ||
138 | if (d == NULL) | ||
139 | return 1; | ||
140 | (void) getc (stdin); | ||
141 | MHD_stop_daemon (d); | ||
142 | MHD_destroy_response (response); | ||
143 | for (i=0;i<SMALL;i++) | ||
144 | if (0 != small_deltas[i]) | ||
145 | fprintf (stdout, "D: %d %u\n", i, small_deltas[i]); | ||
146 | return 0; | ||
147 | } | ||
diff --git a/src/examples/demo.c b/src/examples/demo.c index 3f953a8f..281d594e 100644 --- a/src/examples/demo.c +++ b/src/examples/demo.c | |||
@@ -43,7 +43,7 @@ | |||
43 | * Number of threads to run in the thread pool. Should (roughly) match | 43 | * Number of threads to run in the thread pool. Should (roughly) match |
44 | * the number of cores on your system. | 44 | * the number of cores on your system. |
45 | */ | 45 | */ |
46 | #define NUMBER_OF_THREADS 8 | 46 | #define NUMBER_OF_THREADS 4 |
47 | 47 | ||
48 | /** | 48 | /** |
49 | * How many bytes of a file do we give to libmagic to determine the mime type? | 49 | * How many bytes of a file do we give to libmagic to determine the mime type? |
@@ -362,6 +362,9 @@ update_directory () | |||
362 | rdc.buf, | 362 | rdc.buf, |
363 | MHD_RESPMEM_MUST_FREE); | 363 | MHD_RESPMEM_MUST_FREE); |
364 | mark_as_html (response); | 364 | mark_as_html (response); |
365 | (void) MHD_add_response_header (response, | ||
366 | MHD_HTTP_HEADER_CONNECTION, | ||
367 | "close"); | ||
365 | update_cached_response (response); | 368 | update_cached_response (response); |
366 | } | 369 | } |
367 | 370 | ||
@@ -859,12 +862,18 @@ main (int argc, char *const *argv) | |||
859 | MHD_RESPMEM_PERSISTENT); | 862 | MHD_RESPMEM_PERSISTENT); |
860 | mark_as_html (internal_error_response); | 863 | mark_as_html (internal_error_response); |
861 | update_directory (); | 864 | update_directory (); |
862 | d = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY, | 865 | d = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY | MHD_USE_DEBUG |
866 | #if EPOLL_SUPPORT | ||
867 | | MHD_USE_EPOLL_LINUX_ONLY | ||
868 | #endif | ||
869 | , | ||
863 | port, | 870 | port, |
864 | NULL, NULL, | 871 | NULL, NULL, |
865 | &generate_page, NULL, | 872 | &generate_page, NULL, |
866 | MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (256 * 1024), | 873 | MHD_OPTION_CONNECTION_MEMORY_LIMIT, (size_t) (256 * 1024), |
874 | #if PRODUCTION | ||
867 | MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) (64), | 875 | MHD_OPTION_PER_IP_CONNECTION_LIMIT, (unsigned int) (64), |
876 | #endif | ||
868 | MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) (120 /* seconds */), | 877 | MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) (120 /* seconds */), |
869 | MHD_OPTION_THREAD_POOL_SIZE, (unsigned int) NUMBER_OF_THREADS, | 878 | MHD_OPTION_THREAD_POOL_SIZE, (unsigned int) NUMBER_OF_THREADS, |
870 | MHD_OPTION_NOTIFY_COMPLETED, &response_completed_callback, NULL, | 879 | MHD_OPTION_NOTIFY_COMPLETED, &response_completed_callback, NULL, |