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