]> git.proxmox.com Git - mirror_frr.git/blob - tests/lib/test_timer_performance.c
b67af19aeaa3467e6fb81d82e97119f63954b840
[mirror_frr.git] / tests / lib / test_timer_performance.c
1 /*
2 * Test program which measures the time it takes to schedule and
3 * remove timers.
4 *
5 * Copyright (C) 2013 by Open Source Routing.
6 * Copyright (C) 2013 by Internet Systems Consortium, Inc. ("ISC")
7 *
8 * This file is part of Quagga
9 *
10 * Quagga is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2, or (at your option) any
13 * later version.
14 *
15 * Quagga is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; see the file COPYING; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25 #include <zebra.h>
26
27 #include <stdio.h>
28 #include <unistd.h>
29
30 #include "thread.h"
31 #include "pqueue.h"
32 #include "prng.h"
33
34 #define SCHEDULE_TIMERS 1000000
35 #define REMOVE_TIMERS 500000
36
37 struct thread_master *master;
38
39 static int dummy_func(struct thread *thread)
40 {
41 return 0;
42 }
43
44 int main(int argc, char **argv)
45 {
46 struct prng *prng;
47 int i;
48 struct thread **timers;
49 struct timeval tv_start, tv_lap, tv_stop;
50 unsigned long t_schedule, t_remove;
51
52 master = thread_master_create();
53 prng = prng_new(0);
54 timers = calloc(SCHEDULE_TIMERS, sizeof(*timers));
55
56 /* create thread structures so they won't be allocated during the
57 * time measurement */
58 for (i = 0; i < SCHEDULE_TIMERS; i++) {
59 timers[i] = NULL;
60 thread_add_timer_msec(master, dummy_func, NULL, 0, &timers[i]);
61 }
62 for (i = 0; i < SCHEDULE_TIMERS; i++)
63 thread_cancel(timers[i]);
64
65 monotime(&tv_start);
66
67 for (i = 0; i < SCHEDULE_TIMERS; i++)
68 {
69 long interval_msec;
70
71 interval_msec = prng_rand(prng) % (100 * SCHEDULE_TIMERS);
72 timers[i] = NULL;
73 thread_add_timer_msec(master, dummy_func, NULL, interval_msec,
74 &timers[i]);
75 }
76
77 monotime(&tv_lap);
78
79 for (i = 0; i < REMOVE_TIMERS; i++)
80 {
81 int index;
82
83 index = prng_rand(prng) % SCHEDULE_TIMERS;
84 if (timers[index])
85 thread_cancel(timers[index]);
86 timers[index] = NULL;
87 }
88
89 monotime(&tv_stop);
90
91 t_schedule = 1000 * (tv_lap.tv_sec - tv_start.tv_sec);
92 t_schedule += (tv_lap.tv_usec - tv_start.tv_usec) / 1000;
93
94 t_remove = 1000 * (tv_stop.tv_sec - tv_lap.tv_sec);
95 t_remove += (tv_stop.tv_usec - tv_lap.tv_usec) / 1000;
96
97 printf("Scheduling %d random timers took %ld.%03ld seconds.\n",
98 SCHEDULE_TIMERS, t_schedule/1000, t_schedule%1000);
99 printf("Removing %d random timers took %ld.%03ld seconds.\n",
100 REMOVE_TIMERS, t_remove/1000, t_remove%1000);
101 fflush(stdout);
102
103 free(timers);
104 thread_master_free(master);
105 prng_free(prng);
106 return 0;
107 }