]> git.proxmox.com Git - ceph.git/blame - ceph/src/test/common/test_counter.cc
import quincy beta 17.1.0
[ceph.git] / ceph / src / test / common / test_counter.cc
CommitLineData
adb31ebb
TL
1#include "common/DecayCounter.h"
2
3#include <gtest/gtest.h>
4
5#include <list>
6#include <cmath>
7
8TEST(DecayCounter, steady)
9{
10 static const double duration = 2.0;
11 static const double max = 2048.0;
12 static const double rate = 3.5;
13
14 DecayCounter d{DecayRate{rate}};
15 d.hit(max);
16 const auto start = DecayCounter::clock::now();
17 double total = 0.0;
18 while (1) {
19 const auto now = DecayCounter::clock::now();
20 auto el = std::chrono::duration<double>(now-start);
21 if (el.count() > duration) {
22 break;
23 }
24
25 double v = d.get();
26 double diff = max-v;
27 if (diff > 0.0) {
28 d.hit(diff);
29 total += diff;
30 }
31 }
32
33 /* Decay function: dN/dt = -λM where λ = ln(0.5)/rate
34 * (where M is the maximum value of the counter, not varying with time.)
35 * Integrating over t: N = -λMt (+c)
36 */
37 double expected = -1*std::log(0.5)/rate*max*duration;
38 std::cerr << "t " << total << " e " << expected << std::endl;
20effc67 39 ASSERT_LT(std::abs(total-expected)/expected, 0.05);
adb31ebb 40}