xref: /netbsd-src/external/bsd/libevent/dist/test/bench.c (revision 657871a79c9a2060a6255a242fa1a1ef76b56ec6)
1 /*	$NetBSD: bench.c,v 1.1.1.3 2021/04/07 02:43:15 christos Exp $	*/
2 /*
3  * Copyright 2003-2007 Niels Provos <provos@citi.umich.edu>
4  * Copyright 2007-2012 Niels Provos and Nick Mathewson
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 4. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  *
29  * Mon 03/10/2003 - Modified by Davide Libenzi <davidel@xmailserver.org>
30  *
31  *     Added chain event propagation to improve the sensitivity of
32  *     the measure respect to the event loop efficency.
33  *
34  *
35  */
36 
37 #include "event2/event-config.h"
38 #include <sys/cdefs.h>
39 __RCSID("$NetBSD: bench.c,v 1.1.1.3 2021/04/07 02:43:15 christos Exp $");
40 #include "../util-internal.h"
41 
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #ifdef EVENT__HAVE_SYS_TIME_H
45 #include <sys/time.h>
46 #endif
47 #ifdef _WIN32
48 #define WIN32_LEAN_AND_MEAN
49 #include <windows.h>
50 #else
51 #include <sys/socket.h>
52 #include <signal.h>
53 #include <sys/resource.h>
54 #endif
55 #include <fcntl.h>
56 #include <stdlib.h>
57 #include <stdio.h>
58 #include <string.h>
59 #ifdef EVENT__HAVE_UNISTD_H
60 #include <unistd.h>
61 #endif
62 #include <errno.h>
63 
64 #ifdef _WIN32
65 #include <getopt.h>
66 #endif
67 
68 #include <event.h>
69 #include <evutil.h>
70 
71 static ev_ssize_t count, fired;
72 static int writes, failures;
73 static evutil_socket_t *pipes;
74 static int num_pipes, num_active, num_writes;
75 static struct event *events;
76 static struct event_base *base;
77 
78 
79 static void
read_cb(evutil_socket_t fd,short which,void * arg)80 read_cb(evutil_socket_t fd, short which, void *arg)
81 {
82 	ev_intptr_t idx = (ev_intptr_t) arg, widx = idx + 1;
83 	unsigned char ch;
84 	ev_ssize_t n;
85 
86 	n = recv(fd, (char*)&ch, sizeof(ch), 0);
87 	if (n >= 0)
88 		count += n;
89 	else
90 		failures++;
91 	if (writes) {
92 		if (widx >= num_pipes)
93 			widx -= num_pipes;
94 		n = send(pipes[2 * widx + 1], "e", 1, 0);
95 		if (n != 1)
96 			failures++;
97 		writes--;
98 		fired++;
99 	}
100 }
101 
102 static struct timeval *
run_once(void)103 run_once(void)
104 {
105 	evutil_socket_t *cp, space;
106 	long i;
107 	static struct timeval ts, te;
108 
109 	for (cp = pipes, i = 0; i < num_pipes; i++, cp += 2) {
110 		if (event_initialized(&events[i]))
111 			event_del(&events[i]);
112 		event_assign(&events[i], base, cp[0], EV_READ | EV_PERSIST, read_cb, (void *)(ev_intptr_t) i);
113 		event_add(&events[i], NULL);
114 	}
115 
116 	event_base_loop(base, EVLOOP_ONCE | EVLOOP_NONBLOCK);
117 
118 	fired = 0;
119 	space = num_pipes / num_active;
120 	space = space * 2;
121 	for (i = 0; i < num_active; i++, fired++)
122 		(void) send(pipes[i * space + 1], "e", 1, 0);
123 
124 	count = 0;
125 	writes = num_writes;
126 	{
127 		int xcount = 0;
128 		evutil_gettimeofday(&ts, NULL);
129 		do {
130 			event_base_loop(base, EVLOOP_ONCE | EVLOOP_NONBLOCK);
131 			xcount++;
132 		} while (count != fired);
133 		evutil_gettimeofday(&te, NULL);
134 
135 		if (xcount != count)
136 			fprintf(stderr, "Xcount: %d, Rcount: " EV_SSIZE_FMT "\n",
137 				xcount, count);
138 	}
139 
140 	evutil_timersub(&te, &ts, &te);
141 
142 	return (&te);
143 }
144 
145 int
main(int argc,char ** argv)146 main(int argc, char **argv)
147 {
148 #ifdef EVENT__HAVE_SETRLIMIT
149 	struct rlimit rl;
150 #endif
151 	int i, c;
152 	struct timeval *tv;
153 	evutil_socket_t *cp;
154 	const char **methods;
155 	const char *method = NULL;
156 	struct event_config *cfg = NULL;
157 
158 #ifdef _WIN32
159 	WSADATA WSAData;
160 	WSAStartup(0x101, &WSAData);
161 #endif
162 	num_pipes = 100;
163 	num_active = 1;
164 	num_writes = num_pipes;
165 	while ((c = getopt(argc, argv, "n:a:w:m:l")) != -1) {
166 		switch (c) {
167 		case 'n':
168 			num_pipes = atoi(optarg);
169 			break;
170 		case 'a':
171 			num_active = atoi(optarg);
172 			break;
173 		case 'w':
174 			num_writes = atoi(optarg);
175 			break;
176 		case 'm':
177 			method = optarg;
178 			break;
179 		case 'l':
180 			methods = event_get_supported_methods();
181 			fprintf(stdout, "Using Libevent %s. Available methods are:\n",
182 				event_get_version());
183 			for (i = 0; methods[i] != NULL; ++i)
184 				printf("    %s\n", methods[i]);
185 			exit(0);
186 		default:
187 			fprintf(stderr, "Illegal argument \"%c\"\n", c);
188 			exit(1);
189 		}
190 	}
191 
192 #ifdef EVENT__HAVE_SETRLIMIT
193 	rl.rlim_cur = rl.rlim_max = num_pipes * 2 + 50;
194 	if (setrlimit(RLIMIT_NOFILE, &rl) == -1) {
195 		perror("setrlimit");
196 		exit(1);
197 	}
198 #endif
199 
200 	events = calloc(num_pipes, sizeof(struct event));
201 	pipes = calloc(num_pipes * 2, sizeof(evutil_socket_t));
202 	if (events == NULL || pipes == NULL) {
203 		perror("malloc");
204 		exit(1);
205 	}
206 
207 	if (method != NULL) {
208 		cfg = event_config_new();
209 		methods = event_get_supported_methods();
210 		for (i = 0; methods[i] != NULL; ++i)
211 			if (strcmp(methods[i], method))
212 				event_config_avoid_method(cfg, methods[i]);
213 		base = event_base_new_with_config(cfg);
214 		event_config_free(cfg);
215 	} else
216 		base = event_base_new();
217 
218 	for (cp = pipes, i = 0; i < num_pipes; i++, cp += 2) {
219 #ifdef USE_PIPES
220 		if (pipe(cp) == -1) {
221 #else
222 		if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, cp) == -1) {
223 #endif
224 			perror("pipe");
225 			exit(1);
226 		}
227 	}
228 
229 	for (i = 0; i < 25; i++) {
230 		tv = run_once();
231 		if (tv == NULL)
232 			exit(1);
233 		fprintf(stdout, "%ld\n",
234 			tv->tv_sec * 1000000L + tv->tv_usec);
235 	}
236 
237 	exit(0);
238 }
239