]> git.proxmox.com Git - mirror_frr.git/blob - tests/lib/test_timer_performance.c
Merge pull request #503 from pguibert6WIND/issue_473
[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
21 * along with Quagga; see the file COPYING. If not, write to the Free
22 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23 * 02111-1307, USA.
24 */
25
26 #include <zebra.h>
27
28 #include <stdio.h>
29 #include <unistd.h>
30
31 #include "thread.h"
32 #include "pqueue.h"
33 #include "prng.h"
34
35 #define SCHEDULE_TIMERS 1000000
36 #define REMOVE_TIMERS 500000
37
38 struct thread_master *master;
39
40 static int dummy_func(struct thread *thread)
41 {
42 return 0;
43 }
44
45 int main(int argc, char **argv)
46 {
47 struct prng *prng;
48 int i;
49 struct thread **timers;
50 struct timeval tv_start, tv_lap, tv_stop;
51 unsigned long t_schedule, t_remove;
52
53 master = thread_master_create();
54 prng = prng_new(0);
55 timers = calloc(SCHEDULE_TIMERS, sizeof(*timers));
56
57 /* create thread structures so they won't be allocated during the
58 * time measurement */
59 for (i = 0; i < SCHEDULE_TIMERS; i++) {
60 timers[i] = NULL;
61 thread_add_timer_msec(master, dummy_func, NULL, 0, &timers[i]);
62 }
63 for (i = 0; i < SCHEDULE_TIMERS; i++)
64 thread_cancel(timers[i]);
65
66 monotime(&tv_start);
67
68 for (i = 0; i < SCHEDULE_TIMERS; i++)
69 {
70 long interval_msec;
71
72 interval_msec = prng_rand(prng) % (100 * SCHEDULE_TIMERS);
73 timers[i] = NULL;
74 thread_add_timer_msec(master, dummy_func, NULL, interval_msec,
75 &timers[i]);
76 }
77
78 monotime(&tv_lap);
79
80 for (i = 0; i < REMOVE_TIMERS; i++)
81 {
82 int index;
83
84 index = prng_rand(prng) % SCHEDULE_TIMERS;
85 if (timers[index])
86 thread_cancel(timers[index]);
87 timers[index] = NULL;
88 }
89
90 monotime(&tv_stop);
91
92 t_schedule = 1000 * (tv_lap.tv_sec - tv_start.tv_sec);
93 t_schedule += (tv_lap.tv_usec - tv_start.tv_usec) / 1000;
94
95 t_remove = 1000 * (tv_stop.tv_sec - tv_lap.tv_sec);
96 t_remove += (tv_stop.tv_usec - tv_lap.tv_usec) / 1000;
97
98 printf("Scheduling %d random timers took %ld.%03ld seconds.\n",
99 SCHEDULE_TIMERS, t_schedule/1000, t_schedule%1000);
100 printf("Removing %d random timers took %ld.%03ld seconds.\n",
101 REMOVE_TIMERS, t_remove/1000, t_remove%1000);
102 fflush(stdout);
103
104 free(timers);
105 thread_master_free(master);
106 prng_free(prng);
107 return 0;
108 }