]> arthur.barton.de Git - netatalk.git/blob - libevent/test/bench.c
Add libevent
[netatalk.git] / libevent / test / bench.c
1 /*
2  * Copyright 2003-2007 Niels Provos <provos@citi.umich.edu>
3  * Copyright 2007-2010 Niels Provos and Nick Mathewson
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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  * 4. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
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.
26  *
27  *
28  * Mon 03/10/2003 - Modified by Davide Libenzi <davidel@xmailserver.org>
29  *
30  *     Added chain event propagation to improve the sensitivity of
31  *     the measure respect to the event loop efficency.
32  *
33  *
34  */
35
36 #include "event2/event-config.h"
37
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <sys/time.h>
41 #ifdef WIN32
42 #include <windows.h>
43 #else
44 #include <sys/socket.h>
45 #include <signal.h>
46 #include <sys/resource.h>
47 #endif
48 #include <fcntl.h>
49 #include <stdlib.h>
50 #include <stdio.h>
51 #include <string.h>
52 #include <unistd.h>
53 #include <errno.h>
54
55 #include <event.h>
56 #include <evutil.h>
57
58 static int count, writes, fired;
59 static int *pipes;
60 static int num_pipes, num_active, num_writes;
61 static struct event *events;
62
63
64 static void
65 read_cb(evutil_socket_t fd, short which, void *arg)
66 {
67         long idx = (long) arg, widx = idx + 1;
68         u_char ch;
69
70         count += recv(fd, (char*)&ch, sizeof(ch), 0);
71         if (writes) {
72                 if (widx >= num_pipes)
73                         widx -= num_pipes;
74                 send(pipes[2 * widx + 1], "e", 1, 0);
75                 writes--;
76                 fired++;
77         }
78 }
79
80 static struct timeval *
81 run_once(void)
82 {
83         int *cp, space;
84         long i;
85         static struct timeval ts, te;
86
87         for (cp = pipes, i = 0; i < num_pipes; i++, cp += 2) {
88                 if (event_initialized(&events[i]))
89                         event_del(&events[i]);
90                 event_set(&events[i], cp[0], EV_READ | EV_PERSIST, read_cb, (void *) i);
91                 event_add(&events[i], NULL);
92         }
93
94         event_loop(EVLOOP_ONCE | EVLOOP_NONBLOCK);
95
96         fired = 0;
97         space = num_pipes / num_active;
98         space = space * 2;
99         for (i = 0; i < num_active; i++, fired++)
100                 send(pipes[i * space + 1], "e", 1, 0);
101
102         count = 0;
103         writes = num_writes;
104         { int xcount = 0;
105         gettimeofday(&ts, NULL);
106         do {
107                 event_loop(EVLOOP_ONCE | EVLOOP_NONBLOCK);
108                 xcount++;
109         } while (count != fired);
110         gettimeofday(&te, NULL);
111
112         if (xcount != count) fprintf(stderr, "Xcount: %d, Rcount: %d\n", xcount, count);
113         }
114
115         evutil_timersub(&te, &ts, &te);
116
117         return (&te);
118 }
119
120 int
121 main(int argc, char **argv)
122 {
123 #ifndef WIN32
124         struct rlimit rl;
125 #endif
126         int i, c;
127         struct timeval *tv;
128         int *cp;
129
130 #ifdef WIN32
131         WSADATA WSAData;
132         WSAStartup(0x101, &WSAData);
133 #endif
134         num_pipes = 100;
135         num_active = 1;
136         num_writes = num_pipes;
137         while ((c = getopt(argc, argv, "n:a:w:")) != -1) {
138                 switch (c) {
139                 case 'n':
140                         num_pipes = atoi(optarg);
141                         break;
142                 case 'a':
143                         num_active = atoi(optarg);
144                         break;
145                 case 'w':
146                         num_writes = atoi(optarg);
147                         break;
148                 default:
149                         fprintf(stderr, "Illegal argument \"%c\"\n", c);
150                         exit(1);
151                 }
152         }
153
154 #ifndef WIN32
155         rl.rlim_cur = rl.rlim_max = num_pipes * 2 + 50;
156         if (setrlimit(RLIMIT_NOFILE, &rl) == -1) {
157                 perror("setrlimit");
158                 exit(1);
159         }
160 #endif
161
162         events = calloc(num_pipes, sizeof(struct event));
163         pipes = calloc(num_pipes * 2, sizeof(int));
164         if (events == NULL || pipes == NULL) {
165                 perror("malloc");
166                 exit(1);
167         }
168
169         event_init();
170
171         for (cp = pipes, i = 0; i < num_pipes; i++, cp += 2) {
172 #ifdef USE_PIPES
173                 if (pipe(cp) == -1) {
174 #else
175                 if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, cp) == -1) {
176 #endif
177                         perror("pipe");
178                         exit(1);
179                 }
180         }
181
182         for (i = 0; i < 25; i++) {
183                 tv = run_once();
184                 if (tv == NULL)
185                         exit(1);
186                 fprintf(stdout, "%ld\n",
187                         tv->tv_sec * 1000000L + tv->tv_usec);
188         }
189
190         exit(0);
191 }