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
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
28 * Profiling self-tests
32 /* Forcibly enable assertions */
37 #include <ipxe/test.h>
38 #include <ipxe/profile.h>
40 /** A profiling test */
43 const unsigned long *samples;
44 /** Number of samples */
46 /** Expected mean sample value */
48 /** Expected standard deviation */
52 /** Define inline data */
53 #define DATA(...) { __VA_ARGS__ }
55 /** Define a profiling test */
56 #define PROFILE_TEST( name, MEAN, STDDEV, SAMPLES ) \
57 static const unsigned long name ## _samples[] = SAMPLES; \
58 static struct profile_test name = { \
59 .samples = name ## _samples, \
60 .count = ( sizeof ( name ## _samples ) / \
61 sizeof ( name ## _samples [0] ) ), \
67 PROFILE_TEST ( empty, 0, 0, DATA() );
69 /** Single-element data set (zero) */
70 PROFILE_TEST ( zero, 0, 0, DATA ( 0 ) );
72 /** Single-element data set (non-zero) */
73 PROFILE_TEST ( single, 42, 0, DATA ( 42 ) );
75 /** Multiple identical element data set */
76 PROFILE_TEST ( identical, 69, 0, DATA ( 69, 69, 69, 69, 69, 69, 69 ) );
78 /** Small element data set */
79 PROFILE_TEST ( small, 5, 2, DATA ( 3, 5, 9, 4, 3, 2, 5, 7 ) );
81 /** Random data set */
82 PROFILE_TEST ( random, 70198, 394,
83 DATA ( 69772, 70068, 70769, 69653, 70663, 71078, 70101, 70341,
84 70215, 69600, 70020, 70456, 70421, 69972, 70267, 69999,
87 /** Large-valued random data set */
88 PROFILE_TEST ( large, 93533894UL, 25538UL,
89 DATA ( 93510333UL, 93561169UL, 93492361UL, 93528647UL,
90 93557566UL, 93503465UL, 93540126UL, 93549020UL,
91 93502307UL, 93527320UL, 93537152UL, 93540125UL,
92 93550773UL, 93586731UL, 93521312UL ) );
95 * Report a profiling test result
97 * @v test Profiling test
98 * @v file Test code file
99 * @v line Test code line
101 static void profile_okx ( struct profile_test *test, const char *file,
102 unsigned int line ) {
103 struct profiler profiler;
105 unsigned long stddev;
108 /* Initialise profiler */
109 memset ( &profiler, 0, sizeof ( profiler ) );
111 /* Record sample values */
112 for ( i = 0 ; i < test->count ; i++ )
113 profile_update ( &profiler, test->samples[i] );
115 /* Check resulting statistics */
116 mean = profile_mean ( &profiler );
117 stddev = profile_stddev ( &profiler );
118 DBGC ( test, "PROFILE calculated mean %ld stddev %ld\n", mean, stddev );
119 okx ( mean == test->mean, file, line );
120 okx ( stddev == test->stddev, file, line );
122 #define profile_ok( test ) profile_okx ( test, __FILE__, __LINE__ )
125 * Perform profiling self-tests
128 static void profile_test_exec ( void ) {
130 /* Perform profiling tests */
131 profile_ok ( &empty );
132 profile_ok ( &zero );
133 profile_ok ( &single );
134 profile_ok ( &identical );
135 profile_ok ( &small );
136 profile_ok ( &random );
137 profile_ok ( &large );
140 /** Profiling self-test */
141 struct self_test profile_test __self_test = {
143 .exec = profile_test_exec,