blob: 0772b0d4001df408d3526d82aebcb547b618228e [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 Chen5154a372017-10-30 22:57:06 -070053
54 uint64_t bucketStartTimeNs = 10000000000;
55 uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
56
Yangster-mac94e197c2018-01-02 16:03:03 -080057 int64_t metricId = 1;
58 MaxDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, -1, false, bucketStartTimeNs,
Yao Chend59a6582018-01-08 11:17:11 -080059 bucketSizeNs, false, {});
Yao Chen5154a372017-10-30 22:57:06 -070060
Yao Chend59a6582018-01-08 11:17:11 -080061 tracker.noteStart(key1, true, bucketStartTimeNs, ConditionKey());
Yangster-mace2cd6d52017-11-09 20:38:30 -080062 // Event starts again. This would not change anything as it already starts.
Yao Chend59a6582018-01-08 11:17:11 -080063 tracker.noteStart(key1, true, bucketStartTimeNs + 3, ConditionKey());
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 Chend59a6582018-01-08 11:17:11 -080068 tracker.noteStart(key2, true, bucketStartTimeNs + 20, ConditionKey());
Yao Chend5aa01b32017-12-19 16:46:36 -080069 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;
Yangster-mace2cd6d52017-11-09 20:38:30 -080081
82 uint64_t bucketStartTimeNs = 10000000000;
83 uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
84
Yangster-mac94e197c2018-01-02 16:03:03 -080085 int64_t metricId = 1;
86 MaxDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, -1, false, bucketStartTimeNs,
Yao Chend59a6582018-01-08 11:17:11 -080087 bucketSizeNs, false, {});
Yangster-mace2cd6d52017-11-09 20:38:30 -080088
Yao Chend59a6582018-01-08 11:17:11 -080089 tracker.noteStart(key1, true, bucketStartTimeNs + 1, ConditionKey());
Yangster-mace2cd6d52017-11-09 20:38:30 -080090
91 // Another event starts in this bucket.
Yao Chend59a6582018-01-08 11:17:11 -080092 tracker.noteStart(key2, true, bucketStartTimeNs + 20, ConditionKey());
Yao Chenf60e0ba2017-11-29 15:06:41 -080093 tracker.flushIfNeeded(bucketStartTimeNs + bucketSizeNs + 40, &buckets);
Yangster-mace2cd6d52017-11-09 20:38:30 -080094 tracker.noteStopAll(bucketStartTimeNs + bucketSizeNs + 40);
95 EXPECT_TRUE(tracker.mInfos.empty());
Yangster-mace2cd6d52017-11-09 20:38:30 -080096
Yao Chenf60e0ba2017-11-29 15:06:41 -080097 EXPECT_TRUE(buckets.find(eventKey) != buckets.end());
98 EXPECT_EQ(1u, buckets[eventKey].size());
99 EXPECT_EQ(bucketSizeNs - 1, buckets[eventKey][0].mDuration);
100
101 tracker.flushIfNeeded(bucketStartTimeNs + 3 * bucketSizeNs + 40, &buckets);
102 EXPECT_EQ(2u, buckets[eventKey].size());
103 EXPECT_EQ(bucketSizeNs - 1, buckets[eventKey][0].mDuration);
104 EXPECT_EQ(40ULL, buckets[eventKey][1].mDuration);
Yao Chen5154a372017-10-30 22:57:06 -0700105}
106
107TEST(MaxDurationTrackerTest, TestCrossBucketBoundary) {
108 sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
109
Yao Chenf60e0ba2017-11-29 15:06:41 -0800110 unordered_map<HashableDimensionKey, vector<DurationBucket>> buckets;
Yao Chen5154a372017-10-30 22:57:06 -0700111
112 uint64_t bucketStartTimeNs = 10000000000;
113 uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
114
Yangster-mac94e197c2018-01-02 16:03:03 -0800115 int64_t metricId = 1;
116 MaxDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, -1, false, bucketStartTimeNs,
Yao Chend59a6582018-01-08 11:17:11 -0800117 bucketSizeNs, false, {});
Yao Chen5154a372017-10-30 22:57:06 -0700118
Yangster-mace2cd6d52017-11-09 20:38:30 -0800119 // The event starts.
Yao Chend59a6582018-01-08 11:17:11 -0800120 tracker.noteStart(DEFAULT_DIMENSION_KEY, true, bucketStartTimeNs + 1, ConditionKey());
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,
Yao Chend59a6582018-01-08 11:17:11 -0800124 ConditionKey());
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 Chen0ea19902017-11-15 15:44:45 -0800141
142 uint64_t bucketStartTimeNs = 10000000000;
143 uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
144
Yangster-mac94e197c2018-01-02 16:03:03 -0800145 int64_t metricId = 1;
146 MaxDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, -1, true, bucketStartTimeNs,
Yao Chend59a6582018-01-08 11:17:11 -0800147 bucketSizeNs, false, {});
Yao Chen0ea19902017-11-15 15:44:45 -0800148
149 // 2 starts
Yao Chend59a6582018-01-08 11:17:11 -0800150 tracker.noteStart(DEFAULT_DIMENSION_KEY, true, bucketStartTimeNs + 1, ConditionKey());
151 tracker.noteStart(DEFAULT_DIMENSION_KEY, true, bucketStartTimeNs + 10, ConditionKey());
Yao Chen0ea19902017-11-15 15:44:45 -0800152 // one stop
Yao Chend5aa01b32017-12-19 16:46:36 -0800153 tracker.noteStop(DEFAULT_DIMENSION_KEY, bucketStartTimeNs + 20, false /*stop all*/);
Yao Chen0ea19902017-11-15 15:44:45 -0800154
Yao Chenf60e0ba2017-11-29 15:06:41 -0800155 tracker.flushIfNeeded(bucketStartTimeNs + (2 * bucketSizeNs) + 1, &buckets);
Yao Chen0ea19902017-11-15 15:44:45 -0800156
Yao Chenf60e0ba2017-11-29 15:06:41 -0800157 EXPECT_TRUE(buckets.find(eventKey) != buckets.end());
158 EXPECT_EQ(2u, buckets[eventKey].size());
159 EXPECT_EQ(bucketSizeNs - 1, buckets[eventKey][0].mDuration);
160 EXPECT_EQ(bucketSizeNs, buckets[eventKey][1].mDuration);
Yao Chen0ea19902017-11-15 15:44:45 -0800161
162 // real stop now.
Yao Chend5aa01b32017-12-19 16:46:36 -0800163 tracker.noteStop(DEFAULT_DIMENSION_KEY, bucketStartTimeNs + (2 * bucketSizeNs) + 5, false);
Yao Chenf60e0ba2017-11-29 15:06:41 -0800164 tracker.flushIfNeeded(bucketStartTimeNs + (3 * bucketSizeNs) + 1, &buckets);
Yao Chen0ea19902017-11-15 15:44:45 -0800165
Yao Chenf60e0ba2017-11-29 15:06:41 -0800166 EXPECT_EQ(3u, buckets[eventKey].size());
167 EXPECT_EQ(bucketSizeNs - 1, buckets[eventKey][0].mDuration);
168 EXPECT_EQ(bucketSizeNs, buckets[eventKey][1].mDuration);
169 EXPECT_EQ(5ULL, buckets[eventKey][2].mDuration);
Yao Chen0ea19902017-11-15 15:44:45 -0800170}
171
Yao Chen5154a372017-10-30 22:57:06 -0700172TEST(MaxDurationTrackerTest, TestMaxDurationWithCondition) {
173 sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
174
Yao Chend5aa01b32017-12-19 16:46:36 -0800175 ConditionKey conditionKey1;
Yangster-mac20877162017-12-22 17:19:39 -0800176 HashableDimensionKey eventKey = getMockedDimensionKey(TagId, 2, "maps");
Yangster-mac94e197c2018-01-02 16:03:03 -0800177 conditionKey1[StringToId("APP_BACKGROUND")] = conditionKey;
Yao Chen5154a372017-10-30 22:57:06 -0700178
Yao Chend5aa01b32017-12-19 16:46:36 -0800179 EXPECT_CALL(*wizard, query(_, conditionKey1)) // #4
Yao Chen5154a372017-10-30 22:57:06 -0700180 .WillOnce(Return(ConditionState::kFalse));
181
Yao Chenf60e0ba2017-11-29 15:06:41 -0800182 unordered_map<HashableDimensionKey, vector<DurationBucket>> buckets;
Yao Chen5154a372017-10-30 22:57:06 -0700183
184 uint64_t bucketStartTimeNs = 10000000000;
185 uint64_t eventStartTimeNs = bucketStartTimeNs + 1;
186 uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
187 int64_t durationTimeNs = 2 * 1000;
188
Yangster-mac94e197c2018-01-02 16:03:03 -0800189 int64_t metricId = 1;
190 MaxDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, 1, false, bucketStartTimeNs,
Yao Chend59a6582018-01-08 11:17:11 -0800191 bucketSizeNs, true, {});
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) {
Yangster-mac94e197c2018-01-02 16:03:03 -0800207 int64_t metricId = 1;
Yangster-mace2cd6d52017-11-09 20:38:30 -0800208 Alert alert;
Yangster-mac94e197c2018-01-02 16:03:03 -0800209 alert.set_id(101);
210 alert.set_metric_id(metricId);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800211 alert.set_trigger_if_sum_gt(32 * NS_PER_SEC);
Yangster-maca7fb12d2018-01-03 17:17:20 -0800212 alert.set_num_buckets(2);
Bookatz1bf94382018-01-04 11:43:20 -0800213 const int32_t refPeriodSec = 1;
214 alert.set_refractory_period_secs(refPeriodSec);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800215
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 Chend59a6582018-01-08 11:17:11 -0800218
Yangster-mace2cd6d52017-11-09 20:38:30 -0800219 uint64_t bucketStartTimeNs = 10 * NS_PER_SEC;
220 uint64_t eventStartTimeNs = bucketStartTimeNs + NS_PER_SEC + 1;
221 uint64_t bucketSizeNs = 30 * NS_PER_SEC;
222
Bookatz857aaa52017-12-19 15:29:06 -0800223 sp<DurationAnomalyTracker> anomalyTracker = new DurationAnomalyTracker(alert, kConfigKey);
Yangster-mac94e197c2018-01-02 16:03:03 -0800224 MaxDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, -1, true, bucketStartTimeNs,
Yao Chend59a6582018-01-08 11:17:11 -0800225 bucketSizeNs, false, {anomalyTracker});
Yangster-mace2cd6d52017-11-09 20:38:30 -0800226
Yao Chend59a6582018-01-08 11:17:11 -0800227 tracker.noteStart(key1, true, eventStartTimeNs, ConditionKey());
Yao Chend5aa01b32017-12-19 16:46:36 -0800228 tracker.noteStop(key1, eventStartTimeNs + 10, false);
Bookatz1bf94382018-01-04 11:43:20 -0800229 EXPECT_EQ(anomalyTracker->getRefractoryPeriodEndsSec(eventKey), 0U);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800230 EXPECT_EQ(10LL, tracker.mDuration);
231
Yao Chend59a6582018-01-08 11:17:11 -0800232 tracker.noteStart(key2, true, eventStartTimeNs + 20, ConditionKey());
Yao Chenf60e0ba2017-11-29 15:06:41 -0800233 tracker.flushIfNeeded(eventStartTimeNs + 2 * bucketSizeNs + 3 * NS_PER_SEC, &buckets);
Yao Chend5aa01b32017-12-19 16:46:36 -0800234 tracker.noteStop(key2, eventStartTimeNs + 2 * bucketSizeNs + 3 * NS_PER_SEC, false);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800235 EXPECT_EQ((long long)(4 * NS_PER_SEC + 1LL), tracker.mDuration);
Bookatz1bf94382018-01-04 11:43:20 -0800236
237 EXPECT_EQ(anomalyTracker->getRefractoryPeriodEndsSec(eventKey),
238 (eventStartTimeNs + 2 * bucketSizeNs) / NS_PER_SEC + 3 + refPeriodSec);
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