blob: 7843ca085d5bc5b63982f81c431689a3628df14d [file] [log] [blame]
Yao Chen5154a372017-10-30 22:57:06 -07001// 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
Yao Chend5aa01b32017-12-19 16:46:36 -080015#include "src/metrics/duration_helper/MaxDurationTracker.h"
Yao Chen93fe3a32017-11-02 13:52:59 -070016#include "metrics_test_helper.h"
Yao Chen5154a372017-10-30 22:57:06 -070017#include "src/condition/ConditionWizard.h"
Yao Chen5154a372017-10-30 22:57:06 -070018
Yao Chen93fe3a32017-11-02 13:52:59 -070019#include <gmock/gmock.h>
20#include <gtest/gtest.h>
Yao Chen5154a372017-10-30 22:57:06 -070021#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
Yao Chen93fe3a32017-11-02 13:52:59 -070035namespace android {
36namespace os {
37namespace statsd {
Yao Chen5154a372017-10-30 22:57:06 -070038
Yao Chenb3561512017-11-21 18:07:17 -080039const ConfigKey kConfigKey(0, "test");
Yao Chend5aa01b32017-12-19 16:46:36 -080040
Yangster-mac20877162017-12-22 17:19:39 -080041const int TagId = 1;
42
43const HashableDimensionKey eventKey = getMockedDimensionKey(TagId, 0, "1");
44const std::vector<HashableDimensionKey> conditionKey = {getMockedDimensionKey(TagId, 4, "1")};
45const HashableDimensionKey key1 = getMockedDimensionKey(TagId, 1, "1");
46const HashableDimensionKey key2 = getMockedDimensionKey(TagId, 1, "2");
Yao Chenb3561512017-11-21 18:07:17 -080047
Yao Chen5154a372017-10-30 22:57:06 -070048TEST(MaxDurationTrackerTest, TestSimpleMaxDuration) {
49 sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
50
Yao Chenf60e0ba2017-11-29 15:06:41 -080051 unordered_map<HashableDimensionKey, vector<DurationBucket>> buckets;
Yao Chend5aa01b32017-12-19 16:46:36 -080052 ConditionKey conditionKey1;
53 conditionKey1["condition"] = conditionKey;
Yao Chen5154a372017-10-30 22:57:06 -070054
55 uint64_t bucketStartTimeNs = 10000000000;
56 uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
57
Yao Chenf60e0ba2017-11-29 15:06:41 -080058 MaxDurationTracker tracker(kConfigKey, "metric", eventKey, wizard, -1, false, bucketStartTimeNs,
59 bucketSizeNs, {});
Yao Chen5154a372017-10-30 22:57:06 -070060
Yao Chend5aa01b32017-12-19 16:46:36 -080061 tracker.noteStart(key1, true, bucketStartTimeNs, conditionKey1);
Yangster-mace2cd6d52017-11-09 20:38:30 -080062 // Event starts again. This would not change anything as it already starts.
Yao Chend5aa01b32017-12-19 16:46:36 -080063 tracker.noteStart(key1, true, bucketStartTimeNs + 3, conditionKey1);
Yangster-mace2cd6d52017-11-09 20:38:30 -080064 // Stopped.
Yao Chend5aa01b32017-12-19 16:46:36 -080065 tracker.noteStop(key1, bucketStartTimeNs + 10, false);
Yao Chen5154a372017-10-30 22:57:06 -070066
Yangster-mace2cd6d52017-11-09 20:38:30 -080067 // Another event starts in this bucket.
Yao Chend5aa01b32017-12-19 16:46:36 -080068 tracker.noteStart(key2, true, bucketStartTimeNs + 20, conditionKey1);
69 tracker.noteStop(key2, bucketStartTimeNs + 40, false /*stop all*/);
Yao Chen5154a372017-10-30 22:57:06 -070070
Yao Chenf60e0ba2017-11-29 15:06:41 -080071 tracker.flushIfNeeded(bucketStartTimeNs + bucketSizeNs + 1, &buckets);
72 EXPECT_TRUE(buckets.find(eventKey) != buckets.end());
73 EXPECT_EQ(1u, buckets[eventKey].size());
74 EXPECT_EQ(20ULL, buckets[eventKey][0].mDuration);
Yangster-mace2cd6d52017-11-09 20:38:30 -080075}
76
77TEST(MaxDurationTrackerTest, TestStopAll) {
78 sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
79
Yao Chenf60e0ba2017-11-29 15:06:41 -080080 unordered_map<HashableDimensionKey, vector<DurationBucket>> buckets;
Yao Chend5aa01b32017-12-19 16:46:36 -080081 ConditionKey conditionKey1;
82 conditionKey1["condition"] = conditionKey;
Yangster-mace2cd6d52017-11-09 20:38:30 -080083
84 uint64_t bucketStartTimeNs = 10000000000;
85 uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
86
Yao Chenf60e0ba2017-11-29 15:06:41 -080087 MaxDurationTracker tracker(kConfigKey, "metric", eventKey, wizard, -1, false, bucketStartTimeNs,
88 bucketSizeNs, {});
Yangster-mace2cd6d52017-11-09 20:38:30 -080089
Yao Chend5aa01b32017-12-19 16:46:36 -080090 tracker.noteStart(key1, true, bucketStartTimeNs + 1, conditionKey1);
Yangster-mace2cd6d52017-11-09 20:38:30 -080091
92 // Another event starts in this bucket.
Yao Chend5aa01b32017-12-19 16:46:36 -080093 tracker.noteStart(key2, true, bucketStartTimeNs + 20, conditionKey1);
Yao Chenf60e0ba2017-11-29 15:06:41 -080094 tracker.flushIfNeeded(bucketStartTimeNs + bucketSizeNs + 40, &buckets);
Yangster-mace2cd6d52017-11-09 20:38:30 -080095 tracker.noteStopAll(bucketStartTimeNs + bucketSizeNs + 40);
96 EXPECT_TRUE(tracker.mInfos.empty());
Yangster-mace2cd6d52017-11-09 20:38:30 -080097
Yao Chenf60e0ba2017-11-29 15:06:41 -080098 EXPECT_TRUE(buckets.find(eventKey) != buckets.end());
99 EXPECT_EQ(1u, buckets[eventKey].size());
100 EXPECT_EQ(bucketSizeNs - 1, buckets[eventKey][0].mDuration);
101
102 tracker.flushIfNeeded(bucketStartTimeNs + 3 * bucketSizeNs + 40, &buckets);
103 EXPECT_EQ(2u, buckets[eventKey].size());
104 EXPECT_EQ(bucketSizeNs - 1, buckets[eventKey][0].mDuration);
105 EXPECT_EQ(40ULL, buckets[eventKey][1].mDuration);
Yao Chen5154a372017-10-30 22:57:06 -0700106}
107
108TEST(MaxDurationTrackerTest, TestCrossBucketBoundary) {
109 sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
110
Yao Chenf60e0ba2017-11-29 15:06:41 -0800111 unordered_map<HashableDimensionKey, vector<DurationBucket>> buckets;
Yao Chend5aa01b32017-12-19 16:46:36 -0800112 ConditionKey conditionKey1;
113 conditionKey1["condition"] = conditionKey;
Yao Chen5154a372017-10-30 22:57:06 -0700114
115 uint64_t bucketStartTimeNs = 10000000000;
116 uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
117
Yao Chenf60e0ba2017-11-29 15:06:41 -0800118 MaxDurationTracker tracker(kConfigKey, "metric", eventKey, wizard, -1, false, bucketStartTimeNs,
119 bucketSizeNs, {});
Yao Chen5154a372017-10-30 22:57:06 -0700120
Yangster-mace2cd6d52017-11-09 20:38:30 -0800121 // The event starts.
Yao Chend5aa01b32017-12-19 16:46:36 -0800122 tracker.noteStart(DEFAULT_DIMENSION_KEY, true, bucketStartTimeNs + 1, conditionKey1);
Yao Chen5154a372017-10-30 22:57:06 -0700123
Yangster-mace2cd6d52017-11-09 20:38:30 -0800124 // Starts again. Does not change anything.
Yao Chend5aa01b32017-12-19 16:46:36 -0800125 tracker.noteStart(DEFAULT_DIMENSION_KEY, true, bucketStartTimeNs + bucketSizeNs + 1,
126 conditionKey1);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800127
Yangster-mace2cd6d52017-11-09 20:38:30 -0800128 // The event stops at early 4th bucket.
Yao Chenf60e0ba2017-11-29 15:06:41 -0800129 tracker.flushIfNeeded(bucketStartTimeNs + (3 * bucketSizeNs) + 20, &buckets);
Yao Chend5aa01b32017-12-19 16:46:36 -0800130 tracker.noteStop(DEFAULT_DIMENSION_KEY, bucketStartTimeNs + (3 * bucketSizeNs) + 20,
131 false /*stop all*/);
Yao Chenf60e0ba2017-11-29 15:06:41 -0800132 EXPECT_TRUE(buckets.find(eventKey) != buckets.end());
133 EXPECT_EQ(3u, buckets[eventKey].size());
134 EXPECT_EQ((unsigned long long)(bucketSizeNs - 1), buckets[eventKey][0].mDuration);
135 EXPECT_EQ((unsigned long long)bucketSizeNs, buckets[eventKey][1].mDuration);
136 EXPECT_EQ((unsigned long long)bucketSizeNs, buckets[eventKey][2].mDuration);
Yao Chen5154a372017-10-30 22:57:06 -0700137}
138
Yao Chen0ea19902017-11-15 15:44:45 -0800139TEST(MaxDurationTrackerTest, TestCrossBucketBoundary_nested) {
140 sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
141
Yao Chenf60e0ba2017-11-29 15:06:41 -0800142 unordered_map<HashableDimensionKey, vector<DurationBucket>> buckets;
Yao Chend5aa01b32017-12-19 16:46:36 -0800143 ConditionKey conditionKey1;
144 conditionKey1["condition"] = conditionKey;
Yao Chen0ea19902017-11-15 15:44:45 -0800145
146 uint64_t bucketStartTimeNs = 10000000000;
147 uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
148
Yao Chenf60e0ba2017-11-29 15:06:41 -0800149 MaxDurationTracker tracker(kConfigKey, "metric", eventKey, wizard, -1, true, bucketStartTimeNs,
150 bucketSizeNs, {});
Yao Chen0ea19902017-11-15 15:44:45 -0800151
152 // 2 starts
Yao Chend5aa01b32017-12-19 16:46:36 -0800153 tracker.noteStart(DEFAULT_DIMENSION_KEY, true, bucketStartTimeNs + 1, conditionKey1);
154 tracker.noteStart(DEFAULT_DIMENSION_KEY, true, bucketStartTimeNs + 10, conditionKey1);
Yao Chen0ea19902017-11-15 15:44:45 -0800155 // one stop
Yao Chend5aa01b32017-12-19 16:46:36 -0800156 tracker.noteStop(DEFAULT_DIMENSION_KEY, bucketStartTimeNs + 20, false /*stop all*/);
Yao Chen0ea19902017-11-15 15:44:45 -0800157
Yao Chenf60e0ba2017-11-29 15:06:41 -0800158 tracker.flushIfNeeded(bucketStartTimeNs + (2 * bucketSizeNs) + 1, &buckets);
Yao Chen0ea19902017-11-15 15:44:45 -0800159
Yao Chenf60e0ba2017-11-29 15:06:41 -0800160 EXPECT_TRUE(buckets.find(eventKey) != buckets.end());
161 EXPECT_EQ(2u, buckets[eventKey].size());
162 EXPECT_EQ(bucketSizeNs - 1, buckets[eventKey][0].mDuration);
163 EXPECT_EQ(bucketSizeNs, buckets[eventKey][1].mDuration);
Yao Chen0ea19902017-11-15 15:44:45 -0800164
165 // real stop now.
Yao Chend5aa01b32017-12-19 16:46:36 -0800166 tracker.noteStop(DEFAULT_DIMENSION_KEY, bucketStartTimeNs + (2 * bucketSizeNs) + 5, false);
Yao Chenf60e0ba2017-11-29 15:06:41 -0800167 tracker.flushIfNeeded(bucketStartTimeNs + (3 * bucketSizeNs) + 1, &buckets);
Yao Chen0ea19902017-11-15 15:44:45 -0800168
Yao Chenf60e0ba2017-11-29 15:06:41 -0800169 EXPECT_EQ(3u, buckets[eventKey].size());
170 EXPECT_EQ(bucketSizeNs - 1, buckets[eventKey][0].mDuration);
171 EXPECT_EQ(bucketSizeNs, buckets[eventKey][1].mDuration);
172 EXPECT_EQ(5ULL, buckets[eventKey][2].mDuration);
Yao Chen0ea19902017-11-15 15:44:45 -0800173}
174
Yao Chen5154a372017-10-30 22:57:06 -0700175TEST(MaxDurationTrackerTest, TestMaxDurationWithCondition) {
176 sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
177
Yao Chend5aa01b32017-12-19 16:46:36 -0800178 ConditionKey conditionKey1;
Yangster-mac20877162017-12-22 17:19:39 -0800179 HashableDimensionKey eventKey = getMockedDimensionKey(TagId, 2, "maps");
Yao Chend5aa01b32017-12-19 16:46:36 -0800180 conditionKey1["APP_BACKGROUND"] = conditionKey;
Yao Chen5154a372017-10-30 22:57:06 -0700181
Yao Chend5aa01b32017-12-19 16:46:36 -0800182 EXPECT_CALL(*wizard, query(_, conditionKey1)) // #4
Yao Chen5154a372017-10-30 22:57:06 -0700183 .WillOnce(Return(ConditionState::kFalse));
184
Yao Chenf60e0ba2017-11-29 15:06:41 -0800185 unordered_map<HashableDimensionKey, vector<DurationBucket>> buckets;
Yao Chen5154a372017-10-30 22:57:06 -0700186
187 uint64_t bucketStartTimeNs = 10000000000;
188 uint64_t eventStartTimeNs = bucketStartTimeNs + 1;
189 uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
190 int64_t durationTimeNs = 2 * 1000;
191
Yao Chenf60e0ba2017-11-29 15:06:41 -0800192 MaxDurationTracker tracker(kConfigKey, "metric", eventKey, wizard, 1, false, bucketStartTimeNs,
193 bucketSizeNs, {});
Yangster-mace2cd6d52017-11-09 20:38:30 -0800194 EXPECT_TRUE(tracker.mAnomalyTrackers.empty());
Yao Chen5154a372017-10-30 22:57:06 -0700195
Yao Chend5aa01b32017-12-19 16:46:36 -0800196 tracker.noteStart(key1, true, eventStartTimeNs, conditionKey1);
Yao Chen5154a372017-10-30 22:57:06 -0700197
Yao Chen09294ef2017-11-25 19:54:01 -0800198 tracker.onSlicedConditionMayChange(eventStartTimeNs + 5);
Yao Chen5154a372017-10-30 22:57:06 -0700199
Yao Chend5aa01b32017-12-19 16:46:36 -0800200 tracker.noteStop(key1, eventStartTimeNs + durationTimeNs, false);
Yao Chen09294ef2017-11-25 19:54:01 -0800201
Yao Chenf60e0ba2017-11-29 15:06:41 -0800202 tracker.flushIfNeeded(bucketStartTimeNs + bucketSizeNs + 1, &buckets);
203 EXPECT_TRUE(buckets.find(eventKey) != buckets.end());
204 EXPECT_EQ(1u, buckets[eventKey].size());
205 EXPECT_EQ(5ULL, buckets[eventKey][0].mDuration);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800206}
Yao Chen5154a372017-10-30 22:57:06 -0700207
Yangster-mace2cd6d52017-11-09 20:38:30 -0800208TEST(MaxDurationTrackerTest, TestAnomalyDetection) {
209 Alert alert;
210 alert.set_name("alert");
Yao Chend5aa01b32017-12-19 16:46:36 -0800211 alert.set_metric_name("metric");
Yangster-mace2cd6d52017-11-09 20:38:30 -0800212 alert.set_trigger_if_sum_gt(32 * NS_PER_SEC);
213 alert.set_number_of_buckets(2);
214 alert.set_refractory_period_secs(1);
215
Yao Chenf60e0ba2017-11-29 15:06:41 -0800216 unordered_map<HashableDimensionKey, vector<DurationBucket>> buckets;
Yangster-mace2cd6d52017-11-09 20:38:30 -0800217 sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
Yao Chend5aa01b32017-12-19 16:46:36 -0800218 ConditionKey conditionKey1;
219 conditionKey1["APP_BACKGROUND"] = conditionKey;
Yangster-mace2cd6d52017-11-09 20:38:30 -0800220 uint64_t bucketStartTimeNs = 10 * NS_PER_SEC;
221 uint64_t eventStartTimeNs = bucketStartTimeNs + NS_PER_SEC + 1;
222 uint64_t bucketSizeNs = 30 * NS_PER_SEC;
223
Bookatz857aaa52017-12-19 15:29:06 -0800224 sp<DurationAnomalyTracker> anomalyTracker = new DurationAnomalyTracker(alert, kConfigKey);
Yao Chenf60e0ba2017-11-29 15:06:41 -0800225 MaxDurationTracker tracker(kConfigKey, "metric", eventKey, wizard, -1, true, bucketStartTimeNs,
226 bucketSizeNs, {anomalyTracker});
Yangster-mace2cd6d52017-11-09 20:38:30 -0800227
Yao Chend5aa01b32017-12-19 16:46:36 -0800228 tracker.noteStart(key1, true, eventStartTimeNs, conditionKey1);
229 tracker.noteStop(key1, eventStartTimeNs + 10, false);
Bookatz857aaa52017-12-19 15:29:06 -0800230 EXPECT_EQ(anomalyTracker->mLastAnomalyTimestampNs, -1);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800231 EXPECT_EQ(10LL, tracker.mDuration);
232
Yao Chend5aa01b32017-12-19 16:46:36 -0800233 tracker.noteStart(key2, true, eventStartTimeNs + 20, conditionKey1);
Yao Chenf60e0ba2017-11-29 15:06:41 -0800234 tracker.flushIfNeeded(eventStartTimeNs + 2 * bucketSizeNs + 3 * NS_PER_SEC, &buckets);
Yao Chend5aa01b32017-12-19 16:46:36 -0800235 tracker.noteStop(key2, eventStartTimeNs + 2 * bucketSizeNs + 3 * NS_PER_SEC, false);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800236 EXPECT_EQ((long long)(4 * NS_PER_SEC + 1LL), tracker.mDuration);
Bookatz857aaa52017-12-19 15:29:06 -0800237 EXPECT_EQ(anomalyTracker->mLastAnomalyTimestampNs,
Yangster-mace2cd6d52017-11-09 20:38:30 -0800238 (long long)(eventStartTimeNs + 2 * bucketSizeNs + 3 * NS_PER_SEC));
Yao Chen5154a372017-10-30 22:57:06 -0700239}
240
Yao Chen93fe3a32017-11-02 13:52:59 -0700241} // namespace statsd
242} // namespace os
243} // namespace android
Yao Chen5154a372017-10-30 22:57:06 -0700244#else
245GTEST_LOG_(INFO) << "This test does nothing.\n";
246#endif