]> arthur.barton.de Git - netatalk.git/blob - libevent/test/bench_cascade.c
Add libevent
[netatalk.git] / libevent / test / bench_cascade.c
1 /*
2  * Copyright 2007-2010 Niels Provos and Nick Mathewson
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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  * 4. The name of the author may not be used to endorse or promote products
13  *    derived from this software without specific prior written permission.
14  *
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.
25  *
26  */
27
28 #include "event2/event-config.h"
29
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <sys/time.h>
33 #ifdef WIN32
34 #include <windows.h>
35 #else
36 #include <sys/socket.h>
37 #include <sys/resource.h>
38 #endif
39 #include <signal.h>
40 #include <fcntl.h>
41 #include <stdlib.h>
42 #include <stdio.h>
43 #include <string.h>
44 #include <unistd.h>
45 #include <errno.h>
46
47 #include <event.h>
48 #include <evutil.h>
49
50 /*
51  * This benchmark tests how quickly we can propagate a write down a chain
52  * of socket pairs.  We start by writing to the first socket pair and all
53  * events will fire subsequently until the last socket pair has been reached
54  * and the benchmark terminates.
55  */
56
57 static int fired;
58 static int *pipes;
59 static struct event *events;
60
61 static void
62 read_cb(evutil_socket_t fd, short which, void *arg)
63 {
64         char ch;
65         long idx = (long) arg;
66
67         recv(fd, &ch, sizeof(ch), 0);
68         if (idx >= 0)
69                 send(idx, "e", 1, 0);
70         fired++;
71 }
72
73 static struct timeval *
74 run_once(int num_pipes)
75 {
76         int *cp, i;
77         static struct timeval ts, te, tv_timeout;
78
79         events = calloc(num_pipes, sizeof(struct event));
80         pipes = calloc(num_pipes * 2, sizeof(int));
81
82         if (events == NULL || pipes == NULL) {
83                 perror("malloc");
84                 exit(1);
85         }
86
87         for (cp = pipes, i = 0; i < num_pipes; i++, cp += 2) {
88                 if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, cp) == -1) {
89                         perror("socketpair");
90                         exit(1);
91                 }
92         }
93
94         /* measurements includes event setup */
95         gettimeofday(&ts, NULL);
96
97         /* provide a default timeout for events */
98         evutil_timerclear(&tv_timeout);
99         tv_timeout.tv_sec = 60;
100
101         for (cp = pipes, i = 0; i < num_pipes; i++, cp += 2) {
102                 long fd = i < num_pipes - 1 ? cp[3] : -1;
103                 event_set(&events[i], cp[0], EV_READ, read_cb, (void *) fd);
104                 event_add(&events[i], &tv_timeout);
105         }
106
107         fired = 0;
108
109         /* kick everything off with a single write */
110         send(pipes[1], "e", 1, 0);
111
112         event_dispatch();
113
114         gettimeofday(&te, NULL);
115         evutil_timersub(&te, &ts, &te);
116
117         for (cp = pipes, i = 0; i < num_pipes; i++, cp += 2) {
118                 event_del(&events[i]);
119                 close(cp[0]);
120                 close(cp[1]);
121         }
122
123         free(pipes);
124         free(events);
125
126         return (&te);
127 }
128
129 int
130 main(int argc, char **argv)
131 {
132 #ifndef WIN32
133         struct rlimit rl;
134 #endif
135         int i, c;
136         struct timeval *tv;
137
138         int num_pipes = 100;
139         while ((c = getopt(argc, argv, "n:")) != -1) {
140                 switch (c) {
141                 case 'n':
142                         num_pipes = atoi(optarg);
143                         break;
144                 default:
145                         fprintf(stderr, "Illegal argument \"%c\"\n", c);
146                         exit(1);
147                 }
148         }
149
150 #ifndef WIN32
151         rl.rlim_cur = rl.rlim_max = num_pipes * 2 + 50;
152         if (setrlimit(RLIMIT_NOFILE, &rl) == -1) {
153                 perror("setrlimit");
154                 exit(1);
155         }
156 #endif
157
158         event_init();
159
160         for (i = 0; i < 25; i++) {
161                 tv = run_once(num_pipes);
162                 if (tv == NULL)
163                         exit(1);
164                 fprintf(stdout, "%ld\n",
165                         tv->tv_sec * 1000000L + tv->tv_usec);
166         }
167
168         exit(0);
169 }