2 * Copyright (C) 2014 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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
20 FILE_LICENCE ( GPL2_OR_LATER );
24 * Profiling self-tests
28 /* Forcibly enable assertions */
33 #include <ipxe/test.h>
34 #include <ipxe/profile.h>
36 /** A profiling test */
39 const unsigned long *samples;
40 /** Number of samples */
42 /** Expected mean sample value */
44 /** Expected standard deviation */
48 /** Define inline data */
49 #define DATA(...) { __VA_ARGS__ }
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] ) ), \
63 PROFILE_TEST ( empty, 0, 0, DATA() );
65 /** Single-element data set (zero) */
66 PROFILE_TEST ( zero, 0, 0, DATA ( 0 ) );
68 /** Single-element data set (non-zero) */
69 PROFILE_TEST ( single, 42, 0, DATA ( 42 ) );
71 /** Multiple identical element data set */
72 PROFILE_TEST ( identical, 69, 0, DATA ( 69, 69, 69, 69, 69, 69, 69 ) );
74 /** Small element data set */
75 PROFILE_TEST ( small, 5, 2, DATA ( 3, 5, 9, 4, 3, 2, 5, 7 ) );
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,
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 ) );
91 * Report a profiling test result
93 * @v test Profiling test
94 * @v file Test code file
95 * @v line Test code line
97 static void profile_okx ( struct profile_test *test, const char *file,
99 struct profiler profiler;
101 unsigned long stddev;
104 /* Initialise profiler */
105 memset ( &profiler, 0, sizeof ( profiler ) );
107 /* Record sample values */
108 for ( i = 0 ; i < test->count ; i++ )
109 profile_update ( &profiler, test->samples[i] );
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 );
118 #define profile_ok( test ) profile_okx ( test, __FILE__, __LINE__ )
121 * Perform profiling self-tests
124 static void profile_test_exec ( void ) {
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 );
136 /** Profiling self-test */
137 struct self_test profile_test __self_test = {
139 .exec = profile_test_exec,