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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 * Copyright 2007-2012 Niels Provos and Nick Mathewson
michael@0 3 *
michael@0 4 * Redistribution and use in source and binary forms, with or without
michael@0 5 * modification, are permitted provided that the following conditions
michael@0 6 * are met:
michael@0 7 * 1. Redistributions of source code must retain the above copyright
michael@0 8 * notice, this list of conditions and the following disclaimer.
michael@0 9 * 2. Redistributions in binary form must reproduce the above copyright
michael@0 10 * notice, this list of conditions and the following disclaimer in the
michael@0 11 * documentation and/or other materials provided with the distribution.
michael@0 12 * 4. The name of the author may not be used to endorse or promote products
michael@0 13 * derived from this software without specific prior written permission.
michael@0 14 *
michael@0 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
michael@0 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
michael@0 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
michael@0 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
michael@0 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
michael@0 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
michael@0 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
michael@0 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
michael@0 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 25 *
michael@0 26 */
michael@0 27
michael@0 28 #include "event2/event-config.h"
michael@0 29
michael@0 30 #include <sys/types.h>
michael@0 31 #include <sys/stat.h>
michael@0 32 #ifdef _EVENT_HAVE_SYS_TIME_H
michael@0 33 #include <sys/time.h>
michael@0 34 #endif
michael@0 35 #ifdef WIN32
michael@0 36 #define WIN32_LEAN_AND_MEAN
michael@0 37 #include <windows.h>
michael@0 38 #else
michael@0 39 #include <sys/socket.h>
michael@0 40 #include <sys/resource.h>
michael@0 41 #endif
michael@0 42 #include <signal.h>
michael@0 43 #include <fcntl.h>
michael@0 44 #include <stdlib.h>
michael@0 45 #include <stdio.h>
michael@0 46 #include <string.h>
michael@0 47 #ifdef _EVENT_HAVE_UNISTD_H
michael@0 48 #include <unistd.h>
michael@0 49 #endif
michael@0 50 #include <errno.h>
michael@0 51
michael@0 52 #include <event.h>
michael@0 53 #include <evutil.h>
michael@0 54
michael@0 55 /*
michael@0 56 * This benchmark tests how quickly we can propagate a write down a chain
michael@0 57 * of socket pairs. We start by writing to the first socket pair and all
michael@0 58 * events will fire subsequently until the last socket pair has been reached
michael@0 59 * and the benchmark terminates.
michael@0 60 */
michael@0 61
michael@0 62 static int fired;
michael@0 63 static evutil_socket_t *pipes;
michael@0 64 static struct event *events;
michael@0 65
michael@0 66 static void
michael@0 67 read_cb(evutil_socket_t fd, short which, void *arg)
michael@0 68 {
michael@0 69 char ch;
michael@0 70 evutil_socket_t sock = (evutil_socket_t)(ev_intptr_t)arg;
michael@0 71
michael@0 72 recv(fd, &ch, sizeof(ch), 0);
michael@0 73 if (sock >= 0) {
michael@0 74 if (send(sock, "e", 1, 0) < 0)
michael@0 75 perror("send");
michael@0 76 }
michael@0 77 fired++;
michael@0 78 }
michael@0 79
michael@0 80 static struct timeval *
michael@0 81 run_once(int num_pipes)
michael@0 82 {
michael@0 83 int i;
michael@0 84 evutil_socket_t *cp;
michael@0 85 static struct timeval ts, te, tv_timeout;
michael@0 86
michael@0 87 events = calloc(num_pipes, sizeof(struct event));
michael@0 88 pipes = calloc(num_pipes * 2, sizeof(evutil_socket_t));
michael@0 89
michael@0 90 if (events == NULL || pipes == NULL) {
michael@0 91 perror("malloc");
michael@0 92 exit(1);
michael@0 93 }
michael@0 94
michael@0 95 for (cp = pipes, i = 0; i < num_pipes; i++, cp += 2) {
michael@0 96 if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, cp) == -1) {
michael@0 97 perror("socketpair");
michael@0 98 exit(1);
michael@0 99 }
michael@0 100 }
michael@0 101
michael@0 102 /* measurements includes event setup */
michael@0 103 evutil_gettimeofday(&ts, NULL);
michael@0 104
michael@0 105 /* provide a default timeout for events */
michael@0 106 evutil_timerclear(&tv_timeout);
michael@0 107 tv_timeout.tv_sec = 60;
michael@0 108
michael@0 109 for (cp = pipes, i = 0; i < num_pipes; i++, cp += 2) {
michael@0 110 evutil_socket_t fd = i < num_pipes - 1 ? cp[3] : -1;
michael@0 111 event_set(&events[i], cp[0], EV_READ, read_cb,
michael@0 112 (void *)(ev_intptr_t)fd);
michael@0 113 event_add(&events[i], &tv_timeout);
michael@0 114 }
michael@0 115
michael@0 116 fired = 0;
michael@0 117
michael@0 118 /* kick everything off with a single write */
michael@0 119 if (send(pipes[1], "e", 1, 0) < 0)
michael@0 120 perror("send");
michael@0 121
michael@0 122 event_dispatch();
michael@0 123
michael@0 124 evutil_gettimeofday(&te, NULL);
michael@0 125 evutil_timersub(&te, &ts, &te);
michael@0 126
michael@0 127 for (cp = pipes, i = 0; i < num_pipes; i++, cp += 2) {
michael@0 128 event_del(&events[i]);
michael@0 129 close(cp[0]);
michael@0 130 close(cp[1]);
michael@0 131 }
michael@0 132
michael@0 133 free(pipes);
michael@0 134 free(events);
michael@0 135
michael@0 136 return (&te);
michael@0 137 }
michael@0 138
michael@0 139 int
michael@0 140 main(int argc, char **argv)
michael@0 141 {
michael@0 142 #ifndef WIN32
michael@0 143 struct rlimit rl;
michael@0 144 #endif
michael@0 145 int i, c;
michael@0 146 struct timeval *tv;
michael@0 147
michael@0 148 int num_pipes = 100;
michael@0 149 while ((c = getopt(argc, argv, "n:")) != -1) {
michael@0 150 switch (c) {
michael@0 151 case 'n':
michael@0 152 num_pipes = atoi(optarg);
michael@0 153 break;
michael@0 154 default:
michael@0 155 fprintf(stderr, "Illegal argument \"%c\"\n", c);
michael@0 156 exit(1);
michael@0 157 }
michael@0 158 }
michael@0 159
michael@0 160 #ifndef WIN32
michael@0 161 rl.rlim_cur = rl.rlim_max = num_pipes * 2 + 50;
michael@0 162 if (setrlimit(RLIMIT_NOFILE, &rl) == -1) {
michael@0 163 perror("setrlimit");
michael@0 164 exit(1);
michael@0 165 }
michael@0 166 #endif
michael@0 167
michael@0 168 event_init();
michael@0 169
michael@0 170 for (i = 0; i < 25; i++) {
michael@0 171 tv = run_once(num_pipes);
michael@0 172 if (tv == NULL)
michael@0 173 exit(1);
michael@0 174 fprintf(stdout, "%ld\n",
michael@0 175 tv->tv_sec * 1000000L + tv->tv_usec);
michael@0 176 }
michael@0 177
michael@0 178 exit(0);
michael@0 179 }

mercurial