ipc/chromium/src/third_party/libevent/test/test-changelist.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/test-changelist.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,223 @@
     1.4 +/*
     1.5 + * Copyright (c) 2010-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 + * 3. 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 +#include "event2/event-config.h"
    1.31 +
    1.32 +#ifdef WIN32
    1.33 +#include <winsock2.h>
    1.34 +#include <windows.h>
    1.35 +#else
    1.36 +#include <unistd.h>
    1.37 +#endif
    1.38 +#include <sys/types.h>
    1.39 +#include <sys/stat.h>
    1.40 +#ifdef _EVENT_HAVE_SYS_TIME_H
    1.41 +#include <sys/time.h>
    1.42 +#endif
    1.43 +
    1.44 +#ifdef _EVENT_HAVE_SYS_SOCKET_H
    1.45 +#include <sys/socket.h>
    1.46 +#endif
    1.47 +#include <fcntl.h>
    1.48 +#include <stdlib.h>
    1.49 +#include <stdio.h>
    1.50 +#include <string.h>
    1.51 +#include <errno.h>
    1.52 +
    1.53 +#include "event2/event.h"
    1.54 +#include "event2/util.h"
    1.55 +#include <time.h>
    1.56 +
    1.57 +struct cpu_usage_timer {
    1.58 +#ifdef WIN32
    1.59 +	HANDLE thread;
    1.60 +	FILETIME usertimeBegin;
    1.61 +	FILETIME kerneltimeBegin;
    1.62 +#else
    1.63 +	clock_t ticksBegin;
    1.64 +#endif
    1.65 +	struct timeval timeBegin;
    1.66 +};
    1.67 +static void
    1.68 +start_cpu_usage_timer(struct cpu_usage_timer *timer)
    1.69 +{
    1.70 +#ifdef WIN32
    1.71 +	int r;
    1.72 +	FILETIME createtime, exittime;
    1.73 +	timer->thread = GetCurrentThread();
    1.74 +	r = GetThreadTimes(timer->thread, &createtime, &exittime,
    1.75 +	    &timer->usertimeBegin, &timer->kerneltimeBegin);
    1.76 +	if (r==0) printf("GetThreadTimes failed.");
    1.77 +#else
    1.78 +	timer->ticksBegin = clock();
    1.79 +#endif
    1.80 +
    1.81 +	evutil_gettimeofday(&timer->timeBegin, NULL);
    1.82 +}
    1.83 +#ifdef WIN32
    1.84 +static ev_int64_t
    1.85 +filetime_to_100nsec(const FILETIME *ft)
    1.86 +{
    1.87 +	/* Number of 100-nanosecond units */
    1.88 +	ev_int64_t n = ft->dwHighDateTime;
    1.89 +	n <<= 32;
    1.90 +	n += ft->dwLowDateTime;
    1.91 +	return n;
    1.92 +}
    1.93 +static double
    1.94 +filetime_diff(const FILETIME *ftStart, const FILETIME *ftEnd)
    1.95 +{
    1.96 +	ev_int64_t s, e, diff;
    1.97 +	double r;
    1.98 +	s = filetime_to_100nsec(ftStart);
    1.99 +	e = filetime_to_100nsec(ftEnd);
   1.100 +	diff = e - s;
   1.101 +	r = (double) diff;
   1.102 +	return r / 1.0e7;
   1.103 +}
   1.104 +#endif
   1.105 +
   1.106 +static void
   1.107 +get_cpu_usage(struct cpu_usage_timer *timer, double *secElapsedOut,
   1.108 +    double *secUsedOut, double *usageOut)
   1.109 +{
   1.110 +#ifdef WIN32
   1.111 +	double usertime_seconds, kerneltime_seconds;
   1.112 +	FILETIME createtime, exittime, usertimeEnd, kerneltimeEnd;
   1.113 +	int r;
   1.114 +#else
   1.115 +	clock_t ticksEnd;
   1.116 +#endif
   1.117 +	struct timeval timeEnd, timeDiff;
   1.118 +	double secondsPassed, secondsUsed;
   1.119 +
   1.120 +#ifdef WIN32
   1.121 +	r = GetThreadTimes(timer->thread, &createtime, &exittime,
   1.122 +	    &usertimeEnd, &kerneltimeEnd);
   1.123 +	if (r==0) printf("GetThreadTimes failed.");
   1.124 +	usertime_seconds = filetime_diff(&timer->usertimeBegin, &usertimeEnd);
   1.125 +	kerneltime_seconds = filetime_diff(&timer->kerneltimeBegin, &kerneltimeEnd);
   1.126 +	secondsUsed = kerneltime_seconds + usertime_seconds;
   1.127 +#else
   1.128 +	ticksEnd = clock();
   1.129 +	secondsUsed = (ticksEnd - timer->ticksBegin) / (double)CLOCKS_PER_SEC;
   1.130 +#endif
   1.131 +	evutil_gettimeofday(&timeEnd, NULL);
   1.132 +	evutil_timersub(&timeEnd, &timer->timeBegin, &timeDiff);
   1.133 +	secondsPassed = timeDiff.tv_sec + (timeDiff.tv_usec / 1.0e6);
   1.134 +
   1.135 +	*secElapsedOut = secondsPassed;
   1.136 +	*secUsedOut = secondsUsed;
   1.137 +	*usageOut = secondsUsed / secondsPassed;
   1.138 +}
   1.139 +
   1.140 +static void
   1.141 +write_cb(evutil_socket_t fd, short event, void *arg)
   1.142 +{
   1.143 +	printf("write callback. should only see this once\n");
   1.144 +
   1.145 +	/* got what we want remove the event */
   1.146 +	event_del(*(struct event**)arg);
   1.147 +
   1.148 +	/* opps changed my mind add it back again */
   1.149 +	event_add(*(struct event**)arg,NULL);
   1.150 +
   1.151 +	/* not a good day for decisiveness, I really didn't want it after all */
   1.152 +	event_del(*(struct event**)arg);
   1.153 +
   1.154 +}
   1.155 +
   1.156 +static void
   1.157 +timeout_cb(evutil_socket_t fd, short event, void *arg)
   1.158 +{
   1.159 +	printf("timeout fired, time to end test\n");
   1.160 +	event_del(*(struct event**)arg);
   1.161 +	return;
   1.162 +}
   1.163 +
   1.164 +int
   1.165 +main(int argc, char **argv)
   1.166 +{
   1.167 +	struct event* ev;
   1.168 +	struct event* timeout;
   1.169 +	struct event_base* base;
   1.170 +
   1.171 +	evutil_socket_t pair[2];
   1.172 +	struct timeval tv;
   1.173 +	struct cpu_usage_timer timer;
   1.174 +
   1.175 +	double usage, secPassed, secUsed;
   1.176 +
   1.177 +#ifdef WIN32
   1.178 +	WORD wVersionRequested;
   1.179 +	WSADATA wsaData;
   1.180 +
   1.181 +	wVersionRequested = MAKEWORD(2, 2);
   1.182 +
   1.183 +	(void) WSAStartup(wVersionRequested, &wsaData);
   1.184 +#endif
   1.185 +	if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
   1.186 +		return (1);
   1.187 +
   1.188 +	/* Initalize the event library */
   1.189 +	base = event_base_new();
   1.190 +
   1.191 +	/* Initalize a timeout to terminate the test */
   1.192 +	timeout = evtimer_new(base,timeout_cb,&timeout);
   1.193 +	/* and watch for writability on one end of the pipe */
   1.194 +	ev = event_new(base,pair[1],EV_WRITE | EV_PERSIST, write_cb, &ev);
   1.195 +
   1.196 +	tv.tv_sec  = 5;
   1.197 +	tv.tv_usec = 0;
   1.198 +
   1.199 +	evtimer_add(timeout, &tv);
   1.200 +
   1.201 +	event_add(ev, NULL);
   1.202 +
   1.203 +	start_cpu_usage_timer(&timer);
   1.204 +
   1.205 +	event_base_dispatch(base);
   1.206 +
   1.207 +	event_free(ev);
   1.208 +	event_free(timeout);
   1.209 +	event_base_free(base);
   1.210 +
   1.211 +	get_cpu_usage(&timer, &secPassed, &secUsed, &usage);
   1.212 +
   1.213 +	/* attempt to calculate our cpu usage over the test should be
   1.214 +	   virtually nil */
   1.215 +
   1.216 +	printf("usec used=%d, usec passed=%d, cpu usage=%.2f%%\n",
   1.217 +	    (int)(secUsed*1e6),
   1.218 +	    (int)(secPassed*1e6),
   1.219 +	    usage*100);
   1.220 +
   1.221 +	if (usage > 50.0) /* way too high */
   1.222 +	  return 1;
   1.223 +
   1.224 +	return 0;
   1.225 +}
   1.226 +

mercurial