]> arthur.barton.de Git - netatalk.git/blob - libevent/test/test-ratelim.c
Add libevent
[netatalk.git] / libevent / test / test-ratelim.c
1 /*
2  * Copyright (c) 2009-2010 Niels Provos and Nick Mathewson
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  * 3. The name of the author may not be used to endorse or promote products
13  *    derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <assert.h>
31 #include <math.h>
32
33 #ifdef WIN32
34 #include <winsock2.h>
35 #include <ws2tcpip.h>
36 #else
37 #include <sys/socket.h>
38 #include <netinet/in.h>
39 #endif
40 #include <signal.h>
41
42 #include "event2/bufferevent.h"
43 #include "event2/buffer.h"
44 #include "event2/event.h"
45 #include "event2/util.h"
46 #include "event2/listener.h"
47 #include "event2/thread.h"
48
49 static int cfg_verbose = 0;
50 static int cfg_help = 0;
51
52 static int cfg_n_connections = 30;
53 static int cfg_duration = 5;
54 static int cfg_connlimit = 0;
55 static int cfg_grouplimit = 0;
56 static int cfg_tick_msec = 1000;
57 static int cfg_min_share = -1;
58
59 static int cfg_connlimit_tolerance = -1;
60 static int cfg_grouplimit_tolerance = -1;
61 static int cfg_stddev_tolerance = -1;
62
63 static struct timeval cfg_tick = { 0, 500*1000 };
64
65 static struct ev_token_bucket_cfg *conn_bucket_cfg = NULL;
66 static struct ev_token_bucket_cfg *group_bucket_cfg = NULL;
67 struct bufferevent_rate_limit_group *ratelim_group = NULL;
68 static double seconds_per_tick = 0.0;
69
70 struct client_state {
71         size_t queued;
72         ev_uint64_t received;
73 };
74
75 static int n_echo_conns_open = 0;
76
77 static void
78 loud_writecb(struct bufferevent *bev, void *ctx)
79 {
80         struct client_state *cs = ctx;
81         struct evbuffer *output = bufferevent_get_output(bev);
82         char buf[1024];
83 #ifdef WIN32
84         int r = rand() % 256;
85 #else
86         int r = random() % 256;
87 #endif
88         memset(buf, r, sizeof(buf));
89         while (evbuffer_get_length(output) < 8192) {
90                 evbuffer_add(output, buf, sizeof(buf));
91                 cs->queued += sizeof(buf);
92         }
93 }
94
95 static void
96 discard_readcb(struct bufferevent *bev, void *ctx)
97 {
98         struct client_state *cs = ctx;
99         struct evbuffer *input = bufferevent_get_input(bev);
100         size_t len = evbuffer_get_length(input);
101         evbuffer_drain(input, len);
102         cs->received += len;
103 }
104
105 static void
106 write_on_connectedcb(struct bufferevent *bev, short what, void *ctx)
107 {
108         if (what & BEV_EVENT_CONNECTED) {
109                 loud_writecb(bev, ctx);
110                 /* XXXX this shouldn't be needed. */
111                 bufferevent_enable(bev, EV_READ|EV_WRITE);
112         }
113 }
114
115 static void
116 echo_readcb(struct bufferevent *bev, void *ctx)
117 {
118         struct evbuffer *input = bufferevent_get_input(bev);
119         struct evbuffer *output = bufferevent_get_output(bev);
120
121         evbuffer_add_buffer(output, input);
122         if (evbuffer_get_length(output) > 1024000)
123                 bufferevent_disable(bev, EV_READ);
124 }
125
126 static void
127 echo_writecb(struct bufferevent *bev, void *ctx)
128 {
129         struct evbuffer *output = bufferevent_get_output(bev);
130         if (evbuffer_get_length(output) < 512000)
131                 bufferevent_enable(bev, EV_READ);
132 }
133
134 static void
135 echo_eventcb(struct bufferevent *bev, short what, void *ctx)
136 {
137         if (what & (BEV_EVENT_EOF|BEV_EVENT_ERROR)) {
138                 --n_echo_conns_open;
139                 bufferevent_free(bev);
140         }
141 }
142
143 static void
144 echo_listenercb(struct evconnlistener *listener, evutil_socket_t newsock,
145     struct sockaddr *sourceaddr, int socklen, void *ctx)
146 {
147         struct event_base *base = ctx;
148         int flags = BEV_OPT_CLOSE_ON_FREE|BEV_OPT_THREADSAFE;
149         struct bufferevent *bev;
150
151         bev = bufferevent_socket_new(base, newsock, flags);
152         bufferevent_setcb(bev, echo_readcb, echo_writecb, echo_eventcb, NULL);
153         if (conn_bucket_cfg)
154                 bufferevent_set_rate_limit(bev, conn_bucket_cfg);
155         if (ratelim_group)
156                 bufferevent_add_to_rate_limit_group(bev, ratelim_group);
157         ++n_echo_conns_open;
158         bufferevent_enable(bev, EV_READ|EV_WRITE);
159 }
160
161 static int
162 test_ratelimiting(void)
163 {
164         struct event_base *base;
165         struct sockaddr_in sin;
166         struct evconnlistener *listener;
167
168         struct sockaddr_storage ss;
169         ev_socklen_t slen;
170
171         struct bufferevent **bevs;
172         struct client_state *states;
173         struct bufferevent_rate_limit_group *group = NULL;
174
175         int i;
176
177         struct timeval tv;
178
179         ev_uint64_t total_received;
180         double total_sq_persec, total_persec;
181         double variance;
182         double expected_total_persec = -1.0, expected_avg_persec = -1.0;
183         int ok = 1;
184
185         memset(&sin, 0, sizeof(sin));
186         sin.sin_family = AF_INET;
187         sin.sin_addr.s_addr = htonl(0x7f000001); /* 127.0.0.1 */
188         sin.sin_port = 0; /* unspecified port */
189
190         if (0)
191                 event_enable_debug_mode();
192
193         base = event_base_new();
194
195         listener = evconnlistener_new_bind(base, echo_listenercb, base,
196             LEV_OPT_CLOSE_ON_FREE|LEV_OPT_REUSEABLE, -1,
197             (struct sockaddr *)&sin, sizeof(sin));
198
199         slen = sizeof(ss);
200         if (getsockname(evconnlistener_get_fd(listener), (struct sockaddr *)&ss,
201                 &slen) < 0) {
202                 perror("getsockname");
203                 return 1;
204         }
205
206         if (cfg_connlimit > 0) {
207                 conn_bucket_cfg = ev_token_bucket_cfg_new(
208                         cfg_connlimit, cfg_connlimit * 4,
209                         cfg_connlimit, cfg_connlimit * 4,
210                         &cfg_tick);
211                 assert(conn_bucket_cfg);
212         }
213
214         if (cfg_grouplimit > 0) {
215                 group_bucket_cfg = ev_token_bucket_cfg_new(
216                         cfg_grouplimit, cfg_grouplimit * 4,
217                         cfg_grouplimit, cfg_grouplimit * 4,
218                         &cfg_tick);
219                 group = ratelim_group = bufferevent_rate_limit_group_new(
220                         base, group_bucket_cfg);
221                 expected_total_persec = cfg_grouplimit;
222                 expected_avg_persec = cfg_grouplimit / cfg_n_connections;
223                 if (cfg_connlimit > 0 && expected_avg_persec > cfg_connlimit)
224                         expected_avg_persec = cfg_connlimit;
225                 if (cfg_min_share >= 0)
226                         bufferevent_rate_limit_group_set_min_share(
227                                 ratelim_group, cfg_min_share);
228         }
229
230         if (expected_avg_persec < 0 && cfg_connlimit > 0)
231                 expected_avg_persec = cfg_connlimit;
232
233         if (expected_avg_persec > 0)
234                 expected_avg_persec /= seconds_per_tick;
235         if (expected_total_persec > 0)
236                 expected_total_persec /= seconds_per_tick;
237
238         bevs = calloc(cfg_n_connections, sizeof(struct bufferevent *));
239         states = calloc(cfg_n_connections, sizeof(struct client_state));
240
241         for (i = 0; i < cfg_n_connections; ++i) {
242                 bevs[i] = bufferevent_socket_new(base, -1,
243                     BEV_OPT_CLOSE_ON_FREE|BEV_OPT_THREADSAFE);
244                 assert(bevs[i]);
245                 bufferevent_setcb(bevs[i], discard_readcb, loud_writecb,
246                     write_on_connectedcb, &states[i]);
247                 bufferevent_enable(bevs[i], EV_READ|EV_WRITE);
248                 bufferevent_socket_connect(bevs[i], (struct sockaddr *)&ss,
249                     slen);
250         }
251
252         tv.tv_sec = cfg_duration - 1;
253         tv.tv_usec = 995000;
254
255         event_base_loopexit(base, &tv);
256
257         event_base_dispatch(base);
258
259         ratelim_group = NULL; /* So no more responders get added */
260
261         for (i = 0; i < cfg_n_connections; ++i) {
262                 bufferevent_free(bevs[i]);
263         }
264         evconnlistener_free(listener);
265
266         /* Make sure no new echo_conns get added to the group. */
267         ratelim_group = NULL;
268
269         /* This should get _everybody_ freed */
270         while (n_echo_conns_open) {
271                 printf("waiting for %d conns\n", n_echo_conns_open);
272                 tv.tv_sec = 0;
273                 tv.tv_usec = 300000;
274                 event_base_loopexit(base, &tv);
275                 event_base_dispatch(base);
276         }
277
278         if (group)
279                 bufferevent_rate_limit_group_free(group);
280
281         total_received = 0;
282         total_persec = 0.0;
283         total_sq_persec = 0.0;
284         for (i=0; i < cfg_n_connections; ++i) {
285                 double persec = states[i].received;
286                 persec /= cfg_duration;
287                 total_received += states[i].received;
288                 total_persec += persec;
289                 total_sq_persec += persec*persec;
290                 printf("%d: %f per second\n", i+1, persec);
291         }
292         printf("   total: %f per second\n",
293             ((double)total_received)/cfg_duration);
294         if (expected_total_persec > 0) {
295                 double diff = expected_total_persec -
296                     ((double)total_received/cfg_duration);
297                 printf("  [Off by %lf]\n", diff);
298                 if (cfg_grouplimit_tolerance > 0 &&
299                     fabs(diff) > cfg_grouplimit_tolerance) {
300                         fprintf(stderr, "Group bandwidth out of bounds\n");
301                         ok = 0;
302                 }
303         }
304
305         printf(" average: %f per second\n",
306             (((double)total_received)/cfg_duration)/cfg_n_connections);
307         if (expected_avg_persec > 0) {
308                 double diff = expected_avg_persec - (((double)total_received)/cfg_duration)/cfg_n_connections;
309                 printf("  [Off by %lf]\n", diff);
310                 if (cfg_connlimit_tolerance > 0 &&
311                     fabs(diff) > cfg_connlimit_tolerance) {
312                         fprintf(stderr, "Connection bandwidth out of bounds\n");
313                         ok = 0;
314                 }
315         }
316
317         variance = total_sq_persec/cfg_n_connections - total_persec*total_persec/(cfg_n_connections*cfg_n_connections);
318
319         printf("  stddev: %f per second\n", sqrt(variance));
320         if (cfg_stddev_tolerance > 0 &&
321             sqrt(variance) > cfg_stddev_tolerance) {
322                 fprintf(stderr, "Connection variance out of bounds\n");
323                 ok = 0;
324         }
325
326         event_base_free(base);
327         free(bevs);
328         free(states);
329
330         return ok ? 0 : 1;
331 }
332
333 static struct option {
334         const char *name; int *ptr; int min; int isbool;
335 } options[] = {
336         { "-v", &cfg_verbose, 0, 1 },
337         { "-h", &cfg_help, 0, 1 },
338         { "-n", &cfg_n_connections, 1, 0 },
339         { "-d", &cfg_duration, 1, 0 },
340         { "-c", &cfg_connlimit, 0, 0 },
341         { "-g", &cfg_grouplimit, 0, 0 },
342         { "-t", &cfg_tick_msec, 10, 0 },
343         { "--min-share", &cfg_min_share, 0, 0 },
344         { "--check-connlimit", &cfg_connlimit_tolerance, 0, 0 },
345         { "--check-grouplimit", &cfg_grouplimit_tolerance, 0, 0 },
346         { "--check-stddev", &cfg_stddev_tolerance, 0, 0 },
347         { NULL, NULL, -1, 0 },
348 };
349
350 static int
351 handle_option(int argc, char **argv, int *i, const struct option *opt)
352 {
353         long val;
354         char *endptr = NULL;
355         if (opt->isbool) {
356                 *opt->ptr = 1;
357                 return 0;
358         }
359         if (*i + 1 == argc) {
360                 fprintf(stderr, "Too few arguments to '%s'\n",argv[*i]);
361                 return -1;
362         }
363         val = strtol(argv[*i+1], &endptr, 10);
364         if (*argv[*i+1] == '\0' || !endptr || *endptr != '\0') {
365                 fprintf(stderr, "Couldn't parse numeric value '%s'\n",
366                     argv[*i+1]);
367                 return -1;
368         }
369         if (val < opt->min || val > 0x7fffffff) {
370                 fprintf(stderr, "Value '%s' is out-of-range'\n",
371                     argv[*i+1]);
372                 return -1;
373         }
374         *opt->ptr = (int)val;
375         ++*i;
376         return 0;
377 }
378
379 static void
380 usage(void)
381 {
382         fprintf(stderr,
383 "test-ratelim [-v] [-n INT] [-d INT] [-c INT] [-g INT] [-t INT]\n\n"
384 "Pushes bytes through a number of possibly rate-limited connections, and\n"
385 "displays average throughput.\n\n"
386 "  -n INT: Number of connections to open (default: 30)\n"
387 "  -d INT: Duration of the test in seconds (default: 5 sec)\n");
388         fprintf(stderr,
389 "  -c INT: Connection-rate limit applied to each connection in bytes per second\n"
390 "          (default: None.)\n"
391 "  -g INT: Group-rate limit applied to sum of all usage in bytes per second\n"
392 "          (default: None.)\n"
393 "  -t INT: Granularity of timing, in milliseconds (default: 1000 msec)\n");
394 }
395
396 int
397 main(int argc, char **argv)
398 {
399         int i,j;
400         double ratio;
401
402 #ifdef WIN32
403         WORD wVersionRequested = MAKEWORD(2,2);
404         WSADATA wsaData;
405         int err;
406
407         err = WSAStartup(wVersionRequested, &wsaData);
408 #endif
409
410 #ifndef WIN32
411         if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
412                 return 1;
413 #endif
414         for (i = 1; i < argc; ++i) {
415                 for (j = 0; options[j].name; ++j) {
416                         if (!strcmp(argv[i],options[j].name)) {
417                                 if (handle_option(argc,argv,&i,&options[j])<0)
418                                         return 1;
419                                 goto again;
420                         }
421                 }
422                 fprintf(stderr, "Unknown option '%s'\n", argv[i]);
423                 usage();
424                 return 1;
425         again:
426                 ;
427         }
428         if (cfg_help) {
429                 usage();
430                 return 0;
431         }
432
433         cfg_tick.tv_sec = cfg_tick_msec / 1000;
434         cfg_tick.tv_usec = (cfg_tick_msec % 1000)*1000;
435
436         seconds_per_tick = ratio = cfg_tick_msec / 1000.0;
437
438         cfg_connlimit *= ratio;
439         cfg_grouplimit *= ratio;
440
441         {
442                 struct timeval tv;
443                 evutil_gettimeofday(&tv, NULL);
444 #ifdef WIN32
445                 srand(tv.tv_usec);
446 #else
447                 srandom(tv.tv_usec);
448 #endif
449         }
450
451 #ifndef _EVENT_DISABLE_THREAD_SUPPORT
452         evthread_enable_lock_debuging();
453 #endif
454
455         return test_ratelimiting();
456 }