diff options
Diffstat (limited to 'src/examples/benchmark.c')
-rw-r--r-- | src/examples/benchmark.c | 147 |
1 files changed, 147 insertions, 0 deletions
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 | } | ||