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

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

mercurial