2 * Copyright (c) 2018 Red Hat, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "stopwatch.h"
26 #define MAX_SAMPLES 100
31 unsigned long long samples
[MAX_SAMPLES
];
33 struct stopwatch_stats expected_stats
;
36 static struct test_data data_sets
[] = {
38 .name
= "1-interval-zero-length",
52 .name
= "1-interval-unit-length",
66 .name
= "10-intervals-unit-length",
67 .samples
= { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 },
80 .name
= "10-intervals-linear-growth",
81 .samples
= { 1, 2, 4, 7, 11, 16, 22, 29, 37, 46, 56 },
94 .name
= "60-intervals-unit-length",
95 .samples
= { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
96 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
97 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
98 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
99 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
100 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
114 .name
= "60-intervals-linear-growth",
115 .samples
= { 1, 2, 4, 7, 11, 16, 22, 29, 37, 46,
116 56, 67, 79, 92, 106, 121, 137, 154, 172, 191,
117 211, 232, 254, 277, 301, 326, 352, 379, 407, 436,
118 466, 497, 529, 562, 596, 631, 667, 704, 742, 781,
119 821, 862, 904, 947, 991, 1036, 1082, 1129, 1177, 1226,
120 1276, 1327, 1379, 1432, 1486, 1541, 1597, 1654, 1712, 1771,
128 /* 95th percentile is actually closer to 57, but the estimate is
129 * pretty dang close */
137 #define ASSERT_MSG(COND, MSG, ...) \
139 fprintf(stderr, MSG "\n", ##__VA_ARGS__); \
143 #define ASSERT_ULL_EQ(a, b) \
145 "Assertion '%s == %s' failed: %llu == %llu", \
148 #define ASSERT_DOUBLE_EQ(a, b, eps) \
149 ASSERT_MSG(fabs(a - b) < eps, \
150 "Assertion '|%s - %s| < %s' failed: |%g - %g| < %g", \
151 #a, #b, #eps, a, b, eps)
153 #define ASSERT_STATS_EQ(a, b) \
155 ASSERT_ULL_EQ((a)->count, (b)->count); \
156 ASSERT_ULL_EQ((a)->max, (b)->max); \
157 ASSERT_ULL_EQ((a)->min, (b)->min); \
158 ASSERT_DOUBLE_EQ((a)->pctl_95, (b)->pctl_95, 1e-1); \
159 ASSERT_DOUBLE_EQ((a)->ewma_50, (b)->ewma_50, 1e-1); \
160 ASSERT_DOUBLE_EQ((a)->ewma_1, (b)->ewma_1, 1e-1); \
164 test_stopwatch_calculate_stats(void)
168 for (size_t i
= 0; i
< ARRAY_SIZE(data_sets
); i
++) {
171 fprintf(stderr
, "TEST '%s'\n", d
->name
);
173 stopwatch_create(d
->name
, UNIT
);
174 for (size_t j
= 0; j
< d
->num_samples
- 1; j
++) {
175 stopwatch_start(d
->name
, d
->samples
[j
]);
176 stopwatch_stop(d
->name
, d
->samples
[j
+ 1]);
180 struct stopwatch_stats stats
= { .unit
= UNIT
};
181 stopwatch_get_stats(d
->name
, &stats
);
182 ASSERT_STATS_EQ(&stats
, &d
->expected_stats
);
189 test_stopwatch_main(int argc OVS_UNUSED
, char *argv
[] OVS_UNUSED
)
191 test_stopwatch_calculate_stats();
195 OVSTEST_REGISTER("test-stopwatch", test_stopwatch_main
);