blob: 6d323291e8add94efa293af25caf235725e66122 [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 Chen5154a372017-10-30 22:57:06 -070016#include "src/condition/ConditionWizard.h"
Yangster-mac94e197c2018-01-02 16:03:03 -080017#include "metrics_test_helper.h"
18#include "tests/statsd_test_util.h"
Yao Chen5154a372017-10-30 22:57:06 -070019
Yao Chen93fe3a32017-11-02 13:52:59 -070020#include <gmock/gmock.h>
21#include <gtest/gtest.h>
Yao Chen5154a372017-10-30 22:57:06 -070022#include <stdio.h>
23#include <set>
24#include <unordered_map>
25#include <vector>
26
27using namespace android::os::statsd;
28using namespace testing;
29using android::sp;
30using std::set;
31using std::unordered_map;
32using std::vector;
33
34#ifdef __ANDROID__
35
Yao Chen93fe3a32017-11-02 13:52:59 -070036namespace android {
37namespace os {
38namespace statsd {
Yao Chen5154a372017-10-30 22:57:06 -070039
Yangster-mac94e197c2018-01-02 16:03:03 -080040const ConfigKey kConfigKey(0, 12345);
Yao Chend5aa01b32017-12-19 16:46:36 -080041
Yangster-mac20877162017-12-22 17:19:39 -080042const int TagId = 1;
43
44const HashableDimensionKey eventKey = getMockedDimensionKey(TagId, 0, "1");
45const std::vector<HashableDimensionKey> conditionKey = {getMockedDimensionKey(TagId, 4, "1")};
46const HashableDimensionKey key1 = getMockedDimensionKey(TagId, 1, "1");
47const HashableDimensionKey key2 = getMockedDimensionKey(TagId, 1, "2");
Yao Chenb3561512017-11-21 18:07:17 -080048
Yao Chen5154a372017-10-30 22:57:06 -070049TEST(MaxDurationTrackerTest, TestSimpleMaxDuration) {
50 sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
51
Yao Chenf60e0ba2017-11-29 15:06:41 -080052 unordered_map<HashableDimensionKey, vector<DurationBucket>> buckets;
Yao Chend5aa01b32017-12-19 16:46:36 -080053 ConditionKey conditionKey1;
Yangster-mac94e197c2018-01-02 16:03:03 -080054 conditionKey1[StringToId("condition")] = conditionKey;
Yao Chen5154a372017-10-30 22:57:06 -070055
56 uint64_t bucketStartTimeNs = 10000000000;
57 uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
58
Yangster-mac94e197c2018-01-02 16:03:03 -080059 int64_t metricId = 1;
60 MaxDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, -1, false, bucketStartTimeNs,
Yao Chenf60e0ba2017-11-29 15:06:41 -080061 bucketSizeNs, {});
Yao Chen5154a372017-10-30 22:57:06 -070062
Yao Chend5aa01b32017-12-19 16:46:36 -080063 tracker.noteStart(key1, true, bucketStartTimeNs, conditionKey1);
Yangster-mace2cd6d52017-11-09 20:38:30 -080064 // Event starts again. This would not change anything as it already starts.
Yao Chend5aa01b32017-12-19 16:46:36 -080065 tracker.noteStart(key1, true, bucketStartTimeNs + 3, conditionKey1);
Yangster-mace2cd6d52017-11-09 20:38:30 -080066 // Stopped.
Yao Chend5aa01b32017-12-19 16:46:36 -080067 tracker.noteStop(key1, bucketStartTimeNs + 10, false);
Yao Chen5154a372017-10-30 22:57:06 -070068
Yangster-mace2cd6d52017-11-09 20:38:30 -080069 // Another event starts in this bucket.
Yao Chend5aa01b32017-12-19 16:46:36 -080070 tracker.noteStart(key2, true, bucketStartTimeNs + 20, conditionKey1);
71 tracker.noteStop(key2, bucketStartTimeNs + 40, false /*stop all*/);
Yao Chen5154a372017-10-30 22:57:06 -070072
Yao Chenf60e0ba2017-11-29 15:06:41 -080073 tracker.flushIfNeeded(bucketStartTimeNs + bucketSizeNs + 1, &buckets);
74 EXPECT_TRUE(buckets.find(eventKey) != buckets.end());
75 EXPECT_EQ(1u, buckets[eventKey].size());
76 EXPECT_EQ(20ULL, buckets[eventKey][0].mDuration);
Yangster-mace2cd6d52017-11-09 20:38:30 -080077}
78
79TEST(MaxDurationTrackerTest, TestStopAll) {
80 sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
81
Yao Chenf60e0ba2017-11-29 15:06:41 -080082 unordered_map<HashableDimensionKey, vector<DurationBucket>> buckets;
Yao Chend5aa01b32017-12-19 16:46:36 -080083 ConditionKey conditionKey1;
Yangster-mac94e197c2018-01-02 16:03:03 -080084 conditionKey1[StringToId("condition")] = conditionKey;
Yangster-mace2cd6d52017-11-09 20:38:30 -080085
86 uint64_t bucketStartTimeNs = 10000000000;
87 uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
88
Yangster-mac94e197c2018-01-02 16:03:03 -080089 int64_t metricId = 1;
90 MaxDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, -1, false, bucketStartTimeNs,
Yao Chenf60e0ba2017-11-29 15:06:41 -080091 bucketSizeNs, {});
Yangster-mace2cd6d52017-11-09 20:38:30 -080092
Yao Chend5aa01b32017-12-19 16:46:36 -080093 tracker.noteStart(key1, true, bucketStartTimeNs + 1, conditionKey1);
Yangster-mace2cd6d52017-11-09 20:38:30 -080094
95 // Another event starts in this bucket.
Yao Chend5aa01b32017-12-19 16:46:36 -080096 tracker.noteStart(key2, true, bucketStartTimeNs + 20, conditionKey1);
Yao Chenf60e0ba2017-11-29 15:06:41 -080097 tracker.flushIfNeeded(bucketStartTimeNs + bucketSizeNs + 40, &buckets);
Yangster-mace2cd6d52017-11-09 20:38:30 -080098 tracker.noteStopAll(bucketStartTimeNs + bucketSizeNs + 40);
99 EXPECT_TRUE(tracker.mInfos.empty());
Yangster-mace2cd6d52017-11-09 20:38:30 -0800100
Yao Chenf60e0ba2017-11-29 15:06:41 -0800101 EXPECT_TRUE(buckets.find(eventKey) != buckets.end());
102 EXPECT_EQ(1u, buckets[eventKey].size());
103 EXPECT_EQ(bucketSizeNs - 1, buckets[eventKey][0].mDuration);
104
105 tracker.flushIfNeeded(bucketStartTimeNs + 3 * bucketSizeNs + 40, &buckets);
106 EXPECT_EQ(2u, buckets[eventKey].size());
107 EXPECT_EQ(bucketSizeNs - 1, buckets[eventKey][0].mDuration);
108 EXPECT_EQ(40ULL, buckets[eventKey][1].mDuration);
Yao Chen5154a372017-10-30 22:57:06 -0700109}
110
111TEST(MaxDurationTrackerTest, TestCrossBucketBoundary) {
112 sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
113
Yao Chenf60e0ba2017-11-29 15:06:41 -0800114 unordered_map<HashableDimensionKey, vector<DurationBucket>> buckets;
Yao Chend5aa01b32017-12-19 16:46:36 -0800115 ConditionKey conditionKey1;
Yangster-mac94e197c2018-01-02 16:03:03 -0800116 conditionKey1[StringToId("condition")] = conditionKey;
Yao Chen5154a372017-10-30 22:57:06 -0700117
118 uint64_t bucketStartTimeNs = 10000000000;
119 uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
120
Yangster-mac94e197c2018-01-02 16:03:03 -0800121 int64_t metricId = 1;
122 MaxDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, -1, false, bucketStartTimeNs,
Yao Chenf60e0ba2017-11-29 15:06:41 -0800123 bucketSizeNs, {});
Yao Chen5154a372017-10-30 22:57:06 -0700124
Yangster-mace2cd6d52017-11-09 20:38:30 -0800125 // The event starts.
Yao Chend5aa01b32017-12-19 16:46:36 -0800126 tracker.noteStart(DEFAULT_DIMENSION_KEY, true, bucketStartTimeNs + 1, conditionKey1);
Yao Chen5154a372017-10-30 22:57:06 -0700127
Yangster-mace2cd6d52017-11-09 20:38:30 -0800128 // Starts again. Does not change anything.
Yao Chend5aa01b32017-12-19 16:46:36 -0800129 tracker.noteStart(DEFAULT_DIMENSION_KEY, true, bucketStartTimeNs + bucketSizeNs + 1,
130 conditionKey1);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800131
Yangster-mace2cd6d52017-11-09 20:38:30 -0800132 // The event stops at early 4th bucket.
Yao Chenf60e0ba2017-11-29 15:06:41 -0800133 tracker.flushIfNeeded(bucketStartTimeNs + (3 * bucketSizeNs) + 20, &buckets);
Yao Chend5aa01b32017-12-19 16:46:36 -0800134 tracker.noteStop(DEFAULT_DIMENSION_KEY, bucketStartTimeNs + (3 * bucketSizeNs) + 20,
135 false /*stop all*/);
Yao Chenf60e0ba2017-11-29 15:06:41 -0800136 EXPECT_TRUE(buckets.find(eventKey) != buckets.end());
137 EXPECT_EQ(3u, buckets[eventKey].size());
138 EXPECT_EQ((unsigned long long)(bucketSizeNs - 1), buckets[eventKey][0].mDuration);
139 EXPECT_EQ((unsigned long long)bucketSizeNs, buckets[eventKey][1].mDuration);
140 EXPECT_EQ((unsigned long long)bucketSizeNs, buckets[eventKey][2].mDuration);
Yao Chen5154a372017-10-30 22:57:06 -0700141}
142
Yao Chen0ea19902017-11-15 15:44:45 -0800143TEST(MaxDurationTrackerTest, TestCrossBucketBoundary_nested) {
144 sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
145
Yao Chenf60e0ba2017-11-29 15:06:41 -0800146 unordered_map<HashableDimensionKey, vector<DurationBucket>> buckets;
Yao Chend5aa01b32017-12-19 16:46:36 -0800147 ConditionKey conditionKey1;
Yangster-mac94e197c2018-01-02 16:03:03 -0800148 conditionKey1[StringToId("condition")] = conditionKey;
Yao Chen0ea19902017-11-15 15:44:45 -0800149
150 uint64_t bucketStartTimeNs = 10000000000;
151 uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
152
Yangster-mac94e197c2018-01-02 16:03:03 -0800153 int64_t metricId = 1;
154 MaxDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, -1, true, bucketStartTimeNs,
Yao Chenf60e0ba2017-11-29 15:06:41 -0800155 bucketSizeNs, {});
Yao Chen0ea19902017-11-15 15:44:45 -0800156
157 // 2 starts
Yao Chend5aa01b32017-12-19 16:46:36 -0800158 tracker.noteStart(DEFAULT_DIMENSION_KEY, true, bucketStartTimeNs + 1, conditionKey1);
159 tracker.noteStart(DEFAULT_DIMENSION_KEY, true, bucketStartTimeNs + 10, conditionKey1);
Yao Chen0ea19902017-11-15 15:44:45 -0800160 // one stop
Yao Chend5aa01b32017-12-19 16:46:36 -0800161 tracker.noteStop(DEFAULT_DIMENSION_KEY, bucketStartTimeNs + 20, false /*stop all*/);
Yao Chen0ea19902017-11-15 15:44:45 -0800162
Yao Chenf60e0ba2017-11-29 15:06:41 -0800163 tracker.flushIfNeeded(bucketStartTimeNs + (2 * bucketSizeNs) + 1, &buckets);
Yao Chen0ea19902017-11-15 15:44:45 -0800164
Yao Chenf60e0ba2017-11-29 15:06:41 -0800165 EXPECT_TRUE(buckets.find(eventKey) != buckets.end());
166 EXPECT_EQ(2u, buckets[eventKey].size());
167 EXPECT_EQ(bucketSizeNs - 1, buckets[eventKey][0].mDuration);
168 EXPECT_EQ(bucketSizeNs, buckets[eventKey][1].mDuration);
Yao Chen0ea19902017-11-15 15:44:45 -0800169
170 // real stop now.
Yao Chend5aa01b32017-12-19 16:46:36 -0800171 tracker.noteStop(DEFAULT_DIMENSION_KEY, bucketStartTimeNs + (2 * bucketSizeNs) + 5, false);
Yao Chenf60e0ba2017-11-29 15:06:41 -0800172 tracker.flushIfNeeded(bucketStartTimeNs + (3 * bucketSizeNs) + 1, &buckets);
Yao Chen0ea19902017-11-15 15:44:45 -0800173
Yao Chenf60e0ba2017-11-29 15:06:41 -0800174 EXPECT_EQ(3u, buckets[eventKey].size());
175 EXPECT_EQ(bucketSizeNs - 1, buckets[eventKey][0].mDuration);
176 EXPECT_EQ(bucketSizeNs, buckets[eventKey][1].mDuration);
177 EXPECT_EQ(5ULL, buckets[eventKey][2].mDuration);
Yao Chen0ea19902017-11-15 15:44:45 -0800178}
179
Yao Chen5154a372017-10-30 22:57:06 -0700180TEST(MaxDurationTrackerTest, TestMaxDurationWithCondition) {
181 sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
182
Yao Chend5aa01b32017-12-19 16:46:36 -0800183 ConditionKey conditionKey1;
Yangster-mac20877162017-12-22 17:19:39 -0800184 HashableDimensionKey eventKey = getMockedDimensionKey(TagId, 2, "maps");
Yangster-mac94e197c2018-01-02 16:03:03 -0800185 conditionKey1[StringToId("APP_BACKGROUND")] = conditionKey;
Yao Chen5154a372017-10-30 22:57:06 -0700186
Yao Chend5aa01b32017-12-19 16:46:36 -0800187 EXPECT_CALL(*wizard, query(_, conditionKey1)) // #4
Yao Chen5154a372017-10-30 22:57:06 -0700188 .WillOnce(Return(ConditionState::kFalse));
189
Yao Chenf60e0ba2017-11-29 15:06:41 -0800190 unordered_map<HashableDimensionKey, vector<DurationBucket>> buckets;
Yao Chen5154a372017-10-30 22:57:06 -0700191
192 uint64_t bucketStartTimeNs = 10000000000;
193 uint64_t eventStartTimeNs = bucketStartTimeNs + 1;
194 uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
195 int64_t durationTimeNs = 2 * 1000;
196
Yangster-mac94e197c2018-01-02 16:03:03 -0800197 int64_t metricId = 1;
198 MaxDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, 1, false, bucketStartTimeNs,
Yao Chenf60e0ba2017-11-29 15:06:41 -0800199 bucketSizeNs, {});
Yangster-mace2cd6d52017-11-09 20:38:30 -0800200 EXPECT_TRUE(tracker.mAnomalyTrackers.empty());
Yao Chen5154a372017-10-30 22:57:06 -0700201
Yao Chend5aa01b32017-12-19 16:46:36 -0800202 tracker.noteStart(key1, true, eventStartTimeNs, conditionKey1);
Yao Chen5154a372017-10-30 22:57:06 -0700203
Yao Chen09294ef2017-11-25 19:54:01 -0800204 tracker.onSlicedConditionMayChange(eventStartTimeNs + 5);
Yao Chen5154a372017-10-30 22:57:06 -0700205
Yao Chend5aa01b32017-12-19 16:46:36 -0800206 tracker.noteStop(key1, eventStartTimeNs + durationTimeNs, false);
Yao Chen09294ef2017-11-25 19:54:01 -0800207
Yao Chenf60e0ba2017-11-29 15:06:41 -0800208 tracker.flushIfNeeded(bucketStartTimeNs + bucketSizeNs + 1, &buckets);
209 EXPECT_TRUE(buckets.find(eventKey) != buckets.end());
210 EXPECT_EQ(1u, buckets[eventKey].size());
211 EXPECT_EQ(5ULL, buckets[eventKey][0].mDuration);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800212}
Yao Chen5154a372017-10-30 22:57:06 -0700213
Yangster-mace2cd6d52017-11-09 20:38:30 -0800214TEST(MaxDurationTrackerTest, TestAnomalyDetection) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800215 int64_t metricId = 1;
Yangster-mace2cd6d52017-11-09 20:38:30 -0800216 Alert alert;
Yangster-mac94e197c2018-01-02 16:03:03 -0800217 alert.set_id(101);
218 alert.set_metric_id(metricId);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800219 alert.set_trigger_if_sum_gt(32 * NS_PER_SEC);
220 alert.set_number_of_buckets(2);
221 alert.set_refractory_period_secs(1);
222
Yao Chenf60e0ba2017-11-29 15:06:41 -0800223 unordered_map<HashableDimensionKey, vector<DurationBucket>> buckets;
Yangster-mace2cd6d52017-11-09 20:38:30 -0800224 sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
Yao Chend5aa01b32017-12-19 16:46:36 -0800225 ConditionKey conditionKey1;
Yangster-mac94e197c2018-01-02 16:03:03 -0800226 conditionKey1[StringToId("APP_BACKGROUND")] = conditionKey;
Yangster-mace2cd6d52017-11-09 20:38:30 -0800227 uint64_t bucketStartTimeNs = 10 * NS_PER_SEC;
228 uint64_t eventStartTimeNs = bucketStartTimeNs + NS_PER_SEC + 1;
229 uint64_t bucketSizeNs = 30 * NS_PER_SEC;
230
Bookatz857aaa52017-12-19 15:29:06 -0800231 sp<DurationAnomalyTracker> anomalyTracker = new DurationAnomalyTracker(alert, kConfigKey);
Yangster-mac94e197c2018-01-02 16:03:03 -0800232 MaxDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, -1, true, bucketStartTimeNs,
Yao Chenf60e0ba2017-11-29 15:06:41 -0800233 bucketSizeNs, {anomalyTracker});
Yangster-mace2cd6d52017-11-09 20:38:30 -0800234
Yao Chend5aa01b32017-12-19 16:46:36 -0800235 tracker.noteStart(key1, true, eventStartTimeNs, conditionKey1);
236 tracker.noteStop(key1, eventStartTimeNs + 10, false);
Bookatz857aaa52017-12-19 15:29:06 -0800237 EXPECT_EQ(anomalyTracker->mLastAnomalyTimestampNs, -1);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800238 EXPECT_EQ(10LL, tracker.mDuration);
239
Yao Chend5aa01b32017-12-19 16:46:36 -0800240 tracker.noteStart(key2, true, eventStartTimeNs + 20, conditionKey1);
Yao Chenf60e0ba2017-11-29 15:06:41 -0800241 tracker.flushIfNeeded(eventStartTimeNs + 2 * bucketSizeNs + 3 * NS_PER_SEC, &buckets);
Yao Chend5aa01b32017-12-19 16:46:36 -0800242 tracker.noteStop(key2, eventStartTimeNs + 2 * bucketSizeNs + 3 * NS_PER_SEC, false);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800243 EXPECT_EQ((long long)(4 * NS_PER_SEC + 1LL), tracker.mDuration);
Bookatz857aaa52017-12-19 15:29:06 -0800244 EXPECT_EQ(anomalyTracker->mLastAnomalyTimestampNs,
Yangster-mace2cd6d52017-11-09 20:38:30 -0800245 (long long)(eventStartTimeNs + 2 * bucketSizeNs + 3 * NS_PER_SEC));
Yao Chen5154a372017-10-30 22:57:06 -0700246}
247
Yao Chen93fe3a32017-11-02 13:52:59 -0700248} // namespace statsd
249} // namespace os
250} // namespace android
Yao Chen5154a372017-10-30 22:57:06 -0700251#else
252GTEST_LOG_(INFO) << "This test does nothing.\n";
253#endif