1 /* $NetBSD: bench.c,v 1.1.1.1 2013/04/11 16:43:32 christos Exp $ */
3 * Copyright 2003-2007 Niels Provos <provos@citi.umich.edu>
4 * Copyright 2007-2012 Niels Provos and Nick Mathewson
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 4. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * Mon 03/10/2003 - Modified by Davide Libenzi <davidel@xmailserver.org>
31 * Added chain event propagation to improve the sensitivity of
32 * the measure respect to the event loop efficency.
37 #include "event2/event-config.h"
38 #include <sys/cdefs.h>
39 __RCSID("$NetBSD: bench.c,v 1.1.1.1 2013/04/11 16:43:32 christos Exp $");
41 #include <sys/types.h>
43 #ifdef _EVENT_HAVE_SYS_TIME_H
47 #define WIN32_LEAN_AND_MEAN
50 #include <sys/socket.h>
52 #include <sys/resource.h>
58 #ifdef _EVENT_HAVE_UNISTD_H
66 static int count
, writes
, fired
;
67 static evutil_socket_t
*pipes
;
68 static int num_pipes
, num_active
, num_writes
;
69 static struct event
*events
;
73 read_cb(evutil_socket_t fd
, short which
, void *arg
)
75 ev_intptr_t idx
= (ev_intptr_t
) arg
, widx
= idx
+ 1;
78 count
+= recv(fd
, (char*)&ch
, sizeof(ch
), 0);
80 if (widx
>= num_pipes
)
82 send(pipes
[2 * widx
+ 1], "e", 1, 0);
88 static struct timeval
*
91 evutil_socket_t
*cp
, space
;
93 static struct timeval ts
, te
;
95 for (cp
= pipes
, i
= 0; i
< num_pipes
; i
++, cp
+= 2) {
96 if (event_initialized(&events
[i
]))
97 event_del(&events
[i
]);
98 event_set(&events
[i
], cp
[0], EV_READ
| EV_PERSIST
, read_cb
, (void *)(ev_intptr_t
) i
);
99 event_add(&events
[i
], NULL
);
102 event_loop(EVLOOP_ONCE
| EVLOOP_NONBLOCK
);
105 space
= num_pipes
/ num_active
;
107 for (i
= 0; i
< num_active
; i
++, fired
++)
108 send(pipes
[i
* space
+ 1], "e", 1, 0);
113 evutil_gettimeofday(&ts
, NULL
);
115 event_loop(EVLOOP_ONCE
| EVLOOP_NONBLOCK
);
117 } while (count
!= fired
);
118 evutil_gettimeofday(&te
, NULL
);
120 if (xcount
!= count
) fprintf(stderr
, "Xcount: %d, Rcount: %d\n", xcount
, count
);
123 evutil_timersub(&te
, &ts
, &te
);
129 main(int argc
, char **argv
)
140 WSAStartup(0x101, &WSAData
);
144 num_writes
= num_pipes
;
145 while ((c
= getopt(argc
, argv
, "n:a:w:")) != -1) {
148 num_pipes
= atoi(optarg
);
151 num_active
= atoi(optarg
);
154 num_writes
= atoi(optarg
);
157 fprintf(stderr
, "Illegal argument \"%c\"\n", c
);
163 rl
.rlim_cur
= rl
.rlim_max
= num_pipes
* 2 + 50;
164 if (setrlimit(RLIMIT_NOFILE
, &rl
) == -1) {
170 events
= calloc(num_pipes
, sizeof(struct event
));
171 pipes
= calloc(num_pipes
* 2, sizeof(evutil_socket_t
));
172 if (events
== NULL
|| pipes
== NULL
) {
179 for (cp
= pipes
, i
= 0; i
< num_pipes
; i
++, cp
+= 2) {
181 if (pipe(cp
) == -1) {
183 if (evutil_socketpair(AF_UNIX
, SOCK_STREAM
, 0, cp
) == -1) {
190 for (i
= 0; i
< 25; i
++) {
194 fprintf(stdout
, "%ld\n",
195 tv
->tv_sec
* 1000000L + tv
->tv_usec
);