diff options
Diffstat (limited to 'src/examples/benchmark_https.c')
-rw-r--r-- | src/examples/benchmark_https.c | 203 |
1 files changed, 203 insertions, 0 deletions
diff --git a/src/examples/benchmark_https.c b/src/examples/benchmark_https.c new file mode 100644 index 00000000..3e79fe6f --- /dev/null +++ b/src/examples/benchmark_https.c | |||
@@ -0,0 +1,203 @@ | |||
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_https.c | ||
21 | * @brief minimal code to benchmark MHD GET performance with HTTPS | ||
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 | /* test server key */ | ||
112 | const char srv_signed_key_pem[] = "-----BEGIN RSA PRIVATE KEY-----\n" | ||
113 | "MIIEowIBAAKCAQEAvfTdv+3fgvVTKRnP/HVNG81cr8TrUP/iiyuve/THMzvFXhCW\n" | ||
114 | "+K03KwEku55QvnUndwBfU/ROzLlv+5hotgiDRNFT3HxurmhouySBrJNJv7qWp8IL\n" | ||
115 | "q4sw32vo0fbMu5BZF49bUXK9L3kW2PdhTtSQPWHEzNrCxO+YgCilKHkY3vQNfdJ0\n" | ||
116 | "20Q5EAAEseD1YtWCIpRvJzYlZMpjYB1ubTl24kwrgOKUJYKqM4jmF4DVQp4oOK/6\n" | ||
117 | "QYGGh1QmHRPAy3CBII6sbb+sZT9cAqU6GYQVB35lm4XAgibXV6KgmpVxVQQ69U6x\n" | ||
118 | "yoOl204xuekZOaG9RUPId74Rtmwfi1TLbBzo2wIDAQABAoIBADu09WSICNq5cMe4\n" | ||
119 | "+NKCLlgAT1NiQpLls1gKRbDhKiHU9j8QWNvWWkJWrCya4QdUfLCfeddCMeiQmv3K\n" | ||
120 | "lJMvDs+5OjJSHFoOsGiuW2Ias7IjnIojaJalfBml6frhJ84G27IXmdz6gzOiTIer\n" | ||
121 | "DjeAgcwBaKH5WwIay2TxIaScl7AwHBauQkrLcyb4hTmZuQh6ArVIN6+pzoVuORXM\n" | ||
122 | "bpeNWl2l/HSN3VtUN6aCAKbN/X3o0GavCCMn5Fa85uJFsab4ss/uP+2PusU71+zP\n" | ||
123 | "sBm6p/2IbGvF5k3VPDA7X5YX61sukRjRBihY8xSnNYx1UcoOsX6AiPnbhifD8+xQ\n" | ||
124 | "Tlf8oJUCgYEA0BTfzqNpr9Wxw5/QXaSdw7S/0eP5a0C/nwURvmfSzuTD4equzbEN\n" | ||
125 | "d+dI/s2JMxrdj/I4uoAfUXRGaabevQIjFzC9uyE3LaOyR2zhuvAzX+vVcs6bSXeU\n" | ||
126 | "pKpCAcN+3Z3evMaX2f+z/nfSUAl2i4J2R+/LQAWJW4KwRky/m+cxpfUCgYEA6bN1\n" | ||
127 | "b73bMgM8wpNt6+fcmS+5n0iZihygQ2U2DEud8nZJL4Nrm1dwTnfZfJBnkGj6+0Q0\n" | ||
128 | "cOwj2KS0/wcEdJBP0jucU4v60VMhp75AQeHqidIde0bTViSRo3HWKXHBIFGYoU3T\n" | ||
129 | "LyPyKndbqsOObnsFXHn56Nwhr2HLf6nw4taGQY8CgYBoSW36FLCNbd6QGvLFXBGt\n" | ||
130 | "2lMhEM8az/K58kJ4WXSwOLtr6MD/WjNT2tkcy0puEJLm6BFCd6A6pLn9jaKou/92\n" | ||
131 | "SfltZjJPb3GUlp9zn5tAAeSSi7YMViBrfuFiHObij5LorefBXISLjuYbMwL03MgH\n" | ||
132 | "Ocl2JtA2ywMp2KFXs8GQWQKBgFyIVv5ogQrbZ0pvj31xr9HjqK6d01VxIi+tOmpB\n" | ||
133 | "4ocnOLEcaxX12BzprW55ytfOCVpF1jHD/imAhb3YrHXu0fwe6DXYXfZV4SSG2vB7\n" | ||
134 | "IB9z14KBN5qLHjNGFpMQXHSMek+b/ftTU0ZnPh9uEM5D3YqRLVd7GcdUhHvG8P8Q\n" | ||
135 | "C9aXAoGBAJtID6h8wOGMP0XYX5YYnhlC7dOLfk8UYrzlp3xhqVkzKthTQTj6wx9R\n" | ||
136 | "GtC4k7U1ki8oJsfcIlBNXd768fqDVWjYju5rzShMpo8OCTS6ipAblKjCxPPVhIpv\n" | ||
137 | "tWPlbSn1qj6wylstJ5/3Z+ZW5H4wIKp5jmLiioDhcP0L/Ex3Zx8O\n" | ||
138 | "-----END RSA PRIVATE KEY-----\n"; | ||
139 | |||
140 | /* test server CA signed certificates */ | ||
141 | const char srv_signed_cert_pem[] = "-----BEGIN CERTIFICATE-----\n" | ||
142 | "MIIDGzCCAgWgAwIBAgIES0KCvTALBgkqhkiG9w0BAQUwFzEVMBMGA1UEAxMMdGVz\n" | ||
143 | "dF9jYV9jZXJ0MB4XDTEwMDEwNTAwMDcyNVoXDTQ1MDMxMjAwMDcyNVowFzEVMBMG\n" | ||
144 | "A1UEAxMMdGVzdF9jYV9jZXJ0MIIBHzALBgkqhkiG9w0BAQEDggEOADCCAQkCggEA\n" | ||
145 | "vfTdv+3fgvVTKRnP/HVNG81cr8TrUP/iiyuve/THMzvFXhCW+K03KwEku55QvnUn\n" | ||
146 | "dwBfU/ROzLlv+5hotgiDRNFT3HxurmhouySBrJNJv7qWp8ILq4sw32vo0fbMu5BZ\n" | ||
147 | "F49bUXK9L3kW2PdhTtSQPWHEzNrCxO+YgCilKHkY3vQNfdJ020Q5EAAEseD1YtWC\n" | ||
148 | "IpRvJzYlZMpjYB1ubTl24kwrgOKUJYKqM4jmF4DVQp4oOK/6QYGGh1QmHRPAy3CB\n" | ||
149 | "II6sbb+sZT9cAqU6GYQVB35lm4XAgibXV6KgmpVxVQQ69U6xyoOl204xuekZOaG9\n" | ||
150 | "RUPId74Rtmwfi1TLbBzo2wIDAQABo3YwdDAMBgNVHRMBAf8EAjAAMBMGA1UdJQQM\n" | ||
151 | "MAoGCCsGAQUFBwMBMA8GA1UdDwEB/wQFAwMHIAAwHQYDVR0OBBYEFOFi4ilKOP1d\n" | ||
152 | "XHlWCMwmVKr7mgy8MB8GA1UdIwQYMBaAFP2olB4s2T/xuoQ5pT2RKojFwZo2MAsG\n" | ||
153 | "CSqGSIb3DQEBBQOCAQEAHVWPxazupbOkG7Did+dY9z2z6RjTzYvurTtEKQgzM2Vz\n" | ||
154 | "GQBA+3pZ3c5mS97fPIs9hZXfnQeelMeZ2XP1a+9vp35bJjZBBhVH+pqxjCgiUflg\n" | ||
155 | "A3Zqy0XwwVCgQLE2HyaU3DLUD/aeIFK5gJaOSdNTXZLv43K8kl4cqDbMeRpVTbkt\n" | ||
156 | "YmG4AyEOYRNKGTqMEJXJoxD5E3rBUNrVI/XyTjYrulxbNPcMWEHKNeeqWpKDYTFo\n" | ||
157 | "Bb01PCthGXiq/4A2RLAFosadzRa8SBpoSjPPfZ0b2w4MJpReHqKbR5+T2t6hzml6\n" | ||
158 | "4ToyOKPDmamiTuN5KzLN3cw7DQlvWMvqSOChPLnA3Q==\n" | ||
159 | "-----END CERTIFICATE-----\n"; | ||
160 | |||
161 | |||
162 | int | ||
163 | main (int argc, char *const *argv) | ||
164 | { | ||
165 | struct MHD_Daemon *d; | ||
166 | unsigned int i; | ||
167 | |||
168 | if (argc != 2) | ||
169 | { | ||
170 | printf ("%s PORT\n", argv[0]); | ||
171 | return 1; | ||
172 | } | ||
173 | response = MHD_create_response_from_buffer (strlen (PAGE), | ||
174 | (void *) PAGE, | ||
175 | MHD_RESPMEM_PERSISTENT); | ||
176 | (void) MHD_add_response_header (response, | ||
177 | MHD_HTTP_HEADER_CONNECTION, | ||
178 | "close"); | ||
179 | d = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY | MHD_SUPPRESS_DATE_NO_CLOCK | MHD_USE_SSL | ||
180 | #if EPOLL_SUPPORT | ||
181 | | MHD_USE_EPOLL_LINUX_ONLY | MHD_USE_EPOLL_TURBO | ||
182 | #endif | ||
183 | , | ||
184 | atoi (argv[1]), | ||
185 | NULL, NULL, &ahc_echo, NULL, | ||
186 | MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 120, | ||
187 | MHD_OPTION_THREAD_POOL_SIZE, (unsigned int) NUMBER_OF_THREADS, | ||
188 | MHD_OPTION_URI_LOG_CALLBACK, &uri_logger_cb, NULL, | ||
189 | MHD_OPTION_NOTIFY_COMPLETED, &completed_callback, NULL, | ||
190 | MHD_OPTION_CONNECTION_LIMIT, (unsigned int) 1000, | ||
191 | MHD_OPTION_HTTPS_MEM_KEY, srv_signed_key_pem, | ||
192 | MHD_OPTION_HTTPS_MEM_CERT, srv_signed_cert_pem, | ||
193 | MHD_OPTION_END); | ||
194 | if (d == NULL) | ||
195 | return 1; | ||
196 | (void) getc (stdin); | ||
197 | MHD_stop_daemon (d); | ||
198 | MHD_destroy_response (response); | ||
199 | for (i=0;i<SMALL;i++) | ||
200 | if (0 != small_deltas[i]) | ||
201 | fprintf (stdout, "D: %d %u\n", i, small_deltas[i]); | ||
202 | return 0; | ||
203 | } | ||