diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 18:45:59 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 18:45:59 +0000 |
commit | 19fcec84d8d7d21e796c7624e521b60d28ee21ed (patch) | |
tree | 42d26aa27d1e3f7c0b8bd3fd14e7d7082f5008dc /src/test/common/test_counter.cc | |
parent | Initial commit. (diff) | |
download | ceph-upstream/16.2.11+ds.tar.xz ceph-upstream/16.2.11+ds.zip |
Adding upstream version 16.2.11+ds.upstream/16.2.11+dsupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/test/common/test_counter.cc')
-rw-r--r-- | src/test/common/test_counter.cc | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/src/test/common/test_counter.cc b/src/test/common/test_counter.cc new file mode 100644 index 000000000..f9a7d6e6c --- /dev/null +++ b/src/test/common/test_counter.cc @@ -0,0 +1,40 @@ +#include "common/DecayCounter.h" + +#include <gtest/gtest.h> + +#include <list> +#include <cmath> + +TEST(DecayCounter, steady) +{ + static const double duration = 2.0; + static const double max = 2048.0; + static const double rate = 3.5; + + DecayCounter d{DecayRate{rate}}; + d.hit(max); + const auto start = DecayCounter::clock::now(); + double total = 0.0; + while (1) { + const auto now = DecayCounter::clock::now(); + auto el = std::chrono::duration<double>(now-start); + if (el.count() > duration) { + break; + } + + double v = d.get(); + double diff = max-v; + if (diff > 0.0) { + d.hit(diff); + total += diff; + } + } + + /* Decay function: dN/dt = -λM where λ = ln(0.5)/rate + * (where M is the maximum value of the counter, not varying with time.) + * Integrating over t: N = -λMt (+c) + */ + double expected = -1*std::log(0.5)/rate*max*duration; + std::cerr << "t " << total << " e " << expected << std::endl; + ASSERT_LT(std::abs(total-expected)/expected, 0.05); +} |