2 * Copyright (c) 2009-2010 Niels Provos and Nick Mathewson
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
37 #include <sys/socket.h>
38 #include <netinet/in.h>
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"
49 static int cfg_verbose = 0;
50 static int cfg_help = 0;
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;
59 static int cfg_connlimit_tolerance = -1;
60 static int cfg_grouplimit_tolerance = -1;
61 static int cfg_stddev_tolerance = -1;
63 static struct timeval cfg_tick = { 0, 500*1000 };
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;
75 static int n_echo_conns_open = 0;
78 loud_writecb(struct bufferevent *bev, void *ctx)
80 struct client_state *cs = ctx;
81 struct evbuffer *output = bufferevent_get_output(bev);
86 int r = random() % 256;
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);
96 discard_readcb(struct bufferevent *bev, void *ctx)
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);
106 write_on_connectedcb(struct bufferevent *bev, short what, void *ctx)
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);
116 echo_readcb(struct bufferevent *bev, void *ctx)
118 struct evbuffer *input = bufferevent_get_input(bev);
119 struct evbuffer *output = bufferevent_get_output(bev);
121 evbuffer_add_buffer(output, input);
122 if (evbuffer_get_length(output) > 1024000)
123 bufferevent_disable(bev, EV_READ);
127 echo_writecb(struct bufferevent *bev, void *ctx)
129 struct evbuffer *output = bufferevent_get_output(bev);
130 if (evbuffer_get_length(output) < 512000)
131 bufferevent_enable(bev, EV_READ);
135 echo_eventcb(struct bufferevent *bev, short what, void *ctx)
137 if (what & (BEV_EVENT_EOF|BEV_EVENT_ERROR)) {
139 bufferevent_free(bev);
144 echo_listenercb(struct evconnlistener *listener, evutil_socket_t newsock,
145 struct sockaddr *sourceaddr, int socklen, void *ctx)
147 struct event_base *base = ctx;
148 int flags = BEV_OPT_CLOSE_ON_FREE|BEV_OPT_THREADSAFE;
149 struct bufferevent *bev;
151 bev = bufferevent_socket_new(base, newsock, flags);
152 bufferevent_setcb(bev, echo_readcb, echo_writecb, echo_eventcb, NULL);
154 bufferevent_set_rate_limit(bev, conn_bucket_cfg);
156 bufferevent_add_to_rate_limit_group(bev, ratelim_group);
158 bufferevent_enable(bev, EV_READ|EV_WRITE);
162 test_ratelimiting(void)
164 struct event_base *base;
165 struct sockaddr_in sin;
166 struct evconnlistener *listener;
168 struct sockaddr_storage ss;
171 struct bufferevent **bevs;
172 struct client_state *states;
173 struct bufferevent_rate_limit_group *group = NULL;
179 ev_uint64_t total_received;
180 double total_sq_persec, total_persec;
182 double expected_total_persec = -1.0, expected_avg_persec = -1.0;
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 */
191 event_enable_debug_mode();
193 base = event_base_new();
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));
200 if (getsockname(evconnlistener_get_fd(listener), (struct sockaddr *)&ss,
202 perror("getsockname");
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,
211 assert(conn_bucket_cfg);
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,
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);
230 if (expected_avg_persec < 0 && cfg_connlimit > 0)
231 expected_avg_persec = cfg_connlimit;
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;
238 bevs = calloc(cfg_n_connections, sizeof(struct bufferevent *));
239 states = calloc(cfg_n_connections, sizeof(struct client_state));
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);
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,
252 tv.tv_sec = cfg_duration - 1;
255 event_base_loopexit(base, &tv);
257 event_base_dispatch(base);
259 ratelim_group = NULL; /* So no more responders get added */
261 for (i = 0; i < cfg_n_connections; ++i) {
262 bufferevent_free(bevs[i]);
264 evconnlistener_free(listener);
266 /* Make sure no new echo_conns get added to the group. */
267 ratelim_group = NULL;
269 /* This should get _everybody_ freed */
270 while (n_echo_conns_open) {
271 printf("waiting for %d conns\n", n_echo_conns_open);
274 event_base_loopexit(base, &tv);
275 event_base_dispatch(base);
279 bufferevent_rate_limit_group_free(group);
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);
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");
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");
317 variance = total_sq_persec/cfg_n_connections - total_persec*total_persec/(cfg_n_connections*cfg_n_connections);
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");
326 event_base_free(base);
333 static struct option {
334 const char *name; int *ptr; int min; int isbool;
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 },
351 handle_option(int argc, char **argv, int *i, const struct option *opt)
359 if (*i + 1 == argc) {
360 fprintf(stderr, "Too few arguments to '%s'\n",argv[*i]);
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",
369 if (val < opt->min || val > 0x7fffffff) {
370 fprintf(stderr, "Value '%s' is out-of-range'\n",
374 *opt->ptr = (int)val;
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");
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");
397 main(int argc, char **argv)
403 WORD wVersionRequested = MAKEWORD(2,2);
407 err = WSAStartup(wVersionRequested, &wsaData);
411 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
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)
422 fprintf(stderr, "Unknown option '%s'\n", argv[i]);
433 cfg_tick.tv_sec = cfg_tick_msec / 1000;
434 cfg_tick.tv_usec = (cfg_tick_msec % 1000)*1000;
436 seconds_per_tick = ratio = cfg_tick_msec / 1000.0;
438 cfg_connlimit *= ratio;
439 cfg_grouplimit *= ratio;
443 evutil_gettimeofday(&tv, NULL);
451 #ifndef _EVENT_DISABLE_THREAD_SUPPORT
452 evthread_enable_lock_debuging();
455 return test_ratelimiting();