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