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 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
|
/**
* 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/hdr_histogram.h>
#include <hdr/hdr_interval_recorder.h>
#include "minunit.h"
#include "hdr_test_util.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);
}
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(void)
{
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(void)
{
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(void)
{
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(void)
{
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(void)
{
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(void)
{
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(void)
{
int64_t actual_raw_max, actual_cor_max;
load_histograms();
actual_raw_max = hdr_max(raw_histogram);
mu_assert("hdr_max(raw_histogram) != 100000000L",
hdr_values_are_equivalent(raw_histogram, actual_raw_max, 100000000));
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(void)
{
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(void)
{
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_percentiles_by_value_at_percentiles(void)
{
load_histograms();
int64_t values[5] = { 0 };
double percentiles[5] = { 30.0, 99.0, 99.99, 99.999, 100.0 };
mu_assert(
"value_at_percentiles return should be 0", hdr_value_at_percentiles(raw_histogram, percentiles, values, 5) == 0);
mu_assert("Value at 30% not 1000.0", compare_percentile(values[0], 1000.0, 0.001));
mu_assert("Value at 99% not 1000.0", compare_percentile(values[1], 1000.0, 0.001));
mu_assert("Value at 99.99% not 1000.0", compare_percentile(values[2], 1000.0, 0.001));
mu_assert("Value at 99.999% not 100000000.0", compare_percentile(values[3], 100000000.0, 0.001));
mu_assert("Value at 100% not 100000000.0", compare_percentile(values[4], 100000000.0, 0.001));
return 0;
}
static char* test_recorded_values(void)
{
struct hdr_iter iter;
int index;
int64_t total_added_count = 0;
load_histograms();
/* 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;
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(void)
{
struct hdr_iter iter;
int index;
int64_t total_added_count;
load_histograms();
/* 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;
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(void)
{
struct hdr_iter iter;
int index;
uint64_t total_added_count;
load_histograms();
hdr_iter_log_init(&iter, raw_histogram, 10000, 2.0);
index = 0;
while(hdr_iter_next(&iter))
{
uint64_t 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;
total_added_count = 0;
while (hdr_iter_next(&iter))
{
uint64_t 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(void)
{
load_histograms();
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);
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(void)
{
int64_t expected_99th, scaled_99th;
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));
expected_99th = hdr_value_at_percentile(cor_histogram, 99.0) * 512;
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(void)
{
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 char* test_linear_iter_buckets_correctly(void)
{
int step_count = 0;
int64_t total_count = 0;
struct hdr_histogram *h;
struct hdr_iter iter;
hdr_init(1, 255, 2, &h);
hdr_record_value(h, 193);
hdr_record_value(h, 255);
hdr_record_value(h, 0);
hdr_record_value(h, 1);
hdr_record_value(h, 64);
hdr_record_value(h, 128);
hdr_iter_linear_init(&iter, h, 64);
while (hdr_iter_next(&iter))
{
total_count += iter.specifics.linear.count_added_in_this_iteration_step;
/* start - changes to reproduce issue */
if (step_count == 0)
{
hdr_record_value(h, 2);
}
/* end - changes to reproduce issue */
step_count++;
}
mu_assert("Number of steps", compare_int64(4, step_count));
mu_assert("Total count", compare_int64(6, total_count));
return 0;
}
static char* test_interval_recording(void)
{
int value_count, i, value;
char* result;
struct hdr_histogram* expected_histogram;
struct hdr_histogram* expected_corrected_histogram;
struct hdr_interval_recorder recorder;
struct hdr_interval_recorder recorder_corrected;
struct hdr_histogram* recorder_histogram;
struct hdr_histogram* recorder_corrected_histogram;
value_count = 1000000;
hdr_interval_recorder_init_all(&recorder, 1, INT64_C(24) * 60 * 60 * 1000000, 3);
hdr_interval_recorder_init_all(&recorder_corrected, 1, INT64_C(24) * 60 * 60 * 1000000, 3);
hdr_init(1, INT64_C(24) * 60 * 60 * 1000000, 3, &expected_histogram);
hdr_init(1, INT64_C(24) * 60 * 60 * 1000000, 3, &expected_corrected_histogram);
for (i = 0; i < value_count; i++)
{
value = rand() % 20000;
hdr_record_value(expected_histogram, value);
hdr_record_corrected_value(expected_corrected_histogram, value, 1000);
hdr_interval_recorder_record_value(&recorder, value);
hdr_interval_recorder_record_corrected_value(&recorder_corrected, value, 1000);
}
recorder_histogram = hdr_interval_recorder_sample(&recorder);
result = compare_histograms(expected_histogram, recorder_histogram);
if (result)
{
return result;
}
recorder_corrected_histogram = hdr_interval_recorder_sample(&recorder_corrected);
result = compare_histograms(expected_corrected_histogram, recorder_corrected_histogram);
if (result)
{
return result;
}
return 0;
}
static char* reset_histogram_on_sample_and_recycle(void)
{
struct hdr_interval_recorder recorder;
struct hdr_histogram* inactive1;
struct hdr_histogram* sample1;
struct hdr_histogram* sample2;
inactive1 = NULL;
hdr_interval_recorder_init_all(&recorder, 1, INT64_C(24) * 60 * 60 * 1000000, 3);
hdr_interval_recorder_record_value(&recorder, 1234);
sample1 = hdr_interval_recorder_sample_and_recycle(&recorder, inactive1);
mu_assert("Should have at least one value", compare_int64(1, sample1->total_count));
sample2 = hdr_interval_recorder_sample_and_recycle(&recorder, sample1);
sample1 = hdr_interval_recorder_sample_and_recycle(&recorder, sample2);
mu_assert("Should have been reset", compare_int64(0, sample1->total_count));
return 0;
}
static struct mu_result all_tests(void)
{
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_percentiles_by_value_at_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_run_test(test_linear_iter_buckets_correctly);
mu_run_test(test_interval_recording);
mu_run_test(reset_histogram_on_sample_and_recycle);
mu_ok;
}
static int hdr_histogram_run_tests(void)
{
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(void)
{
return hdr_histogram_run_tests();
}
|