test-time.c revision 5821806d5e7f356e8fa4b058a389a808ea183019
1/*
2 * Compile with:
3 * cc -I/usr/local/include -o time-test time-test.c -L/usr/local/lib -levent
4 */
5#ifdef HAVE_CONFIG_H
6#include "config.h"
7#endif
8
9#ifdef WIN32
10#include <winsock2.h>
11#endif
12
13#include <sys/types.h>
14#include <sys/stat.h>
15#ifdef HAVE_SYS_TIME_H
16#include <sys/time.h>
17#endif
18#include <fcntl.h>
19#include <stdlib.h>
20#include <stdio.h>
21#include <string.h>
22#ifdef HAVE_UNISTD_H
23#include <unistd.h>
24#endif
25#include <errno.h>
26
27#include <event.h>
28
29int called = 0;
30
31#define NEVENT	20000
32
33struct event *ev[NEVENT];
34
35static int
36rand_int(int n)
37{
38#ifdef WIN32
39	return (int)(rand() * n);
40#else
41	return (int)(random() % n);
42#endif
43}
44
45static void
46time_cb(int fd, short event, void *arg)
47{
48	struct timeval tv;
49	int i, j;
50
51	called++;
52
53	if (called < 10*NEVENT) {
54		for (i = 0; i < 10; i++) {
55			j = rand_int(NEVENT);
56			tv.tv_sec = 0;
57			tv.tv_usec = rand_int(50000);
58			if (tv.tv_usec % 2)
59				evtimer_add(ev[j], &tv);
60			else
61				evtimer_del(ev[j]);
62		}
63	}
64}
65
66int
67main (int argc, char **argv)
68{
69	struct timeval tv;
70	int i;
71
72	/* Initalize the event library */
73	event_init();
74
75	for (i = 0; i < NEVENT; i++) {
76		ev[i] = malloc(sizeof(struct event));
77
78		/* Initalize one event */
79		evtimer_set(ev[i], time_cb, ev[i]);
80		tv.tv_sec = 0;
81		tv.tv_usec = rand_int(50000);
82		evtimer_add(ev[i], &tv);
83	}
84
85	event_dispatch();
86
87	return (called < NEVENT);
88}
89
90