]> arthur.barton.de Git - netatalk.git/blob - libevent/test/bench_httpclient.c
Add libevent
[netatalk.git] / libevent / test / bench_httpclient.c
1 /*
2  * Copyright 2009-2010 Niels Provos and Nick Mathewson
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
14  *
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.
25  *
26  */
27
28 #include <sys/types.h>
29 #ifdef WIN32
30 #include <winsock2.h>
31 #else
32 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #endif
35 #include <stdlib.h>
36 #include <errno.h>
37
38 #include "event2/event.h"
39 #include "event2/bufferevent.h"
40 #include "event2/buffer.h"
41 #include "event2/util.h"
42
43 /* for EVUTIL_ERR_CONNECT_RETRIABLE macro */
44 #include "util-internal.h"
45
46 const char *resource = NULL;
47 struct event_base *base = NULL;
48
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};
54 int n_errors = 0;
55
56 const int PARALLELISM = 200;
57 const int N_REQUESTS = 20000;
58
59 struct request_info {
60         size_t n_read;
61         struct timeval started;
62 };
63
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);
67
68 static void
69 readcb(struct bufferevent *b, void *arg)
70 {
71         struct request_info *ri = arg;
72         struct evbuffer *input = bufferevent_get_input(b);
73         size_t n = evbuffer_get_length(input);
74
75         ri->n_read += n;
76         evbuffer_drain(input, n);
77 }
78
79 static void
80 errorcb(struct bufferevent *b, short what, void *arg)
81 {
82         struct request_info *ri = arg;
83         struct timeval now, diff;
84         if (what & BEV_EVENT_EOF) {
85                 ++total_n_handled;
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);
90
91                 if (total_n_handled && (total_n_handled%1000)==0)
92                         printf("%d requests done\n",total_n_handled);
93
94                 if (total_n_launched < N_REQUESTS) {
95                         if (launch_request() < 0)
96                                 perror("Can't launch");
97                 }
98         } else {
99                 ++total_n_errors;
100                 perror("Unexpected error");
101         }
102
103         bufferevent_setcb(b, NULL, NULL, NULL, NULL);
104         free(ri);
105         bufferevent_disable(b, EV_READ|EV_WRITE);
106         bufferevent_free(b);
107 }
108
109 static void
110 frob_socket(evutil_socket_t sock)
111 {
112         struct linger l;
113         int one = 1;
114         setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*)&one, sizeof(one));
115         l.l_onoff = 1;
116         l.l_linger = 0;
117         if (setsockopt(sock, SOL_SOCKET, SO_LINGER, (void*)&l, sizeof(l))<0)
118                 perror("setsockopt");
119 }
120
121 static int
122 launch_request(void)
123 {
124         evutil_socket_t sock;
125         struct sockaddr_in sin;
126         struct bufferevent *b;
127
128         struct request_info *ri;
129
130         ++total_n_launched;
131
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)
136                 return -1;
137         if (evutil_make_socket_nonblocking(sock) < 0)
138                 return -1;
139         frob_socket(sock);
140         if (connect(sock, (struct sockaddr*)&sin, sizeof(sin)) < 0) {
141                 int e = errno;
142                 if (! EVUTIL_ERR_CONNECT_RETRIABLE(e)) {
143                         return -1;
144                 }
145         }
146
147         ri = malloc(sizeof(*ri));
148         ri->n_read = 0;
149         gettimeofday(&ri->started, NULL);
150
151         b = bufferevent_socket_new(base, sock, BEV_OPT_CLOSE_ON_FREE);
152
153         bufferevent_setcb(b, readcb, NULL, errorcb, ri);
154         bufferevent_enable(b, EV_READ|EV_WRITE);
155
156         evbuffer_add_printf(bufferevent_get_output(b),
157             "GET %s HTTP/1.0\r\n\r\n", resource);
158
159         return 0;
160 }
161
162
163 int
164 main(int argc, char **argv)
165 {
166         int i;
167         struct timeval start, end, total;
168         long long usec;
169         double throughput;
170         resource = "/ref";
171
172         setvbuf(stdout, NULL, _IONBF, 0);
173
174         base = event_base_new();
175
176         for (i=0; i < PARALLELISM; ++i) {
177                 if (launch_request() < 0)
178                         perror("launch");
179         }
180
181         gettimeofday(&start, NULL);
182
183         event_base_dispatch(base);
184
185         gettimeofday(&end, NULL);
186         evutil_timersub(&end, &start, &total);
187         usec = total_time.tv_sec * 1000000 + total_time.tv_usec;
188
189         if (!total_n_handled) {
190                 puts("Nothing worked.  You probably did something dumb.");
191                 return 0;
192         }
193
194
195         throughput = total_n_handled /
196             (total.tv_sec+ ((double)total.tv_usec)/1000000.0);
197
198 #ifdef WIN32
199 #define I64_FMT "%I64d"
200 #define I64_TYP __int64
201 #else
202 #define I64_FMT "%lld"
203 #define I64_TYP long long int
204 #endif
205
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",
209             total_n_handled,
210             (int)total.tv_sec, (int)total.tv_usec,
211             throughput,
212             (double)(usec/1000) / total_n_handled,
213             (I64_TYP)total_n_bytes, n_errors);
214
215         return 0;
216 }