Debian Codegraph @

tests/isc/histo_test.c

raw
1 /*
2 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3 *
4 * SPDX-License-Identifier: MPL-2.0
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public
7 * License, v. 2.0. If a copy of the MPL was not distributed with this
8 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
9 *
10 * See the COPYRIGHT file distributed with this work for additional
11 * information regarding copyright ownership.
12 */
13
14 /* ! \file */
15
16 #include <inttypes.h>
17 #include <math.h>
18 #include <sched.h> /* IWYU pragma: keep */
19 #include <setjmp.h>
20 #include <stdarg.h>
21 #include <stddef.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #define UNIT_TESTING
26 #include <cmocka.h>
27
28 #include <isc/histo.h>
29 #include <isc/result.h>
30 #include <isc/time.h>
31
32 #include <tests/isc.h>
33
34 #define TIME_LIMIT (123 * NS_PER_MS)
35
36 #define SUBRANGE 69
37
38 #if VERBOSE
39
40 #define TRACE(fmt, ...) \
41 fprintf(stderr, "%s:%u:%s(): " fmt "\n", __FILE__, __LINE__, __func__, \
42 __VA_ARGS__)
43
44 #define TRACETIME(fmt, ...) \
45 TRACE("%u bits %.1f ms " fmt, bits, millis_since(start), ##__VA_ARGS__)
46
47 static double
48 millis_since(isc_nanosecs_t start) {
49 isc_nanosecs_t end = isc_time_monotonic();
50 return (double)(end - start) / NS_PER_MS;
51 }
52
53 #else
54 #define TRACE(...)
55 #define TRACETIME(...) UNUSED(start)
56 #endif
57
58 /*
59 * Note: in many of these tests when adding data to a histogram,
60 * we need to iterate using `key++` instead of `isc_histo_next()`
61 * because the latter skips chunks that we want to fill but have
62 * not yet done so.
63 */
64
65 ISC_RUN_TEST_IMPL(basics) {
66 isc_result_t result;
67 for (uint bits = ISC_HISTO_MINBITS; bits <= ISC_HISTO_MAXBITS; bits++) {
68 isc_nanosecs_t start = isc_time_monotonic();
69
70 isc_histo_t *hg = NULL;
71 isc_histo_create(mctx, bits, &hg);
72
73 isc_histo_inc(hg, 0);
74
75 uint64_t min, max, count;
76
77 uint64_t prev_max = 0;
78 uint key = 0;
79 result = isc_histo_get(hg, key, &min, &max, &count);
80 while (result == ISC_R_SUCCESS) {
81 /* previous iteration already bumped this bucket */
82 assert_int_equal(count, 1);
83
84 /* min maps to this bucket */
85 isc_histo_inc(hg, min);
86 result = isc_histo_get(hg, key, &min, &max, &count);
87 assert_int_equal(result, ISC_R_SUCCESS);
88 assert_int_equal(count, 2);
89
90 /* max maps to this bucket */
91 isc_histo_add(hg, max, 2);
92 result = isc_histo_get(hg, key, &min, &max, &count);
93 assert_int_equal(result, ISC_R_SUCCESS);
94 assert_int_equal(count, 4);
95
96 /* put range covers this bucket */
97 isc_histo_put(hg, min, max, 4);
98 result = isc_histo_get(hg, key, &min, &max, &count);
99 assert_int_equal(result, ISC_R_SUCCESS);
100 assert_int_equal(count, 8);
101
102 if (max < UINT64_MAX) {
103 /* max + 1 maps to next bucket */
104 isc_histo_inc(hg, max + 1);
105 result = isc_histo_get(hg, key, &min, &max,
106 &count);
107 assert_int_equal(result, ISC_R_SUCCESS);
108 /* this bucket was not bumped */
109 assert_int_equal(count, 8);
110 }
111
112 if (key == 0) {
113 assert_int_equal(min, 0);
114 assert_int_equal(max, 0);
115 } else {
116 /* no gap between buckets */
117 assert_int_equal(min, prev_max + 1);
118 }
119
120 prev_max = max;
121 key++;
122 result = isc_histo_get(hg, key, &min, &max, &count);
123
124 /* these tests can be slow */
125 if (isc_time_monotonic() > start + TIME_LIMIT) {
126 break;
127 }
128 }
129
130 /* if we did not stop early */
131 if (result != ISC_R_SUCCESS) {
132 /* last bucket goes up to last possible value */
133 assert_int_equal(max, UINT64_MAX);
134
135 double pop;
136 isc_histo_moments(hg, &pop, NULL, NULL);
137 assert_int_equal((uint64_t)pop, key * 8);
138 }
139
140 isc_histo_destroy(&hg);
141
142 TRACETIME("%u keys", key);
143 }
144 }
145
146 ISC_RUN_TEST_IMPL(quantiles) {
147 for (uint bits = ISC_HISTO_MINBITS; bits <= ISC_HISTO_MAXBITS; bits++) {
148 isc_result_t result;
149 uint64_t min, max, count;
150 double pop, mean, sd;
151 uint key;
152
153 isc_nanosecs_t start = isc_time_monotonic();
154
155 isc_histo_t *hg = NULL;
156 isc_histo_create(mctx, bits, &hg);
157
158 /* ensure empty histogram does not divide by zero */
159 isc_histo_moments(hg, &pop, &mean, &sd);
160 assert_true(pop == 0.0);
161 assert_true(mean == 0.0);
162 assert_true(sd == 0.0);
163
164 for (key = 0; isc_histo_get(hg, key, &min, &max, &count) ==
165 ISC_R_SUCCESS;
166 key++)
167 {
168 /* inc twice so we can check bucket's midpoint */
169 assert_int_equal(count, 0);
170 isc_histo_inc(hg, min);
171 isc_histo_inc(hg, max);
172 }
173
174 const uint buckets = key;
175
176 /* no incs were lost */
177 isc_histo_moments(hg, &pop, NULL, NULL);
178 assert_float_equal(pop, buckets * 2, 0.5);
179
180 /* two ranks per bucket */
181 const uint quantum = ISC_HISTO_MAXQUANTILES / 2 - 1;
182 uint64_t value[ISC_HISTO_MAXQUANTILES];
183 double frac[ISC_HISTO_MAXQUANTILES];
184 uint base = 0;
185
186 for (key = 0; key < buckets; key++) {
187 /* fill in the values one quantum at a time */
188 if (key == 0 || key % quantum == buckets % quantum) {
189 base = key;
190 for (uint k = 0; k < quantum; k++) {
191 double rank = (base + k) * 2;
192 uint i = (quantum - k) * 2;
193 frac[i - 1] = (rank + 1.0) / pop;
194 frac[i - 0] = rank / pop;
195 }
196 frac[0] = (base + quantum) * 2 / pop;
197 result = isc_histo_quantiles(
198 hg, quantum * 2 + 1, frac, value);
199 assert_int_equal(result, ISC_R_SUCCESS);
200 }
201
202 result = isc_histo_get(hg, key, &min, &max, &count);
203 assert_int_equal(result, ISC_R_SUCCESS);
204 assert_int_equal(count, 2);
205
206 uint64_t lomin = min == 0 ? min : min - 1;
207 uint64_t himin = min;
208 uint64_t lomid = floor(min / 2.0 + max / 2.0);
209 uint64_t himid = ceil(min / 2.0 + max / 2.0);
210 uint64_t lomax = max;
211 uint64_t himax = max == UINT64_MAX ? max : max + 1;
212
213 uint i = (quantum + base - key) * 2;
214
215 /* check fenceposts */
216 assert_in_range(value[i - 0], lomin, himin);
217 assert_in_range(value[i - 1], lomid, himid);
218 assert_in_range(value[i - 2], lomax, himax);
219
220 /* these tests can be slow */
221 if (isc_time_monotonic() > start + TIME_LIMIT) {
222 break;
223 }
224 }
225
226 isc_histo_destroy(&hg);
227
228 TRACETIME("");
229 }
230 }
231
232 /*
233 * ensure relative error is as expected
234 */
235 ISC_RUN_TEST_IMPL(sigfigs) {
236 assert_int_equal(ISC_HISTO_MINBITS,
237 isc_histo_digits_to_bits(ISC_HISTO_MINDIGITS));
238 assert_int_equal(ISC_HISTO_MINDIGITS,
239 isc_histo_bits_to_digits(ISC_HISTO_MINBITS));
240 assert_int_equal(ISC_HISTO_MAXBITS,
241 isc_histo_digits_to_bits(ISC_HISTO_MAXDIGITS));
242 assert_int_equal(ISC_HISTO_MAXDIGITS,
243 isc_histo_bits_to_digits(ISC_HISTO_MAXBITS));
244
245 uint log10 = 1;
246 double exp10 = 1.0; /* sigdigs == 1 gives relative error of 1 */
247
248 for (uint bits = ISC_HISTO_MINBITS; bits <= ISC_HISTO_MAXBITS; bits++) {
249 isc_histo_t *hg = NULL;
250 isc_histo_create(mctx, bits, &hg);
251
252 uint digits = isc_histo_bits_to_digits(bits);
253 assert_true(bits >= isc_histo_digits_to_bits(digits));
254
255 if (log10 < digits) {
256 log10 += 1;
257 exp10 *= 10.0;
258 assert_int_equal(log10, digits);
259 }
260
261 TRACE("%u binary %f decimal", 1 << bits, exp10);
262
263 /* binary precision is better than decimal precision */
264 double nominal = 1.0 / (double)(1 << bits);
265 assert_true(nominal < 1.0 / exp10);
266
267 /* start with key = 1 to avoid division by zero */
268 uint64_t imin, imax;
269 for (uint key = 1; isc_histo_get(hg, key, &imin, &imax, NULL) ==
270 ISC_R_SUCCESS;
271 key++)
272 {
273 double min = (double)imin;
274 double max = (double)imax;
275 double error = (max - min) / (max + min);
276 assert_true(error < nominal);
277 }
278
279 isc_histo_destroy(&hg);
280 }
281 }
282
283 ISC_RUN_TEST_IMPL(subrange) {
284 for (uint bits = ISC_HISTO_MINBITS; bits <= ISC_HISTO_MAXBITS; bits++) {
285 isc_result_t result;
286 uint64_t min, max, count;
287
288 isc_nanosecs_t start = isc_time_monotonic();
289
290 isc_histo_t *hg = NULL;
291 isc_histo_create(mctx, bits, &hg);
292
293 uint64_t value[SUBRANGE + 1];
294 double frac[SUBRANGE + 1];
295 for (uint i = 0; i <= SUBRANGE; i++) {
296 frac[i] = (double)(SUBRANGE - i) / (double)(SUBRANGE);
297 }
298
299 result = isc_histo_quantiles(hg, ARRAY_SIZE(frac), frac, value);
300 assert_int_equal(result, ISC_R_UNSET);
301
302 for (uint key = 0, top = SUBRANGE - 1;; key++, top++) {
303 if (isc_histo_get(hg, key, &min, NULL, NULL) !=
304 ISC_R_SUCCESS)
305 {
306 break;
307 }
308 if (isc_histo_get(hg, top, NULL, &max, NULL) !=
309 ISC_R_SUCCESS)
310 {
311 break;
312 }
313 /*
314 * If we try adding more than one sample per bucket
315 * here, the test fails when buckets have different
316 * sizes because [min,max] spans multiple chunks.
317 */
318 isc_histo_put(hg, min, max, SUBRANGE);
319
320 result = isc_histo_quantiles(hg, ARRAY_SIZE(frac), frac,
321 value);
322 assert_int_equal(result, ISC_R_SUCCESS);
323
324 for (uint bucket = 0; bucket < SUBRANGE; bucket++) {
325 result = isc_histo_get(hg, key + bucket, &min,
326 &max, &count);
327 assert_int_equal(result, ISC_R_SUCCESS);
328 /* did isc_histo_put() spread evenly? */
329 assert_int_equal(count, 1);
330 /* do the quantile values match? */
331 assert_int_equal(value[SUBRANGE - bucket], min);
332 }
333 assert_int_equal(value[0], max);
334
335 isc_histo_destroy(&hg);
336 isc_histo_create(mctx, bits, &hg);
337
338 /* these tests can be slow */
339 if (isc_time_monotonic() > start + TIME_LIMIT) {
340 break;
341 }
342 }
343 isc_histo_destroy(&hg);
344
345 TRACETIME("");
346 }
347 }
348
349 ISC_TEST_LIST_START
350
351 ISC_TEST_ENTRY(basics)
352 ISC_TEST_ENTRY(quantiles)
353 ISC_TEST_ENTRY(sigfigs)
354 ISC_TEST_ENTRY(subrange)
355
356 ISC_TEST_LIST_END
357
358 ISC_TEST_MAIN