Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / tests / profile_test.c
1 /*
2  * Copyright (C) 2014 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  */
19
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 /** @file
23  *
24  * Profiling self-tests
25  *
26  */
27
28 /* Forcibly enable assertions */
29 #undef NDEBUG
30
31 #include <string.h>
32 #include <assert.h>
33 #include <ipxe/test.h>
34 #include <ipxe/profile.h>
35
36 /** A profiling test */
37 struct profile_test {
38         /** Sample values */
39         const unsigned long *samples;
40         /** Number of samples */
41         unsigned int count;
42         /** Expected mean sample value */
43         unsigned long mean;
44         /** Expected standard deviation */
45         unsigned long stddev;
46 };
47
48 /** Define inline data */
49 #define DATA(...) { __VA_ARGS__ }
50
51 /** Define a profiling test */
52 #define PROFILE_TEST( name, MEAN, STDDEV, SAMPLES )                     \
53         static const unsigned long name ## _samples[] = SAMPLES;        \
54         static struct profile_test name = {                             \
55                 .samples = name ## _samples,                            \
56                 .count = ( sizeof ( name ## _samples ) /                \
57                            sizeof ( name ## _samples [0] ) ),           \
58                 .mean = MEAN,                                           \
59                 .stddev = STDDEV,                                       \
60         }
61
62 /** Empty data set */
63 PROFILE_TEST ( empty, 0, 0, DATA() );
64
65 /** Single-element data set (zero) */
66 PROFILE_TEST ( zero, 0, 0, DATA ( 0 ) );
67
68 /** Single-element data set (non-zero) */
69 PROFILE_TEST ( single, 42, 0, DATA ( 42 ) );
70
71 /** Multiple identical element data set */
72 PROFILE_TEST ( identical, 69, 0, DATA ( 69, 69, 69, 69, 69, 69, 69 ) );
73
74 /** Small element data set */
75 PROFILE_TEST ( small, 5, 2, DATA ( 3, 5, 9, 4, 3, 2, 5, 7 ) );
76
77 /** Random data set */
78 PROFILE_TEST ( random, 70198, 394,
79                DATA ( 69772, 70068, 70769, 69653, 70663, 71078, 70101, 70341,
80                       70215, 69600, 70020, 70456, 70421, 69972, 70267, 69999,
81                       69972 ) );
82
83 /** Large-valued random data set */
84 PROFILE_TEST ( large, 93533894UL, 25538UL,
85                DATA ( 93510333UL, 93561169UL, 93492361UL, 93528647UL,
86                       93557566UL, 93503465UL, 93540126UL, 93549020UL,
87                       93502307UL, 93527320UL, 93537152UL, 93540125UL,
88                       93550773UL, 93586731UL, 93521312UL ) );
89
90 /**
91  * Report a profiling test result
92  *
93  * @v test              Profiling test
94  * @v file              Test code file
95  * @v line              Test code line
96  */
97 static void profile_okx ( struct profile_test *test, const char *file,
98                           unsigned int line ) {
99         struct profiler profiler;
100         unsigned long mean;
101         unsigned long stddev;
102         unsigned int i;
103
104         /* Initialise profiler */
105         memset ( &profiler, 0, sizeof ( profiler ) );
106
107         /* Record sample values */
108         for ( i = 0 ; i < test->count ; i++ )
109                 profile_update ( &profiler, test->samples[i] );
110
111         /* Check resulting statistics */
112         mean = profile_mean ( &profiler );
113         stddev = profile_stddev ( &profiler );
114         DBGC ( test, "PROFILE calculated mean %ld stddev %ld\n", mean, stddev );
115         okx ( mean == test->mean, file, line );
116         okx ( stddev == test->stddev, file, line );
117 }
118 #define profile_ok( test ) profile_okx ( test, __FILE__, __LINE__ )
119
120 /**
121  * Perform profiling self-tests
122  *
123  */
124 static void profile_test_exec ( void ) {
125
126         /* Perform profiling tests */
127         profile_ok ( &empty );
128         profile_ok ( &zero );
129         profile_ok ( &single );
130         profile_ok ( &identical );
131         profile_ok ( &small );
132         profile_ok ( &random );
133         profile_ok ( &large );
134 }
135
136 /** Profiling self-test */
137 struct self_test profile_test __self_test = {
138         .name = "profile",
139         .exec = profile_test_exec,
140 };