2 * Copyright 2009-2012 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>
34 # ifdef _XOPEN_SOURCE_EXTENDED
35 # include <arpa/inet.h>
42 #include "event2/event.h"
43 #include "event2/bufferevent.h"
44 #include "event2/buffer.h"
45 #include "event2/util.h"
47 /* for EVUTIL_ERR_CONNECT_RETRIABLE macro */
48 #include "util-internal.h"
50 const char *resource
= NULL
;
51 struct event_base
*base
= NULL
;
53 int total_n_handled
= 0;
54 int total_n_errors
= 0;
55 int total_n_launched
= 0;
56 size_t total_n_bytes
= 0;
57 struct timeval total_time
= {0,0};
60 const int PARALLELISM
= 200;
61 const int N_REQUESTS
= 20000;
65 struct timeval started
;
68 static int launch_request(void);
69 static void readcb(struct bufferevent
*b
, void *arg
);
70 static void errorcb(struct bufferevent
*b
, short what
, void *arg
);
73 readcb(struct bufferevent
*b
, void *arg
)
75 struct request_info
*ri
= arg
;
76 struct evbuffer
*input
= bufferevent_get_input(b
);
77 size_t n
= evbuffer_get_length(input
);
80 evbuffer_drain(input
, n
);
84 errorcb(struct bufferevent
*b
, short what
, void *arg
)
86 struct request_info
*ri
= arg
;
87 struct timeval now
, diff
;
88 if (what
& BEV_EVENT_EOF
) {
90 total_n_bytes
+= ri
->n_read
;
91 evutil_gettimeofday(&now
, NULL
);
92 evutil_timersub(&now
, &ri
->started
, &diff
);
93 evutil_timeradd(&diff
, &total_time
, &total_time
);
95 if (total_n_handled
&& (total_n_handled
%1000)==0)
96 printf("%d requests done\n",total_n_handled
);
98 if (total_n_launched
< N_REQUESTS
) {
99 if (launch_request() < 0)
100 perror("Can't launch");
104 perror("Unexpected error");
107 bufferevent_setcb(b
, NULL
, NULL
, NULL
, NULL
);
109 bufferevent_disable(b
, EV_READ
|EV_WRITE
);
114 frob_socket(evutil_socket_t sock
)
118 setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
, (void*)&one
, sizeof(one
));
121 if (setsockopt(sock
, SOL_SOCKET
, SO_LINGER
, (void*)&l
, sizeof(l
))<0)
122 perror("setsockopt");
128 evutil_socket_t sock
;
129 struct sockaddr_in sin
;
130 struct bufferevent
*b
;
132 struct request_info
*ri
;
134 memset(&sin
, 0, sizeof(sin
));
138 sin
.sin_family
= AF_INET
;
139 sin
.sin_addr
.s_addr
= htonl(0x7f000001);
140 sin
.sin_port
= htons(8080);
141 if ((sock
= socket(AF_INET
, SOCK_STREAM
, 0)) < 0)
143 if (evutil_make_socket_nonblocking(sock
) < 0)
146 if (connect(sock
, (struct sockaddr
*)&sin
, sizeof(sin
)) < 0) {
148 if (! EVUTIL_ERR_CONNECT_RETRIABLE(e
)) {
153 ri
= malloc(sizeof(*ri
));
155 evutil_gettimeofday(&ri
->started
, NULL
);
157 b
= bufferevent_socket_new(base
, sock
, BEV_OPT_CLOSE_ON_FREE
);
159 bufferevent_setcb(b
, readcb
, NULL
, errorcb
, ri
);
160 bufferevent_enable(b
, EV_READ
|EV_WRITE
);
162 evbuffer_add_printf(bufferevent_get_output(b
),
163 "GET %s HTTP/1.0\r\n\r\n", resource
);
170 main(int argc
, char **argv
)
173 struct timeval start
, end
, total
;
178 setvbuf(stdout
, NULL
, _IONBF
, 0);
180 base
= event_base_new();
182 for (i
=0; i
< PARALLELISM
; ++i
) {
183 if (launch_request() < 0)
187 evutil_gettimeofday(&start
, NULL
);
189 event_base_dispatch(base
);
191 evutil_gettimeofday(&end
, NULL
);
192 evutil_timersub(&end
, &start
, &total
);
193 usec
= total_time
.tv_sec
* 1000000 + total_time
.tv_usec
;
195 if (!total_n_handled
) {
196 puts("Nothing worked. You probably did something dumb.");
201 throughput
= total_n_handled
/
202 (total
.tv_sec
+ ((double)total
.tv_usec
)/1000000.0);
205 #define I64_FMT "%I64d"
206 #define I64_TYP __int64
208 #define I64_FMT "%lld"
209 #define I64_TYP long long int
212 printf("\n%d requests in %d.%06d sec. (%.2f throughput)\n"
213 "Each took about %.02f msec latency\n"
214 I64_FMT
"bytes read. %d errors.\n",
216 (int)total
.tv_sec
, (int)total
.tv_usec
,
218 (double)(usec
/1000) / total_n_handled
,
219 (I64_TYP
)total_n_bytes
, n_errors
);