aboutsummaryrefslogtreecommitdiffstats
path: root/searchcore/src/tests/proton/metrics/job_load_sampler/job_load_sampler_test.cpp
blob: 2b74d1425a1b663d9c1d5dc797066c072a698e60 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/log/log.h>
LOG_SETUP("job_load_sampler_test");

#include <vespa/searchcore/proton/metrics/job_load_sampler.h>
#include <vespa/vespalib/testkit/testapp.h>
#include <chrono>

using namespace proton;

constexpr double EPS = 0.000001;

using time_point = std::chrono::time_point<std::chrono::steady_clock>;
using std::chrono::duration;
using std::chrono::duration_cast;
using std::chrono::steady_clock;

time_point fakeTime(double now)
{
    return time_point(duration_cast<steady_clock::duration>(duration<double>(now)));
}

struct Fixture
{
    JobLoadSampler _sampler;
    Fixture()
        : _sampler(fakeTime(10))
    {
    }
    Fixture &start(double now) {
        _sampler.startJob(fakeTime(now));
        return *this;
    }
    Fixture &end(double now) {
        _sampler.endJob(fakeTime(now));
        return *this;
    }
    double sample(double now) {
        return _sampler.sampleLoad(fakeTime(now));
    }
};

TEST_F("require that empty sampler gives 0 load", Fixture)
{
    EXPECT_APPROX(0.0, f.sample(11), EPS);
}

TEST_F("require that empty time interval gives 0 load", Fixture)
{
    EXPECT_APPROX(0.0, f.sample(10), EPS);
}

TEST_F("require that job that starts and ends in interval gets correct load", Fixture)
{
    f.start(12).end(17);
    EXPECT_APPROX(0.5, f.sample(20), EPS);
    EXPECT_APPROX(0.0, f.sample(21), EPS);
}

TEST_F("require that job that starts in interval gets correct load", Fixture)
{
    f.start(12);
    EXPECT_APPROX(0.8, f.sample(20), EPS);
    EXPECT_APPROX(1.0, f.sample(21), EPS);
}

TEST_F("require that job that ends in interval gets correct load", Fixture)
{
    f.start(12).sample(20);
    f.end(27);
    EXPECT_APPROX(0.7, f.sample(30), EPS);
    EXPECT_APPROX(0.0, f.sample(31), EPS);
}

TEST_F("require that job that runs in complete interval gets correct load", Fixture)
{
    f.start(12).sample(20);
    EXPECT_APPROX(1.0, f.sample(30), EPS);
    EXPECT_APPROX(1.0, f.sample(31), EPS);
}

TEST_F("require that multiple jobs that starts and ends in interval gets correct load", Fixture)
{
    // job1: 12->17: 0.5
    // job2: 14->16: 0.2
    f.start(12).start(14).end(16).end(17);
    EXPECT_APPROX(0.7, f.sample(20), EPS);
}

TEST_F("require that multiple jobs that starts and ends in several intervals gets correct load", Fixture)
{
    // job1: 12->22
    // job2: 14->34
    // job3: 25->45
    f.start(12).start(14);
    EXPECT_APPROX(1.4, f.sample(20), EPS);
    f.end(22).start(25);
    EXPECT_APPROX(1.7, f.sample(30), EPS);
    f.end(34);
    EXPECT_APPROX(1.4, f.sample(40), EPS);
    f.end(45);
    EXPECT_APPROX(0.5, f.sample(50), EPS);
}

TEST_MAIN() { TEST_RUN_ALL(); }