12b15cb3dSCy Schubert /*
22b15cb3dSCy Schubert  * Copyright 2009-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 /* for EVUTIL_ERR_CONNECT_RETRIABLE macro */
292b15cb3dSCy Schubert #include "util-internal.h"
302b15cb3dSCy Schubert 
312b15cb3dSCy Schubert #include <sys/types.h>
322b15cb3dSCy Schubert #ifdef _WIN32
332b15cb3dSCy Schubert #include <winsock2.h>
342b15cb3dSCy Schubert #else
352b15cb3dSCy Schubert #include <sys/socket.h>
362b15cb3dSCy Schubert #include <netinet/in.h>
372b15cb3dSCy Schubert # ifdef _XOPEN_SOURCE_EXTENDED
382b15cb3dSCy Schubert #  include <arpa/inet.h>
392b15cb3dSCy Schubert # endif
402b15cb3dSCy Schubert #endif
412b15cb3dSCy Schubert #include <stdlib.h>
422b15cb3dSCy Schubert #include <string.h>
432b15cb3dSCy Schubert #include <errno.h>
442b15cb3dSCy Schubert 
452b15cb3dSCy Schubert #include "event2/event.h"
462b15cb3dSCy Schubert #include "event2/bufferevent.h"
472b15cb3dSCy Schubert #include "event2/buffer.h"
482b15cb3dSCy Schubert #include "event2/util.h"
492b15cb3dSCy Schubert 
502b15cb3dSCy Schubert const char *resource = NULL;
512b15cb3dSCy Schubert struct event_base *base = NULL;
522b15cb3dSCy Schubert 
532b15cb3dSCy Schubert int total_n_handled = 0;
542b15cb3dSCy Schubert int total_n_errors = 0;
552b15cb3dSCy Schubert int total_n_launched = 0;
562b15cb3dSCy Schubert size_t total_n_bytes = 0;
572b15cb3dSCy Schubert struct timeval total_time = {0,0};
582b15cb3dSCy Schubert int n_errors = 0;
592b15cb3dSCy Schubert 
602b15cb3dSCy Schubert const int PARALLELISM = 200;
612b15cb3dSCy Schubert const int N_REQUESTS = 20000;
622b15cb3dSCy Schubert 
632b15cb3dSCy Schubert struct request_info {
642b15cb3dSCy Schubert 	size_t n_read;
652b15cb3dSCy Schubert 	struct timeval started;
662b15cb3dSCy Schubert };
672b15cb3dSCy Schubert 
682b15cb3dSCy Schubert static int launch_request(void);
692b15cb3dSCy Schubert static void readcb(struct bufferevent *b, void *arg);
702b15cb3dSCy Schubert static void errorcb(struct bufferevent *b, short what, void *arg);
712b15cb3dSCy Schubert 
722b15cb3dSCy Schubert static void
readcb(struct bufferevent * b,void * arg)732b15cb3dSCy Schubert readcb(struct bufferevent *b, void *arg)
742b15cb3dSCy Schubert {
752b15cb3dSCy Schubert 	struct request_info *ri = arg;
762b15cb3dSCy Schubert 	struct evbuffer *input = bufferevent_get_input(b);
772b15cb3dSCy Schubert 	size_t n = evbuffer_get_length(input);
782b15cb3dSCy Schubert 
792b15cb3dSCy Schubert 	ri->n_read += n;
802b15cb3dSCy Schubert 	evbuffer_drain(input, n);
812b15cb3dSCy Schubert }
822b15cb3dSCy Schubert 
832b15cb3dSCy Schubert static void
errorcb(struct bufferevent * b,short what,void * arg)842b15cb3dSCy Schubert errorcb(struct bufferevent *b, short what, void *arg)
852b15cb3dSCy Schubert {
862b15cb3dSCy Schubert 	struct request_info *ri = arg;
872b15cb3dSCy Schubert 	struct timeval now, diff;
882b15cb3dSCy Schubert 	if (what & BEV_EVENT_EOF) {
892b15cb3dSCy Schubert 		++total_n_handled;
902b15cb3dSCy Schubert 		total_n_bytes += ri->n_read;
912b15cb3dSCy Schubert 		evutil_gettimeofday(&now, NULL);
922b15cb3dSCy Schubert 		evutil_timersub(&now, &ri->started, &diff);
932b15cb3dSCy Schubert 		evutil_timeradd(&diff, &total_time, &total_time);
942b15cb3dSCy Schubert 
952b15cb3dSCy Schubert 		if (total_n_handled && (total_n_handled%1000)==0)
962b15cb3dSCy Schubert 			printf("%d requests done\n",total_n_handled);
972b15cb3dSCy Schubert 
982b15cb3dSCy Schubert 		if (total_n_launched < N_REQUESTS) {
992b15cb3dSCy Schubert 			if (launch_request() < 0)
1002b15cb3dSCy Schubert 				perror("Can't launch");
1012b15cb3dSCy Schubert 		}
1022b15cb3dSCy Schubert 	} else {
1032b15cb3dSCy Schubert 		++total_n_errors;
1042b15cb3dSCy Schubert 		perror("Unexpected error");
1052b15cb3dSCy Schubert 	}
1062b15cb3dSCy Schubert 
1072b15cb3dSCy Schubert 	bufferevent_setcb(b, NULL, NULL, NULL, NULL);
1082b15cb3dSCy Schubert 	free(ri);
1092b15cb3dSCy Schubert 	bufferevent_disable(b, EV_READ|EV_WRITE);
1102b15cb3dSCy Schubert 	bufferevent_free(b);
1112b15cb3dSCy Schubert }
1122b15cb3dSCy Schubert 
1132b15cb3dSCy Schubert static void
frob_socket(evutil_socket_t sock)1142b15cb3dSCy Schubert frob_socket(evutil_socket_t sock)
1152b15cb3dSCy Schubert {
116*a466cc55SCy Schubert #ifdef EVENT__HAVE_STRUCT_LINGER
1172b15cb3dSCy Schubert 	struct linger l;
1182b15cb3dSCy Schubert #endif
1192b15cb3dSCy Schubert 	int one = 1;
1202b15cb3dSCy Schubert 	if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*)&one, sizeof(one))<0)
1212b15cb3dSCy Schubert 		perror("setsockopt(SO_REUSEADDR)");
122*a466cc55SCy Schubert #ifdef EVENT__HAVE_STRUCT_LINGER
1232b15cb3dSCy Schubert 	l.l_onoff = 1;
1242b15cb3dSCy Schubert 	l.l_linger = 0;
1252b15cb3dSCy Schubert 	if (setsockopt(sock, SOL_SOCKET, SO_LINGER, (void*)&l, sizeof(l))<0)
1262b15cb3dSCy Schubert 		perror("setsockopt(SO_LINGER)");
1272b15cb3dSCy Schubert #endif
1282b15cb3dSCy Schubert }
1292b15cb3dSCy Schubert 
1302b15cb3dSCy Schubert static int
launch_request(void)1312b15cb3dSCy Schubert launch_request(void)
1322b15cb3dSCy Schubert {
1332b15cb3dSCy Schubert 	evutil_socket_t sock;
1342b15cb3dSCy Schubert 	struct sockaddr_in sin;
1352b15cb3dSCy Schubert 	struct bufferevent *b;
1362b15cb3dSCy Schubert 
1372b15cb3dSCy Schubert 	struct request_info *ri;
1382b15cb3dSCy Schubert 
1392b15cb3dSCy Schubert 	memset(&sin, 0, sizeof(sin));
1402b15cb3dSCy Schubert 
1412b15cb3dSCy Schubert 	++total_n_launched;
1422b15cb3dSCy Schubert 
1432b15cb3dSCy Schubert 	sin.sin_family = AF_INET;
1442b15cb3dSCy Schubert 	sin.sin_addr.s_addr = htonl(0x7f000001);
1452b15cb3dSCy Schubert 	sin.sin_port = htons(8080);
1462b15cb3dSCy Schubert 	if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
1472b15cb3dSCy Schubert 		return -1;
1482b15cb3dSCy Schubert 	if (evutil_make_socket_nonblocking(sock) < 0) {
1492b15cb3dSCy Schubert 		evutil_closesocket(sock);
1502b15cb3dSCy Schubert 		return -1;
1512b15cb3dSCy Schubert 	}
1522b15cb3dSCy Schubert 	frob_socket(sock);
1532b15cb3dSCy Schubert 	if (connect(sock, (struct sockaddr*)&sin, sizeof(sin)) < 0) {
154*a466cc55SCy Schubert 		int e = evutil_socket_geterror(sock);
1552b15cb3dSCy Schubert 		if (! EVUTIL_ERR_CONNECT_RETRIABLE(e)) {
1562b15cb3dSCy Schubert 			evutil_closesocket(sock);
1572b15cb3dSCy Schubert 			return -1;
1582b15cb3dSCy Schubert 		}
1592b15cb3dSCy Schubert 	}
1602b15cb3dSCy Schubert 
1612b15cb3dSCy Schubert 	ri = malloc(sizeof(*ri));
1622b15cb3dSCy Schubert 	ri->n_read = 0;
1632b15cb3dSCy Schubert 	evutil_gettimeofday(&ri->started, NULL);
1642b15cb3dSCy Schubert 
1652b15cb3dSCy Schubert 	b = bufferevent_socket_new(base, sock, BEV_OPT_CLOSE_ON_FREE);
1662b15cb3dSCy Schubert 
1672b15cb3dSCy Schubert 	bufferevent_setcb(b, readcb, NULL, errorcb, ri);
1682b15cb3dSCy Schubert 	bufferevent_enable(b, EV_READ|EV_WRITE);
1692b15cb3dSCy Schubert 
1702b15cb3dSCy Schubert 	evbuffer_add_printf(bufferevent_get_output(b),
1712b15cb3dSCy Schubert 	    "GET %s HTTP/1.0\r\n\r\n", resource);
1722b15cb3dSCy Schubert 
1732b15cb3dSCy Schubert 	return 0;
1742b15cb3dSCy Schubert }
1752b15cb3dSCy Schubert 
1762b15cb3dSCy Schubert 
1772b15cb3dSCy Schubert int
main(int argc,char ** argv)1782b15cb3dSCy Schubert main(int argc, char **argv)
1792b15cb3dSCy Schubert {
1802b15cb3dSCy Schubert 	int i;
1812b15cb3dSCy Schubert 	struct timeval start, end, total;
1822b15cb3dSCy Schubert 	long long usec;
1832b15cb3dSCy Schubert 	double throughput;
184*a466cc55SCy Schubert 
185*a466cc55SCy Schubert #ifdef _WIN32
186*a466cc55SCy Schubert 	WSADATA WSAData;
187*a466cc55SCy Schubert 	WSAStartup(0x101, &WSAData);
188*a466cc55SCy Schubert #endif
189*a466cc55SCy Schubert 
1902b15cb3dSCy Schubert 	resource = "/ref";
1912b15cb3dSCy Schubert 
1922b15cb3dSCy Schubert 	setvbuf(stdout, NULL, _IONBF, 0);
1932b15cb3dSCy Schubert 
1942b15cb3dSCy Schubert 	base = event_base_new();
1952b15cb3dSCy Schubert 
1962b15cb3dSCy Schubert 	for (i=0; i < PARALLELISM; ++i) {
1972b15cb3dSCy Schubert 		if (launch_request() < 0)
1982b15cb3dSCy Schubert 			perror("launch");
1992b15cb3dSCy Schubert 	}
2002b15cb3dSCy Schubert 
2012b15cb3dSCy Schubert 	evutil_gettimeofday(&start, NULL);
2022b15cb3dSCy Schubert 
2032b15cb3dSCy Schubert 	event_base_dispatch(base);
2042b15cb3dSCy Schubert 
2052b15cb3dSCy Schubert 	evutil_gettimeofday(&end, NULL);
2062b15cb3dSCy Schubert 	evutil_timersub(&end, &start, &total);
2072b15cb3dSCy Schubert 	usec = total_time.tv_sec * (long long)1000000 + total_time.tv_usec;
2082b15cb3dSCy Schubert 
2092b15cb3dSCy Schubert 	if (!total_n_handled) {
2102b15cb3dSCy Schubert 		puts("Nothing worked.  You probably did something dumb.");
2112b15cb3dSCy Schubert 		return 0;
2122b15cb3dSCy Schubert 	}
2132b15cb3dSCy Schubert 
2142b15cb3dSCy Schubert 
2152b15cb3dSCy Schubert 	throughput = total_n_handled /
2162b15cb3dSCy Schubert 	    (total.tv_sec+ ((double)total.tv_usec)/1000000.0);
2172b15cb3dSCy Schubert 
2182b15cb3dSCy Schubert #ifdef _WIN32
2192b15cb3dSCy Schubert #define I64_FMT "%I64d"
2202b15cb3dSCy Schubert #define I64_TYP __int64
2212b15cb3dSCy Schubert #else
2222b15cb3dSCy Schubert #define I64_FMT "%lld"
2232b15cb3dSCy Schubert #define I64_TYP long long int
2242b15cb3dSCy Schubert #endif
2252b15cb3dSCy Schubert 
2262b15cb3dSCy Schubert 	printf("\n%d requests in %d.%06d sec. (%.2f throughput)\n"
2272b15cb3dSCy Schubert 	    "Each took about %.02f msec latency\n"
2282b15cb3dSCy Schubert 	    I64_FMT "bytes read. %d errors.\n",
2292b15cb3dSCy Schubert 	    total_n_handled,
2302b15cb3dSCy Schubert 	    (int)total.tv_sec, (int)total.tv_usec,
2312b15cb3dSCy Schubert 	    throughput,
2322b15cb3dSCy Schubert 	    (double)(usec/1000) / total_n_handled,
2332b15cb3dSCy Schubert 	    (I64_TYP)total_n_bytes, n_errors);
2342b15cb3dSCy Schubert 
235*a466cc55SCy Schubert #ifdef _WIN32
236*a466cc55SCy Schubert 	WSACleanup();
237*a466cc55SCy Schubert #endif
238*a466cc55SCy Schubert 
2392b15cb3dSCy Schubert 	return 0;
2402b15cb3dSCy Schubert }
241