aboutsummaryrefslogtreecommitdiffstats
path: root/metrics/src/tests/snapshottest.cpp
blob: bee3e910fb3c4db5ebf49fe04cae9f103047d2bc (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/fastos/fastos.h>
#include <vespa/metrics/metrics.h>
#include <vespa/metrics/loadmetric.hpp>
#include <vespa/metrics/summetric.hpp>
#include <vespa/metrics/metricmanager.h>
#include <vespa/vdstestlib/cppunit/macros.h>

namespace metrics {

struct SnapshotTest : public CppUnit::TestFixture {
    void testSnapshotTwoDays();

    CPPUNIT_TEST_SUITE(SnapshotTest);
    CPPUNIT_TEST(testSnapshotTwoDays);
    CPPUNIT_TEST_SUITE_END();

};

CPPUNIT_TEST_SUITE_REGISTRATION(SnapshotTest);

namespace {

struct SubSubMetricSet : public MetricSet {
    const LoadTypeSet& loadTypes;
    int incVal;
    LongCountMetric count1;
    LongCountMetric count2;
    LoadMetric<LongCountMetric> loadCount;
    SumMetric<LongCountMetric> countSum;
    DoubleValueMetric value1;
    DoubleValueMetric value2;
    LoadMetric<DoubleValueMetric> loadValue;
    SumMetric<DoubleValueMetric> valueSum;
    DoubleAverageMetric average1;
    DoubleAverageMetric average2;
    LoadMetric<DoubleAverageMetric> loadAverage;
    SumMetric<DoubleAverageMetric> averageSum;

    SubSubMetricSet(vespalib::stringref name, const LoadTypeSet& loadTypes_, MetricSet* owner = 0);
    ~SubSubMetricSet();
    virtual MetricSet* clone(std::vector<Metric::UP> &ownerList, CopyType copyType,
                             metrics::MetricSet* owner, bool includeUnused) const override;
    void incValues();
};

SubSubMetricSet::SubSubMetricSet(vespalib::stringref name, const LoadTypeSet& loadTypes_, MetricSet* owner)
    : MetricSet(name, "", "", owner),
      loadTypes(loadTypes_),
      incVal(1),
      count1("count1", "", "", this),
      count2("count2", "", "", this),
      loadCount(loadTypes, LongCountMetric("loadCount", "", ""), this),
      countSum("countSum", "", "", this),
      value1("value1", "", "", this),
      value2("value2", "", "", this),
      loadValue(loadTypes, DoubleValueMetric("loadValue", "", ""), this),
      valueSum("valueSum", "", "", this),
      average1("average1", "", "", this),
      average2("average2", "", "", this),
      loadAverage(loadTypes, DoubleAverageMetric("loadAverage", "", ""), this),
      averageSum("averageSum", "", "", this)
{
    countSum.addMetricToSum(count1);
    countSum.addMetricToSum(count2);
    valueSum.addMetricToSum(value1);
    valueSum.addMetricToSum(value2);
    averageSum.addMetricToSum(average1);
    averageSum.addMetricToSum(average2);
}
SubSubMetricSet::~SubSubMetricSet() { }

MetricSet*
SubSubMetricSet::clone(std::vector<Metric::UP> &ownerList,
                       CopyType copyType, metrics::MetricSet* owner,
                       bool includeUnused) const
{
    if (copyType == INACTIVE) {
        return MetricSet::clone(ownerList, INACTIVE, owner, includeUnused);
    }
    return (SubSubMetricSet*) (new SubSubMetricSet(
            getName(), loadTypes, owner))
            ->assignValues(*this);
}

void
SubSubMetricSet::incValues() {
    count1.inc(incVal);
    count2.inc(incVal);
    for (uint32_t i=0; i<loadTypes.size(); ++i) {
        loadCount[loadTypes[i]].inc(incVal);
    }
    value1.set(incVal);
    value2.set(incVal);
    for (uint32_t i=0; i<loadTypes.size(); ++i) {
        loadValue[loadTypes[i]].set(incVal);
    }
    average1.set(incVal);
    average2.set(incVal);
    for (uint32_t i=0; i<loadTypes.size(); ++i) {
        loadAverage[loadTypes[i]].set(incVal);
    }
}


struct SubMetricSet : public MetricSet {
    const LoadTypeSet& loadTypes;
    SubSubMetricSet set1;
    SubSubMetricSet set2;
    LoadMetric<SubSubMetricSet> loadSet;
    SumMetric<SubSubMetricSet> setSum;

    SubMetricSet(vespalib::stringref name, const LoadTypeSet& loadTypes_, MetricSet* owner = 0);
    ~SubMetricSet();

    MetricSet* clone(std::vector<Metric::UP> &ownerList, CopyType copyType,
                     metrics::MetricSet* owner, bool includeUnused) const override;

    void incValues();
};

SubMetricSet::SubMetricSet(vespalib::stringref name, const LoadTypeSet& loadTypes_, MetricSet* owner)
    : MetricSet(name, "", "", owner),
      loadTypes(loadTypes_),
      set1("set1", loadTypes, this),
      set2("set2", loadTypes, this),
      loadSet(loadTypes, *std::unique_ptr<SubSubMetricSet>(new SubSubMetricSet("loadSet", loadTypes)), this),
      setSum("setSum", "", "", this)
{
    setSum.addMetricToSum(set1);
    setSum.addMetricToSum(set2);
}
SubMetricSet::~SubMetricSet() { }

MetricSet*
SubMetricSet::clone(std::vector<Metric::UP> &ownerList, CopyType copyType,
                    metrics::MetricSet* owner, bool includeUnused) const
{
    if (copyType == INACTIVE) {
        return MetricSet::clone(ownerList, INACTIVE, owner, includeUnused);
    }
    return (SubMetricSet*) (new SubMetricSet(getName(), loadTypes, owner))
            ->assignValues(*this);
}

void
SubMetricSet::incValues() {
    set1.incValues();
    set2.incValues();
    for (uint32_t i=0; i<loadTypes.size(); ++i) {
        loadSet[loadTypes[i]].incValues();
    }
}

struct TestMetricSet : public MetricSet {
    const LoadTypeSet& loadTypes;
    SubMetricSet set1;
    SubMetricSet set2;
    LoadMetric<SubMetricSet> loadSet;
    SumMetric<SubMetricSet> setSum;

    TestMetricSet(vespalib::stringref name, const LoadTypeSet& loadTypes_, MetricSet* owner = 0);
    ~TestMetricSet();

    void incValues();
};


TestMetricSet::TestMetricSet(vespalib::stringref name, const LoadTypeSet& loadTypes_, MetricSet* owner)
    : MetricSet(name, "", "", owner),
      loadTypes(loadTypes_),
      set1("set1", loadTypes, this),
      set2("set2", loadTypes, this),
      loadSet(loadTypes, *std::unique_ptr<SubMetricSet>(new SubMetricSet("loadSet", loadTypes)), this),
      setSum("setSum", "", "", this)
{
    setSum.addMetricToSum(set1);
    setSum.addMetricToSum(set2);
}
TestMetricSet::~TestMetricSet() { }

void
TestMetricSet::incValues() {
    set1.incValues();
    set2.incValues();
    for (uint32_t i=0; i<loadTypes.size(); ++i) {
        loadSet[loadTypes[i]].incValues();
    }
}

struct FakeTimer : public MetricManager::Timer {
    uint32_t _timeInSecs;

    FakeTimer() : _timeInSecs(1) {}

    virtual time_t getTime() const override { return _timeInSecs; }
};

} // End of anonymous namespace

#define ASSERT_VALUE(value, snapshot, name) \
{ \
    const Metric* _metricValue_((snapshot).getMetrics().getMetric(name)); \
    if (_metricValue_ == 0) { \
        CPPUNIT_FAIL("Metric value '" + std::string(name) \
                     + "' not found in snapshot"); \
    } \
    CPPUNIT_ASSERT_EQUAL(value, \
                         int32_t(_metricValue_->getLongValue("value"))); \
}

void SnapshotTest::testSnapshotTwoDays()
{
        // Create load types
    LoadTypeSet loadTypes;
    loadTypes.push_back(LoadType(1, "foo"));
    loadTypes.push_back(LoadType(2, "bar"));

    TestMetricSet set("test", loadTypes);

    FakeTimer* timer;
    FastOS_ThreadPool threadPool(256 * 1024);
    MetricManager mm(
            std::unique_ptr<MetricManager::Timer>(timer = new FakeTimer));
    {
        MetricLockGuard lockGuard(mm.getMetricLock());
        mm.registerMetric(lockGuard, set);
    }
    mm.init("raw:consumer[1]\n"
            "consumer[0].name \"log\"", threadPool, false);
    mm.tick(mm.getMetricLock(), timer->_timeInSecs * 1000);

    for (uint32_t days=0; days<2; ++days) {
        for (uint32_t hour=0; hour<24; ++hour) {
            for (uint32_t fiveMin=0; fiveMin<12; ++fiveMin) {
                set.incValues();
                timer->_timeInSecs += 5 * 60;
                mm.tick(mm.getMetricLock(), timer->_timeInSecs * 1000);
            }
        }
    }

    // Print all data. Useful for debugging. It's too much to verify
    // everything, so test will just verify some parts. Add more parts if you
    // find a failure.
    /*
    std::string regex = ".*";
    bool verbose = true;
    std::cout << "\n" << mm.getActiveMetrics().toString(mm, "", regex, verbose)
              << "\n\n";
    std::vector<uint32_t> periods(mm.getSnapshotPeriods());
    for (uint32_t i=0; i<periods.size(); ++i) {
        const MetricSnapshot& snap(mm.getMetricSnapshot(periods[i]));
        std::cout << snap.toString(mm, "", regex, verbose) << "\n\n";
    }

    std::cout << mm.getTotalMetricSnapshot().toString(mm, "", regex, verbose)
              << "\n";
    */

    const MetricSnapshot* snap = 0;
        // active snapshot
    MetricLockGuard lockGuard(mm.getMetricLock());
    snap = &mm.getActiveMetrics(lockGuard);
    ASSERT_VALUE(0, *snap, "test.set1.set1.count1");
    ASSERT_VALUE(0, *snap, "test.set1.set1.loadCount.foo");
    ASSERT_VALUE(0, *snap, "test.set1.set1.loadCount.sum");
    ASSERT_VALUE(0, *snap, "test.set1.set1.countSum");
    ASSERT_VALUE(0, *snap, "test.set1.loadSet.foo.count1");
    ASSERT_VALUE(0, *snap, "test.set1.loadSet.foo.countSum");
/* Current test procedure for fetching values, don't work in active sums of sets
    ASSERT_VALUE(0, *snap, "test.set1.loadSet.sum.count1");
    ASSERT_VALUE(0, *snap, "test.set1.loadSet.sum.loadCount.foo");
    ASSERT_VALUE(0, *snap, "test.set1.loadSet.sum.loadCount.sum");
    ASSERT_VALUE(0, *snap, "test.set1.loadSet.sum.countSum");
*/

        // 5 minute snapshot
    snap = &mm.getMetricSnapshot(lockGuard, 5 * 60);
    ASSERT_VALUE(1, *snap, "test.set1.set1.count1");
    ASSERT_VALUE(1, *snap, "test.set1.set1.loadCount.foo");
    ASSERT_VALUE(2, *snap, "test.set1.set1.loadCount.sum");
    ASSERT_VALUE(2, *snap, "test.set1.set1.countSum");
    ASSERT_VALUE(1, *snap, "test.set1.loadSet.foo.count1");
    ASSERT_VALUE(2, *snap, "test.set1.loadSet.foo.countSum");
    ASSERT_VALUE(2, *snap, "test.set1.loadSet.sum.count1");
    ASSERT_VALUE(2, *snap, "test.set1.loadSet.sum.loadCount.foo");
    ASSERT_VALUE(4, *snap, "test.set1.loadSet.sum.loadCount.sum");
    ASSERT_VALUE(4, *snap, "test.set1.loadSet.sum.countSum");

    ASSERT_VALUE(1, *snap, "test.set1.set1.average1");
    ASSERT_VALUE(1, *snap, "test.set1.set1.loadAverage.foo");
    ASSERT_VALUE(1, *snap, "test.set1.set1.loadAverage.sum");
    ASSERT_VALUE(1, *snap, "test.set1.set1.averageSum");
    ASSERT_VALUE(1, *snap, "test.set1.loadSet.foo.average1");
    ASSERT_VALUE(1, *snap, "test.set1.loadSet.foo.averageSum");
    ASSERT_VALUE(1, *snap, "test.set1.loadSet.sum.average1");
    ASSERT_VALUE(1, *snap, "test.set1.loadSet.sum.loadAverage.foo");
    ASSERT_VALUE(1, *snap, "test.set1.loadSet.sum.loadAverage.sum");
    ASSERT_VALUE(1, *snap, "test.set1.loadSet.sum.averageSum");

        // 1 hour snapshot
    snap = &mm.getMetricSnapshot(lockGuard, 60 * 60);
    ASSERT_VALUE(12, *snap, "test.set1.set1.count1");
    ASSERT_VALUE(12, *snap, "test.set1.set1.loadCount.foo");
    ASSERT_VALUE(24, *snap, "test.set1.set1.loadCount.sum");
    ASSERT_VALUE(24, *snap, "test.set1.set1.countSum");
    ASSERT_VALUE(12, *snap, "test.set1.loadSet.foo.count1");
    ASSERT_VALUE(24, *snap, "test.set1.loadSet.foo.countSum");
    ASSERT_VALUE(24, *snap, "test.set1.loadSet.sum.count1");
    ASSERT_VALUE(24, *snap, "test.set1.loadSet.sum.loadCount.foo");
    ASSERT_VALUE(48, *snap, "test.set1.loadSet.sum.loadCount.sum");
    ASSERT_VALUE(48, *snap, "test.set1.loadSet.sum.countSum");

    ASSERT_VALUE(1, *snap, "test.set1.set1.average1");
    ASSERT_VALUE(1, *snap, "test.set1.set1.loadAverage.foo");
    ASSERT_VALUE(1, *snap, "test.set1.set1.loadAverage.sum");
    ASSERT_VALUE(1, *snap, "test.set1.set1.averageSum");
    ASSERT_VALUE(1, *snap, "test.set1.loadSet.foo.average1");
    ASSERT_VALUE(1, *snap, "test.set1.loadSet.foo.averageSum");
    ASSERT_VALUE(1, *snap, "test.set1.loadSet.sum.average1");
    ASSERT_VALUE(1, *snap, "test.set1.loadSet.sum.loadAverage.foo");
    ASSERT_VALUE(1, *snap, "test.set1.loadSet.sum.loadAverage.sum");
    ASSERT_VALUE(1, *snap, "test.set1.loadSet.sum.averageSum");

        // 1 day snapshot
    snap = &mm.getMetricSnapshot(lockGuard, 24 * 60 * 60);
    ASSERT_VALUE(288, *snap, "test.set1.set1.count1");
    ASSERT_VALUE(288, *snap, "test.set1.set1.loadCount.foo");
    ASSERT_VALUE(576, *snap, "test.set1.set1.loadCount.sum");
    ASSERT_VALUE(576, *snap, "test.set1.set1.countSum");
    ASSERT_VALUE(288, *snap, "test.set1.loadSet.foo.count1");
    ASSERT_VALUE(576, *snap, "test.set1.loadSet.foo.countSum");
    ASSERT_VALUE(576, *snap, "test.set1.loadSet.sum.count1");
    ASSERT_VALUE(576, *snap, "test.set1.loadSet.sum.loadCount.foo");
    ASSERT_VALUE(1152, *snap, "test.set1.loadSet.sum.loadCount.sum");
    ASSERT_VALUE(1152, *snap, "test.set1.loadSet.sum.countSum");

    ASSERT_VALUE(1, *snap, "test.set1.set1.average1");
    ASSERT_VALUE(1, *snap, "test.set1.set1.loadAverage.foo");
    ASSERT_VALUE(1, *snap, "test.set1.set1.loadAverage.sum");
    ASSERT_VALUE(1, *snap, "test.set1.set1.averageSum");
    ASSERT_VALUE(1, *snap, "test.set1.loadSet.foo.average1");
    ASSERT_VALUE(1, *snap, "test.set1.loadSet.foo.averageSum");
    ASSERT_VALUE(1, *snap, "test.set1.loadSet.sum.average1");
    ASSERT_VALUE(1, *snap, "test.set1.loadSet.sum.loadAverage.foo");
    ASSERT_VALUE(1, *snap, "test.set1.loadSet.sum.loadAverage.sum");
    ASSERT_VALUE(1, *snap, "test.set1.loadSet.sum.averageSum");

        // total snapshot (2 days currently, not testing weeks)
    snap = &mm.getTotalMetricSnapshot(lockGuard);
    ASSERT_VALUE(576, *snap, "test.set1.set1.count1");
    ASSERT_VALUE(576, *snap, "test.set1.set1.loadCount.foo");
    ASSERT_VALUE(1152, *snap, "test.set1.set1.loadCount.sum");
    ASSERT_VALUE(1152, *snap, "test.set1.set1.countSum");
    ASSERT_VALUE(576, *snap, "test.set1.loadSet.foo.count1");
    ASSERT_VALUE(1152, *snap, "test.set1.loadSet.foo.countSum");
    ASSERT_VALUE(1152, *snap, "test.set1.loadSet.sum.count1");
    ASSERT_VALUE(1152, *snap, "test.set1.loadSet.sum.loadCount.foo");
    ASSERT_VALUE(2304, *snap, "test.set1.loadSet.sum.loadCount.sum");
    ASSERT_VALUE(2304, *snap, "test.set1.loadSet.sum.countSum");

    ASSERT_VALUE(1, *snap, "test.set1.set1.average1");
    ASSERT_VALUE(1, *snap, "test.set1.set1.loadAverage.foo");
    ASSERT_VALUE(1, *snap, "test.set1.set1.loadAverage.sum");
    ASSERT_VALUE(1, *snap, "test.set1.set1.averageSum");
    ASSERT_VALUE(1, *snap, "test.set1.loadSet.foo.average1");
    ASSERT_VALUE(1, *snap, "test.set1.loadSet.foo.averageSum");
    ASSERT_VALUE(1, *snap, "test.set1.loadSet.sum.average1");
    ASSERT_VALUE(1, *snap, "test.set1.loadSet.sum.loadAverage.foo");
    ASSERT_VALUE(1, *snap, "test.set1.loadSet.sum.loadAverage.sum");
    ASSERT_VALUE(1, *snap, "test.set1.loadSet.sum.averageSum");
}

} // metrics