1c43e99fdSEd Maste /*
2c43e99fdSEd Maste * Copyright 2007-2012 Niels Provos and Nick Mathewson
3c43e99fdSEd Maste *
4c43e99fdSEd Maste * Redistribution and use in source and binary forms, with or without
5c43e99fdSEd Maste * modification, are permitted provided that the following conditions
6c43e99fdSEd Maste * are met:
7c43e99fdSEd Maste * 1. Redistributions of source code must retain the above copyright
8c43e99fdSEd Maste * notice, this list of conditions and the following disclaimer.
9c43e99fdSEd Maste * 2. Redistributions in binary form must reproduce the above copyright
10c43e99fdSEd Maste * notice, this list of conditions and the following disclaimer in the
11c43e99fdSEd Maste * documentation and/or other materials provided with the distribution.
12c43e99fdSEd Maste * 4. The name of the author may not be used to endorse or promote products
13c43e99fdSEd Maste * derived from this software without specific prior written permission.
14c43e99fdSEd Maste *
15c43e99fdSEd Maste * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16c43e99fdSEd Maste * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17c43e99fdSEd Maste * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18c43e99fdSEd Maste * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19c43e99fdSEd Maste * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20c43e99fdSEd Maste * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21c43e99fdSEd Maste * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22c43e99fdSEd Maste * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23c43e99fdSEd Maste * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24c43e99fdSEd Maste * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25c43e99fdSEd Maste *
26c43e99fdSEd Maste */
27c43e99fdSEd Maste
28c43e99fdSEd Maste #include "event2/event-config.h"
29c43e99fdSEd Maste
30c43e99fdSEd Maste #include <sys/types.h>
31c43e99fdSEd Maste #include <sys/stat.h>
32c43e99fdSEd Maste #ifdef EVENT__HAVE_SYS_TIME_H
33c43e99fdSEd Maste #include <sys/time.h>
34c43e99fdSEd Maste #endif
35c43e99fdSEd Maste #ifdef _WIN32
36c43e99fdSEd Maste #define WIN32_LEAN_AND_MEAN
37c43e99fdSEd Maste #include <windows.h>
38*b50261e2SCy Schubert #include <getopt.h>
39*b50261e2SCy Schubert #else /* _WIN32 */
40c43e99fdSEd Maste #include <sys/socket.h>
41c43e99fdSEd Maste #include <sys/resource.h>
42c43e99fdSEd Maste #endif
43c43e99fdSEd Maste #include <signal.h>
44c43e99fdSEd Maste #include <fcntl.h>
45c43e99fdSEd Maste #include <stdlib.h>
46c43e99fdSEd Maste #include <stdio.h>
47c43e99fdSEd Maste #include <string.h>
48c43e99fdSEd Maste #ifdef EVENT__HAVE_UNISTD_H
49c43e99fdSEd Maste #include <unistd.h>
50c43e99fdSEd Maste #endif
51c43e99fdSEd Maste #include <errno.h>
52c43e99fdSEd Maste #include <event.h>
53c43e99fdSEd Maste #include <evutil.h>
54c43e99fdSEd Maste
55c43e99fdSEd Maste /*
56c43e99fdSEd Maste * This benchmark tests how quickly we can propagate a write down a chain
57c43e99fdSEd Maste * of socket pairs. We start by writing to the first socket pair and all
58c43e99fdSEd Maste * events will fire subsequently until the last socket pair has been reached
59c43e99fdSEd Maste * and the benchmark terminates.
60c43e99fdSEd Maste */
61c43e99fdSEd Maste
62c43e99fdSEd Maste static int fired;
63c43e99fdSEd Maste static evutil_socket_t *pipes;
64c43e99fdSEd Maste static struct event *events;
65c43e99fdSEd Maste
66c43e99fdSEd Maste static void
read_cb(evutil_socket_t fd,short which,void * arg)67c43e99fdSEd Maste read_cb(evutil_socket_t fd, short which, void *arg)
68c43e99fdSEd Maste {
69c43e99fdSEd Maste char ch;
70c43e99fdSEd Maste evutil_socket_t sock = (evutil_socket_t)(ev_intptr_t)arg;
71c43e99fdSEd Maste
72c43e99fdSEd Maste (void) recv(fd, &ch, sizeof(ch), 0);
73c43e99fdSEd Maste if (sock >= 0) {
74c43e99fdSEd Maste if (send(sock, "e", 1, 0) < 0)
75c43e99fdSEd Maste perror("send");
76c43e99fdSEd Maste }
77c43e99fdSEd Maste fired++;
78c43e99fdSEd Maste }
79c43e99fdSEd Maste
80c43e99fdSEd Maste static struct timeval *
run_once(int num_pipes)81c43e99fdSEd Maste run_once(int num_pipes)
82c43e99fdSEd Maste {
83c43e99fdSEd Maste int i;
84c43e99fdSEd Maste evutil_socket_t *cp;
85c43e99fdSEd Maste static struct timeval ts, te, tv_timeout;
86c43e99fdSEd Maste
87c43e99fdSEd Maste events = (struct event *)calloc(num_pipes, sizeof(struct event));
88c43e99fdSEd Maste pipes = (evutil_socket_t *)calloc(num_pipes * 2, sizeof(evutil_socket_t));
89c43e99fdSEd Maste
90c43e99fdSEd Maste if (events == NULL || pipes == NULL) {
91c43e99fdSEd Maste perror("malloc");
92c43e99fdSEd Maste exit(1);
93c43e99fdSEd Maste }
94c43e99fdSEd Maste
95c43e99fdSEd Maste for (cp = pipes, i = 0; i < num_pipes; i++, cp += 2) {
96c43e99fdSEd Maste if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, cp) == -1) {
97c43e99fdSEd Maste perror("socketpair");
98c43e99fdSEd Maste exit(1);
99c43e99fdSEd Maste }
100c43e99fdSEd Maste }
101c43e99fdSEd Maste
102c43e99fdSEd Maste /* measurements includes event setup */
103c43e99fdSEd Maste evutil_gettimeofday(&ts, NULL);
104c43e99fdSEd Maste
105c43e99fdSEd Maste /* provide a default timeout for events */
106c43e99fdSEd Maste evutil_timerclear(&tv_timeout);
107c43e99fdSEd Maste tv_timeout.tv_sec = 60;
108c43e99fdSEd Maste
109c43e99fdSEd Maste for (cp = pipes, i = 0; i < num_pipes; i++, cp += 2) {
110c43e99fdSEd Maste evutil_socket_t fd = i < num_pipes - 1 ? cp[3] : -1;
111c43e99fdSEd Maste event_set(&events[i], cp[0], EV_READ, read_cb,
112c43e99fdSEd Maste (void *)(ev_intptr_t)fd);
113c43e99fdSEd Maste event_add(&events[i], &tv_timeout);
114c43e99fdSEd Maste }
115c43e99fdSEd Maste
116c43e99fdSEd Maste fired = 0;
117c43e99fdSEd Maste
118c43e99fdSEd Maste /* kick everything off with a single write */
119c43e99fdSEd Maste if (send(pipes[1], "e", 1, 0) < 0)
120c43e99fdSEd Maste perror("send");
121c43e99fdSEd Maste
122c43e99fdSEd Maste event_dispatch();
123c43e99fdSEd Maste
124c43e99fdSEd Maste evutil_gettimeofday(&te, NULL);
125c43e99fdSEd Maste evutil_timersub(&te, &ts, &te);
126c43e99fdSEd Maste
127c43e99fdSEd Maste for (cp = pipes, i = 0; i < num_pipes; i++, cp += 2) {
128c43e99fdSEd Maste event_del(&events[i]);
129c43e99fdSEd Maste evutil_closesocket(cp[0]);
130c43e99fdSEd Maste evutil_closesocket(cp[1]);
131c43e99fdSEd Maste }
132c43e99fdSEd Maste
133c43e99fdSEd Maste free(pipes);
134c43e99fdSEd Maste free(events);
135c43e99fdSEd Maste
136c43e99fdSEd Maste return (&te);
137c43e99fdSEd Maste }
138c43e99fdSEd Maste
139c43e99fdSEd Maste int
main(int argc,char ** argv)140c43e99fdSEd Maste main(int argc, char **argv)
141c43e99fdSEd Maste {
142*b50261e2SCy Schubert #ifdef EVENT__HAVE_SETRLIMIT
143c43e99fdSEd Maste struct rlimit rl;
144c43e99fdSEd Maste #endif
145c43e99fdSEd Maste int i, c;
146c43e99fdSEd Maste struct timeval *tv;
147c43e99fdSEd Maste
148c43e99fdSEd Maste int num_pipes = 100;
149c43e99fdSEd Maste #ifdef _WIN32
150c43e99fdSEd Maste WSADATA WSAData;
151c43e99fdSEd Maste WSAStartup(0x101, &WSAData);
152c43e99fdSEd Maste #endif
153c43e99fdSEd Maste
154c43e99fdSEd Maste while ((c = getopt(argc, argv, "n:")) != -1) {
155c43e99fdSEd Maste switch (c) {
156c43e99fdSEd Maste case 'n':
157c43e99fdSEd Maste num_pipes = atoi(optarg);
158c43e99fdSEd Maste break;
159c43e99fdSEd Maste default:
160c43e99fdSEd Maste fprintf(stderr, "Illegal argument \"%c\"\n", c);
161c43e99fdSEd Maste exit(1);
162c43e99fdSEd Maste }
163c43e99fdSEd Maste }
164c43e99fdSEd Maste
165*b50261e2SCy Schubert #ifdef EVENT__HAVE_SETRLIMIT
166c43e99fdSEd Maste rl.rlim_cur = rl.rlim_max = num_pipes * 2 + 50;
167c43e99fdSEd Maste if (setrlimit(RLIMIT_NOFILE, &rl) == -1) {
168c43e99fdSEd Maste perror("setrlimit");
169c43e99fdSEd Maste exit(1);
170c43e99fdSEd Maste }
171c43e99fdSEd Maste #endif
172c43e99fdSEd Maste
173c43e99fdSEd Maste event_init();
174c43e99fdSEd Maste
175c43e99fdSEd Maste for (i = 0; i < 25; i++) {
176c43e99fdSEd Maste tv = run_once(num_pipes);
177c43e99fdSEd Maste if (tv == NULL)
178c43e99fdSEd Maste exit(1);
179c43e99fdSEd Maste fprintf(stdout, "%ld\n",
180c43e99fdSEd Maste tv->tv_sec * 1000000L + tv->tv_usec);
181c43e99fdSEd Maste }
182c43e99fdSEd Maste
183c43e99fdSEd Maste #ifdef _WIN32
184c43e99fdSEd Maste WSACleanup();
185c43e99fdSEd Maste #endif
186c43e99fdSEd Maste
187c43e99fdSEd Maste exit(0);
188c43e99fdSEd Maste }
189