MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
test-time.c
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 
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <sys/time.h>
13 #include <fcntl.h>
14 #include <stdlib.h>
15 #include <stdio.h>
16 #include <string.h>
17 #include <unistd.h>
18 #include <errno.h>
19 
20 #include <event.h>
21 
22 int called = 0;
23 
24 #define NEVENT 20000
25 
26 struct event *ev[NEVENT];
27 
28 static int
29 rand_int(int n)
30 {
31 #ifdef WIN32
32  return (int)(rand() * n);
33 #else
34  return (int)(random() % n);
35 #endif
36 }
37 
38 static void
39 time_cb(int fd, short event, void *arg)
40 {
41  struct timeval tv;
42  int i, j;
43 
44  called++;
45 
46  if (called < 10*NEVENT) {
47  for (i = 0; i < 10; i++) {
48  j = rand_int(NEVENT);
49  tv.tv_sec = 0;
50  tv.tv_usec = rand_int(50000);
51  if (tv.tv_usec % 2)
52  evtimer_add(ev[j], &tv);
53  else
54  evtimer_del(ev[j]);
55  }
56  }
57 }
58 
59 int
60 main (int argc, char **argv)
61 {
62  struct timeval tv;
63  int i;
64 
65  /* Initalize the event library */
66  event_init();
67 
68  for (i = 0; i < NEVENT; i++) {
69  ev[i] = malloc(sizeof(struct event));
70 
71  /* Initalize one event */
72  evtimer_set(ev[i], time_cb, ev[i]);
73  tv.tv_sec = 0;
74  tv.tv_usec = rand_int(50000);
75  evtimer_add(ev[i], &tv);
76  }
77 
78  event_dispatch();
79 
80  return (called < NEVENT);
81 }
82