ipc/chromium/src/third_party/libevent/test/bench_httpclient.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial