51 lines
1.6 KiB
C++
51 lines
1.6 KiB
C++
#include <statistics/Sliding_Statistics.hpp>
|
|
#include <gtest/gtest.h>
|
|
#include <cmath>
|
|
#include <limits>
|
|
|
|
namespace aethera::web {
|
|
TEST(Sliding_Statistics, Publishes_Derived_Window_Statistics_On_Submit) {
|
|
Sliding_Statistics statistics{20};
|
|
for (int value = 1; value <= 20; ++value)
|
|
statistics.add(static_cast<double>(value));
|
|
|
|
const auto state = statistics.summary();
|
|
EXPECT_EQ(state.sample_count, 20U);
|
|
EXPECT_DOUBLE_EQ(state.average, 10.5);
|
|
EXPECT_DOUBLE_EQ(state.p95, 19.0);
|
|
EXPECT_GT(state.standard_deviation, 0.0);
|
|
}
|
|
|
|
TEST(Sliding_Statistics, Overwrites_Oldest_And_Ignores_Nonfinite_Values) {
|
|
Sliding_Statistics statistics{3};
|
|
statistics.add(1.0);
|
|
statistics.add(2.0);
|
|
statistics.add(std::numeric_limits<double>::infinity());
|
|
statistics.add(3.0);
|
|
statistics.add(4.0);
|
|
|
|
const auto state = statistics.summary();
|
|
EXPECT_EQ(state.sample_count, 3U);
|
|
EXPECT_DOUBLE_EQ(state.average, 3.0);
|
|
EXPECT_DOUBLE_EQ(state.p95, 4.0);
|
|
}
|
|
|
|
TEST(Sliding_Statistics, Computes_Quantiles_From_The_Authoritative_Window) {
|
|
Sliding_Statistics statistics{64};
|
|
for (int value = 1; value <= 1000; ++value)
|
|
statistics.add(static_cast<double>(value));
|
|
|
|
const auto state = statistics.summary();
|
|
EXPECT_EQ(state.sample_count, 64U);
|
|
EXPECT_NEAR(state.average, 968.5, 0.001);
|
|
EXPECT_NEAR(state.p95, 997.0, 1.0);
|
|
}
|
|
|
|
TEST(Sliding_Statistics, Clear_Drops_All_Window_State) {
|
|
Sliding_Statistics statistics{16};
|
|
statistics.add(1.0);
|
|
statistics.clear();
|
|
EXPECT_EQ(statistics.summary().sample_count, 0U);
|
|
}
|
|
}
|