1e4f24219SNick Mathewson /* 2e4f24219SNick Mathewson * Copyright 2009 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> 29e4f24219SNick Mathewson #include <sys/socket.h> 30e4f24219SNick Mathewson #include <netinet/in.h> 31e4f24219SNick Mathewson #include <stdlib.h> 32e4f24219SNick Mathewson #include <errno.h> 33e4f24219SNick Mathewson 34e4f24219SNick Mathewson #include <event2/event.h> 35e4f24219SNick Mathewson #include <event2/bufferevent.h> 36e4f24219SNick Mathewson #include <event2/buffer.h> 37e4f24219SNick Mathewson #include <event2/util.h> 38e4f24219SNick Mathewson 39e4f24219SNick Mathewson const char *resource = NULL; 40e4f24219SNick Mathewson struct event_base *base = NULL; 41e4f24219SNick Mathewson 42e4f24219SNick Mathewson int total_n_handled = 0; 43e4f24219SNick Mathewson int total_n_errors = 0; 44e4f24219SNick Mathewson int total_n_launched = 0; 45e4f24219SNick Mathewson size_t total_n_bytes = 0; 46e4f24219SNick Mathewson struct timeval total_time = {0,0}; 47e4f24219SNick Mathewson int n_errors = 0; 48e4f24219SNick Mathewson 49b182ed76SNick Mathewson const int PARALLELISM = 200; 50b182ed76SNick Mathewson const int N_REQUESTS = 20000; 51e4f24219SNick Mathewson 52e4f24219SNick Mathewson struct request_info { 53e4f24219SNick Mathewson size_t n_read; 54e4f24219SNick Mathewson struct timeval started; 55e4f24219SNick Mathewson }; 56e4f24219SNick Mathewson 57e4f24219SNick Mathewson static int launch_request(void); 58e4f24219SNick Mathewson static void readcb(struct bufferevent *b, void *arg); 59e4f24219SNick Mathewson static void errorcb(struct bufferevent *b, short what, void *arg); 60e4f24219SNick Mathewson 61e4f24219SNick Mathewson static void 62e4f24219SNick Mathewson readcb(struct bufferevent *b, void *arg) 63e4f24219SNick Mathewson { 64e4f24219SNick Mathewson struct request_info *ri = arg; 65e4f24219SNick Mathewson struct evbuffer *input = bufferevent_get_input(b); 66e4f24219SNick Mathewson size_t n = evbuffer_get_length(input); 67e4f24219SNick Mathewson 68e4f24219SNick Mathewson ri->n_read += n; 69e4f24219SNick Mathewson evbuffer_drain(input, n); 70e4f24219SNick Mathewson } 71e4f24219SNick Mathewson 72e4f24219SNick Mathewson static void 73e4f24219SNick Mathewson errorcb(struct bufferevent *b, short what, void *arg) 74e4f24219SNick Mathewson { 75e4f24219SNick Mathewson struct request_info *ri = arg; 76e4f24219SNick Mathewson struct timeval now, diff; 77e4f24219SNick Mathewson if (what & EVBUFFER_EOF) { 78e4f24219SNick Mathewson ++total_n_handled; 79e4f24219SNick Mathewson total_n_bytes += ri->n_read; 80e4f24219SNick Mathewson gettimeofday(&now, NULL); 81e4f24219SNick Mathewson evutil_timersub(&now, &ri->started, &diff); 82e4f24219SNick Mathewson evutil_timeradd(&diff, &total_time, &total_time); 83e4f24219SNick Mathewson 84e4f24219SNick Mathewson if (total_n_handled && (total_n_handled%1000)==0) 85e4f24219SNick Mathewson printf("%d requests done\n",total_n_handled); 86e4f24219SNick Mathewson 87e4f24219SNick Mathewson if (total_n_launched < N_REQUESTS) { 88e4f24219SNick Mathewson if (launch_request() < 0) 89e4f24219SNick Mathewson perror("Can't launch"); 90e4f24219SNick Mathewson } 91e4f24219SNick Mathewson } else { 92e4f24219SNick Mathewson ++total_n_errors; 93e4f24219SNick Mathewson perror("Unexpected error"); 94e4f24219SNick Mathewson } 95e4f24219SNick Mathewson 96e4f24219SNick Mathewson bufferevent_setcb(b, NULL, NULL, NULL, NULL); 97e4f24219SNick Mathewson free(ri); 98e4f24219SNick Mathewson bufferevent_disable(b, EV_READ|EV_WRITE); 99e4f24219SNick Mathewson bufferevent_free(b); 100e4f24219SNick Mathewson } 101e4f24219SNick Mathewson 102b182ed76SNick Mathewson static void 103b182ed76SNick Mathewson frob_socket(int sock) 104b182ed76SNick Mathewson { 105b182ed76SNick Mathewson struct linger l; 106b182ed76SNick Mathewson int one = 1; 107b182ed76SNick Mathewson setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)); 108b182ed76SNick Mathewson l.l_onoff = 1; 109b182ed76SNick Mathewson l.l_linger = 0; 110b182ed76SNick Mathewson if (setsockopt(sock, SOL_SOCKET, SO_LINGER, &l, sizeof(l))<0) 111b182ed76SNick Mathewson perror("setsockopt"); 112b182ed76SNick Mathewson } 113b182ed76SNick Mathewson 114e4f24219SNick Mathewson static int 115e4f24219SNick Mathewson launch_request(void) 116e4f24219SNick Mathewson { 117e4f24219SNick Mathewson int sock; 118e4f24219SNick Mathewson struct sockaddr_in sin; 119e4f24219SNick Mathewson struct bufferevent *b; 120e4f24219SNick Mathewson 121e4f24219SNick Mathewson struct request_info *ri; 122e4f24219SNick Mathewson 123e4f24219SNick Mathewson ++total_n_launched; 124e4f24219SNick Mathewson 125e4f24219SNick Mathewson sin.sin_family = AF_INET; 126e4f24219SNick Mathewson sin.sin_addr.s_addr = htonl(0x7f000001); 127e4f24219SNick Mathewson sin.sin_port = htons(8080); 128e4f24219SNick Mathewson if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) 129e4f24219SNick Mathewson return -1; 130e4f24219SNick Mathewson if (evutil_make_socket_nonblocking(sock) < 0) 131e4f24219SNick Mathewson return -1; 132b182ed76SNick Mathewson frob_socket(sock); 133e4f24219SNick Mathewson if (connect(sock, (struct sockaddr*)&sin, sizeof(sin)) < 0) { 134e4f24219SNick Mathewson if (errno != EINTR && errno != EINPROGRESS) { 135e4f24219SNick Mathewson return -1; 136e4f24219SNick Mathewson } 137e4f24219SNick Mathewson } 138e4f24219SNick Mathewson 139e4f24219SNick Mathewson ri = malloc(sizeof(*ri)); 140e4f24219SNick Mathewson ri->n_read = 0; 141e4f24219SNick Mathewson gettimeofday(&ri->started, NULL); 142e4f24219SNick Mathewson 143e4f24219SNick Mathewson b = bufferevent_socket_new(base, sock, BEV_OPT_CLOSE_ON_FREE); 144e4f24219SNick Mathewson 145e4f24219SNick Mathewson bufferevent_setcb(b, readcb, NULL, errorcb, ri); 146e4f24219SNick Mathewson bufferevent_enable(b, EV_READ|EV_WRITE); 147e4f24219SNick Mathewson 148e4f24219SNick Mathewson evbuffer_add_printf(bufferevent_get_output(b), 149e4f24219SNick Mathewson "GET %s HTTP/1.0\r\n\r\n", resource); 150e4f24219SNick Mathewson 151e4f24219SNick Mathewson return 0; 152e4f24219SNick Mathewson } 153e4f24219SNick Mathewson 154e4f24219SNick Mathewson 155e4f24219SNick Mathewson int 156e4f24219SNick Mathewson main(int argc, char **argv) 157e4f24219SNick Mathewson { 158e4f24219SNick Mathewson int i; 159e4f24219SNick Mathewson struct timeval start, end, total; 160e4f24219SNick Mathewson long long usec; 161b182ed76SNick Mathewson double throughput; 162e4f24219SNick Mathewson resource = "/ref"; 163e4f24219SNick Mathewson 164e4f24219SNick Mathewson setvbuf(stdout, NULL, _IONBF, 0); 165e4f24219SNick Mathewson 166e4f24219SNick Mathewson base = event_base_new(); 167e4f24219SNick Mathewson 168e4f24219SNick Mathewson for (i=0; i < PARALLELISM; ++i) { 169e4f24219SNick Mathewson if (launch_request() < 0) 170e4f24219SNick Mathewson perror("launch"); 171e4f24219SNick Mathewson } 172e4f24219SNick Mathewson 173e4f24219SNick Mathewson gettimeofday(&start, NULL); 174e4f24219SNick Mathewson 175e4f24219SNick Mathewson event_base_dispatch(base); 176e4f24219SNick Mathewson 177e4f24219SNick Mathewson gettimeofday(&end, NULL); 178e4f24219SNick Mathewson evutil_timersub(&end, &start, &total); 179e4f24219SNick Mathewson usec = total_time.tv_sec * 1000000 + total_time.tv_usec; 180e4f24219SNick Mathewson 181b182ed76SNick Mathewson if (!total_n_handled) { 182b182ed76SNick Mathewson puts("Nothing worked. You probably did something dumb."); 183b182ed76SNick Mathewson return 0; 184b182ed76SNick Mathewson } 185b182ed76SNick Mathewson 186b182ed76SNick Mathewson 187b182ed76SNick Mathewson throughput = total_n_handled / 188b182ed76SNick Mathewson (total.tv_sec+ ((double)total.tv_usec)/1000000.0); 189b182ed76SNick Mathewson 190b182ed76SNick Mathewson printf("\n%d requests in %d.%06d sec. (%.2f throughput)\n" 191b182ed76SNick Mathewson "Each took about %.02f msec latency\n" 192e4f24219SNick Mathewson "%lld bytes read. %d errors.\n", 193e4f24219SNick Mathewson total_n_handled, 194*1bb8e010SNick Mathewson (int)total.tv_sec, (int)total.tv_usec, 195b182ed76SNick Mathewson throughput, 196b182ed76SNick Mathewson (double)(usec/1000) / total_n_handled, 197e4f24219SNick Mathewson (long long)total_n_bytes, n_errors); 198e4f24219SNick Mathewson 199e4f24219SNick Mathewson return 0; 200e4f24219SNick Mathewson } 201