diff options
| author | Devtools Arcadia <[email protected]> | 2022-02-07 18:08:42 +0300 | 
|---|---|---|
| committer | Devtools Arcadia <[email protected]> | 2022-02-07 18:08:42 +0300 | 
| commit | 1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch) | |
| tree | e26c9fed0de5d9873cce7e00bc214573dc2195b7 /library/cpp/monlib/counters/timer_ut.cpp | |
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'library/cpp/monlib/counters/timer_ut.cpp')
| -rw-r--r-- | library/cpp/monlib/counters/timer_ut.cpp | 81 | 
1 files changed, 81 insertions, 0 deletions
| diff --git a/library/cpp/monlib/counters/timer_ut.cpp b/library/cpp/monlib/counters/timer_ut.cpp new file mode 100644 index 00000000000..c5cd07e89d5 --- /dev/null +++ b/library/cpp/monlib/counters/timer_ut.cpp @@ -0,0 +1,81 @@ +#include "timer.h" + +#include <library/cpp/testing/unittest/registar.h> + +using namespace NMonitoring; +using namespace std::literals::chrono_literals; + +class TCallback { +public: +    explicit TCallback(int value) +        : Value_(value){}; +    void operator()(std::chrono::high_resolution_clock::duration duration) { +        Value_ = duration.count(); +    }; + +    int Value_; +}; + +Y_UNIT_TEST_SUITE(TTimerTest) { +    Y_UNIT_TEST(RecordValue) { +        TTimerNs timerNs(1ns, 1s); +        UNIT_ASSERT(timerNs.RecordValue(10us)); + +        TTimerUs timerUs(1us, 1s); +        UNIT_ASSERT(timerUs.RecordValue(10us)); + +        THistogramSnapshot snapshot; +        timerNs.TakeSnapshot(&snapshot); +        UNIT_ASSERT_EQUAL(snapshot.Min, 10000); +        UNIT_ASSERT_EQUAL(snapshot.Max, 10007); +        UNIT_ASSERT_DOUBLES_EQUAL(snapshot.StdDeviation, 0.0, 1e-6); + +        timerUs.TakeSnapshot(&snapshot); +        UNIT_ASSERT_EQUAL(snapshot.Min, 10); +        UNIT_ASSERT_EQUAL(snapshot.Max, 10); +        UNIT_ASSERT_DOUBLES_EQUAL(snapshot.StdDeviation, 0.0, 1e-6); +    } + +    Y_UNIT_TEST(Measure) { +        TTimerNs timer(1ns, 1s); +        timer.Measure([]() { +            Sleep(TDuration::MilliSeconds(1)); +        }); +        THistogramSnapshot snapshot; +        timer.TakeSnapshot(&snapshot); + +        UNIT_ASSERT(snapshot.Min > std::chrono::nanoseconds(1ms).count()); +        UNIT_ASSERT(snapshot.Max > std::chrono::nanoseconds(1ms).count()); +        UNIT_ASSERT_DOUBLES_EQUAL(snapshot.StdDeviation, 0.0, 1e-6); +    } + +    Y_UNIT_TEST(TimerScope) { +        TTimerUs timer(1us, 1000s); +        { +            TTimerScope<TTimerUs> scope(&timer); +            Sleep(TDuration::MilliSeconds(10)); +        } +        THistogramSnapshot snapshot; +        timer.TakeSnapshot(&snapshot); + +        UNIT_ASSERT(snapshot.Min > std::chrono::microseconds(10ms).count()); +        UNIT_ASSERT(snapshot.Max > std::chrono::microseconds(10ms).count()); +        UNIT_ASSERT_DOUBLES_EQUAL(snapshot.StdDeviation, 0.0, 1e-6); +    } + +    Y_UNIT_TEST(TimerScopeWithCallback) { +        TCallback callback(0); +        TTimerUs timer(1us, 1000s); +        { +            TTimerScope<TTimerUs, TCallback> scope(&timer, &callback); +            Sleep(TDuration::MilliSeconds(10)); +        } +        THistogramSnapshot snapshot; +        timer.TakeSnapshot(&snapshot); + +        UNIT_ASSERT(snapshot.Min > std::chrono::microseconds(10ms).count()); +        UNIT_ASSERT(snapshot.Max > std::chrono::microseconds(10ms).count()); +        UNIT_ASSERT_DOUBLES_EQUAL(snapshot.StdDeviation, 0.0, 1e-6); +        UNIT_ASSERT(callback.Value_ > std::chrono::microseconds(10ms).count()); +    } +} | 
