etc/services - sync with NetBSD-8
[minix.git] / external / bsd / libevent / dist / test / bench.c
blob071c13d2c56a08f6bc760c71d6ff14ffa2788f98
1 /* $NetBSD: bench.c,v 1.1.1.1 2013/04/11 16:43:32 christos Exp $ */
2 /*
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
8 * are met:
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>
42 #include <sys/stat.h>
43 #ifdef _EVENT_HAVE_SYS_TIME_H
44 #include <sys/time.h>
45 #endif
46 #ifdef WIN32
47 #define WIN32_LEAN_AND_MEAN
48 #include <windows.h>
49 #else
50 #include <sys/socket.h>
51 #include <signal.h>
52 #include <sys/resource.h>
53 #endif
54 #include <fcntl.h>
55 #include <stdlib.h>
56 #include <stdio.h>
57 #include <string.h>
58 #ifdef _EVENT_HAVE_UNISTD_H
59 #include <unistd.h>
60 #endif
61 #include <errno.h>
63 #include <event.h>
64 #include <evutil.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;
72 static void
73 read_cb(evutil_socket_t fd, short which, void *arg)
75 ev_intptr_t idx = (ev_intptr_t) arg, widx = idx + 1;
76 u_char ch;
78 count += recv(fd, (char*)&ch, sizeof(ch), 0);
79 if (writes) {
80 if (widx >= num_pipes)
81 widx -= num_pipes;
82 send(pipes[2 * widx + 1], "e", 1, 0);
83 writes--;
84 fired++;
88 static struct timeval *
89 run_once(void)
91 evutil_socket_t *cp, space;
92 long i;
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);
104 fired = 0;
105 space = num_pipes / num_active;
106 space = space * 2;
107 for (i = 0; i < num_active; i++, fired++)
108 send(pipes[i * space + 1], "e", 1, 0);
110 count = 0;
111 writes = num_writes;
112 { int xcount = 0;
113 evutil_gettimeofday(&ts, NULL);
114 do {
115 event_loop(EVLOOP_ONCE | EVLOOP_NONBLOCK);
116 xcount++;
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);
125 return (&te);
129 main(int argc, char **argv)
131 #ifndef WIN32
132 struct rlimit rl;
133 #endif
134 int i, c;
135 struct timeval *tv;
136 evutil_socket_t *cp;
138 #ifdef WIN32
139 WSADATA WSAData;
140 WSAStartup(0x101, &WSAData);
141 #endif
142 num_pipes = 100;
143 num_active = 1;
144 num_writes = num_pipes;
145 while ((c = getopt(argc, argv, "n:a:w:")) != -1) {
146 switch (c) {
147 case 'n':
148 num_pipes = atoi(optarg);
149 break;
150 case 'a':
151 num_active = atoi(optarg);
152 break;
153 case 'w':
154 num_writes = atoi(optarg);
155 break;
156 default:
157 fprintf(stderr, "Illegal argument \"%c\"\n", c);
158 exit(1);
162 #ifndef WIN32
163 rl.rlim_cur = rl.rlim_max = num_pipes * 2 + 50;
164 if (setrlimit(RLIMIT_NOFILE, &rl) == -1) {
165 perror("setrlimit");
166 exit(1);
168 #endif
170 events = calloc(num_pipes, sizeof(struct event));
171 pipes = calloc(num_pipes * 2, sizeof(evutil_socket_t));
172 if (events == NULL || pipes == NULL) {
173 perror("malloc");
174 exit(1);
177 event_init();
179 for (cp = pipes, i = 0; i < num_pipes; i++, cp += 2) {
180 #ifdef USE_PIPES
181 if (pipe(cp) == -1) {
182 #else
183 if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, cp) == -1) {
184 #endif
185 perror("pipe");
186 exit(1);
190 for (i = 0; i < 25; i++) {
191 tv = run_once();
192 if (tv == NULL)
193 exit(1);
194 fprintf(stdout, "%ld\n",
195 tv->tv_sec * 1000000L + tv->tv_usec);
198 exit(0);