1*da668aa1SThomas Huth /*
2*da668aa1SThomas Huth * Timed average computation tests
3*da668aa1SThomas Huth *
4*da668aa1SThomas Huth * Copyright Nodalink, EURL. 2014
5*da668aa1SThomas Huth *
6*da668aa1SThomas Huth * Authors:
7*da668aa1SThomas Huth * Benoît Canet <benoit.canet@nodalink.com>
8*da668aa1SThomas Huth *
9*da668aa1SThomas Huth * This work is licensed under the terms of the GNU LGPL, version 2 or later.
10*da668aa1SThomas Huth * See the COPYING.LIB file in the top-level directory.
11*da668aa1SThomas Huth */
12*da668aa1SThomas Huth
13*da668aa1SThomas Huth #include "qemu/osdep.h"
14*da668aa1SThomas Huth #include "sysemu/cpu-timers.h"
15*da668aa1SThomas Huth #include "qemu/timed-average.h"
16*da668aa1SThomas Huth
17*da668aa1SThomas Huth /* This is the clock for QEMU_CLOCK_VIRTUAL */
18*da668aa1SThomas Huth static int64_t my_clock_value;
19*da668aa1SThomas Huth
cpu_get_clock(void)20*da668aa1SThomas Huth int64_t cpu_get_clock(void)
21*da668aa1SThomas Huth {
22*da668aa1SThomas Huth return my_clock_value;
23*da668aa1SThomas Huth }
24*da668aa1SThomas Huth
account(TimedAverage * ta)25*da668aa1SThomas Huth static void account(TimedAverage *ta)
26*da668aa1SThomas Huth {
27*da668aa1SThomas Huth timed_average_account(ta, 1);
28*da668aa1SThomas Huth timed_average_account(ta, 5);
29*da668aa1SThomas Huth timed_average_account(ta, 2);
30*da668aa1SThomas Huth timed_average_account(ta, 4);
31*da668aa1SThomas Huth timed_average_account(ta, 3);
32*da668aa1SThomas Huth }
33*da668aa1SThomas Huth
test_average(void)34*da668aa1SThomas Huth static void test_average(void)
35*da668aa1SThomas Huth {
36*da668aa1SThomas Huth TimedAverage ta;
37*da668aa1SThomas Huth uint64_t result;
38*da668aa1SThomas Huth int i;
39*da668aa1SThomas Huth
40*da668aa1SThomas Huth /* we will compute some average on a period of 1 second */
41*da668aa1SThomas Huth timed_average_init(&ta, QEMU_CLOCK_VIRTUAL, NANOSECONDS_PER_SECOND);
42*da668aa1SThomas Huth
43*da668aa1SThomas Huth result = timed_average_min(&ta);
44*da668aa1SThomas Huth g_assert(result == 0);
45*da668aa1SThomas Huth result = timed_average_avg(&ta);
46*da668aa1SThomas Huth g_assert(result == 0);
47*da668aa1SThomas Huth result = timed_average_max(&ta);
48*da668aa1SThomas Huth g_assert(result == 0);
49*da668aa1SThomas Huth
50*da668aa1SThomas Huth for (i = 0; i < 100; i++) {
51*da668aa1SThomas Huth account(&ta);
52*da668aa1SThomas Huth result = timed_average_min(&ta);
53*da668aa1SThomas Huth g_assert(result == 1);
54*da668aa1SThomas Huth result = timed_average_avg(&ta);
55*da668aa1SThomas Huth g_assert(result == 3);
56*da668aa1SThomas Huth result = timed_average_max(&ta);
57*da668aa1SThomas Huth g_assert(result == 5);
58*da668aa1SThomas Huth my_clock_value += NANOSECONDS_PER_SECOND / 10;
59*da668aa1SThomas Huth }
60*da668aa1SThomas Huth
61*da668aa1SThomas Huth my_clock_value += NANOSECONDS_PER_SECOND * 100;
62*da668aa1SThomas Huth
63*da668aa1SThomas Huth result = timed_average_min(&ta);
64*da668aa1SThomas Huth g_assert(result == 0);
65*da668aa1SThomas Huth result = timed_average_avg(&ta);
66*da668aa1SThomas Huth g_assert(result == 0);
67*da668aa1SThomas Huth result = timed_average_max(&ta);
68*da668aa1SThomas Huth g_assert(result == 0);
69*da668aa1SThomas Huth
70*da668aa1SThomas Huth for (i = 0; i < 100; i++) {
71*da668aa1SThomas Huth account(&ta);
72*da668aa1SThomas Huth result = timed_average_min(&ta);
73*da668aa1SThomas Huth g_assert(result == 1);
74*da668aa1SThomas Huth result = timed_average_avg(&ta);
75*da668aa1SThomas Huth g_assert(result == 3);
76*da668aa1SThomas Huth result = timed_average_max(&ta);
77*da668aa1SThomas Huth g_assert(result == 5);
78*da668aa1SThomas Huth my_clock_value += NANOSECONDS_PER_SECOND / 10;
79*da668aa1SThomas Huth }
80*da668aa1SThomas Huth }
81*da668aa1SThomas Huth
main(int argc,char ** argv)82*da668aa1SThomas Huth int main(int argc, char **argv)
83*da668aa1SThomas Huth {
84*da668aa1SThomas Huth /* tests in the same order as the header function declarations */
85*da668aa1SThomas Huth g_test_init(&argc, &argv, NULL);
86*da668aa1SThomas Huth g_test_add_func("/timed-average/average", test_average);
87*da668aa1SThomas Huth return g_test_run();
88*da668aa1SThomas Huth }
89*da668aa1SThomas Huth
90