blob: 8ee94c75356fdb0c3422a30be75592f3520aaad3 [file] [log] [blame]
Yangsterf2bee6f2017-11-29 12:01:05 -08001// Copyright (C) 2017 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include "src/metrics/DurationMetricProducer.h"
16#include "metrics_test_helper.h"
17#include "src/condition/ConditionWizard.h"
18
19#include <gmock/gmock.h>
20#include <gtest/gtest.h>
21#include <stdio.h>
22#include <set>
23#include <unordered_map>
24#include <vector>
25
26using namespace android::os::statsd;
27using namespace testing;
28using android::sp;
29using std::set;
30using std::unordered_map;
31using std::vector;
32
33#ifdef __ANDROID__
34
35namespace android {
36namespace os {
37namespace statsd {
38
39const ConfigKey kConfigKey(0, "test");
40
41TEST(DurationMetricTrackerTest, TestNoCondition) {
42 sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
43 uint64_t bucketStartTimeNs = 10000000000;
44 uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
45
46 DurationMetric metric;
47 metric.set_name("1");
48 metric.mutable_bucket()->set_bucket_size_millis(bucketSizeNs / 1000000);
49 metric.set_aggregation_type(DurationMetric_AggregationType_SUM);
50
51 int tagId = 1;
52 LogEvent event1(tagId, bucketStartTimeNs + 1);
53 LogEvent event2(tagId, bucketStartTimeNs + bucketSizeNs + 2);
54
Yangster-mac20877162017-12-22 17:19:39 -080055 FieldMatcher dimensions;
Yangsterf2bee6f2017-11-29 12:01:05 -080056 DurationMetricProducer durationProducer(
57 kConfigKey, metric, -1 /*no condition*/, 1 /* start index */, 2 /* stop index */,
Yangster-mac20877162017-12-22 17:19:39 -080058 3 /* stop_all index */, false /*nesting*/, wizard, dimensions, bucketStartTimeNs);
Yangsterf2bee6f2017-11-29 12:01:05 -080059
Chenjie Yua7259ab2017-12-10 08:31:05 -080060 durationProducer.onMatchedLogEvent(1 /* start index*/, event1);
61 durationProducer.onMatchedLogEvent(2 /* stop index*/, event2);
Yangsterf2bee6f2017-11-29 12:01:05 -080062 durationProducer.flushIfNeededLocked(bucketStartTimeNs + 2 * bucketSizeNs + 1);
63 EXPECT_EQ(1UL, durationProducer.mPastBuckets.size());
64 EXPECT_TRUE(durationProducer.mPastBuckets.find(DEFAULT_DIMENSION_KEY) !=
65 durationProducer.mPastBuckets.end());
66 const auto& buckets = durationProducer.mPastBuckets[DEFAULT_DIMENSION_KEY];
67 EXPECT_EQ(2UL, buckets.size());
68 EXPECT_EQ(bucketStartTimeNs, buckets[0].mBucketStartNs);
69 EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, buckets[0].mBucketEndNs);
70 EXPECT_EQ(bucketSizeNs - 1ULL, buckets[0].mDuration);
71 EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, buckets[1].mBucketStartNs);
72 EXPECT_EQ(bucketStartTimeNs + 2 * bucketSizeNs, buckets[1].mBucketEndNs);
73 EXPECT_EQ(2ULL, buckets[1].mDuration);
74}
75
76TEST(DurationMetricTrackerTest, TestNonSlicedCondition) {
77 sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
78 uint64_t bucketStartTimeNs = 10000000000;
79 uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
80
81 DurationMetric metric;
82 metric.set_name("1");
83 metric.mutable_bucket()->set_bucket_size_millis(bucketSizeNs / 1000000);
84 metric.set_aggregation_type(DurationMetric_AggregationType_SUM);
85
86 int tagId = 1;
87 LogEvent event1(tagId, bucketStartTimeNs + 1);
88 LogEvent event2(tagId, bucketStartTimeNs + 2);
89 LogEvent event3(tagId, bucketStartTimeNs + bucketSizeNs + 1);
90 LogEvent event4(tagId, bucketStartTimeNs + bucketSizeNs + 3);
91
Yangster-mac20877162017-12-22 17:19:39 -080092 FieldMatcher dimensions;
Yangsterf2bee6f2017-11-29 12:01:05 -080093 DurationMetricProducer durationProducer(
Yao Chenf60e0ba2017-11-29 15:06:41 -080094 kConfigKey, metric, 0 /* condition index */, 1 /* start index */, 2 /* stop index */,
Yangster-mac20877162017-12-22 17:19:39 -080095 3 /* stop_all index */, false /*nesting*/, wizard, dimensions, bucketStartTimeNs);
Yangsterf2bee6f2017-11-29 12:01:05 -080096 EXPECT_FALSE(durationProducer.mCondition);
97 EXPECT_FALSE(durationProducer.isConditionSliced());
98
Chenjie Yua7259ab2017-12-10 08:31:05 -080099 durationProducer.onMatchedLogEvent(1 /* start index*/, event1);
100 durationProducer.onMatchedLogEvent(2 /* stop index*/, event2);
Yangsterf2bee6f2017-11-29 12:01:05 -0800101 durationProducer.flushIfNeededLocked(bucketStartTimeNs + bucketSizeNs + 1);
Yao Chenf60e0ba2017-11-29 15:06:41 -0800102 EXPECT_EQ(0UL, durationProducer.mPastBuckets.size());
Yangsterf2bee6f2017-11-29 12:01:05 -0800103
Chenjie Yua7259ab2017-12-10 08:31:05 -0800104 durationProducer.onMatchedLogEvent(1 /* start index*/, event3);
Yangsterf2bee6f2017-11-29 12:01:05 -0800105 durationProducer.onConditionChanged(true /* condition */, bucketStartTimeNs + bucketSizeNs + 2);
Chenjie Yua7259ab2017-12-10 08:31:05 -0800106 durationProducer.onMatchedLogEvent(2 /* stop index*/, event4);
Yangsterf2bee6f2017-11-29 12:01:05 -0800107 durationProducer.flushIfNeededLocked(bucketStartTimeNs + 2 * bucketSizeNs + 1);
108 EXPECT_EQ(1UL, durationProducer.mPastBuckets.size());
109 EXPECT_TRUE(durationProducer.mPastBuckets.find(DEFAULT_DIMENSION_KEY) !=
110 durationProducer.mPastBuckets.end());
111 const auto& buckets2 = durationProducer.mPastBuckets[DEFAULT_DIMENSION_KEY];
112 EXPECT_EQ(1UL, buckets2.size());
113 EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, buckets2[0].mBucketStartNs);
114 EXPECT_EQ(bucketStartTimeNs + 2 * bucketSizeNs, buckets2[0].mBucketEndNs);
115 EXPECT_EQ(1ULL, buckets2[0].mDuration);
116}
117
118} // namespace statsd
119} // namespace os
120} // namespace android
121#else
122GTEST_LOG_(INFO) << "This test does nothing.\n";
123#endif