1 /* $NetBSD: test-ratelim.c,v 1.1.1.1 2013/04/11 16:43:32 christos Exp $ */
3 * Copyright (c) 2009-2012 Niels Provos and Nick Mathewson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include <sys/socket.h>
39 #include <netinet/in.h>
40 # ifdef _XOPEN_SOURCE_EXTENDED
41 # include <arpa/inet.h>
46 #include "event2/bufferevent.h"
47 #include "event2/buffer.h"
48 #include "event2/event.h"
49 #include "event2/util.h"
50 #include "event2/listener.h"
51 #include "event2/thread.h"
53 #include "../util-internal.h"
55 static int cfg_verbose
= 0;
56 static int cfg_help
= 0;
58 static int cfg_n_connections
= 30;
59 static int cfg_duration
= 5;
60 static int cfg_connlimit
= 0;
61 static int cfg_grouplimit
= 0;
62 static int cfg_tick_msec
= 1000;
63 static int cfg_min_share
= -1;
65 static int cfg_connlimit_tolerance
= -1;
66 static int cfg_grouplimit_tolerance
= -1;
67 static int cfg_stddev_tolerance
= -1;
70 static int cfg_enable_iocp
= 0;
73 static struct timeval cfg_tick
= { 0, 500*1000 };
75 static struct ev_token_bucket_cfg
*conn_bucket_cfg
= NULL
;
76 static struct ev_token_bucket_cfg
*group_bucket_cfg
= NULL
;
77 struct bufferevent_rate_limit_group
*ratelim_group
= NULL
;
78 static double seconds_per_tick
= 0.0;
85 static int n_echo_conns_open
= 0;
88 loud_writecb(struct bufferevent
*bev
, void *ctx
)
90 struct client_state
*cs
= ctx
;
91 struct evbuffer
*output
= bufferevent_get_output(bev
);
96 int r
= random() % 256;
98 memset(buf
, r
, sizeof(buf
));
99 while (evbuffer_get_length(output
) < 8192) {
100 evbuffer_add(output
, buf
, sizeof(buf
));
101 cs
->queued
+= sizeof(buf
);
106 discard_readcb(struct bufferevent
*bev
, void *ctx
)
108 struct client_state
*cs
= ctx
;
109 struct evbuffer
*input
= bufferevent_get_input(bev
);
110 size_t len
= evbuffer_get_length(input
);
111 evbuffer_drain(input
, len
);
116 write_on_connectedcb(struct bufferevent
*bev
, short what
, void *ctx
)
118 if (what
& BEV_EVENT_CONNECTED
) {
119 loud_writecb(bev
, ctx
);
120 /* XXXX this shouldn't be needed. */
121 bufferevent_enable(bev
, EV_READ
|EV_WRITE
);
126 echo_readcb(struct bufferevent
*bev
, void *ctx
)
128 struct evbuffer
*input
= bufferevent_get_input(bev
);
129 struct evbuffer
*output
= bufferevent_get_output(bev
);
131 evbuffer_add_buffer(output
, input
);
132 if (evbuffer_get_length(output
) > 1024000)
133 bufferevent_disable(bev
, EV_READ
);
137 echo_writecb(struct bufferevent
*bev
, void *ctx
)
139 struct evbuffer
*output
= bufferevent_get_output(bev
);
140 if (evbuffer_get_length(output
) < 512000)
141 bufferevent_enable(bev
, EV_READ
);
145 echo_eventcb(struct bufferevent
*bev
, short what
, void *ctx
)
147 if (what
& (BEV_EVENT_EOF
|BEV_EVENT_ERROR
)) {
149 bufferevent_free(bev
);
154 echo_listenercb(struct evconnlistener
*listener
, evutil_socket_t newsock
,
155 struct sockaddr
*sourceaddr
, int socklen
, void *ctx
)
157 struct event_base
*base
= ctx
;
158 int flags
= BEV_OPT_CLOSE_ON_FREE
|BEV_OPT_THREADSAFE
;
159 struct bufferevent
*bev
;
161 bev
= bufferevent_socket_new(base
, newsock
, flags
);
162 bufferevent_setcb(bev
, echo_readcb
, echo_writecb
, echo_eventcb
, NULL
);
164 bufferevent_set_rate_limit(bev
, conn_bucket_cfg
);
166 bufferevent_add_to_rate_limit_group(bev
, ratelim_group
);
168 bufferevent_enable(bev
, EV_READ
|EV_WRITE
);
172 test_ratelimiting(void)
174 struct event_base
*base
;
175 struct sockaddr_in sin
;
176 struct evconnlistener
*listener
;
178 struct sockaddr_storage ss
;
181 struct bufferevent
**bevs
;
182 struct client_state
*states
;
183 struct bufferevent_rate_limit_group
*group
= NULL
;
189 ev_uint64_t total_received
;
190 double total_sq_persec
, total_persec
;
192 double expected_total_persec
= -1.0, expected_avg_persec
= -1.0;
194 struct event_config
*base_cfg
;
196 memset(&sin
, 0, sizeof(sin
));
197 sin
.sin_family
= AF_INET
;
198 sin
.sin_addr
.s_addr
= htonl(0x7f000001); /* 127.0.0.1 */
199 sin
.sin_port
= 0; /* unspecified port */
202 event_enable_debug_mode();
204 base_cfg
= event_config_new();
207 if (cfg_enable_iocp
) {
208 evthread_use_windows_threads();
209 event_config_set_flag(base_cfg
, EVENT_BASE_FLAG_STARTUP_IOCP
);
213 base
= event_base_new_with_config(base_cfg
);
214 event_config_free(base_cfg
);
216 listener
= evconnlistener_new_bind(base
, echo_listenercb
, base
,
217 LEV_OPT_CLOSE_ON_FREE
|LEV_OPT_REUSEABLE
, -1,
218 (struct sockaddr
*)&sin
, sizeof(sin
));
221 if (getsockname(evconnlistener_get_fd(listener
), (struct sockaddr
*)&ss
,
223 perror("getsockname");
227 if (cfg_connlimit
> 0) {
228 conn_bucket_cfg
= ev_token_bucket_cfg_new(
229 cfg_connlimit
, cfg_connlimit
* 4,
230 cfg_connlimit
, cfg_connlimit
* 4,
232 assert(conn_bucket_cfg
);
235 if (cfg_grouplimit
> 0) {
236 group_bucket_cfg
= ev_token_bucket_cfg_new(
237 cfg_grouplimit
, cfg_grouplimit
* 4,
238 cfg_grouplimit
, cfg_grouplimit
* 4,
240 group
= ratelim_group
= bufferevent_rate_limit_group_new(
241 base
, group_bucket_cfg
);
242 expected_total_persec
= cfg_grouplimit
;
243 expected_avg_persec
= cfg_grouplimit
/ cfg_n_connections
;
244 if (cfg_connlimit
> 0 && expected_avg_persec
> cfg_connlimit
)
245 expected_avg_persec
= cfg_connlimit
;
246 if (cfg_min_share
>= 0)
247 bufferevent_rate_limit_group_set_min_share(
248 ratelim_group
, cfg_min_share
);
251 if (expected_avg_persec
< 0 && cfg_connlimit
> 0)
252 expected_avg_persec
= cfg_connlimit
;
254 if (expected_avg_persec
> 0)
255 expected_avg_persec
/= seconds_per_tick
;
256 if (expected_total_persec
> 0)
257 expected_total_persec
/= seconds_per_tick
;
259 bevs
= calloc(cfg_n_connections
, sizeof(struct bufferevent
*));
260 states
= calloc(cfg_n_connections
, sizeof(struct client_state
));
262 for (i
= 0; i
< cfg_n_connections
; ++i
) {
263 bevs
[i
] = bufferevent_socket_new(base
, -1,
264 BEV_OPT_CLOSE_ON_FREE
|BEV_OPT_THREADSAFE
);
266 bufferevent_setcb(bevs
[i
], discard_readcb
, loud_writecb
,
267 write_on_connectedcb
, &states
[i
]);
268 bufferevent_enable(bevs
[i
], EV_READ
|EV_WRITE
);
269 bufferevent_socket_connect(bevs
[i
], (struct sockaddr
*)&ss
,
273 tv
.tv_sec
= cfg_duration
- 1;
276 event_base_loopexit(base
, &tv
);
278 event_base_dispatch(base
);
280 ratelim_group
= NULL
; /* So no more responders get added */
282 for (i
= 0; i
< cfg_n_connections
; ++i
) {
283 bufferevent_free(bevs
[i
]);
285 evconnlistener_free(listener
);
287 /* Make sure no new echo_conns get added to the group. */
288 ratelim_group
= NULL
;
290 /* This should get _everybody_ freed */
291 while (n_echo_conns_open
) {
292 printf("waiting for %d conns\n", n_echo_conns_open
);
295 event_base_loopexit(base
, &tv
);
296 event_base_dispatch(base
);
300 bufferevent_rate_limit_group_free(group
);
304 total_sq_persec
= 0.0;
305 for (i
=0; i
< cfg_n_connections
; ++i
) {
306 double persec
= states
[i
].received
;
307 persec
/= cfg_duration
;
308 total_received
+= states
[i
].received
;
309 total_persec
+= persec
;
310 total_sq_persec
+= persec
*persec
;
311 printf("%d: %f per second\n", i
+1, persec
);
313 printf(" total: %f per second\n",
314 ((double)total_received
)/cfg_duration
);
315 if (expected_total_persec
> 0) {
316 double diff
= expected_total_persec
-
317 ((double)total_received
/cfg_duration
);
318 printf(" [Off by %lf]\n", diff
);
319 if (cfg_grouplimit_tolerance
> 0 &&
320 fabs(diff
) > cfg_grouplimit_tolerance
) {
321 fprintf(stderr
, "Group bandwidth out of bounds\n");
326 printf(" average: %f per second\n",
327 (((double)total_received
)/cfg_duration
)/cfg_n_connections
);
328 if (expected_avg_persec
> 0) {
329 double diff
= expected_avg_persec
- (((double)total_received
)/cfg_duration
)/cfg_n_connections
;
330 printf(" [Off by %lf]\n", diff
);
331 if (cfg_connlimit_tolerance
> 0 &&
332 fabs(diff
) > cfg_connlimit_tolerance
) {
333 fprintf(stderr
, "Connection bandwidth out of bounds\n");
338 variance
= total_sq_persec
/cfg_n_connections
- total_persec
*total_persec
/(cfg_n_connections
*cfg_n_connections
);
340 printf(" stddev: %f per second\n", sqrt(variance
));
341 if (cfg_stddev_tolerance
> 0 &&
342 sqrt(variance
) > cfg_stddev_tolerance
) {
343 fprintf(stderr
, "Connection variance out of bounds\n");
347 event_base_free(base
);
354 static struct option
{
355 const char *name
; int *ptr
; int min
; int isbool
;
357 { "-v", &cfg_verbose
, 0, 1 },
358 { "-h", &cfg_help
, 0, 1 },
359 { "-n", &cfg_n_connections
, 1, 0 },
360 { "-d", &cfg_duration
, 1, 0 },
361 { "-c", &cfg_connlimit
, 0, 0 },
362 { "-g", &cfg_grouplimit
, 0, 0 },
363 { "-t", &cfg_tick_msec
, 10, 0 },
364 { "--min-share", &cfg_min_share
, 0, 0 },
365 { "--check-connlimit", &cfg_connlimit_tolerance
, 0, 0 },
366 { "--check-grouplimit", &cfg_grouplimit_tolerance
, 0, 0 },
367 { "--check-stddev", &cfg_stddev_tolerance
, 0, 0 },
369 { "--iocp", &cfg_enable_iocp
, 0, 1 },
371 { NULL
, NULL
, -1, 0 },
375 handle_option(int argc
, char **argv
, int *i
, const struct option
*opt
)
383 if (*i
+ 1 == argc
) {
384 fprintf(stderr
, "Too few arguments to '%s'\n",argv
[*i
]);
387 val
= strtol(argv
[*i
+1], &endptr
, 10);
388 if (*argv
[*i
+1] == '\0' || !endptr
|| *endptr
!= '\0') {
389 fprintf(stderr
, "Couldn't parse numeric value '%s'\n",
393 if (val
< opt
->min
|| val
> 0x7fffffff) {
394 fprintf(stderr
, "Value '%s' is out-of-range'\n",
398 *opt
->ptr
= (int)val
;
407 "test-ratelim [-v] [-n INT] [-d INT] [-c INT] [-g INT] [-t INT]\n\n"
408 "Pushes bytes through a number of possibly rate-limited connections, and\n"
409 "displays average throughput.\n\n"
410 " -n INT: Number of connections to open (default: 30)\n"
411 " -d INT: Duration of the test in seconds (default: 5 sec)\n");
413 " -c INT: Connection-rate limit applied to each connection in bytes per second\n"
414 " (default: None.)\n"
415 " -g INT: Group-rate limit applied to sum of all usage in bytes per second\n"
416 " (default: None.)\n"
417 " -t INT: Granularity of timing, in milliseconds (default: 1000 msec)\n");
421 main(int argc
, char **argv
)
427 WORD wVersionRequested
= MAKEWORD(2,2);
430 (void) WSAStartup(wVersionRequested
, &wsaData
);
434 if (signal(SIGPIPE
, SIG_IGN
) == SIG_ERR
)
437 for (i
= 1; i
< argc
; ++i
) {
438 for (j
= 0; options
[j
].name
; ++j
) {
439 if (!strcmp(argv
[i
],options
[j
].name
)) {
440 if (handle_option(argc
,argv
,&i
,&options
[j
])<0)
445 fprintf(stderr
, "Unknown option '%s'\n", argv
[i
]);
456 cfg_tick
.tv_sec
= cfg_tick_msec
/ 1000;
457 cfg_tick
.tv_usec
= (cfg_tick_msec
% 1000)*1000;
459 seconds_per_tick
= ratio
= cfg_tick_msec
/ 1000.0;
461 cfg_connlimit
*= ratio
;
462 cfg_grouplimit
*= ratio
;
466 evutil_gettimeofday(&tv
, NULL
);
474 #ifndef _EVENT_DISABLE_THREAD_SUPPORT
475 evthread_enable_lock_debuging();
478 return test_ratelimiting();