michael@0: /* michael@0: * Copyright 2009-2012 Niels Provos and Nick Mathewson michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * 1. Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * 2. Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in the michael@0: * documentation and/or other materials provided with the distribution. michael@0: * 4. The name of the author may not be used to endorse or promote products michael@0: * derived from this software without specific prior written permission. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR michael@0: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES michael@0: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. michael@0: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, michael@0: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT michael@0: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF michael@0: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: * michael@0: */ michael@0: michael@0: #include michael@0: #ifdef WIN32 michael@0: #include michael@0: #else michael@0: #include michael@0: #include michael@0: # ifdef _XOPEN_SOURCE_EXTENDED michael@0: # include michael@0: # endif michael@0: #endif michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include "event2/event.h" michael@0: #include "event2/bufferevent.h" michael@0: #include "event2/buffer.h" michael@0: #include "event2/util.h" michael@0: michael@0: /* for EVUTIL_ERR_CONNECT_RETRIABLE macro */ michael@0: #include "util-internal.h" michael@0: michael@0: const char *resource = NULL; michael@0: struct event_base *base = NULL; michael@0: michael@0: int total_n_handled = 0; michael@0: int total_n_errors = 0; michael@0: int total_n_launched = 0; michael@0: size_t total_n_bytes = 0; michael@0: struct timeval total_time = {0,0}; michael@0: int n_errors = 0; michael@0: michael@0: const int PARALLELISM = 200; michael@0: const int N_REQUESTS = 20000; michael@0: michael@0: struct request_info { michael@0: size_t n_read; michael@0: struct timeval started; michael@0: }; michael@0: michael@0: static int launch_request(void); michael@0: static void readcb(struct bufferevent *b, void *arg); michael@0: static void errorcb(struct bufferevent *b, short what, void *arg); michael@0: michael@0: static void michael@0: readcb(struct bufferevent *b, void *arg) michael@0: { michael@0: struct request_info *ri = arg; michael@0: struct evbuffer *input = bufferevent_get_input(b); michael@0: size_t n = evbuffer_get_length(input); michael@0: michael@0: ri->n_read += n; michael@0: evbuffer_drain(input, n); michael@0: } michael@0: michael@0: static void michael@0: errorcb(struct bufferevent *b, short what, void *arg) michael@0: { michael@0: struct request_info *ri = arg; michael@0: struct timeval now, diff; michael@0: if (what & BEV_EVENT_EOF) { michael@0: ++total_n_handled; michael@0: total_n_bytes += ri->n_read; michael@0: evutil_gettimeofday(&now, NULL); michael@0: evutil_timersub(&now, &ri->started, &diff); michael@0: evutil_timeradd(&diff, &total_time, &total_time); michael@0: michael@0: if (total_n_handled && (total_n_handled%1000)==0) michael@0: printf("%d requests done\n",total_n_handled); michael@0: michael@0: if (total_n_launched < N_REQUESTS) { michael@0: if (launch_request() < 0) michael@0: perror("Can't launch"); michael@0: } michael@0: } else { michael@0: ++total_n_errors; michael@0: perror("Unexpected error"); michael@0: } michael@0: michael@0: bufferevent_setcb(b, NULL, NULL, NULL, NULL); michael@0: free(ri); michael@0: bufferevent_disable(b, EV_READ|EV_WRITE); michael@0: bufferevent_free(b); michael@0: } michael@0: michael@0: static void michael@0: frob_socket(evutil_socket_t sock) michael@0: { michael@0: struct linger l; michael@0: int one = 1; michael@0: if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*)&one, sizeof(one))<0) michael@0: perror("setsockopt(SO_REUSEADDR)"); michael@0: l.l_onoff = 1; michael@0: l.l_linger = 0; michael@0: if (setsockopt(sock, SOL_SOCKET, SO_LINGER, (void*)&l, sizeof(l))<0) michael@0: perror("setsockopt(SO_LINGER)"); michael@0: } michael@0: michael@0: static int michael@0: launch_request(void) michael@0: { michael@0: evutil_socket_t sock; michael@0: struct sockaddr_in sin; michael@0: struct bufferevent *b; michael@0: michael@0: struct request_info *ri; michael@0: michael@0: memset(&sin, 0, sizeof(sin)); michael@0: michael@0: ++total_n_launched; michael@0: michael@0: sin.sin_family = AF_INET; michael@0: sin.sin_addr.s_addr = htonl(0x7f000001); michael@0: sin.sin_port = htons(8080); michael@0: if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) michael@0: return -1; michael@0: if (evutil_make_socket_nonblocking(sock) < 0) michael@0: return -1; michael@0: frob_socket(sock); michael@0: if (connect(sock, (struct sockaddr*)&sin, sizeof(sin)) < 0) { michael@0: int e = errno; michael@0: if (! EVUTIL_ERR_CONNECT_RETRIABLE(e)) { michael@0: return -1; michael@0: } michael@0: } michael@0: michael@0: ri = malloc(sizeof(*ri)); michael@0: ri->n_read = 0; michael@0: evutil_gettimeofday(&ri->started, NULL); michael@0: michael@0: b = bufferevent_socket_new(base, sock, BEV_OPT_CLOSE_ON_FREE); michael@0: michael@0: bufferevent_setcb(b, readcb, NULL, errorcb, ri); michael@0: bufferevent_enable(b, EV_READ|EV_WRITE); michael@0: michael@0: evbuffer_add_printf(bufferevent_get_output(b), michael@0: "GET %s HTTP/1.0\r\n\r\n", resource); michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: michael@0: int michael@0: main(int argc, char **argv) michael@0: { michael@0: int i; michael@0: struct timeval start, end, total; michael@0: long long usec; michael@0: double throughput; michael@0: resource = "/ref"; michael@0: michael@0: setvbuf(stdout, NULL, _IONBF, 0); michael@0: michael@0: base = event_base_new(); michael@0: michael@0: for (i=0; i < PARALLELISM; ++i) { michael@0: if (launch_request() < 0) michael@0: perror("launch"); michael@0: } michael@0: michael@0: evutil_gettimeofday(&start, NULL); michael@0: michael@0: event_base_dispatch(base); michael@0: michael@0: evutil_gettimeofday(&end, NULL); michael@0: evutil_timersub(&end, &start, &total); michael@0: usec = total_time.tv_sec * 1000000 + total_time.tv_usec; michael@0: michael@0: if (!total_n_handled) { michael@0: puts("Nothing worked. You probably did something dumb."); michael@0: return 0; michael@0: } michael@0: michael@0: michael@0: throughput = total_n_handled / michael@0: (total.tv_sec+ ((double)total.tv_usec)/1000000.0); michael@0: michael@0: #ifdef WIN32 michael@0: #define I64_FMT "%I64d" michael@0: #define I64_TYP __int64 michael@0: #else michael@0: #define I64_FMT "%lld" michael@0: #define I64_TYP long long int michael@0: #endif michael@0: michael@0: printf("\n%d requests in %d.%06d sec. (%.2f throughput)\n" michael@0: "Each took about %.02f msec latency\n" michael@0: I64_FMT "bytes read. %d errors.\n", michael@0: total_n_handled, michael@0: (int)total.tv_sec, (int)total.tv_usec, michael@0: throughput, michael@0: (double)(usec/1000) / total_n_handled, michael@0: (I64_TYP)total_n_bytes, n_errors); michael@0: michael@0: return 0; michael@0: }