diff options
author | Sergey Polovko <sergey@polovko.me> | 2022-02-10 16:47:03 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:47:03 +0300 |
commit | 2e714b5ebd40a1f4cc31c27f1ad6e49ca6d895f5 (patch) | |
tree | b83306b6e37edeea782e9eed673d89286c4fef35 /library/cpp/monlib/metrics/metric_registry_ut.cpp | |
parent | 3e0b762a82514bac89c1dd6ea7211e381d8aa248 (diff) | |
download | ydb-2e714b5ebd40a1f4cc31c27f1ad6e49ca6d895f5.tar.gz |
Restoring authorship annotation for Sergey Polovko <sergey@polovko.me>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/monlib/metrics/metric_registry_ut.cpp')
-rw-r--r-- | library/cpp/monlib/metrics/metric_registry_ut.cpp | 248 |
1 files changed, 124 insertions, 124 deletions
diff --git a/library/cpp/monlib/metrics/metric_registry_ut.cpp b/library/cpp/monlib/metrics/metric_registry_ut.cpp index e10d2efc02..86d9a52ec0 100644 --- a/library/cpp/monlib/metrics/metric_registry_ut.cpp +++ b/library/cpp/monlib/metrics/metric_registry_ut.cpp @@ -1,15 +1,15 @@ -#include "metric_registry.h" - -#include <library/cpp/monlib/encode/protobuf/protobuf.h> -#include <library/cpp/monlib/encode/json/json.h> +#include "metric_registry.h" + +#include <library/cpp/monlib/encode/protobuf/protobuf.h> +#include <library/cpp/monlib/encode/json/json.h> #include <library/cpp/resource/resource.h> - + #include <library/cpp/testing/unittest/registar.h> - -#include <util/stream/str.h> - -using namespace NMonitoring; - + +#include <util/stream/str.h> + +using namespace NMonitoring; + template<> void Out<NMonitoring::NProto::TSingleSample::ValueCase>(IOutputStream& os, NMonitoring::NProto::TSingleSample::ValueCase val) { switch (val) { @@ -37,14 +37,14 @@ void Out<NMonitoring::NProto::TSingleSample::ValueCase>(IOutputStream& os, NMoni } } -Y_UNIT_TEST_SUITE(TMetricRegistryTest) { +Y_UNIT_TEST_SUITE(TMetricRegistryTest) { Y_UNIT_TEST(Gauge) { - TMetricRegistry registry(TLabels{{"common", "label"}}); - TGauge* g = registry.Gauge({{"my", "gauge"}}); - - UNIT_ASSERT_DOUBLES_EQUAL(g->Get(), 0.0, 1E-6); - g->Set(12.34); - UNIT_ASSERT_DOUBLES_EQUAL(g->Get(), 12.34, 1E-6); + TMetricRegistry registry(TLabels{{"common", "label"}}); + TGauge* g = registry.Gauge({{"my", "gauge"}}); + + UNIT_ASSERT_DOUBLES_EQUAL(g->Get(), 0.0, 1E-6); + g->Set(12.34); + UNIT_ASSERT_DOUBLES_EQUAL(g->Get(), 12.34, 1E-6); double val; @@ -55,8 +55,8 @@ Y_UNIT_TEST_SUITE(TMetricRegistryTest) { val = g->Add(-3.47); UNIT_ASSERT_DOUBLES_EQUAL(g->Get(), 10.07, 1E-6); UNIT_ASSERT_DOUBLES_EQUAL(g->Get(), val, 1E-6); - } - + } + Y_UNIT_TEST(LazyGauge) { TMetricRegistry registry(TLabels{{"common", "label"}}); double val = 0.0; @@ -76,7 +76,7 @@ Y_UNIT_TEST_SUITE(TMetricRegistryTest) { } Y_UNIT_TEST(IntGauge) { - TMetricRegistry registry(TLabels{{"common", "label"}}); + TMetricRegistry registry(TLabels{{"common", "label"}}); TIntGauge* g = registry.IntGauge({{"my", "gauge"}}); UNIT_ASSERT_VALUES_EQUAL(g->Get(), 0); @@ -123,16 +123,16 @@ Y_UNIT_TEST_SUITE(TMetricRegistryTest) { } Y_UNIT_TEST(Counter) { - TMetricRegistry registry(TLabels{{"common", "label"}}); - TCounter* c = registry.Counter({{"my", "counter"}}); - + TMetricRegistry registry(TLabels{{"common", "label"}}); + TCounter* c = registry.Counter({{"my", "counter"}}); + UNIT_ASSERT_VALUES_EQUAL(c->Get(), 0); UNIT_ASSERT_VALUES_EQUAL(c->Inc(), 1); UNIT_ASSERT_VALUES_EQUAL(c->Get(), 1); UNIT_ASSERT_VALUES_EQUAL(c->Add(10), 11); UNIT_ASSERT_VALUES_EQUAL(c->Get(), 11); - } - + } + Y_UNIT_TEST(LazyCounter) { TMetricRegistry registry(TLabels{{"common", "label"}}); ui64 val = 0; @@ -156,9 +156,9 @@ Y_UNIT_TEST_SUITE(TMetricRegistryTest) { } Y_UNIT_TEST(DoubleCounter) { - TMetricRegistry registry(TLabels{{"common", "label"}}); + TMetricRegistry registry(TLabels{{"common", "label"}}); - TCounter* c = registry.Counter({{"my", "counter"}}); + TCounter* c = registry.Counter({{"my", "counter"}}); UNIT_ASSERT_VALUES_EQUAL(c->Get(), 0); c->Add(10); @@ -167,19 +167,19 @@ Y_UNIT_TEST_SUITE(TMetricRegistryTest) { } Y_UNIT_TEST(Sample) { - TMetricRegistry registry(TLabels{{"common", "label"}}); - - TGauge* g = registry.Gauge({{"my", "gauge"}}); - g->Set(12.34); - - TCounter* c = registry.Counter({{"my", "counter"}}); - c->Add(10); - - NProto::TSingleSamplesList samples; - auto encoder = EncoderProtobuf(&samples); - auto now = TInstant::Now(); - registry.Accept(now, encoder.Get()); - + TMetricRegistry registry(TLabels{{"common", "label"}}); + + TGauge* g = registry.Gauge({{"my", "gauge"}}); + g->Set(12.34); + + TCounter* c = registry.Counter({{"my", "counter"}}); + c->Add(10); + + NProto::TSingleSamplesList samples; + auto encoder = EncoderProtobuf(&samples); + auto now = TInstant::Now(); + registry.Accept(now, encoder.Get()); + UNIT_ASSERT_VALUES_EQUAL(samples.SamplesSize(), 2); UNIT_ASSERT_VALUES_EQUAL(samples.CommonLabelsSize(), 1); { @@ -187,65 +187,65 @@ Y_UNIT_TEST_SUITE(TMetricRegistryTest) { UNIT_ASSERT_STRINGS_EQUAL(label.GetName(), "common"); UNIT_ASSERT_STRINGS_EQUAL(label.GetValue(), "label"); } - - for (const NProto::TSingleSample& sample : samples.GetSamples()) { + + for (const NProto::TSingleSample& sample : samples.GetSamples()) { UNIT_ASSERT_VALUES_EQUAL(sample.LabelsSize(), 1); UNIT_ASSERT_VALUES_EQUAL(sample.GetTime(), now.MilliSeconds()); - - if (sample.GetMetricType() == NProto::GAUGE) { + + if (sample.GetMetricType() == NProto::GAUGE) { UNIT_ASSERT_VALUES_EQUAL(sample.GetValueCase(), NProto::TSingleSample::kFloat64); - UNIT_ASSERT_DOUBLES_EQUAL(sample.GetFloat64(), 12.34, 1E-6); - + UNIT_ASSERT_DOUBLES_EQUAL(sample.GetFloat64(), 12.34, 1E-6); + const NProto::TLabel& label = sample.GetLabels(0); - UNIT_ASSERT_STRINGS_EQUAL(label.GetName(), "my"); - UNIT_ASSERT_STRINGS_EQUAL(label.GetValue(), "gauge"); - } else if (sample.GetMetricType() == NProto::COUNTER) { + UNIT_ASSERT_STRINGS_EQUAL(label.GetName(), "my"); + UNIT_ASSERT_STRINGS_EQUAL(label.GetValue(), "gauge"); + } else if (sample.GetMetricType() == NProto::COUNTER) { UNIT_ASSERT_VALUES_EQUAL(sample.GetValueCase(), NProto::TSingleSample::kUint64); UNIT_ASSERT_VALUES_EQUAL(sample.GetUint64(), 10); - + const NProto::TLabel& label = sample.GetLabels(0); - UNIT_ASSERT_STRINGS_EQUAL(label.GetName(), "my"); - UNIT_ASSERT_STRINGS_EQUAL(label.GetValue(), "counter"); - } else { - UNIT_FAIL("unexpected sample type"); - } - } - } - - Y_UNIT_TEST(Histograms) { - TMetricRegistry registry(TLabels{{"common", "label"}}); - - THistogram* h1 = registry.HistogramCounter( - {{"sensor", "readTimeMillis"}}, - ExponentialHistogram(5, 2)); - - THistogram* h2 = registry.HistogramRate( - {{"sensor", "writeTimeMillis"}}, - ExplicitHistogram({1, 5, 15, 20, 25})); - - for (i64 i = 0; i < 100; i++) { - h1->Record(i); - h2->Record(i); - } - - TStringStream ss; - { - auto encoder = EncoderJson(&ss, 2); - registry.Accept(TInstant::Zero(), encoder.Get()); - } - ss << '\n'; - - UNIT_ASSERT_NO_DIFF(ss.Str(), NResource::Find("/histograms.json")); - } - + UNIT_ASSERT_STRINGS_EQUAL(label.GetName(), "my"); + UNIT_ASSERT_STRINGS_EQUAL(label.GetValue(), "counter"); + } else { + UNIT_FAIL("unexpected sample type"); + } + } + } + + Y_UNIT_TEST(Histograms) { + TMetricRegistry registry(TLabels{{"common", "label"}}); + + THistogram* h1 = registry.HistogramCounter( + {{"sensor", "readTimeMillis"}}, + ExponentialHistogram(5, 2)); + + THistogram* h2 = registry.HistogramRate( + {{"sensor", "writeTimeMillis"}}, + ExplicitHistogram({1, 5, 15, 20, 25})); + + for (i64 i = 0; i < 100; i++) { + h1->Record(i); + h2->Record(i); + } + + TStringStream ss; + { + auto encoder = EncoderJson(&ss, 2); + registry.Accept(TInstant::Zero(), encoder.Get()); + } + ss << '\n'; + + UNIT_ASSERT_NO_DIFF(ss.Str(), NResource::Find("/histograms.json")); + } + Y_UNIT_TEST(StreamingEncoderTest) { const TString expected { "{\"commonLabels\":{\"common\":\"label\"}," "\"sensors\":[{\"kind\":\"GAUGE\",\"labels\":{\"my\":\"gauge\"},\"value\":12.34}]}" }; - TMetricRegistry registry(TLabels{{"common", "label"}}); + TMetricRegistry registry(TLabels{{"common", "label"}}); TGauge* g = registry.Gauge({{"my", "gauge"}}); g->Set(12.34); @@ -257,48 +257,48 @@ Y_UNIT_TEST_SUITE(TMetricRegistryTest) { UNIT_ASSERT_STRINGS_EQUAL(os.Str(), expected); } - Y_UNIT_TEST(CreatingSameMetricWithDifferentTypesShouldThrow) { - TMetricRegistry registry; - + Y_UNIT_TEST(CreatingSameMetricWithDifferentTypesShouldThrow) { + TMetricRegistry registry; + registry.Gauge({{"foo", "bar"}}); UNIT_ASSERT_EXCEPTION(registry.Counter({{"foo", "bar"}}), yexception); - + registry.HistogramCounter({{"bar", "baz"}}, nullptr); UNIT_ASSERT_EXCEPTION(registry.HistogramRate({{"bar", "baz"}}, nullptr), yexception); - } - - Y_UNIT_TEST(EncodeRegistryWithCommonLabels) { - TMetricRegistry registry(TLabels{{"common", "label"}}); - - TGauge* g = registry.Gauge({{"my", "gauge"}}); - g->Set(12.34); - - // Append() adds common labels to each metric, allowing to combine - // several metric registries in one resulting blob - { - TStringStream os; - auto encoder = EncoderJson(&os); - encoder->OnStreamBegin(); - registry.Append(TInstant::Zero(), encoder.Get()); - encoder->OnStreamEnd(); - - UNIT_ASSERT_STRINGS_EQUAL( - os.Str(), - "{\"sensors\":[{\"kind\":\"GAUGE\",\"labels\":{\"common\":\"label\",\"my\":\"gauge\"},\"value\":12.34}]}"); - } - - // Accept() adds common labels to the beginning of the blob - { - TStringStream os; - auto encoder = EncoderJson(&os); - registry.Accept(TInstant::Zero(), encoder.Get()); - - UNIT_ASSERT_STRINGS_EQUAL( - os.Str(), - "{\"commonLabels\":{\"common\":\"label\"}," - "\"sensors\":[{\"kind\":\"GAUGE\",\"labels\":{\"my\":\"gauge\"},\"value\":12.34}]}"); - } - } + } + + Y_UNIT_TEST(EncodeRegistryWithCommonLabels) { + TMetricRegistry registry(TLabels{{"common", "label"}}); + + TGauge* g = registry.Gauge({{"my", "gauge"}}); + g->Set(12.34); + + // Append() adds common labels to each metric, allowing to combine + // several metric registries in one resulting blob + { + TStringStream os; + auto encoder = EncoderJson(&os); + encoder->OnStreamBegin(); + registry.Append(TInstant::Zero(), encoder.Get()); + encoder->OnStreamEnd(); + + UNIT_ASSERT_STRINGS_EQUAL( + os.Str(), + "{\"sensors\":[{\"kind\":\"GAUGE\",\"labels\":{\"common\":\"label\",\"my\":\"gauge\"},\"value\":12.34}]}"); + } + + // Accept() adds common labels to the beginning of the blob + { + TStringStream os; + auto encoder = EncoderJson(&os); + registry.Accept(TInstant::Zero(), encoder.Get()); + + UNIT_ASSERT_STRINGS_EQUAL( + os.Str(), + "{\"commonLabels\":{\"common\":\"label\"}," + "\"sensors\":[{\"kind\":\"GAUGE\",\"labels\":{\"my\":\"gauge\"},\"value\":12.34}]}"); + } + } Y_UNIT_TEST(MetricsRegistryClear) { TMetricRegistry registry; @@ -316,4 +316,4 @@ Y_UNIT_TEST_SUITE(TMetricRegistryTest) { UNIT_ASSERT(samples.SamplesSize() == 0); } -} +} |