2 * Copyright 2007-2012 Niels Provos and Nick Mathewson
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
28 #include "event2/event-config.h"
30 #include <sys/types.h>
32 #ifdef EVENT__HAVE_SYS_TIME_H
36 #define WIN32_LEAN_AND_MEAN
39 #include <sys/socket.h>
40 #include <sys/resource.h>
47 #ifdef EVENT__HAVE_UNISTD_H
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.
63 static evutil_socket_t *pipes;
64 static struct event *events;
67 read_cb(evutil_socket_t fd, short which, void *arg)
70 evutil_socket_t sock = (evutil_socket_t)(ev_intptr_t)arg;
72 (void) recv(fd, &ch, sizeof(ch), 0);
74 if (send(sock, "e", 1, 0) < 0)
80 static struct timeval *
81 run_once(int num_pipes)
85 static struct timeval ts, te, tv_timeout;
87 events = (struct event *)calloc(num_pipes, sizeof(struct event));
88 pipes = (evutil_socket_t *)calloc(num_pipes * 2, sizeof(evutil_socket_t));
90 if (events == NULL || pipes == NULL) {
95 for (cp = pipes, i = 0; i < num_pipes; i++, cp += 2) {
96 if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, cp) == -1) {
102 /* measurements includes event setup */
103 evutil_gettimeofday(&ts, NULL);
105 /* provide a default timeout for events */
106 evutil_timerclear(&tv_timeout);
107 tv_timeout.tv_sec = 60;
109 for (cp = pipes, i = 0; i < num_pipes; i++, cp += 2) {
110 evutil_socket_t fd = i < num_pipes - 1 ? cp[3] : -1;
111 event_set(&events[i], cp[0], EV_READ, read_cb,
112 (void *)(ev_intptr_t)fd);
113 event_add(&events[i], &tv_timeout);
118 /* kick everything off with a single write */
119 if (send(pipes[1], "e", 1, 0) < 0)
124 evutil_gettimeofday(&te, NULL);
125 evutil_timersub(&te, &ts, &te);
127 for (cp = pipes, i = 0; i < num_pipes; i++, cp += 2) {
128 event_del(&events[i]);
129 evutil_closesocket(cp[0]);
130 evutil_closesocket(cp[1]);
140 main(int argc, char **argv)
142 #ifdef HAVE_SETRLIMIT
151 WSAStartup(0x101, &WSAData);
154 while ((c = getopt(argc, argv, "n:")) != -1) {
157 num_pipes = atoi(optarg);
160 fprintf(stderr, "Illegal argument \"%c\"\n", c);
165 #ifdef HAVE_SETRLIMIT
166 rl.rlim_cur = rl.rlim_max = num_pipes * 2 + 50;
167 if (setrlimit(RLIMIT_NOFILE, &rl) == -1) {
175 for (i = 0; i < 25; i++) {
176 tv = run_once(num_pipes);
179 fprintf(stdout, "%ld\n",
180 tv->tv_sec * 1000000L + tv->tv_usec);