xref: /freebsd-src/contrib/ntp/sntp/libevent/test/bench_cascade.c (revision a466cc55373fc3cf86837f09da729535b57e69a1)
12b15cb3dSCy Schubert /*
22b15cb3dSCy Schubert  * Copyright 2007-2012 Niels Provos and Nick Mathewson
32b15cb3dSCy Schubert  *
42b15cb3dSCy Schubert  * Redistribution and use in source and binary forms, with or without
52b15cb3dSCy Schubert  * modification, are permitted provided that the following conditions
62b15cb3dSCy Schubert  * are met:
72b15cb3dSCy Schubert  * 1. Redistributions of source code must retain the above copyright
82b15cb3dSCy Schubert  *    notice, this list of conditions and the following disclaimer.
92b15cb3dSCy Schubert  * 2. Redistributions in binary form must reproduce the above copyright
102b15cb3dSCy Schubert  *    notice, this list of conditions and the following disclaimer in the
112b15cb3dSCy Schubert  *    documentation and/or other materials provided with the distribution.
122b15cb3dSCy Schubert  * 4. The name of the author may not be used to endorse or promote products
132b15cb3dSCy Schubert  *    derived from this software without specific prior written permission.
142b15cb3dSCy Schubert  *
152b15cb3dSCy Schubert  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
162b15cb3dSCy Schubert  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
172b15cb3dSCy Schubert  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
182b15cb3dSCy Schubert  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
192b15cb3dSCy Schubert  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
202b15cb3dSCy Schubert  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
212b15cb3dSCy Schubert  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
222b15cb3dSCy Schubert  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
232b15cb3dSCy Schubert  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
242b15cb3dSCy Schubert  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
252b15cb3dSCy Schubert  *
262b15cb3dSCy Schubert  */
272b15cb3dSCy Schubert 
282b15cb3dSCy Schubert #include "event2/event-config.h"
292b15cb3dSCy Schubert 
302b15cb3dSCy Schubert #include <sys/types.h>
312b15cb3dSCy Schubert #include <sys/stat.h>
322b15cb3dSCy Schubert #ifdef EVENT__HAVE_SYS_TIME_H
332b15cb3dSCy Schubert #include <sys/time.h>
342b15cb3dSCy Schubert #endif
352b15cb3dSCy Schubert #ifdef _WIN32
362b15cb3dSCy Schubert #define WIN32_LEAN_AND_MEAN
372b15cb3dSCy Schubert #include <windows.h>
38*a466cc55SCy Schubert #include <getopt.h>
39*a466cc55SCy Schubert #else /* _WIN32 */
402b15cb3dSCy Schubert #include <sys/socket.h>
412b15cb3dSCy Schubert #include <sys/resource.h>
422b15cb3dSCy Schubert #endif
432b15cb3dSCy Schubert #include <signal.h>
442b15cb3dSCy Schubert #include <fcntl.h>
452b15cb3dSCy Schubert #include <stdlib.h>
462b15cb3dSCy Schubert #include <stdio.h>
472b15cb3dSCy Schubert #include <string.h>
482b15cb3dSCy Schubert #ifdef EVENT__HAVE_UNISTD_H
492b15cb3dSCy Schubert #include <unistd.h>
502b15cb3dSCy Schubert #endif
512b15cb3dSCy Schubert #include <errno.h>
522b15cb3dSCy Schubert #include <event.h>
532b15cb3dSCy Schubert #include <evutil.h>
542b15cb3dSCy Schubert 
552b15cb3dSCy Schubert /*
562b15cb3dSCy Schubert  * This benchmark tests how quickly we can propagate a write down a chain
572b15cb3dSCy Schubert  * of socket pairs.  We start by writing to the first socket pair and all
582b15cb3dSCy Schubert  * events will fire subsequently until the last socket pair has been reached
592b15cb3dSCy Schubert  * and the benchmark terminates.
602b15cb3dSCy Schubert  */
612b15cb3dSCy Schubert 
622b15cb3dSCy Schubert static int fired;
632b15cb3dSCy Schubert static evutil_socket_t *pipes;
642b15cb3dSCy Schubert static struct event *events;
652b15cb3dSCy Schubert 
662b15cb3dSCy Schubert static void
read_cb(evutil_socket_t fd,short which,void * arg)672b15cb3dSCy Schubert read_cb(evutil_socket_t fd, short which, void *arg)
682b15cb3dSCy Schubert {
692b15cb3dSCy Schubert 	char ch;
702b15cb3dSCy Schubert 	evutil_socket_t sock = (evutil_socket_t)(ev_intptr_t)arg;
712b15cb3dSCy Schubert 
722b15cb3dSCy Schubert 	(void) recv(fd, &ch, sizeof(ch), 0);
732b15cb3dSCy Schubert 	if (sock >= 0) {
742b15cb3dSCy Schubert 		if (send(sock, "e", 1, 0) < 0)
752b15cb3dSCy Schubert 			perror("send");
762b15cb3dSCy Schubert 	}
772b15cb3dSCy Schubert 	fired++;
782b15cb3dSCy Schubert }
792b15cb3dSCy Schubert 
802b15cb3dSCy Schubert static struct timeval *
run_once(int num_pipes)812b15cb3dSCy Schubert run_once(int num_pipes)
822b15cb3dSCy Schubert {
832b15cb3dSCy Schubert 	int i;
842b15cb3dSCy Schubert 	evutil_socket_t *cp;
852b15cb3dSCy Schubert 	static struct timeval ts, te, tv_timeout;
862b15cb3dSCy Schubert 
872b15cb3dSCy Schubert 	events = (struct event *)calloc(num_pipes, sizeof(struct event));
882b15cb3dSCy Schubert 	pipes = (evutil_socket_t *)calloc(num_pipes * 2, sizeof(evutil_socket_t));
892b15cb3dSCy Schubert 
902b15cb3dSCy Schubert 	if (events == NULL || pipes == NULL) {
912b15cb3dSCy Schubert 		perror("malloc");
922b15cb3dSCy Schubert 		exit(1);
932b15cb3dSCy Schubert 	}
942b15cb3dSCy Schubert 
952b15cb3dSCy Schubert 	for (cp = pipes, i = 0; i < num_pipes; i++, cp += 2) {
962b15cb3dSCy Schubert 		if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, cp) == -1) {
972b15cb3dSCy Schubert 			perror("socketpair");
982b15cb3dSCy Schubert 			exit(1);
992b15cb3dSCy Schubert 		}
1002b15cb3dSCy Schubert 	}
1012b15cb3dSCy Schubert 
1022b15cb3dSCy Schubert 	/* measurements includes event setup */
1032b15cb3dSCy Schubert 	evutil_gettimeofday(&ts, NULL);
1042b15cb3dSCy Schubert 
1052b15cb3dSCy Schubert 	/* provide a default timeout for events */
1062b15cb3dSCy Schubert 	evutil_timerclear(&tv_timeout);
1072b15cb3dSCy Schubert 	tv_timeout.tv_sec = 60;
1082b15cb3dSCy Schubert 
1092b15cb3dSCy Schubert 	for (cp = pipes, i = 0; i < num_pipes; i++, cp += 2) {
1102b15cb3dSCy Schubert 		evutil_socket_t fd = i < num_pipes - 1 ? cp[3] : -1;
1112b15cb3dSCy Schubert 		event_set(&events[i], cp[0], EV_READ, read_cb,
1122b15cb3dSCy Schubert 		    (void *)(ev_intptr_t)fd);
1132b15cb3dSCy Schubert 		event_add(&events[i], &tv_timeout);
1142b15cb3dSCy Schubert 	}
1152b15cb3dSCy Schubert 
1162b15cb3dSCy Schubert 	fired = 0;
1172b15cb3dSCy Schubert 
1182b15cb3dSCy Schubert 	/* kick everything off with a single write */
1192b15cb3dSCy Schubert 	if (send(pipes[1], "e", 1, 0) < 0)
1202b15cb3dSCy Schubert 		perror("send");
1212b15cb3dSCy Schubert 
1222b15cb3dSCy Schubert 	event_dispatch();
1232b15cb3dSCy Schubert 
1242b15cb3dSCy Schubert 	evutil_gettimeofday(&te, NULL);
1252b15cb3dSCy Schubert 	evutil_timersub(&te, &ts, &te);
1262b15cb3dSCy Schubert 
1272b15cb3dSCy Schubert 	for (cp = pipes, i = 0; i < num_pipes; i++, cp += 2) {
1282b15cb3dSCy Schubert 		event_del(&events[i]);
1292b15cb3dSCy Schubert 		evutil_closesocket(cp[0]);
1302b15cb3dSCy Schubert 		evutil_closesocket(cp[1]);
1312b15cb3dSCy Schubert 	}
1322b15cb3dSCy Schubert 
1332b15cb3dSCy Schubert 	free(pipes);
1342b15cb3dSCy Schubert 	free(events);
1352b15cb3dSCy Schubert 
1362b15cb3dSCy Schubert 	return (&te);
1372b15cb3dSCy Schubert }
1382b15cb3dSCy Schubert 
1392b15cb3dSCy Schubert int
main(int argc,char ** argv)1402b15cb3dSCy Schubert main(int argc, char **argv)
1412b15cb3dSCy Schubert {
142*a466cc55SCy Schubert #ifdef EVENT__HAVE_SETRLIMIT
1432b15cb3dSCy Schubert 	struct rlimit rl;
1442b15cb3dSCy Schubert #endif
1452b15cb3dSCy Schubert 	int i, c;
1462b15cb3dSCy Schubert 	struct timeval *tv;
1472b15cb3dSCy Schubert 
1482b15cb3dSCy Schubert 	int num_pipes = 100;
1492b15cb3dSCy Schubert #ifdef _WIN32
1502b15cb3dSCy Schubert 	WSADATA WSAData;
1512b15cb3dSCy Schubert 	WSAStartup(0x101, &WSAData);
1522b15cb3dSCy Schubert #endif
1532b15cb3dSCy Schubert 
1542b15cb3dSCy Schubert 	while ((c = getopt(argc, argv, "n:")) != -1) {
1552b15cb3dSCy Schubert 		switch (c) {
1562b15cb3dSCy Schubert 		case 'n':
1572b15cb3dSCy Schubert 			num_pipes = atoi(optarg);
1582b15cb3dSCy Schubert 			break;
1592b15cb3dSCy Schubert 		default:
1602b15cb3dSCy Schubert 			fprintf(stderr, "Illegal argument \"%c\"\n", c);
1612b15cb3dSCy Schubert 			exit(1);
1622b15cb3dSCy Schubert 		}
1632b15cb3dSCy Schubert 	}
1642b15cb3dSCy Schubert 
165*a466cc55SCy Schubert #ifdef EVENT__HAVE_SETRLIMIT
1662b15cb3dSCy Schubert 	rl.rlim_cur = rl.rlim_max = num_pipes * 2 + 50;
1672b15cb3dSCy Schubert 	if (setrlimit(RLIMIT_NOFILE, &rl) == -1) {
1682b15cb3dSCy Schubert 		perror("setrlimit");
1692b15cb3dSCy Schubert 		exit(1);
1702b15cb3dSCy Schubert 	}
1712b15cb3dSCy Schubert #endif
1722b15cb3dSCy Schubert 
1732b15cb3dSCy Schubert 	event_init();
1742b15cb3dSCy Schubert 
1752b15cb3dSCy Schubert 	for (i = 0; i < 25; i++) {
1762b15cb3dSCy Schubert 		tv = run_once(num_pipes);
1772b15cb3dSCy Schubert 		if (tv == NULL)
1782b15cb3dSCy Schubert 			exit(1);
1792b15cb3dSCy Schubert 		fprintf(stdout, "%ld\n",
1802b15cb3dSCy Schubert 			tv->tv_sec * 1000000L + tv->tv_usec);
1812b15cb3dSCy Schubert 	}
1822b15cb3dSCy Schubert 
1832b15cb3dSCy Schubert #ifdef _WIN32
1842b15cb3dSCy Schubert 	WSACleanup();
1852b15cb3dSCy Schubert #endif
1862b15cb3dSCy Schubert 
1872b15cb3dSCy Schubert 	exit(0);
1882b15cb3dSCy Schubert }
189