123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- #include "histogram_iter.h"
- #include <library/cpp/testing/unittest/registar.h>
- using namespace NHdr;
- Y_UNIT_TEST_SUITE(THistogramIterTest) {
- Y_UNIT_TEST(RecordedValues) {
- THistogram h(TDuration::Hours(1).MicroSeconds(), 3);
- UNIT_ASSERT(h.RecordValues(1000, 1000));
- UNIT_ASSERT(h.RecordValue(1000 * 1000));
- int index = 0;
- TRecordedValuesIterator it(h);
- while (it.Next()) {
- i64 countInBucket = it.GetCount();
- i64 countInStep = it.GetCountAddedInThisIterationStep();
- if (index == 0) {
- UNIT_ASSERT_EQUAL(countInBucket, 1000);
- UNIT_ASSERT_EQUAL(countInStep, 1000);
- } else if (index == 1) {
- UNIT_ASSERT_EQUAL(countInBucket, 1);
- UNIT_ASSERT_EQUAL(countInStep, 1);
- } else {
- UNIT_FAIL("unexpected index value: " << index);
- }
- index++;
- }
- UNIT_ASSERT_EQUAL(index, 2);
- }
- Y_UNIT_TEST(CorrectedRecordedValues) {
- THistogram h(TDuration::Hours(1).MicroSeconds(), 3);
- UNIT_ASSERT(h.RecordValuesWithExpectedInterval(1000, 1000, 1000));
- UNIT_ASSERT(h.RecordValueWithExpectedInterval(1000 * 1000, 1000));
- int index = 0;
- i64 totalCount = 0;
- TRecordedValuesIterator it(h);
- while (it.Next()) {
- i64 countInBucket = it.GetCount();
- i64 countInStep = it.GetCountAddedInThisIterationStep();
- if (index == 0) {
- UNIT_ASSERT_EQUAL(countInBucket, 1001);
- UNIT_ASSERT_EQUAL(countInStep, 1001);
- } else {
- UNIT_ASSERT(countInBucket >= 1);
- UNIT_ASSERT(countInStep >= 1);
- }
- index++;
- totalCount += countInStep;
- }
- UNIT_ASSERT_EQUAL(index, 1000);
- UNIT_ASSERT_EQUAL(totalCount, 2000);
- }
- Y_UNIT_TEST(LinearValues) {
- THistogram h(TDuration::Hours(1).MicroSeconds(), 3);
- UNIT_ASSERT(h.RecordValues(1000, 1000));
- UNIT_ASSERT(h.RecordValue(1000 * 1000));
- int index = 0;
- TLinearIterator it(h, 1000);
- while (it.Next()) {
- i64 countInBucket = it.GetCount();
- i64 countInStep = it.GetCountAddedInThisIterationStep();
- if (index == 0) {
- UNIT_ASSERT_EQUAL(countInBucket, 1000);
- UNIT_ASSERT_EQUAL(countInStep, 1000);
- } else if (index == 999) {
- UNIT_ASSERT_EQUAL(countInBucket, 1);
- UNIT_ASSERT_EQUAL(countInStep, 1);
- } else {
- UNIT_ASSERT_EQUAL(countInBucket, 0);
- UNIT_ASSERT_EQUAL(countInStep, 0);
- }
- index++;
- }
- UNIT_ASSERT_EQUAL(index, 1000);
- }
- Y_UNIT_TEST(CorrectLinearValues) {
- THistogram h(TDuration::Hours(1).MicroSeconds(), 3);
- UNIT_ASSERT(h.RecordValuesWithExpectedInterval(1000, 1000, 1000));
- UNIT_ASSERT(h.RecordValueWithExpectedInterval(1000 * 1000, 1000));
- int index = 0;
- i64 totalCount = 0;
- TLinearIterator it(h, 1000);
- while (it.Next()) {
- i64 countInBucket = it.GetCount();
- i64 countInStep = it.GetCountAddedInThisIterationStep();
- if (index == 0) {
- UNIT_ASSERT_EQUAL(countInBucket, 1001);
- UNIT_ASSERT_EQUAL(countInStep, 1001);
- } else {
- UNIT_ASSERT_EQUAL(countInBucket, 1);
- UNIT_ASSERT_EQUAL(countInStep, 1);
- }
- index++;
- totalCount += countInStep;
- }
- UNIT_ASSERT_EQUAL(index, 1000);
- UNIT_ASSERT_EQUAL(totalCount, 2000);
- }
- Y_UNIT_TEST(LogarithmicValues) {
- THistogram h(TDuration::Hours(1).MicroSeconds(), 3);
- UNIT_ASSERT(h.RecordValues(1000, 1000));
- UNIT_ASSERT(h.RecordValue(1000 * 1000));
- int index = 0;
- i64 expectedValue = 1000;
- TLogarithmicIterator it(h, 1000, 2.0);
- while (it.Next()) {
- i64 value = it.GetValue();
- i64 countInBucket = it.GetCount();
- i64 countInStep = it.GetCountAddedInThisIterationStep();
- UNIT_ASSERT_EQUAL(value, expectedValue);
- if (index == 0) {
- UNIT_ASSERT_EQUAL(countInBucket, 1000);
- UNIT_ASSERT_EQUAL(countInStep, 1000);
- } else if (index == 10) {
- UNIT_ASSERT_EQUAL(countInBucket, 0);
- UNIT_ASSERT_EQUAL(countInStep, 1);
- } else {
- UNIT_ASSERT_EQUAL(countInBucket, 0);
- UNIT_ASSERT_EQUAL(countInStep, 0);
- }
- index++;
- expectedValue *= 2;
- }
- UNIT_ASSERT_EQUAL(index, 11);
- }
- Y_UNIT_TEST(CorrectedLogarithmicValues) {
- THistogram h(TDuration::Hours(1).MicroSeconds(), 3);
- UNIT_ASSERT(h.RecordValuesWithExpectedInterval(1000, 1000, 1000));
- UNIT_ASSERT(h.RecordValueWithExpectedInterval(1000 * 1000, 1000));
- int index = 0;
- i64 totalCount = 0;
- i64 expectedValue = 1000;
- TLogarithmicIterator it(h, 1000, 2.0);
- while (it.Next()) {
- i64 value = it.GetValue();
- i64 countInBucket = it.GetCount();
- i64 countInStep = it.GetCountAddedInThisIterationStep();
- UNIT_ASSERT_EQUAL(value, expectedValue);
- if (index == 0) {
- UNIT_ASSERT_EQUAL(countInBucket, 1001);
- UNIT_ASSERT_EQUAL(countInStep, 1001);
- }
- index++;
- totalCount += countInStep;
- expectedValue *= 2;
- }
- UNIT_ASSERT_EQUAL(index, 11);
- UNIT_ASSERT_EQUAL(totalCount, 2000);
- }
- Y_UNIT_TEST(LinearIterBucketsCorrectly) {
- THistogram h(255, 2);
- UNIT_ASSERT(h.RecordValue(193));
- UNIT_ASSERT(h.RecordValue(255));
- UNIT_ASSERT(h.RecordValue(0));
- UNIT_ASSERT(h.RecordValue(1));
- UNIT_ASSERT(h.RecordValue(64));
- UNIT_ASSERT(h.RecordValue(128));
- int index = 0;
- i64 totalCount = 0;
- TLinearIterator it(h, 64);
- while (it.Next()) {
- if (index == 0) {
- // change after iterator was created
- UNIT_ASSERT(h.RecordValue(2));
- }
- index++;
- totalCount += it.GetCountAddedInThisIterationStep();
- }
- UNIT_ASSERT_EQUAL(index, 4);
- UNIT_ASSERT_EQUAL(totalCount, 6);
- }
- }
|