1e4f24219SNick Mathewson /*
217efc1cdSNick Mathewson  * Copyright 2009-2010 Niels Provos and Nick Mathewson
3e4f24219SNick Mathewson  *
4e4f24219SNick Mathewson  * Redistribution and use in source and binary forms, with or without
5e4f24219SNick Mathewson  * modification, are permitted provided that the following conditions
6e4f24219SNick Mathewson  * are met:
7e4f24219SNick Mathewson  * 1. Redistributions of source code must retain the above copyright
8e4f24219SNick Mathewson  *    notice, this list of conditions and the following disclaimer.
9e4f24219SNick Mathewson  * 2. Redistributions in binary form must reproduce the above copyright
10e4f24219SNick Mathewson  *    notice, this list of conditions and the following disclaimer in the
11e4f24219SNick Mathewson  *    documentation and/or other materials provided with the distribution.
12e4f24219SNick Mathewson  * 4. The name of the author may not be used to endorse or promote products
13e4f24219SNick Mathewson  *    derived from this software without specific prior written permission.
14e4f24219SNick Mathewson  *
15e4f24219SNick Mathewson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16e4f24219SNick Mathewson  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17e4f24219SNick Mathewson  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18e4f24219SNick Mathewson  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19e4f24219SNick Mathewson  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20e4f24219SNick Mathewson  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21e4f24219SNick Mathewson  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22e4f24219SNick Mathewson  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23e4f24219SNick Mathewson  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24e4f24219SNick Mathewson  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25e4f24219SNick Mathewson  *
26e4f24219SNick Mathewson  */
27e4f24219SNick Mathewson 
28e4f24219SNick Mathewson #include <sys/types.h>
291ad03264SNick Mathewson #ifdef WIN32
301ad03264SNick Mathewson #include <winsock2.h>
311ad03264SNick Mathewson #else
32e4f24219SNick Mathewson #include <sys/socket.h>
33e4f24219SNick Mathewson #include <netinet/in.h>
34*10c834c4SHarlan Stenn # ifdef _XOPEN_SOURCE_EXTENDED
35*10c834c4SHarlan Stenn #  include <arpa/inet.h>
36*10c834c4SHarlan Stenn # endif
371ad03264SNick Mathewson #endif
38e4f24219SNick Mathewson #include <stdlib.h>
39e4f24219SNick Mathewson #include <errno.h>
40e4f24219SNick Mathewson 
41c0bf63ceSEvan Jones #include "event2/event.h"
42c0bf63ceSEvan Jones #include "event2/bufferevent.h"
43c0bf63ceSEvan Jones #include "event2/buffer.h"
44c0bf63ceSEvan Jones #include "event2/util.h"
45e4f24219SNick Mathewson 
461ad03264SNick Mathewson /* for EVUTIL_ERR_CONNECT_RETRIABLE macro */
471ad03264SNick Mathewson #include "util-internal.h"
481ad03264SNick Mathewson 
49e4f24219SNick Mathewson const char *resource = NULL;
50e4f24219SNick Mathewson struct event_base *base = NULL;
51e4f24219SNick Mathewson 
52e4f24219SNick Mathewson int total_n_handled = 0;
53e4f24219SNick Mathewson int total_n_errors = 0;
54e4f24219SNick Mathewson int total_n_launched = 0;
55e4f24219SNick Mathewson size_t total_n_bytes = 0;
56e4f24219SNick Mathewson struct timeval total_time = {0,0};
57e4f24219SNick Mathewson int n_errors = 0;
58e4f24219SNick Mathewson 
59b182ed76SNick Mathewson const int PARALLELISM = 200;
60b182ed76SNick Mathewson const int N_REQUESTS = 20000;
61e4f24219SNick Mathewson 
62e4f24219SNick Mathewson struct request_info {
63e4f24219SNick Mathewson 	size_t n_read;
64e4f24219SNick Mathewson 	struct timeval started;
65e4f24219SNick Mathewson };
66e4f24219SNick Mathewson 
67e4f24219SNick Mathewson static int launch_request(void);
68e4f24219SNick Mathewson static void readcb(struct bufferevent *b, void *arg);
69e4f24219SNick Mathewson static void errorcb(struct bufferevent *b, short what, void *arg);
70e4f24219SNick Mathewson 
71e4f24219SNick Mathewson static void
72e4f24219SNick Mathewson readcb(struct bufferevent *b, void *arg)
73e4f24219SNick Mathewson {
74e4f24219SNick Mathewson 	struct request_info *ri = arg;
75e4f24219SNick Mathewson 	struct evbuffer *input = bufferevent_get_input(b);
76e4f24219SNick Mathewson 	size_t n = evbuffer_get_length(input);
77e4f24219SNick Mathewson 
78e4f24219SNick Mathewson 	ri->n_read += n;
79e4f24219SNick Mathewson 	evbuffer_drain(input, n);
80e4f24219SNick Mathewson }
81e4f24219SNick Mathewson 
82e4f24219SNick Mathewson static void
83e4f24219SNick Mathewson errorcb(struct bufferevent *b, short what, void *arg)
84e4f24219SNick Mathewson {
85e4f24219SNick Mathewson 	struct request_info *ri = arg;
86e4f24219SNick Mathewson 	struct timeval now, diff;
8783f46e51SNick Mathewson 	if (what & BEV_EVENT_EOF) {
88e4f24219SNick Mathewson 		++total_n_handled;
89e4f24219SNick Mathewson 		total_n_bytes += ri->n_read;
90e4f24219SNick Mathewson 		gettimeofday(&now, NULL);
91e4f24219SNick Mathewson 		evutil_timersub(&now, &ri->started, &diff);
92e4f24219SNick Mathewson 		evutil_timeradd(&diff, &total_time, &total_time);
93e4f24219SNick Mathewson 
94e4f24219SNick Mathewson 		if (total_n_handled && (total_n_handled%1000)==0)
95e4f24219SNick Mathewson 			printf("%d requests done\n",total_n_handled);
96e4f24219SNick Mathewson 
97e4f24219SNick Mathewson 		if (total_n_launched < N_REQUESTS) {
98e4f24219SNick Mathewson 			if (launch_request() < 0)
99e4f24219SNick Mathewson 				perror("Can't launch");
100e4f24219SNick Mathewson 		}
101e4f24219SNick Mathewson 	} else {
102e4f24219SNick Mathewson 		++total_n_errors;
103e4f24219SNick Mathewson 		perror("Unexpected error");
104e4f24219SNick Mathewson 	}
105e4f24219SNick Mathewson 
106e4f24219SNick Mathewson 	bufferevent_setcb(b, NULL, NULL, NULL, NULL);
107e4f24219SNick Mathewson 	free(ri);
108e4f24219SNick Mathewson 	bufferevent_disable(b, EV_READ|EV_WRITE);
109e4f24219SNick Mathewson 	bufferevent_free(b);
110e4f24219SNick Mathewson }
111e4f24219SNick Mathewson 
112b182ed76SNick Mathewson static void
113c7cf6f00SNick Mathewson frob_socket(evutil_socket_t sock)
114b182ed76SNick Mathewson {
115b182ed76SNick Mathewson 	struct linger l;
116b182ed76SNick Mathewson 	int one = 1;
1171ad03264SNick Mathewson 	setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*)&one, sizeof(one));
118b182ed76SNick Mathewson 	l.l_onoff = 1;
119b182ed76SNick Mathewson 	l.l_linger = 0;
1201ad03264SNick Mathewson 	if (setsockopt(sock, SOL_SOCKET, SO_LINGER, (void*)&l, sizeof(l))<0)
121b182ed76SNick Mathewson 		perror("setsockopt");
122b182ed76SNick Mathewson }
123b182ed76SNick Mathewson 
124e4f24219SNick Mathewson static int
125e4f24219SNick Mathewson launch_request(void)
126e4f24219SNick Mathewson {
127c7cf6f00SNick Mathewson 	evutil_socket_t sock;
128e4f24219SNick Mathewson 	struct sockaddr_in sin;
129e4f24219SNick Mathewson 	struct bufferevent *b;
130e4f24219SNick Mathewson 
131e4f24219SNick Mathewson 	struct request_info *ri;
132e4f24219SNick Mathewson 
133e4f24219SNick Mathewson 	++total_n_launched;
134e4f24219SNick Mathewson 
135e4f24219SNick Mathewson 	sin.sin_family = AF_INET;
136e4f24219SNick Mathewson 	sin.sin_addr.s_addr = htonl(0x7f000001);
137e4f24219SNick Mathewson 	sin.sin_port = htons(8080);
138e4f24219SNick Mathewson 	if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
139e4f24219SNick Mathewson 		return -1;
140e4f24219SNick Mathewson 	if (evutil_make_socket_nonblocking(sock) < 0)
141e4f24219SNick Mathewson 		return -1;
142b182ed76SNick Mathewson 	frob_socket(sock);
143e4f24219SNick Mathewson 	if (connect(sock, (struct sockaddr*)&sin, sizeof(sin)) < 0) {
1441ad03264SNick Mathewson 		int e = errno;
1451ad03264SNick Mathewson 		if (! EVUTIL_ERR_CONNECT_RETRIABLE(e)) {
146e4f24219SNick Mathewson 			return -1;
147e4f24219SNick Mathewson 		}
148e4f24219SNick Mathewson 	}
149e4f24219SNick Mathewson 
150e4f24219SNick Mathewson 	ri = malloc(sizeof(*ri));
151e4f24219SNick Mathewson 	ri->n_read = 0;
152e4f24219SNick Mathewson 	gettimeofday(&ri->started, NULL);
153e4f24219SNick Mathewson 
154e4f24219SNick Mathewson 	b = bufferevent_socket_new(base, sock, BEV_OPT_CLOSE_ON_FREE);
155e4f24219SNick Mathewson 
156e4f24219SNick Mathewson 	bufferevent_setcb(b, readcb, NULL, errorcb, ri);
157e4f24219SNick Mathewson 	bufferevent_enable(b, EV_READ|EV_WRITE);
158e4f24219SNick Mathewson 
159e4f24219SNick Mathewson 	evbuffer_add_printf(bufferevent_get_output(b),
160e4f24219SNick Mathewson 	    "GET %s HTTP/1.0\r\n\r\n", resource);
161e4f24219SNick Mathewson 
162e4f24219SNick Mathewson 	return 0;
163e4f24219SNick Mathewson }
164e4f24219SNick Mathewson 
165e4f24219SNick Mathewson 
166e4f24219SNick Mathewson int
167e4f24219SNick Mathewson main(int argc, char **argv)
168e4f24219SNick Mathewson {
169e4f24219SNick Mathewson 	int i;
170e4f24219SNick Mathewson 	struct timeval start, end, total;
171e4f24219SNick Mathewson 	long long usec;
172b182ed76SNick Mathewson 	double throughput;
173e4f24219SNick Mathewson 	resource = "/ref";
174e4f24219SNick Mathewson 
175e4f24219SNick Mathewson 	setvbuf(stdout, NULL, _IONBF, 0);
176e4f24219SNick Mathewson 
177e4f24219SNick Mathewson 	base = event_base_new();
178e4f24219SNick Mathewson 
179e4f24219SNick Mathewson 	for (i=0; i < PARALLELISM; ++i) {
180e4f24219SNick Mathewson 		if (launch_request() < 0)
181e4f24219SNick Mathewson 			perror("launch");
182e4f24219SNick Mathewson 	}
183e4f24219SNick Mathewson 
184e4f24219SNick Mathewson 	gettimeofday(&start, NULL);
185e4f24219SNick Mathewson 
186e4f24219SNick Mathewson 	event_base_dispatch(base);
187e4f24219SNick Mathewson 
188e4f24219SNick Mathewson 	gettimeofday(&end, NULL);
189e4f24219SNick Mathewson 	evutil_timersub(&end, &start, &total);
190e4f24219SNick Mathewson 	usec = total_time.tv_sec * 1000000 + total_time.tv_usec;
191e4f24219SNick Mathewson 
192b182ed76SNick Mathewson 	if (!total_n_handled) {
193b182ed76SNick Mathewson 		puts("Nothing worked.  You probably did something dumb.");
194b182ed76SNick Mathewson 		return 0;
195b182ed76SNick Mathewson 	}
196b182ed76SNick Mathewson 
197b182ed76SNick Mathewson 
198b182ed76SNick Mathewson 	throughput = total_n_handled /
199b182ed76SNick Mathewson 	    (total.tv_sec+ ((double)total.tv_usec)/1000000.0);
200b182ed76SNick Mathewson 
2015b7a3706SNick Mathewson #ifdef WIN32
2025b7a3706SNick Mathewson #define I64_FMT "%I64d"
2035b7a3706SNick Mathewson #define I64_TYP __int64
2045b7a3706SNick Mathewson #else
2055b7a3706SNick Mathewson #define I64_FMT "%lld"
2065b7a3706SNick Mathewson #define I64_TYP long long int
2075b7a3706SNick Mathewson #endif
2085b7a3706SNick Mathewson 
209b182ed76SNick Mathewson 	printf("\n%d requests in %d.%06d sec. (%.2f throughput)\n"
210b182ed76SNick Mathewson 	    "Each took about %.02f msec latency\n"
2115b7a3706SNick Mathewson 	    I64_FMT "bytes read. %d errors.\n",
212e4f24219SNick Mathewson 	    total_n_handled,
2131bb8e010SNick Mathewson 	    (int)total.tv_sec, (int)total.tv_usec,
214b182ed76SNick Mathewson 	    throughput,
215b182ed76SNick Mathewson 	    (double)(usec/1000) / total_n_handled,
2165b7a3706SNick Mathewson 	    (I64_TYP)total_n_bytes, n_errors);
217e4f24219SNick Mathewson 
218e4f24219SNick Mathewson 	return 0;
219e4f24219SNick Mathewson }
220