blob: 4ad1db128bde72a3ff02f23fc997021c4c73e08f [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
41const HashableDimensionKey eventKey = getMockedDimensionKey(0, "1");
42const HashableDimensionKey conditionKey = getMockedDimensionKey(4, "1");
43const HashableDimensionKey key1 = getMockedDimensionKey(1, "1");
44const HashableDimensionKey key2 = getMockedDimensionKey(1, "2");
Yao Chenb3561512017-11-21 18:07:17 -080045
Yao Chen5154a372017-10-30 22:57:06 -070046TEST(MaxDurationTrackerTest, TestSimpleMaxDuration) {
47 sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
48
Yao Chenf60e0ba2017-11-29 15:06:41 -080049 unordered_map<HashableDimensionKey, vector<DurationBucket>> buckets;
Yao Chend5aa01b32017-12-19 16:46:36 -080050 ConditionKey conditionKey1;
51 conditionKey1["condition"] = conditionKey;
Yao Chen5154a372017-10-30 22:57:06 -070052
53 uint64_t bucketStartTimeNs = 10000000000;
54 uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
55
Yao Chenf60e0ba2017-11-29 15:06:41 -080056 MaxDurationTracker tracker(kConfigKey, "metric", eventKey, wizard, -1, false, bucketStartTimeNs,
57 bucketSizeNs, {});
Yao Chen5154a372017-10-30 22:57:06 -070058
Yao Chend5aa01b32017-12-19 16:46:36 -080059 tracker.noteStart(key1, true, bucketStartTimeNs, conditionKey1);
Yangster-mace2cd6d52017-11-09 20:38:30 -080060 // Event starts again. This would not change anything as it already starts.
Yao Chend5aa01b32017-12-19 16:46:36 -080061 tracker.noteStart(key1, true, bucketStartTimeNs + 3, conditionKey1);
Yangster-mace2cd6d52017-11-09 20:38:30 -080062 // Stopped.
Yao Chend5aa01b32017-12-19 16:46:36 -080063 tracker.noteStop(key1, bucketStartTimeNs + 10, false);
Yao Chen5154a372017-10-30 22:57:06 -070064
Yangster-mace2cd6d52017-11-09 20:38:30 -080065 // Another event starts in this bucket.
Yao Chend5aa01b32017-12-19 16:46:36 -080066 tracker.noteStart(key2, true, bucketStartTimeNs + 20, conditionKey1);
67 tracker.noteStop(key2, bucketStartTimeNs + 40, false /*stop all*/);
Yao Chen5154a372017-10-30 22:57:06 -070068
Yao Chenf60e0ba2017-11-29 15:06:41 -080069 tracker.flushIfNeeded(bucketStartTimeNs + bucketSizeNs + 1, &buckets);
70 EXPECT_TRUE(buckets.find(eventKey) != buckets.end());
71 EXPECT_EQ(1u, buckets[eventKey].size());
72 EXPECT_EQ(20ULL, buckets[eventKey][0].mDuration);
Yangster-mace2cd6d52017-11-09 20:38:30 -080073}
74
75TEST(MaxDurationTrackerTest, TestStopAll) {
76 sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
77
Yao Chenf60e0ba2017-11-29 15:06:41 -080078 unordered_map<HashableDimensionKey, vector<DurationBucket>> buckets;
Yao Chend5aa01b32017-12-19 16:46:36 -080079 ConditionKey conditionKey1;
80 conditionKey1["condition"] = conditionKey;
Yangster-mace2cd6d52017-11-09 20:38:30 -080081
82 uint64_t bucketStartTimeNs = 10000000000;
83 uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
84
Yao Chenf60e0ba2017-11-29 15:06:41 -080085 MaxDurationTracker tracker(kConfigKey, "metric", eventKey, wizard, -1, false, bucketStartTimeNs,
86 bucketSizeNs, {});
Yangster-mace2cd6d52017-11-09 20:38:30 -080087
Yao Chend5aa01b32017-12-19 16:46:36 -080088 tracker.noteStart(key1, true, bucketStartTimeNs + 1, conditionKey1);
Yangster-mace2cd6d52017-11-09 20:38:30 -080089
90 // Another event starts in this bucket.
Yao Chend5aa01b32017-12-19 16:46:36 -080091 tracker.noteStart(key2, true, bucketStartTimeNs + 20, conditionKey1);
Yao Chenf60e0ba2017-11-29 15:06:41 -080092 tracker.flushIfNeeded(bucketStartTimeNs + bucketSizeNs + 40, &buckets);
Yangster-mace2cd6d52017-11-09 20:38:30 -080093 tracker.noteStopAll(bucketStartTimeNs + bucketSizeNs + 40);
94 EXPECT_TRUE(tracker.mInfos.empty());
Yangster-mace2cd6d52017-11-09 20:38:30 -080095
Yao Chenf60e0ba2017-11-29 15:06:41 -080096 EXPECT_TRUE(buckets.find(eventKey) != buckets.end());
97 EXPECT_EQ(1u, buckets[eventKey].size());
98 EXPECT_EQ(bucketSizeNs - 1, buckets[eventKey][0].mDuration);
99
100 tracker.flushIfNeeded(bucketStartTimeNs + 3 * bucketSizeNs + 40, &buckets);
101 EXPECT_EQ(2u, buckets[eventKey].size());
102 EXPECT_EQ(bucketSizeNs - 1, buckets[eventKey][0].mDuration);
103 EXPECT_EQ(40ULL, buckets[eventKey][1].mDuration);
Yao Chen5154a372017-10-30 22:57:06 -0700104}
105
106TEST(MaxDurationTrackerTest, TestCrossBucketBoundary) {
107 sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
108
Yao Chenf60e0ba2017-11-29 15:06:41 -0800109 unordered_map<HashableDimensionKey, vector<DurationBucket>> buckets;
Yao Chend5aa01b32017-12-19 16:46:36 -0800110 ConditionKey conditionKey1;
111 conditionKey1["condition"] = conditionKey;
Yao Chen5154a372017-10-30 22:57:06 -0700112
113 uint64_t bucketStartTimeNs = 10000000000;
114 uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
115
Yao Chenf60e0ba2017-11-29 15:06:41 -0800116 MaxDurationTracker tracker(kConfigKey, "metric", eventKey, wizard, -1, false, bucketStartTimeNs,
117 bucketSizeNs, {});
Yao Chen5154a372017-10-30 22:57:06 -0700118
Yangster-mace2cd6d52017-11-09 20:38:30 -0800119 // The event starts.
Yao Chend5aa01b32017-12-19 16:46:36 -0800120 tracker.noteStart(DEFAULT_DIMENSION_KEY, true, bucketStartTimeNs + 1, conditionKey1);
Yao Chen5154a372017-10-30 22:57:06 -0700121
Yangster-mace2cd6d52017-11-09 20:38:30 -0800122 // Starts again. Does not change anything.
Yao Chend5aa01b32017-12-19 16:46:36 -0800123 tracker.noteStart(DEFAULT_DIMENSION_KEY, true, bucketStartTimeNs + bucketSizeNs + 1,
124 conditionKey1);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800125
Yangster-mace2cd6d52017-11-09 20:38:30 -0800126 // The event stops at early 4th bucket.
Yao Chenf60e0ba2017-11-29 15:06:41 -0800127 tracker.flushIfNeeded(bucketStartTimeNs + (3 * bucketSizeNs) + 20, &buckets);
Yao Chend5aa01b32017-12-19 16:46:36 -0800128 tracker.noteStop(DEFAULT_DIMENSION_KEY, bucketStartTimeNs + (3 * bucketSizeNs) + 20,
129 false /*stop all*/);
Yao Chenf60e0ba2017-11-29 15:06:41 -0800130 EXPECT_TRUE(buckets.find(eventKey) != buckets.end());
131 EXPECT_EQ(3u, buckets[eventKey].size());
132 EXPECT_EQ((unsigned long long)(bucketSizeNs - 1), buckets[eventKey][0].mDuration);
133 EXPECT_EQ((unsigned long long)bucketSizeNs, buckets[eventKey][1].mDuration);
134 EXPECT_EQ((unsigned long long)bucketSizeNs, buckets[eventKey][2].mDuration);
Yao Chen5154a372017-10-30 22:57:06 -0700135}
136
Yao Chen0ea19902017-11-15 15:44:45 -0800137TEST(MaxDurationTrackerTest, TestCrossBucketBoundary_nested) {
138 sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
139
Yao Chenf60e0ba2017-11-29 15:06:41 -0800140 unordered_map<HashableDimensionKey, vector<DurationBucket>> buckets;
Yao Chend5aa01b32017-12-19 16:46:36 -0800141 ConditionKey conditionKey1;
142 conditionKey1["condition"] = conditionKey;
Yao Chen0ea19902017-11-15 15:44:45 -0800143
144 uint64_t bucketStartTimeNs = 10000000000;
145 uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
146
Yao Chenf60e0ba2017-11-29 15:06:41 -0800147 MaxDurationTracker tracker(kConfigKey, "metric", eventKey, wizard, -1, true, bucketStartTimeNs,
148 bucketSizeNs, {});
Yao Chen0ea19902017-11-15 15:44:45 -0800149
150 // 2 starts
Yao Chend5aa01b32017-12-19 16:46:36 -0800151 tracker.noteStart(DEFAULT_DIMENSION_KEY, true, bucketStartTimeNs + 1, conditionKey1);
152 tracker.noteStart(DEFAULT_DIMENSION_KEY, true, bucketStartTimeNs + 10, conditionKey1);
Yao Chen0ea19902017-11-15 15:44:45 -0800153 // one stop
Yao Chend5aa01b32017-12-19 16:46:36 -0800154 tracker.noteStop(DEFAULT_DIMENSION_KEY, bucketStartTimeNs + 20, false /*stop all*/);
Yao Chen0ea19902017-11-15 15:44:45 -0800155
Yao Chenf60e0ba2017-11-29 15:06:41 -0800156 tracker.flushIfNeeded(bucketStartTimeNs + (2 * bucketSizeNs) + 1, &buckets);
Yao Chen0ea19902017-11-15 15:44:45 -0800157
Yao Chenf60e0ba2017-11-29 15:06:41 -0800158 EXPECT_TRUE(buckets.find(eventKey) != buckets.end());
159 EXPECT_EQ(2u, buckets[eventKey].size());
160 EXPECT_EQ(bucketSizeNs - 1, buckets[eventKey][0].mDuration);
161 EXPECT_EQ(bucketSizeNs, buckets[eventKey][1].mDuration);
Yao Chen0ea19902017-11-15 15:44:45 -0800162
163 // real stop now.
Yao Chend5aa01b32017-12-19 16:46:36 -0800164 tracker.noteStop(DEFAULT_DIMENSION_KEY, bucketStartTimeNs + (2 * bucketSizeNs) + 5, false);
Yao Chenf60e0ba2017-11-29 15:06:41 -0800165 tracker.flushIfNeeded(bucketStartTimeNs + (3 * bucketSizeNs) + 1, &buckets);
Yao Chen0ea19902017-11-15 15:44:45 -0800166
Yao Chenf60e0ba2017-11-29 15:06:41 -0800167 EXPECT_EQ(3u, buckets[eventKey].size());
168 EXPECT_EQ(bucketSizeNs - 1, buckets[eventKey][0].mDuration);
169 EXPECT_EQ(bucketSizeNs, buckets[eventKey][1].mDuration);
170 EXPECT_EQ(5ULL, buckets[eventKey][2].mDuration);
Yao Chen0ea19902017-11-15 15:44:45 -0800171}
172
Yao Chen5154a372017-10-30 22:57:06 -0700173TEST(MaxDurationTrackerTest, TestMaxDurationWithCondition) {
174 sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
175
Yao Chend5aa01b32017-12-19 16:46:36 -0800176 ConditionKey conditionKey1;
177 HashableDimensionKey eventKey = getMockedDimensionKey(2, "maps");
178 conditionKey1["APP_BACKGROUND"] = conditionKey;
Yao Chen5154a372017-10-30 22:57:06 -0700179
Yao Chend5aa01b32017-12-19 16:46:36 -0800180 EXPECT_CALL(*wizard, query(_, conditionKey1)) // #4
Yao Chen5154a372017-10-30 22:57:06 -0700181 .WillOnce(Return(ConditionState::kFalse));
182
Yao Chenf60e0ba2017-11-29 15:06:41 -0800183 unordered_map<HashableDimensionKey, vector<DurationBucket>> buckets;
Yao Chen5154a372017-10-30 22:57:06 -0700184
185 uint64_t bucketStartTimeNs = 10000000000;
186 uint64_t eventStartTimeNs = bucketStartTimeNs + 1;
187 uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
188 int64_t durationTimeNs = 2 * 1000;
189
Yao Chenf60e0ba2017-11-29 15:06:41 -0800190 MaxDurationTracker tracker(kConfigKey, "metric", eventKey, wizard, 1, false, bucketStartTimeNs,
191 bucketSizeNs, {});
Yangster-mace2cd6d52017-11-09 20:38:30 -0800192 EXPECT_TRUE(tracker.mAnomalyTrackers.empty());
Yao Chen5154a372017-10-30 22:57:06 -0700193
Yao Chend5aa01b32017-12-19 16:46:36 -0800194 tracker.noteStart(key1, true, eventStartTimeNs, conditionKey1);
Yao Chen5154a372017-10-30 22:57:06 -0700195
Yao Chen09294ef2017-11-25 19:54:01 -0800196 tracker.onSlicedConditionMayChange(eventStartTimeNs + 5);
Yao Chen5154a372017-10-30 22:57:06 -0700197
Yao Chend5aa01b32017-12-19 16:46:36 -0800198 tracker.noteStop(key1, eventStartTimeNs + durationTimeNs, false);
Yao Chen09294ef2017-11-25 19:54:01 -0800199
Yao Chenf60e0ba2017-11-29 15:06:41 -0800200 tracker.flushIfNeeded(bucketStartTimeNs + bucketSizeNs + 1, &buckets);
201 EXPECT_TRUE(buckets.find(eventKey) != buckets.end());
202 EXPECT_EQ(1u, buckets[eventKey].size());
203 EXPECT_EQ(5ULL, buckets[eventKey][0].mDuration);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800204}
Yao Chen5154a372017-10-30 22:57:06 -0700205
Yangster-mace2cd6d52017-11-09 20:38:30 -0800206TEST(MaxDurationTrackerTest, TestAnomalyDetection) {
207 Alert alert;
208 alert.set_name("alert");
Yao Chend5aa01b32017-12-19 16:46:36 -0800209 alert.set_metric_name("metric");
Yangster-mace2cd6d52017-11-09 20:38:30 -0800210 alert.set_trigger_if_sum_gt(32 * NS_PER_SEC);
211 alert.set_number_of_buckets(2);
212 alert.set_refractory_period_secs(1);
213
Yao Chenf60e0ba2017-11-29 15:06:41 -0800214 unordered_map<HashableDimensionKey, vector<DurationBucket>> buckets;
Yangster-mace2cd6d52017-11-09 20:38:30 -0800215 sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
Yao Chend5aa01b32017-12-19 16:46:36 -0800216 ConditionKey conditionKey1;
217 conditionKey1["APP_BACKGROUND"] = conditionKey;
Yangster-mace2cd6d52017-11-09 20:38:30 -0800218 uint64_t bucketStartTimeNs = 10 * NS_PER_SEC;
219 uint64_t eventStartTimeNs = bucketStartTimeNs + NS_PER_SEC + 1;
220 uint64_t bucketSizeNs = 30 * NS_PER_SEC;
221
Bookatz857aaa52017-12-19 15:29:06 -0800222 sp<DurationAnomalyTracker> anomalyTracker = new DurationAnomalyTracker(alert, kConfigKey);
Yao Chenf60e0ba2017-11-29 15:06:41 -0800223 MaxDurationTracker tracker(kConfigKey, "metric", eventKey, wizard, -1, true, bucketStartTimeNs,
224 bucketSizeNs, {anomalyTracker});
Yangster-mace2cd6d52017-11-09 20:38:30 -0800225
Yao Chend5aa01b32017-12-19 16:46:36 -0800226 tracker.noteStart(key1, true, eventStartTimeNs, conditionKey1);
227 tracker.noteStop(key1, eventStartTimeNs + 10, false);
Bookatz857aaa52017-12-19 15:29:06 -0800228 EXPECT_EQ(anomalyTracker->mLastAnomalyTimestampNs, -1);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800229 EXPECT_EQ(10LL, tracker.mDuration);
230
Yao Chend5aa01b32017-12-19 16:46:36 -0800231 tracker.noteStart(key2, true, eventStartTimeNs + 20, conditionKey1);
Yao Chenf60e0ba2017-11-29 15:06:41 -0800232 tracker.flushIfNeeded(eventStartTimeNs + 2 * bucketSizeNs + 3 * NS_PER_SEC, &buckets);
Yao Chend5aa01b32017-12-19 16:46:36 -0800233 tracker.noteStop(key2, eventStartTimeNs + 2 * bucketSizeNs + 3 * NS_PER_SEC, false);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800234 EXPECT_EQ((long long)(4 * NS_PER_SEC + 1LL), tracker.mDuration);
Bookatz857aaa52017-12-19 15:29:06 -0800235 EXPECT_EQ(anomalyTracker->mLastAnomalyTimestampNs,
Yangster-mace2cd6d52017-11-09 20:38:30 -0800236 (long long)(eventStartTimeNs + 2 * bucketSizeNs + 3 * NS_PER_SEC));
Yao Chen5154a372017-10-30 22:57:06 -0700237}
238
Yao Chen93fe3a32017-11-02 13:52:59 -0700239} // namespace statsd
240} // namespace os
241} // namespace android
Yao Chen5154a372017-10-30 22:57:06 -0700242#else
243GTEST_LOG_(INFO) << "This test does nothing.\n";
244#endif