2 * Copyright 2009-2010 Niels Provos and Nick Mathewson
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 4. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <netinet/in.h>
38 #include "event2/event.h"
39 #include "event2/bufferevent.h"
40 #include "event2/buffer.h"
41 #include "event2/util.h"
43 /* for EVUTIL_ERR_CONNECT_RETRIABLE macro */
44 #include "util-internal.h"
46 const char *resource = NULL;
47 struct event_base *base = NULL;
49 int total_n_handled = 0;
50 int total_n_errors = 0;
51 int total_n_launched = 0;
52 size_t total_n_bytes = 0;
53 struct timeval total_time = {0,0};
56 const int PARALLELISM = 200;
57 const int N_REQUESTS = 20000;
61 struct timeval started;
64 static int launch_request(void);
65 static void readcb(struct bufferevent *b, void *arg);
66 static void errorcb(struct bufferevent *b, short what, void *arg);
69 readcb(struct bufferevent *b, void *arg)
71 struct request_info *ri = arg;
72 struct evbuffer *input = bufferevent_get_input(b);
73 size_t n = evbuffer_get_length(input);
76 evbuffer_drain(input, n);
80 errorcb(struct bufferevent *b, short what, void *arg)
82 struct request_info *ri = arg;
83 struct timeval now, diff;
84 if (what & BEV_EVENT_EOF) {
86 total_n_bytes += ri->n_read;
87 gettimeofday(&now, NULL);
88 evutil_timersub(&now, &ri->started, &diff);
89 evutil_timeradd(&diff, &total_time, &total_time);
91 if (total_n_handled && (total_n_handled%1000)==0)
92 printf("%d requests done\n",total_n_handled);
94 if (total_n_launched < N_REQUESTS) {
95 if (launch_request() < 0)
96 perror("Can't launch");
100 perror("Unexpected error");
103 bufferevent_setcb(b, NULL, NULL, NULL, NULL);
105 bufferevent_disable(b, EV_READ|EV_WRITE);
110 frob_socket(evutil_socket_t sock)
114 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*)&one, sizeof(one));
117 if (setsockopt(sock, SOL_SOCKET, SO_LINGER, (void*)&l, sizeof(l))<0)
118 perror("setsockopt");
124 evutil_socket_t sock;
125 struct sockaddr_in sin;
126 struct bufferevent *b;
128 struct request_info *ri;
132 sin.sin_family = AF_INET;
133 sin.sin_addr.s_addr = htonl(0x7f000001);
134 sin.sin_port = htons(8080);
135 if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
137 if (evutil_make_socket_nonblocking(sock) < 0)
140 if (connect(sock, (struct sockaddr*)&sin, sizeof(sin)) < 0) {
142 if (! EVUTIL_ERR_CONNECT_RETRIABLE(e)) {
147 ri = malloc(sizeof(*ri));
149 gettimeofday(&ri->started, NULL);
151 b = bufferevent_socket_new(base, sock, BEV_OPT_CLOSE_ON_FREE);
153 bufferevent_setcb(b, readcb, NULL, errorcb, ri);
154 bufferevent_enable(b, EV_READ|EV_WRITE);
156 evbuffer_add_printf(bufferevent_get_output(b),
157 "GET %s HTTP/1.0\r\n\r\n", resource);
164 main(int argc, char **argv)
167 struct timeval start, end, total;
172 setvbuf(stdout, NULL, _IONBF, 0);
174 base = event_base_new();
176 for (i=0; i < PARALLELISM; ++i) {
177 if (launch_request() < 0)
181 gettimeofday(&start, NULL);
183 event_base_dispatch(base);
185 gettimeofday(&end, NULL);
186 evutil_timersub(&end, &start, &total);
187 usec = total_time.tv_sec * 1000000 + total_time.tv_usec;
189 if (!total_n_handled) {
190 puts("Nothing worked. You probably did something dumb.");
195 throughput = total_n_handled /
196 (total.tv_sec+ ((double)total.tv_usec)/1000000.0);
199 #define I64_FMT "%I64d"
200 #define I64_TYP __int64
202 #define I64_FMT "%lld"
203 #define I64_TYP long long int
206 printf("\n%d requests in %d.%06d sec. (%.2f throughput)\n"
207 "Each took about %.02f msec latency\n"
208 I64_FMT "bytes read. %d errors.\n",
210 (int)total.tv_sec, (int)total.tv_usec,
212 (double)(usec/1000) / total_n_handled,
213 (I64_TYP)total_n_bytes, n_errors);