1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
|
/**
* hdr_histogram_test.c
* Written by Michael Barker and released to the public domain,
* as explained at http://creativecommons.org/publicdomain/zero/1.0/
*/
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <hdr_histogram.h>
#include "minunit.h"
static bool compare_values(double a, double b, double variation)
{
return compare_double(a, b, b * variation);
}
static bool compare_percentile(int64_t a, double b, double variation)
{
return compare_values((double) a, b, variation);
// return fabs(a - b) <= b * variation;
}
int tests_run = 0;
static struct hdr_histogram* raw_histogram = NULL;
static struct hdr_histogram* cor_histogram = NULL;
static struct hdr_histogram* scaled_raw_histogram = NULL;
static struct hdr_histogram* scaled_cor_histogram = NULL;
static void load_histograms()
{
const int64_t highest_trackable_value = INT64_C(3600) * 1000 * 1000;
const int32_t significant_figures = 3;
const int64_t interval = INT64_C(10000);
const int64_t scale = 512;
const int64_t scaled_interval = interval * scale;
int i;
if (raw_histogram)
{
free(raw_histogram);
}
hdr_init(1, highest_trackable_value, significant_figures, &raw_histogram);
if (cor_histogram)
{
free(cor_histogram);
}
hdr_init(1, highest_trackable_value, significant_figures, &cor_histogram);
if (scaled_raw_histogram)
{
free(scaled_raw_histogram);
}
hdr_init(1000, highest_trackable_value * 512, significant_figures, &scaled_raw_histogram);
if (scaled_cor_histogram)
{
free(scaled_cor_histogram);
}
hdr_init(1000, highest_trackable_value * 512, significant_figures, &scaled_cor_histogram);
for (i = 0; i < 10000; i++)
{
hdr_record_value(raw_histogram, 1000);
hdr_record_corrected_value(cor_histogram, 1000, interval);
hdr_record_value(scaled_raw_histogram, 1000 * scale);
hdr_record_corrected_value(scaled_cor_histogram, 1000 * scale, scaled_interval);
}
hdr_record_value(raw_histogram, 100000000);
hdr_record_corrected_value(cor_histogram, 100000000, 10000L);
hdr_record_value(scaled_raw_histogram, 100000000 * scale);
hdr_record_corrected_value(scaled_cor_histogram, 100000000 * scale, scaled_interval);
}
static char* test_create()
{
struct hdr_histogram* h = NULL;
int r = hdr_init(1, INT64_C(3600000000), 3, &h);
mu_assert("Failed to allocate hdr_histogram", r == 0);
mu_assert("Failed to allocate hdr_histogram", h != NULL);
mu_assert("Incorrect array length", compare_int64(h->counts_len, 23552));
free(h);
return 0;
}
static char* test_create_with_large_values()
{
struct hdr_histogram* h = NULL;
int r = hdr_init(20000000, 100000000, 5, &h);
mu_assert("Didn't create", r == 0);
hdr_record_value(h, 100000000);
hdr_record_value(h, 20000000);
hdr_record_value(h, 30000000);
mu_assert(
"50.0% Percentile",
hdr_values_are_equivalent(h, 20000000, hdr_value_at_percentile(h, 50.0)));
mu_assert(
"83.33% Percentile",
hdr_values_are_equivalent(h, 30000000, hdr_value_at_percentile(h, 83.33)));
mu_assert(
"83.34% Percentile",
hdr_values_are_equivalent(h, 100000000, hdr_value_at_percentile(h, 83.34)));
mu_assert(
"99.0% Percentile",
hdr_values_are_equivalent(h, 100000000, hdr_value_at_percentile(h, 99.0)));
return 0;
}
static char* test_invalid_significant_figures()
{
struct hdr_histogram* h = NULL;
int r = hdr_alloc(36000000, -1, &h);
mu_assert("Result was not EINVAL", r == EINVAL);
mu_assert("Histogram was not null", h == 0);
r = hdr_alloc(36000000, 6, &h);
mu_assert("Result was not EINVAL", r == EINVAL);
mu_assert("Histogram was not null", h == 0);
return 0;
}
static char* test_invalid_init()
{
struct hdr_histogram* h = NULL;
mu_assert("Should not allow 0 as lowest trackable value", EINVAL == hdr_init(0, 64*1024, 2, &h));
mu_assert("Should have lowest < 2 * highest", EINVAL == hdr_init(80, 110, 5, &h));
return 0;
}
static char* test_total_count()
{
load_histograms();
mu_assert("Total raw count != 10001", raw_histogram->total_count == 10001);
mu_assert("Total corrected count != 20000", cor_histogram->total_count == 20000);
return 0;
}
static char* test_get_max_value()
{
load_histograms();
int64_t actual_raw_max = hdr_max(raw_histogram);
mu_assert("hdr_max(raw_histogram) != 100000000L",
hdr_values_are_equivalent(raw_histogram, actual_raw_max, 100000000));
int64_t actual_cor_max = hdr_max(cor_histogram);
mu_assert("hdr_max(cor_histogram) != 100000000L",
hdr_values_are_equivalent(cor_histogram, actual_cor_max, 100000000));
return 0;
}
static char* test_get_min_value()
{
load_histograms();
mu_assert("hdr_min(raw_histogram) != 1000", hdr_min(raw_histogram) == 1000);
mu_assert("hdr_min(cor_histogram) != 1000", hdr_min(cor_histogram) == 1000);
return 0;
}
static char* test_percentiles()
{
load_histograms();
mu_assert("Value at 30% not 1000.0",
compare_percentile(hdr_value_at_percentile(raw_histogram, 30.0), 1000.0, 0.001));
mu_assert("Value at 99% not 1000.0",
compare_percentile(hdr_value_at_percentile(raw_histogram, 99.0), 1000.0, 0.001));
mu_assert("Value at 99.99% not 1000.0",
compare_percentile(hdr_value_at_percentile(raw_histogram, 99.99), 1000.0, 0.001));
mu_assert("Value at 99.999% not 100000000.0",
compare_percentile(hdr_value_at_percentile(raw_histogram, 99.999), 100000000.0, 0.001));
mu_assert("Value at 100% not 100000000.0",
compare_percentile(hdr_value_at_percentile(raw_histogram, 100.0), 100000000.0, 0.001));
mu_assert("Value at 30% not 1000.0",
compare_percentile(hdr_value_at_percentile(cor_histogram, 30.0), 1000.0, 0.001));
mu_assert("Value at 50% not 1000.0",
compare_percentile(hdr_value_at_percentile(cor_histogram, 50.0), 1000.0, 0.001));
mu_assert("Value at 75% not 50000000.0",
compare_percentile(hdr_value_at_percentile(cor_histogram, 75.0), 50000000.0, 0.001));
mu_assert("Value at 90% not 80000000.0",
compare_percentile(hdr_value_at_percentile(cor_histogram, 90.0), 80000000.0, 0.001));
mu_assert("Value at 99% not 98000000.0",
compare_percentile(hdr_value_at_percentile(cor_histogram, 99.0), 98000000.0, 0.001));
mu_assert("Value at 99.999% not 100000000.0",
compare_percentile(hdr_value_at_percentile(cor_histogram, 99.999), 100000000.0, 0.001));
mu_assert("Value at 100% not 100000000.0",
compare_percentile(hdr_value_at_percentile(cor_histogram, 100.0), 100000000.0, 0.001));
return 0;
}
static char* test_recorded_values()
{
load_histograms();
struct hdr_iter iter;
int index;
// Raw Histogram
hdr_iter_recorded_init(&iter, raw_histogram);
index = 0;
while (hdr_iter_next(&iter))
{
int64_t count_added_in_this_bucket = iter.specifics.recorded.count_added_in_this_iteration_step;
if (index == 0)
{
mu_assert("Value at 0 is not 10000", count_added_in_this_bucket == 10000);
}
else
{
mu_assert("Value at 1 is not 1", count_added_in_this_bucket == 1);
}
index++;
}
mu_assert("Should have encountered 2 values", index == 2);
// Corrected Histogram
hdr_iter_recorded_init(&iter, cor_histogram);
index = 0;
int64_t total_added_count = 0;
while (hdr_iter_next(&iter))
{
int64_t count_added_in_this_bucket = iter.specifics.recorded.count_added_in_this_iteration_step;
if (index == 0)
{
mu_assert("Count at 0 is not 10000", count_added_in_this_bucket == 10000);
}
mu_assert("Count should not be 0", iter.count != 0);
mu_assert("Count at value iterated to should be count added in this step",
iter.count == count_added_in_this_bucket);
total_added_count += count_added_in_this_bucket;
index++;
}
mu_assert("Total counts should be 20000", total_added_count == 20000);
return 0;
}
static char* test_linear_values()
{
load_histograms();
struct hdr_iter iter;
int index;
// Raw Histogram
hdr_iter_linear_init(&iter, raw_histogram, 100000);
index = 0;
while (hdr_iter_next(&iter))
{
int64_t count_added_in_this_bucket = iter.specifics.linear.count_added_in_this_iteration_step;
if (index == 0)
{
mu_assert("Count at 0 is not 10000", count_added_in_this_bucket == 10000);
}
else if (index == 999)
{
mu_assert("Count at 999 is not 1", count_added_in_this_bucket == 1);
}
else
{
mu_assert("Count should be 0", count_added_in_this_bucket == 0);
}
index++;
}
mu_assert("Should of met 1000 values", compare_int64(index, 1000));
// Corrected Histogram
hdr_iter_linear_init(&iter, cor_histogram, 10000);
index = 0;
int64_t total_added_count = 0;
while (hdr_iter_next(&iter))
{
int64_t count_added_in_this_bucket = iter.specifics.linear.count_added_in_this_iteration_step;
if (index == 0)
{
mu_assert("Count at 0 is not 10001", count_added_in_this_bucket == 10001);
}
total_added_count += count_added_in_this_bucket;
index++;
}
mu_assert("Should of met 10001 values", index == 10000);
mu_assert("Should of met 20000 counts", total_added_count == 20000);
return 0;
}
static char* test_logarithmic_values()
{
load_histograms();
struct hdr_iter iter;
int index;
hdr_iter_log_init(&iter, raw_histogram, 10000, 2.0);
index = 0;
while(hdr_iter_next(&iter))
{
long count_added_in_this_bucket = iter.specifics.log.count_added_in_this_iteration_step;
if (index == 0)
{
mu_assert("Raw Logarithmic 10 msec bucket # 0 added a count of 10000", 10000 == count_added_in_this_bucket);
}
else if (index == 14)
{
mu_assert("Raw Logarithmic 10 msec bucket # 14 added a count of 1", 1 == count_added_in_this_bucket);
}
else
{
mu_assert("Raw Logarithmic 10 msec bucket added a count of 0", 0 == count_added_in_this_bucket);
}
index++;
}
mu_assert("Should of seen 14 values", index - 1 == 14);
hdr_iter_log_init(&iter, cor_histogram, 10000, 2.0);
index = 0;
int total_added_count = 0;
while (hdr_iter_next(&iter))
{
long count_added_in_this_bucket = iter.specifics.log.count_added_in_this_iteration_step;
if (index == 0)
{
mu_assert("Corrected Logarithmic 10 msec bucket # 0 added a count of 10001", 10001 == count_added_in_this_bucket);
}
total_added_count += count_added_in_this_bucket;
index++;
}
mu_assert("Should of seen 14 values", index - 1 == 14);
mu_assert("Should of seen count of 20000", total_added_count == 20000);
return 0;
}
static char* test_reset()
{
load_histograms();
// before
mu_assert("Value at 99% == 0.0", hdr_value_at_percentile(raw_histogram, 99.0) != 0);
mu_assert("Value at 99% == 0.0", hdr_value_at_percentile(cor_histogram, 99.0) != 0);
hdr_reset(raw_histogram);
hdr_reset(cor_histogram);
//after
mu_assert("Total raw count != 0", raw_histogram->total_count == 0);
mu_assert("Total corrected count != 0", cor_histogram->total_count == 0);
mu_assert("Value at 99% not 0.0", hdr_value_at_percentile(raw_histogram, 99.0) == 0);
mu_assert("Value at 99% not 0.0", hdr_value_at_percentile(cor_histogram, 99.0) == 0);
return 0;
}
static char* test_scaling_equivalence()
{
load_histograms();
mu_assert(
"Averages should be equivalent",
compare_values(
hdr_mean(cor_histogram) * 512,
hdr_mean(scaled_cor_histogram),
0.000001));
mu_assert(
"Total count should be equivalent",
compare_int64(
cor_histogram->total_count,
scaled_cor_histogram->total_count));
int64_t expected_99th = hdr_value_at_percentile(cor_histogram, 99.0) * 512;
int64_t scaled_99th = hdr_value_at_percentile(scaled_cor_histogram, 99.0);
mu_assert(
"99%'iles should be equivalent",
compare_int64(
hdr_lowest_equivalent_value(cor_histogram, expected_99th),
hdr_lowest_equivalent_value(scaled_cor_histogram, scaled_99th)));
return 0;
}
static char* test_out_of_range_values()
{
struct hdr_histogram *h;
hdr_init(1, 1000, 4, &h);
mu_assert("Should successfully record value", hdr_record_value(h, 32767));
mu_assert("Should not record value", !hdr_record_value(h, 32768));
return 0;
}
static struct mu_result all_tests()
{
mu_run_test(test_create);
mu_run_test(test_invalid_init);
mu_run_test(test_create_with_large_values);
mu_run_test(test_invalid_significant_figures);
mu_run_test(test_total_count);
mu_run_test(test_get_min_value);
mu_run_test(test_get_max_value);
mu_run_test(test_percentiles);
mu_run_test(test_recorded_values);
mu_run_test(test_linear_values);
mu_run_test(test_logarithmic_values);
mu_run_test(test_reset);
mu_run_test(test_scaling_equivalence);
mu_run_test(test_out_of_range_values);
mu_ok;
}
static int hdr_histogram_run_tests()
{
struct mu_result result = all_tests();
if (result.message != 0)
{
printf("hdr_histogram_test.%s(): %s\n", result.test, result.message);
}
else
{
printf("ALL TESTS PASSED\n");
}
printf("Tests run: %d\n", tests_run);
return result.message == NULL ? 0 : -1;
}
int main()
{
return hdr_histogram_run_tests();
}
|