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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ipc/chromium/src/third_party/libevent/test/bench_httpclient.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,223 @@
     1.4 +/*
     1.5 + * Copyright 2009-2012 Niels Provos and Nick Mathewson
     1.6 + *
     1.7 + * Redistribution and use in source and binary forms, with or without
     1.8 + * modification, are permitted provided that the following conditions
     1.9 + * are met:
    1.10 + * 1. Redistributions of source code must retain the above copyright
    1.11 + *    notice, this list of conditions and the following disclaimer.
    1.12 + * 2. Redistributions in binary form must reproduce the above copyright
    1.13 + *    notice, this list of conditions and the following disclaimer in the
    1.14 + *    documentation and/or other materials provided with the distribution.
    1.15 + * 4. The name of the author may not be used to endorse or promote products
    1.16 + *    derived from this software without specific prior written permission.
    1.17 + *
    1.18 + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
    1.19 + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
    1.20 + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
    1.21 + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
    1.22 + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
    1.23 + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    1.24 + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    1.25 + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    1.26 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
    1.27 + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.28 + *
    1.29 + */
    1.30 +
    1.31 +#include <sys/types.h>
    1.32 +#ifdef WIN32
    1.33 +#include <winsock2.h>
    1.34 +#else
    1.35 +#include <sys/socket.h>
    1.36 +#include <netinet/in.h>
    1.37 +# ifdef _XOPEN_SOURCE_EXTENDED
    1.38 +#  include <arpa/inet.h>
    1.39 +# endif
    1.40 +#endif
    1.41 +#include <stdlib.h>
    1.42 +#include <string.h>
    1.43 +#include <errno.h>
    1.44 +
    1.45 +#include "event2/event.h"
    1.46 +#include "event2/bufferevent.h"
    1.47 +#include "event2/buffer.h"
    1.48 +#include "event2/util.h"
    1.49 +
    1.50 +/* for EVUTIL_ERR_CONNECT_RETRIABLE macro */
    1.51 +#include "util-internal.h"
    1.52 +
    1.53 +const char *resource = NULL;
    1.54 +struct event_base *base = NULL;
    1.55 +
    1.56 +int total_n_handled = 0;
    1.57 +int total_n_errors = 0;
    1.58 +int total_n_launched = 0;
    1.59 +size_t total_n_bytes = 0;
    1.60 +struct timeval total_time = {0,0};
    1.61 +int n_errors = 0;
    1.62 +
    1.63 +const int PARALLELISM = 200;
    1.64 +const int N_REQUESTS = 20000;
    1.65 +
    1.66 +struct request_info {
    1.67 +	size_t n_read;
    1.68 +	struct timeval started;
    1.69 +};
    1.70 +
    1.71 +static int launch_request(void);
    1.72 +static void readcb(struct bufferevent *b, void *arg);
    1.73 +static void errorcb(struct bufferevent *b, short what, void *arg);
    1.74 +
    1.75 +static void
    1.76 +readcb(struct bufferevent *b, void *arg)
    1.77 +{
    1.78 +	struct request_info *ri = arg;
    1.79 +	struct evbuffer *input = bufferevent_get_input(b);
    1.80 +	size_t n = evbuffer_get_length(input);
    1.81 +
    1.82 +	ri->n_read += n;
    1.83 +	evbuffer_drain(input, n);
    1.84 +}
    1.85 +
    1.86 +static void
    1.87 +errorcb(struct bufferevent *b, short what, void *arg)
    1.88 +{
    1.89 +	struct request_info *ri = arg;
    1.90 +	struct timeval now, diff;
    1.91 +	if (what & BEV_EVENT_EOF) {
    1.92 +		++total_n_handled;
    1.93 +		total_n_bytes += ri->n_read;
    1.94 +		evutil_gettimeofday(&now, NULL);
    1.95 +		evutil_timersub(&now, &ri->started, &diff);
    1.96 +		evutil_timeradd(&diff, &total_time, &total_time);
    1.97 +
    1.98 +		if (total_n_handled && (total_n_handled%1000)==0)
    1.99 +			printf("%d requests done\n",total_n_handled);
   1.100 +
   1.101 +		if (total_n_launched < N_REQUESTS) {
   1.102 +			if (launch_request() < 0)
   1.103 +				perror("Can't launch");
   1.104 +		}
   1.105 +	} else {
   1.106 +		++total_n_errors;
   1.107 +		perror("Unexpected error");
   1.108 +	}
   1.109 +
   1.110 +	bufferevent_setcb(b, NULL, NULL, NULL, NULL);
   1.111 +	free(ri);
   1.112 +	bufferevent_disable(b, EV_READ|EV_WRITE);
   1.113 +	bufferevent_free(b);
   1.114 +}
   1.115 +
   1.116 +static void
   1.117 +frob_socket(evutil_socket_t sock)
   1.118 +{
   1.119 +	struct linger l;
   1.120 +	int one = 1;
   1.121 +	if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*)&one, sizeof(one))<0)
   1.122 +		perror("setsockopt(SO_REUSEADDR)");
   1.123 +	l.l_onoff = 1;
   1.124 +	l.l_linger = 0;
   1.125 +	if (setsockopt(sock, SOL_SOCKET, SO_LINGER, (void*)&l, sizeof(l))<0)
   1.126 +		perror("setsockopt(SO_LINGER)");
   1.127 +}
   1.128 +
   1.129 +static int
   1.130 +launch_request(void)
   1.131 +{
   1.132 +	evutil_socket_t sock;
   1.133 +	struct sockaddr_in sin;
   1.134 +	struct bufferevent *b;
   1.135 +
   1.136 +	struct request_info *ri;
   1.137 +
   1.138 +	memset(&sin, 0, sizeof(sin));
   1.139 +
   1.140 +	++total_n_launched;
   1.141 +
   1.142 +	sin.sin_family = AF_INET;
   1.143 +	sin.sin_addr.s_addr = htonl(0x7f000001);
   1.144 +	sin.sin_port = htons(8080);
   1.145 +	if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
   1.146 +		return -1;
   1.147 +	if (evutil_make_socket_nonblocking(sock) < 0)
   1.148 +		return -1;
   1.149 +	frob_socket(sock);
   1.150 +	if (connect(sock, (struct sockaddr*)&sin, sizeof(sin)) < 0) {
   1.151 +		int e = errno;
   1.152 +		if (! EVUTIL_ERR_CONNECT_RETRIABLE(e)) {
   1.153 +			return -1;
   1.154 +		}
   1.155 +	}
   1.156 +
   1.157 +	ri = malloc(sizeof(*ri));
   1.158 +	ri->n_read = 0;
   1.159 +	evutil_gettimeofday(&ri->started, NULL);
   1.160 +
   1.161 +	b = bufferevent_socket_new(base, sock, BEV_OPT_CLOSE_ON_FREE);
   1.162 +
   1.163 +	bufferevent_setcb(b, readcb, NULL, errorcb, ri);
   1.164 +	bufferevent_enable(b, EV_READ|EV_WRITE);
   1.165 +
   1.166 +	evbuffer_add_printf(bufferevent_get_output(b),
   1.167 +	    "GET %s HTTP/1.0\r\n\r\n", resource);
   1.168 +
   1.169 +	return 0;
   1.170 +}
   1.171 +
   1.172 +
   1.173 +int
   1.174 +main(int argc, char **argv)
   1.175 +{
   1.176 +	int i;
   1.177 +	struct timeval start, end, total;
   1.178 +	long long usec;
   1.179 +	double throughput;
   1.180 +	resource = "/ref";
   1.181 +
   1.182 +	setvbuf(stdout, NULL, _IONBF, 0);
   1.183 +
   1.184 +	base = event_base_new();
   1.185 +
   1.186 +	for (i=0; i < PARALLELISM; ++i) {
   1.187 +		if (launch_request() < 0)
   1.188 +			perror("launch");
   1.189 +	}
   1.190 +
   1.191 +	evutil_gettimeofday(&start, NULL);
   1.192 +
   1.193 +	event_base_dispatch(base);
   1.194 +
   1.195 +	evutil_gettimeofday(&end, NULL);
   1.196 +	evutil_timersub(&end, &start, &total);
   1.197 +	usec = total_time.tv_sec * 1000000 + total_time.tv_usec;
   1.198 +
   1.199 +	if (!total_n_handled) {
   1.200 +		puts("Nothing worked.  You probably did something dumb.");
   1.201 +		return 0;
   1.202 +	}
   1.203 +
   1.204 +
   1.205 +	throughput = total_n_handled /
   1.206 +	    (total.tv_sec+ ((double)total.tv_usec)/1000000.0);
   1.207 +
   1.208 +#ifdef WIN32
   1.209 +#define I64_FMT "%I64d"
   1.210 +#define I64_TYP __int64
   1.211 +#else
   1.212 +#define I64_FMT "%lld"
   1.213 +#define I64_TYP long long int
   1.214 +#endif
   1.215 +
   1.216 +	printf("\n%d requests in %d.%06d sec. (%.2f throughput)\n"
   1.217 +	    "Each took about %.02f msec latency\n"
   1.218 +	    I64_FMT "bytes read. %d errors.\n",
   1.219 +	    total_n_handled,
   1.220 +	    (int)total.tv_sec, (int)total.tv_usec,
   1.221 +	    throughput,
   1.222 +	    (double)(usec/1000) / total_n_handled,
   1.223 +	    (I64_TYP)total_n_bytes, n_errors);
   1.224 +
   1.225 +	return 0;
   1.226 +}

mercurial