Changes to update Tomato RAF.
[tomato.git] / release / src / router / dnscrypt / src / libevent / test / test-changelist.c
blob48ff9b16811812c8b9e827f27521add6d9ba425f
1 /*
2 * Copyright (c) 2010-2012 Niels Provos and Nick Mathewson
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "event2/event-config.h"
29 #ifdef WIN32
30 #include <winsock2.h>
31 #include <windows.h>
32 #else
33 #include <unistd.h>
34 #endif
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #ifdef _EVENT_HAVE_SYS_TIME_H
38 #include <sys/time.h>
39 #endif
41 #ifdef _EVENT_HAVE_SYS_SOCKET_H
42 #include <sys/socket.h>
43 #endif
44 #include <fcntl.h>
45 #include <stdlib.h>
46 #include <stdio.h>
47 #include <string.h>
48 #include <errno.h>
50 #include "event2/event.h"
51 #include "event2/util.h"
52 #include <time.h>
54 struct cpu_usage_timer {
55 #ifdef WIN32
56 HANDLE thread;
57 FILETIME usertimeBegin;
58 FILETIME kerneltimeBegin;
59 #else
60 clock_t ticksBegin;
61 #endif
62 struct timeval timeBegin;
64 static void
65 start_cpu_usage_timer(struct cpu_usage_timer *timer)
67 #ifdef WIN32
68 int r;
69 FILETIME createtime, exittime;
70 timer->thread = GetCurrentThread();
71 r = GetThreadTimes(timer->thread, &createtime, &exittime,
72 &timer->usertimeBegin, &timer->kerneltimeBegin);
73 if (r==0) printf("GetThreadTimes failed.");
74 #else
75 timer->ticksBegin = clock();
76 #endif
78 evutil_gettimeofday(&timer->timeBegin, NULL);
80 #ifdef WIN32
81 static ev_int64_t
82 filetime_to_100nsec(const FILETIME *ft)
84 /* Number of 100-nanosecond units */
85 ev_int64_t n = ft->dwHighDateTime;
86 n <<= 32;
87 n += ft->dwLowDateTime;
88 return n;
90 static double
91 filetime_diff(const FILETIME *ftStart, const FILETIME *ftEnd)
93 ev_int64_t s, e, diff;
94 double r;
95 s = filetime_to_100nsec(ftStart);
96 e = filetime_to_100nsec(ftEnd);
97 diff = e - s;
98 r = (double) diff;
99 return r / 1.0e7;
101 #endif
103 static void
104 get_cpu_usage(struct cpu_usage_timer *timer, double *secElapsedOut,
105 double *secUsedOut, double *usageOut)
107 #ifdef WIN32
108 double usertime_seconds, kerneltime_seconds;
109 FILETIME createtime, exittime, usertimeEnd, kerneltimeEnd;
110 int r;
111 #else
112 clock_t ticksEnd;
113 #endif
114 struct timeval timeEnd, timeDiff;
115 double secondsPassed, secondsUsed;
117 #ifdef WIN32
118 r = GetThreadTimes(timer->thread, &createtime, &exittime,
119 &usertimeEnd, &kerneltimeEnd);
120 if (r==0) printf("GetThreadTimes failed.");
121 usertime_seconds = filetime_diff(&timer->usertimeBegin, &usertimeEnd);
122 kerneltime_seconds = filetime_diff(&timer->kerneltimeBegin, &kerneltimeEnd);
123 secondsUsed = kerneltime_seconds + usertime_seconds;
124 #else
125 ticksEnd = clock();
126 secondsUsed = (ticksEnd - timer->ticksBegin) / (double)CLOCKS_PER_SEC;
127 #endif
128 evutil_gettimeofday(&timeEnd, NULL);
129 evutil_timersub(&timeEnd, &timer->timeBegin, &timeDiff);
130 secondsPassed = timeDiff.tv_sec + (timeDiff.tv_usec / 1.0e6);
132 *secElapsedOut = secondsPassed;
133 *secUsedOut = secondsUsed;
134 *usageOut = secondsUsed / secondsPassed;
137 static void
138 write_cb(evutil_socket_t fd, short event, void *arg)
140 printf("write callback. should only see this once\n");
142 /* got what we want remove the event */
143 event_del(*(struct event**)arg);
145 /* opps changed my mind add it back again */
146 event_add(*(struct event**)arg,NULL);
148 /* not a good day for decisiveness, I really didn't want it after all */
149 event_del(*(struct event**)arg);
153 static void
154 timeout_cb(evutil_socket_t fd, short event, void *arg)
156 printf("timeout fired, time to end test\n");
157 event_del(*(struct event**)arg);
158 return;
162 main(int argc, char **argv)
164 struct event* ev;
165 struct event* timeout;
166 struct event_base* base;
168 evutil_socket_t pair[2];
169 struct timeval tv;
170 struct cpu_usage_timer timer;
172 double usage, secPassed, secUsed;
174 #ifdef WIN32
175 WORD wVersionRequested;
176 WSADATA wsaData;
177 int err;
179 wVersionRequested = MAKEWORD(2, 2);
181 err = WSAStartup(wVersionRequested, &wsaData);
182 #endif
183 if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
184 return (1);
186 /* Initalize the event library */
187 base = event_base_new();
189 /* Initalize a timeout to terminate the test */
190 timeout = evtimer_new(base,timeout_cb,&timeout);
191 /* and watch for writability on one end of the pipe */
192 ev = event_new(base,pair[1],EV_WRITE | EV_PERSIST, write_cb, &ev);
194 tv.tv_sec = 5;
195 tv.tv_usec = 0;
197 evtimer_add(timeout, &tv);
199 event_add(ev, NULL);
201 start_cpu_usage_timer(&timer);
203 event_base_dispatch(base);
205 event_free(ev);
206 event_free(timeout);
207 event_base_free(base);
209 get_cpu_usage(&timer, &secPassed, &secUsed, &usage);
211 /* attempt to calculate our cpu usage over the test should be
212 virtually nil */
214 printf("usec used=%d, usec passed=%d, cpu usage=%.2f%%\n",
215 (int)(secUsed*1e6),
216 (int)(secPassed*1e6),
217 usage*100);
219 if (usage > 50.0) /* way too high */
220 return 1;
222 return 0;