1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/ipc/chromium/src/third_party/libevent/test/bench.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,196 @@ 1.4 +/* 1.5 + * Copyright 2003-2007 Niels Provos <provos@citi.umich.edu> 1.6 + * Copyright 2007-2012 Niels Provos and Nick Mathewson 1.7 + * 1.8 + * Redistribution and use in source and binary forms, with or without 1.9 + * modification, are permitted provided that the following conditions 1.10 + * are met: 1.11 + * 1. Redistributions of source code must retain the above copyright 1.12 + * notice, this list of conditions and the following disclaimer. 1.13 + * 2. Redistributions in binary form must reproduce the above copyright 1.14 + * notice, this list of conditions and the following disclaimer in the 1.15 + * documentation and/or other materials provided with the distribution. 1.16 + * 4. The name of the author may not be used to endorse or promote products 1.17 + * derived from this software without specific prior written permission. 1.18 + * 1.19 + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1.20 + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1.21 + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 1.22 + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 1.23 + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 1.24 + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.25 + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.26 + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.27 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 1.28 + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.29 + * 1.30 + * 1.31 + * Mon 03/10/2003 - Modified by Davide Libenzi <davidel@xmailserver.org> 1.32 + * 1.33 + * Added chain event propagation to improve the sensitivity of 1.34 + * the measure respect to the event loop efficency. 1.35 + * 1.36 + * 1.37 + */ 1.38 + 1.39 +#include "event2/event-config.h" 1.40 + 1.41 +#include <sys/types.h> 1.42 +#include <sys/stat.h> 1.43 +#ifdef _EVENT_HAVE_SYS_TIME_H 1.44 +#include <sys/time.h> 1.45 +#endif 1.46 +#ifdef WIN32 1.47 +#define WIN32_LEAN_AND_MEAN 1.48 +#include <windows.h> 1.49 +#else 1.50 +#include <sys/socket.h> 1.51 +#include <signal.h> 1.52 +#include <sys/resource.h> 1.53 +#endif 1.54 +#include <fcntl.h> 1.55 +#include <stdlib.h> 1.56 +#include <stdio.h> 1.57 +#include <string.h> 1.58 +#ifdef _EVENT_HAVE_UNISTD_H 1.59 +#include <unistd.h> 1.60 +#endif 1.61 +#include <errno.h> 1.62 + 1.63 +#include <event.h> 1.64 +#include <evutil.h> 1.65 + 1.66 +static int count, writes, fired; 1.67 +static evutil_socket_t *pipes; 1.68 +static int num_pipes, num_active, num_writes; 1.69 +static struct event *events; 1.70 + 1.71 + 1.72 +static void 1.73 +read_cb(evutil_socket_t fd, short which, void *arg) 1.74 +{ 1.75 + ev_intptr_t idx = (ev_intptr_t) arg, widx = idx + 1; 1.76 + u_char ch; 1.77 + 1.78 + count += recv(fd, (char*)&ch, sizeof(ch), 0); 1.79 + if (writes) { 1.80 + if (widx >= num_pipes) 1.81 + widx -= num_pipes; 1.82 + send(pipes[2 * widx + 1], "e", 1, 0); 1.83 + writes--; 1.84 + fired++; 1.85 + } 1.86 +} 1.87 + 1.88 +static struct timeval * 1.89 +run_once(void) 1.90 +{ 1.91 + evutil_socket_t *cp, space; 1.92 + long i; 1.93 + static struct timeval ts, te; 1.94 + 1.95 + for (cp = pipes, i = 0; i < num_pipes; i++, cp += 2) { 1.96 + if (event_initialized(&events[i])) 1.97 + event_del(&events[i]); 1.98 + event_set(&events[i], cp[0], EV_READ | EV_PERSIST, read_cb, (void *)(ev_intptr_t) i); 1.99 + event_add(&events[i], NULL); 1.100 + } 1.101 + 1.102 + event_loop(EVLOOP_ONCE | EVLOOP_NONBLOCK); 1.103 + 1.104 + fired = 0; 1.105 + space = num_pipes / num_active; 1.106 + space = space * 2; 1.107 + for (i = 0; i < num_active; i++, fired++) 1.108 + send(pipes[i * space + 1], "e", 1, 0); 1.109 + 1.110 + count = 0; 1.111 + writes = num_writes; 1.112 + { int xcount = 0; 1.113 + evutil_gettimeofday(&ts, NULL); 1.114 + do { 1.115 + event_loop(EVLOOP_ONCE | EVLOOP_NONBLOCK); 1.116 + xcount++; 1.117 + } while (count != fired); 1.118 + evutil_gettimeofday(&te, NULL); 1.119 + 1.120 + if (xcount != count) fprintf(stderr, "Xcount: %d, Rcount: %d\n", xcount, count); 1.121 + } 1.122 + 1.123 + evutil_timersub(&te, &ts, &te); 1.124 + 1.125 + return (&te); 1.126 +} 1.127 + 1.128 +int 1.129 +main(int argc, char **argv) 1.130 +{ 1.131 +#ifndef WIN32 1.132 + struct rlimit rl; 1.133 +#endif 1.134 + int i, c; 1.135 + struct timeval *tv; 1.136 + evutil_socket_t *cp; 1.137 + 1.138 +#ifdef WIN32 1.139 + WSADATA WSAData; 1.140 + WSAStartup(0x101, &WSAData); 1.141 +#endif 1.142 + num_pipes = 100; 1.143 + num_active = 1; 1.144 + num_writes = num_pipes; 1.145 + while ((c = getopt(argc, argv, "n:a:w:")) != -1) { 1.146 + switch (c) { 1.147 + case 'n': 1.148 + num_pipes = atoi(optarg); 1.149 + break; 1.150 + case 'a': 1.151 + num_active = atoi(optarg); 1.152 + break; 1.153 + case 'w': 1.154 + num_writes = atoi(optarg); 1.155 + break; 1.156 + default: 1.157 + fprintf(stderr, "Illegal argument \"%c\"\n", c); 1.158 + exit(1); 1.159 + } 1.160 + } 1.161 + 1.162 +#ifndef WIN32 1.163 + rl.rlim_cur = rl.rlim_max = num_pipes * 2 + 50; 1.164 + if (setrlimit(RLIMIT_NOFILE, &rl) == -1) { 1.165 + perror("setrlimit"); 1.166 + exit(1); 1.167 + } 1.168 +#endif 1.169 + 1.170 + events = calloc(num_pipes, sizeof(struct event)); 1.171 + pipes = calloc(num_pipes * 2, sizeof(evutil_socket_t)); 1.172 + if (events == NULL || pipes == NULL) { 1.173 + perror("malloc"); 1.174 + exit(1); 1.175 + } 1.176 + 1.177 + event_init(); 1.178 + 1.179 + for (cp = pipes, i = 0; i < num_pipes; i++, cp += 2) { 1.180 +#ifdef USE_PIPES 1.181 + if (pipe(cp) == -1) { 1.182 +#else 1.183 + if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, cp) == -1) { 1.184 +#endif 1.185 + perror("pipe"); 1.186 + exit(1); 1.187 + } 1.188 + } 1.189 + 1.190 + for (i = 0; i < 25; i++) { 1.191 + tv = run_once(); 1.192 + if (tv == NULL) 1.193 + exit(1); 1.194 + fprintf(stdout, "%ld\n", 1.195 + tv->tv_sec * 1000000L + tv->tv_usec); 1.196 + } 1.197 + 1.198 + exit(0); 1.199 +}