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