]> git.proxmox.com Git - mirror_frr.git/blame - tests/lib/test_timer_performance.c
Merge pull request #4877 from mjstapp/dplane_neighs
[mirror_frr.git] / tests / lib / test_timer_performance.c
CommitLineData
ba32db1e
CF
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 *
896014f4
DL
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
ba32db1e 23 */
ba32db1e
CF
24
25#include <zebra.h>
26
7a2fbbf0
DL
27#include <stdio.h>
28#include <unistd.h>
29
ba32db1e 30#include "thread.h"
ba32db1e
CF
31#include "prng.h"
32
33#define SCHEDULE_TIMERS 1000000
34#define REMOVE_TIMERS 500000
35
36struct thread_master *master;
37
38static int dummy_func(struct thread *thread)
39{
d62a17ae 40 return 0;
ba32db1e
CF
41}
42
43int main(int argc, char **argv)
44{
d62a17ae 45 struct prng *prng;
46 int i;
47 struct thread **timers;
48 struct timeval tv_start, tv_lap, tv_stop;
49 unsigned long t_schedule, t_remove;
50
51 master = thread_master_create(NULL);
52 prng = prng_new(0);
53 timers = calloc(SCHEDULE_TIMERS, sizeof(*timers));
54
55 /* create thread structures so they won't be allocated during the
56 * time measurement */
57 for (i = 0; i < SCHEDULE_TIMERS; i++) {
58 timers[i] = NULL;
59 thread_add_timer_msec(master, dummy_func, NULL, 0, &timers[i]);
60 }
61 for (i = 0; i < SCHEDULE_TIMERS; i++)
62 thread_cancel(timers[i]);
63
64 monotime(&tv_start);
65
66 for (i = 0; i < SCHEDULE_TIMERS; i++) {
67 long interval_msec;
68
69 interval_msec = prng_rand(prng) % (100 * SCHEDULE_TIMERS);
70 timers[i] = NULL;
71 thread_add_timer_msec(master, dummy_func, NULL, interval_msec,
72 &timers[i]);
73 }
74
75 monotime(&tv_lap);
76
77 for (i = 0; i < REMOVE_TIMERS; i++) {
78 int index;
79
80 index = prng_rand(prng) % SCHEDULE_TIMERS;
81 if (timers[index])
82 thread_cancel(timers[index]);
83 timers[index] = NULL;
84 }
85
86 monotime(&tv_stop);
87
88 t_schedule = 1000 * (tv_lap.tv_sec - tv_start.tv_sec);
89 t_schedule += (tv_lap.tv_usec - tv_start.tv_usec) / 1000;
90
91 t_remove = 1000 * (tv_stop.tv_sec - tv_lap.tv_sec);
92 t_remove += (tv_stop.tv_usec - tv_lap.tv_usec) / 1000;
93
2ec42b85 94 printf("Scheduling %d random timers took %lu.%03lu seconds.\n",
d62a17ae 95 SCHEDULE_TIMERS, t_schedule / 1000, t_schedule % 1000);
2ec42b85 96 printf("Removing %d random timers took %lu.%03lu seconds.\n",
d62a17ae 97 REMOVE_TIMERS, t_remove / 1000, t_remove % 1000);
98 fflush(stdout);
99
100 free(timers);
101 thread_master_free(master);
102 prng_free(prng);
103 return 0;
ba32db1e 104}