blob: fda3daaa56aacb1f2204225ce3d304e92c4c2d4c [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
David Chen27785a82018-01-19 17:06:45 -080044const HashableDimensionKey eventKey = getMockedDimensionKey(TagId, 0, "1");
Yangster13fb7e42018-03-07 17:30:49 -080045const HashableDimensionKey conditionKey = getMockedDimensionKey(TagId, 4, "1");
David Chen27785a82018-01-19 17:06:45 -080046const HashableDimensionKey key1 = getMockedDimensionKey(TagId, 1, "1");
47const HashableDimensionKey key2 = getMockedDimensionKey(TagId, 1, "2");
Yangster-macb142cc82018-03-30 15:22:08 -070048const int64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
Yao Chenb3561512017-11-21 18:07:17 -080049
Yao Chen5154a372017-10-30 22:57:06 -070050TEST(MaxDurationTrackerTest, TestSimpleMaxDuration) {
Yangster-mac93694462018-01-22 20:49:31 -080051 const MetricDimensionKey eventKey = getMockedMetricDimensionKey(TagId, 0, "1");
Yangster-mac93694462018-01-22 20:49:31 -080052 const HashableDimensionKey key1 = getMockedDimensionKey(TagId, 1, "1");
53 const HashableDimensionKey key2 = getMockedDimensionKey(TagId, 1, "2");
54
Yao Chen8a8d16c2018-02-08 14:50:40 -080055
Yao Chen5154a372017-10-30 22:57:06 -070056 sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
57
Yangster-mac93694462018-01-22 20:49:31 -080058 unordered_map<MetricDimensionKey, vector<DurationBucket>> buckets;
Yao Chen5154a372017-10-30 22:57:06 -070059
Yangster-macb142cc82018-03-30 15:22:08 -070060 int64_t bucketStartTimeNs = 10000000000;
61 int64_t bucketEndTimeNs = bucketStartTimeNs + bucketSizeNs;
62 int64_t bucketNum = 0;
Yao Chen5154a372017-10-30 22:57:06 -070063
Yangster-mac94e197c2018-01-02 16:03:03 -080064 int64_t metricId = 1;
tsaichristine1449fa42020-01-02 12:12:05 -080065 MaxDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, -1, false, bucketStartTimeNs,
66 bucketNum, bucketStartTimeNs, bucketSizeNs, false, false, {});
Yao Chen5154a372017-10-30 22:57:06 -070067
Yao Chend59a6582018-01-08 11:17:11 -080068 tracker.noteStart(key1, true, bucketStartTimeNs, ConditionKey());
Yangster-mace2cd6d52017-11-09 20:38:30 -080069 // Event starts again. This would not change anything as it already starts.
Yao Chend59a6582018-01-08 11:17:11 -080070 tracker.noteStart(key1, true, bucketStartTimeNs + 3, ConditionKey());
Yangster-mace2cd6d52017-11-09 20:38:30 -080071 // Stopped.
Yao Chend5aa01b32017-12-19 16:46:36 -080072 tracker.noteStop(key1, bucketStartTimeNs + 10, false);
Yao Chen5154a372017-10-30 22:57:06 -070073
Yangster-mace2cd6d52017-11-09 20:38:30 -080074 // Another event starts in this bucket.
Yao Chend59a6582018-01-08 11:17:11 -080075 tracker.noteStart(key2, true, bucketStartTimeNs + 20, ConditionKey());
Yao Chend5aa01b32017-12-19 16:46:36 -080076 tracker.noteStop(key2, bucketStartTimeNs + 40, false /*stop all*/);
Yao Chen5154a372017-10-30 22:57:06 -070077
Yao Chenf60e0ba2017-11-29 15:06:41 -080078 tracker.flushIfNeeded(bucketStartTimeNs + bucketSizeNs + 1, &buckets);
79 EXPECT_TRUE(buckets.find(eventKey) != buckets.end());
Muhammad Qureshidff78d62020-05-11 13:37:43 -070080 ASSERT_EQ(1u, buckets[eventKey].size());
Yangster-macb142cc82018-03-30 15:22:08 -070081 EXPECT_EQ(20LL, buckets[eventKey][0].mDuration);
Yangster-mace2cd6d52017-11-09 20:38:30 -080082}
83
84TEST(MaxDurationTrackerTest, TestStopAll) {
Yangster-mac93694462018-01-22 20:49:31 -080085 const MetricDimensionKey eventKey = getMockedMetricDimensionKey(TagId, 0, "1");
Yangster-mac93694462018-01-22 20:49:31 -080086 const HashableDimensionKey key1 = getMockedDimensionKey(TagId, 1, "1");
87 const HashableDimensionKey key2 = getMockedDimensionKey(TagId, 1, "2");
88
Yangster-mace2cd6d52017-11-09 20:38:30 -080089 sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
90
Yangster-mac93694462018-01-22 20:49:31 -080091 unordered_map<MetricDimensionKey, vector<DurationBucket>> buckets;
Yangster-mace2cd6d52017-11-09 20:38:30 -080092
Yangster-macb142cc82018-03-30 15:22:08 -070093 int64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
94 int64_t bucketStartTimeNs = 10000000000;
95 int64_t bucketEndTimeNs = bucketStartTimeNs + bucketSizeNs;
96 int64_t bucketNum = 0;
Yangster-mace2cd6d52017-11-09 20:38:30 -080097
Yangster-mac94e197c2018-01-02 16:03:03 -080098 int64_t metricId = 1;
tsaichristine1449fa42020-01-02 12:12:05 -080099 MaxDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, -1, false, bucketStartTimeNs,
100 bucketNum, bucketStartTimeNs, bucketSizeNs, false, false, {});
Yangster-mace2cd6d52017-11-09 20:38:30 -0800101
Yao Chend59a6582018-01-08 11:17:11 -0800102 tracker.noteStart(key1, true, bucketStartTimeNs + 1, ConditionKey());
Yangster-mace2cd6d52017-11-09 20:38:30 -0800103
104 // Another event starts in this bucket.
Yao Chend59a6582018-01-08 11:17:11 -0800105 tracker.noteStart(key2, true, bucketStartTimeNs + 20, ConditionKey());
Yao Chenf60e0ba2017-11-29 15:06:41 -0800106 tracker.flushIfNeeded(bucketStartTimeNs + bucketSizeNs + 40, &buckets);
Yangster-mace2cd6d52017-11-09 20:38:30 -0800107 tracker.noteStopAll(bucketStartTimeNs + bucketSizeNs + 40);
108 EXPECT_TRUE(tracker.mInfos.empty());
David Chen27785a82018-01-19 17:06:45 -0800109 EXPECT_TRUE(buckets.find(eventKey) == buckets.end());
Yao Chenf60e0ba2017-11-29 15:06:41 -0800110
111 tracker.flushIfNeeded(bucketStartTimeNs + 3 * bucketSizeNs + 40, &buckets);
David Chen27785a82018-01-19 17:06:45 -0800112 EXPECT_TRUE(buckets.find(eventKey) != buckets.end());
Muhammad Qureshidff78d62020-05-11 13:37:43 -0700113 ASSERT_EQ(1u, buckets[eventKey].size());
David Chen27785a82018-01-19 17:06:45 -0800114 EXPECT_EQ(bucketSizeNs + 40 - 1, buckets[eventKey][0].mDuration);
115 EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, buckets[eventKey][0].mBucketStartNs);
116 EXPECT_EQ(bucketStartTimeNs + 2 * bucketSizeNs, buckets[eventKey][0].mBucketEndNs);
Yao Chen5154a372017-10-30 22:57:06 -0700117}
118
119TEST(MaxDurationTrackerTest, TestCrossBucketBoundary) {
Yangster-mac93694462018-01-22 20:49:31 -0800120 const MetricDimensionKey eventKey = getMockedMetricDimensionKey(TagId, 0, "1");
Yangster-mac93694462018-01-22 20:49:31 -0800121 const HashableDimensionKey key1 = getMockedDimensionKey(TagId, 1, "1");
122 const HashableDimensionKey key2 = getMockedDimensionKey(TagId, 1, "2");
Yao Chen5154a372017-10-30 22:57:06 -0700123 sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
124
Yangster-mac93694462018-01-22 20:49:31 -0800125 unordered_map<MetricDimensionKey, vector<DurationBucket>> buckets;
Yao Chen5154a372017-10-30 22:57:06 -0700126
Yangster-macb142cc82018-03-30 15:22:08 -0700127 int64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
128 int64_t bucketStartTimeNs = 10000000000;
129 int64_t bucketEndTimeNs = bucketStartTimeNs + bucketSizeNs;
130 int64_t bucketNum = 0;
Yao Chen5154a372017-10-30 22:57:06 -0700131
Yangster-mac94e197c2018-01-02 16:03:03 -0800132 int64_t metricId = 1;
tsaichristine1449fa42020-01-02 12:12:05 -0800133 MaxDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, -1, false, bucketStartTimeNs,
134 bucketNum, bucketStartTimeNs, bucketSizeNs, false, false, {});
Yao Chen5154a372017-10-30 22:57:06 -0700135
Yangster-mace2cd6d52017-11-09 20:38:30 -0800136 // The event starts.
Yao Chend59a6582018-01-08 11:17:11 -0800137 tracker.noteStart(DEFAULT_DIMENSION_KEY, true, bucketStartTimeNs + 1, ConditionKey());
Yao Chen5154a372017-10-30 22:57:06 -0700138
Yangster-mac93694462018-01-22 20:49:31 -0800139 // Starts again. Does not DEFAULT_DIMENSION_KEY anything.
Yao Chend5aa01b32017-12-19 16:46:36 -0800140 tracker.noteStart(DEFAULT_DIMENSION_KEY, true, bucketStartTimeNs + bucketSizeNs + 1,
Yao Chend59a6582018-01-08 11:17:11 -0800141 ConditionKey());
Yangster-mace2cd6d52017-11-09 20:38:30 -0800142
Yangster-mace2cd6d52017-11-09 20:38:30 -0800143 // The event stops at early 4th bucket.
David Chen27785a82018-01-19 17:06:45 -0800144 // Notestop is called from DurationMetricProducer's onMatchedLogEvent, which calls
145 // flushIfneeded.
Yao Chenf60e0ba2017-11-29 15:06:41 -0800146 tracker.flushIfNeeded(bucketStartTimeNs + (3 * bucketSizeNs) + 20, &buckets);
Yao Chend5aa01b32017-12-19 16:46:36 -0800147 tracker.noteStop(DEFAULT_DIMENSION_KEY, bucketStartTimeNs + (3 * bucketSizeNs) + 20,
148 false /*stop all*/);
David Chen27785a82018-01-19 17:06:45 -0800149 EXPECT_TRUE(buckets.find(eventKey) == buckets.end());
150
151 tracker.flushIfNeeded(bucketStartTimeNs + 4 * bucketSizeNs, &buckets);
Muhammad Qureshidff78d62020-05-11 13:37:43 -0700152 ASSERT_EQ(1u, buckets[eventKey].size());
David Chen27785a82018-01-19 17:06:45 -0800153 EXPECT_EQ((3 * bucketSizeNs) + 20 - 1, buckets[eventKey][0].mDuration);
154 EXPECT_EQ(bucketStartTimeNs + 3 * bucketSizeNs, buckets[eventKey][0].mBucketStartNs);
155 EXPECT_EQ(bucketStartTimeNs + 4 * bucketSizeNs, buckets[eventKey][0].mBucketEndNs);
Yao Chen5154a372017-10-30 22:57:06 -0700156}
157
Yao Chen0ea19902017-11-15 15:44:45 -0800158TEST(MaxDurationTrackerTest, TestCrossBucketBoundary_nested) {
Yangster-mac93694462018-01-22 20:49:31 -0800159 const MetricDimensionKey eventKey = getMockedMetricDimensionKey(TagId, 0, "1");
Yangster-mac93694462018-01-22 20:49:31 -0800160 const HashableDimensionKey key1 = getMockedDimensionKey(TagId, 1, "1");
161 const HashableDimensionKey key2 = getMockedDimensionKey(TagId, 1, "2");
Yao Chen0ea19902017-11-15 15:44:45 -0800162 sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
163
Yangster-mac93694462018-01-22 20:49:31 -0800164 unordered_map<MetricDimensionKey, vector<DurationBucket>> buckets;
Yao Chen0ea19902017-11-15 15:44:45 -0800165
Yangster-macb142cc82018-03-30 15:22:08 -0700166 int64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
167 int64_t bucketStartTimeNs = 10000000000;
168 int64_t bucketEndTimeNs = bucketStartTimeNs + bucketSizeNs;
169 int64_t bucketNum = 0;
Yao Chen0ea19902017-11-15 15:44:45 -0800170
Yangster-mac94e197c2018-01-02 16:03:03 -0800171 int64_t metricId = 1;
tsaichristine1449fa42020-01-02 12:12:05 -0800172 MaxDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, -1, true, bucketStartTimeNs,
173 bucketNum, bucketStartTimeNs, bucketSizeNs, false, false, {});
Yao Chen0ea19902017-11-15 15:44:45 -0800174
175 // 2 starts
Yao Chend59a6582018-01-08 11:17:11 -0800176 tracker.noteStart(DEFAULT_DIMENSION_KEY, true, bucketStartTimeNs + 1, ConditionKey());
177 tracker.noteStart(DEFAULT_DIMENSION_KEY, true, bucketStartTimeNs + 10, ConditionKey());
Yao Chen0ea19902017-11-15 15:44:45 -0800178 // one stop
Yao Chend5aa01b32017-12-19 16:46:36 -0800179 tracker.noteStop(DEFAULT_DIMENSION_KEY, bucketStartTimeNs + 20, false /*stop all*/);
Yao Chen0ea19902017-11-15 15:44:45 -0800180
Yao Chenf60e0ba2017-11-29 15:06:41 -0800181 tracker.flushIfNeeded(bucketStartTimeNs + (2 * bucketSizeNs) + 1, &buckets);
David Chen27785a82018-01-19 17:06:45 -0800182 // Because of nesting, still not stopped.
183 EXPECT_TRUE(buckets.find(eventKey) == buckets.end());
Yao Chen0ea19902017-11-15 15:44:45 -0800184
185 // real stop now.
Yangster-mac93694462018-01-22 20:49:31 -0800186 tracker.noteStop(DEFAULT_DIMENSION_KEY,
187 bucketStartTimeNs + (2 * bucketSizeNs) + 5, false);
Yao Chenf60e0ba2017-11-29 15:06:41 -0800188 tracker.flushIfNeeded(bucketStartTimeNs + (3 * bucketSizeNs) + 1, &buckets);
Yao Chen0ea19902017-11-15 15:44:45 -0800189
Muhammad Qureshidff78d62020-05-11 13:37:43 -0700190 ASSERT_EQ(1u, buckets[eventKey].size());
David Chen27785a82018-01-19 17:06:45 -0800191 EXPECT_EQ(2 * bucketSizeNs + 5 - 1, buckets[eventKey][0].mDuration);
Yao Chen0ea19902017-11-15 15:44:45 -0800192}
193
Yao Chen5154a372017-10-30 22:57:06 -0700194TEST(MaxDurationTrackerTest, TestMaxDurationWithCondition) {
Yangster13fb7e42018-03-07 17:30:49 -0800195 const HashableDimensionKey conditionDimKey = key1;
David Chen2e414b92018-02-12 17:24:40 -0800196
David Chen2e414b92018-02-12 17:24:40 -0800197 sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
198
199 ConditionKey conditionKey1;
200 MetricDimensionKey eventKey = getMockedMetricDimensionKey(TagId, 1, "1");
Yangster13fb7e42018-03-07 17:30:49 -0800201 conditionKey1[StringToId("APP_BACKGROUND")] = conditionDimKey;
David Chen2e414b92018-02-12 17:24:40 -0800202
203 /**
204 Start in first bucket, stop in second bucket. Condition turns on and off in the first bucket
205 and again turns on and off in the second bucket.
206 */
Yangster-macb142cc82018-03-30 15:22:08 -0700207 int64_t bucketStartTimeNs = 10000000000;
208 int64_t bucketEndTimeNs = bucketStartTimeNs + bucketSizeNs;
209 int64_t eventStartTimeNs = bucketStartTimeNs + 1 * NS_PER_SEC;
210 int64_t conditionStarts1 = bucketStartTimeNs + 11 * NS_PER_SEC;
211 int64_t conditionStops1 = bucketStartTimeNs + 14 * NS_PER_SEC;
212 int64_t conditionStarts2 = bucketStartTimeNs + bucketSizeNs + 5 * NS_PER_SEC;
213 int64_t conditionStops2 = conditionStarts2 + 10 * NS_PER_SEC;
214 int64_t eventStopTimeNs = conditionStops2 + 8 * NS_PER_SEC;
David Chen2e414b92018-02-12 17:24:40 -0800215
216 int64_t metricId = 1;
tsaichristine1449fa42020-01-02 12:12:05 -0800217 MaxDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, 1, false, bucketStartTimeNs,
218 0, bucketStartTimeNs, bucketSizeNs, true, false, {});
David Chen2e414b92018-02-12 17:24:40 -0800219 EXPECT_TRUE(tracker.mAnomalyTrackers.empty());
220
221 tracker.noteStart(key1, false, eventStartTimeNs, conditionKey1);
222 tracker.noteConditionChanged(key1, true, conditionStarts1);
223 tracker.noteConditionChanged(key1, false, conditionStops1);
224 unordered_map<MetricDimensionKey, vector<DurationBucket>> buckets;
225 tracker.flushIfNeeded(bucketStartTimeNs + bucketSizeNs + 1, &buckets);
Muhammad Qureshidff78d62020-05-11 13:37:43 -0700226 ASSERT_EQ(0U, buckets.size());
David Chen2e414b92018-02-12 17:24:40 -0800227
228 tracker.noteConditionChanged(key1, true, conditionStarts2);
229 tracker.noteConditionChanged(key1, false, conditionStops2);
230 tracker.noteStop(key1, eventStopTimeNs, false);
231 tracker.flushIfNeeded(bucketStartTimeNs + 2 * bucketSizeNs + 1, &buckets);
Muhammad Qureshidff78d62020-05-11 13:37:43 -0700232 ASSERT_EQ(1U, buckets.size());
David Chen2e414b92018-02-12 17:24:40 -0800233 vector<DurationBucket> item = buckets.begin()->second;
Muhammad Qureshidff78d62020-05-11 13:37:43 -0700234 ASSERT_EQ(1UL, item.size());
Yangster-macb142cc82018-03-30 15:22:08 -0700235 EXPECT_EQ((int64_t)(13LL * NS_PER_SEC), item[0].mDuration);
David Chen2e414b92018-02-12 17:24:40 -0800236}
237
238TEST(MaxDurationTrackerTest, TestAnomalyDetection) {
Yao Chen5154a372017-10-30 22:57:06 -0700239 sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
240
Yao Chend5aa01b32017-12-19 16:46:36 -0800241 ConditionKey conditionKey1;
Yangster-mac93694462018-01-22 20:49:31 -0800242 MetricDimensionKey eventKey = getMockedMetricDimensionKey(TagId, 2, "maps");
Yangster-mac94e197c2018-01-02 16:03:03 -0800243 conditionKey1[StringToId("APP_BACKGROUND")] = conditionKey;
Yao Chen5154a372017-10-30 22:57:06 -0700244
Yangster-mac93694462018-01-22 20:49:31 -0800245 unordered_map<MetricDimensionKey, vector<DurationBucket>> buckets;
Yao Chen5154a372017-10-30 22:57:06 -0700246
Yangster-macb142cc82018-03-30 15:22:08 -0700247 int64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
248 int64_t bucketStartTimeNs = 10000000000;
249 int64_t bucketEndTimeNs = bucketStartTimeNs + bucketSizeNs;
250 int64_t bucketNum = 0;
251 int64_t eventStartTimeNs = 13000000000;
Yao Chen5154a372017-10-30 22:57:06 -0700252 int64_t durationTimeNs = 2 * 1000;
253
Yangster-mac94e197c2018-01-02 16:03:03 -0800254 int64_t metricId = 1;
David Chen2e414b92018-02-12 17:24:40 -0800255 Alert alert;
256 alert.set_id(101);
257 alert.set_metric_id(1);
258 alert.set_trigger_if_sum_gt(40 * NS_PER_SEC);
259 alert.set_num_buckets(2);
260 const int32_t refPeriodSec = 45;
261 alert.set_refractory_period_secs(refPeriodSec);
Yangster-mac932ecec2018-02-01 10:23:52 -0800262 sp<AlarmMonitor> alarmMonitor;
263 sp<DurationAnomalyTracker> anomalyTracker =
264 new DurationAnomalyTracker(alert, kConfigKey, alarmMonitor);
tsaichristine1449fa42020-01-02 12:12:05 -0800265 MaxDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, 1, false, bucketStartTimeNs,
266 bucketNum, bucketStartTimeNs, bucketSizeNs, true, false,
267 {anomalyTracker});
Yao Chen5154a372017-10-30 22:57:06 -0700268
Yao Chend5aa01b32017-12-19 16:46:36 -0800269 tracker.noteStart(key1, true, eventStartTimeNs, conditionKey1);
Yangster-mac932ecec2018-02-01 10:23:52 -0800270 sp<const InternalAlarm> alarm = anomalyTracker->mAlarms.begin()->second;
David Chen2e414b92018-02-12 17:24:40 -0800271 EXPECT_EQ((long long)(53ULL * NS_PER_SEC), (long long)(alarm->timestampSec * NS_PER_SEC));
Yao Chen5154a372017-10-30 22:57:06 -0700272
David Chen2e414b92018-02-12 17:24:40 -0800273 // Remove the anomaly alarm when the duration is no longer fully met.
274 tracker.noteConditionChanged(key1, false, eventStartTimeNs + 15 * NS_PER_SEC);
Muhammad Qureshidff78d62020-05-11 13:37:43 -0700275 ASSERT_EQ(0U, anomalyTracker->mAlarms.size());
Yao Chen5154a372017-10-30 22:57:06 -0700276
David Chen2e414b92018-02-12 17:24:40 -0800277 // Since the condition was off for 10 seconds, the anomaly should trigger 10 sec later.
278 tracker.noteConditionChanged(key1, true, eventStartTimeNs + 25 * NS_PER_SEC);
Muhammad Qureshidff78d62020-05-11 13:37:43 -0700279 ASSERT_EQ(1U, anomalyTracker->mAlarms.size());
David Chen2e414b92018-02-12 17:24:40 -0800280 alarm = anomalyTracker->mAlarms.begin()->second;
281 EXPECT_EQ((long long)(63ULL * NS_PER_SEC), (long long)(alarm->timestampSec * NS_PER_SEC));
282}
Yao Chen09294ef2017-11-25 19:54:01 -0800283
David Chen2e414b92018-02-12 17:24:40 -0800284// This tests that we correctly compute the predicted time of an anomaly assuming that the current
285// state continues forward as-is.
286TEST(MaxDurationTrackerTest, TestAnomalyPredictedTimestamp) {
David Chen2e414b92018-02-12 17:24:40 -0800287 sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
288
289 ConditionKey conditionKey1;
290 MetricDimensionKey eventKey = getMockedMetricDimensionKey(TagId, 2, "maps");
291 conditionKey1[StringToId("APP_BACKGROUND")] = conditionKey;
292 ConditionKey conditionKey2;
Yangster13fb7e42018-03-07 17:30:49 -0800293 conditionKey2[StringToId("APP_BACKGROUND")] = getMockedDimensionKey(TagId, 4, "2");
David Chen2e414b92018-02-12 17:24:40 -0800294
295 unordered_map<MetricDimensionKey, vector<DurationBucket>> buckets;
296
297 /**
298 * Suppose we have two sub-dimensions that we're taking the MAX over. In the first of these
299 * nested dimensions, we enter the pause state after 3 seconds. When we resume, the second
300 * dimension has already been running for 4 seconds. Thus, we have 40-4=36 seconds remaining
301 * before we trigger the anomaly.
302 */
Yangster-macb142cc82018-03-30 15:22:08 -0700303 int64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
304 int64_t bucketStartTimeNs = 10000000000;
305 int64_t bucketEndTimeNs = bucketStartTimeNs + bucketSizeNs;
306 int64_t bucketNum = 0;
307 int64_t eventStartTimeNs = bucketStartTimeNs + 5 * NS_PER_SEC; // Condition is off at start.
308 int64_t conditionStarts1 = bucketStartTimeNs + 11 * NS_PER_SEC;
309 int64_t conditionStops1 = bucketStartTimeNs + 14 * NS_PER_SEC;
310 int64_t conditionStarts2 = bucketStartTimeNs + 20 * NS_PER_SEC;
311 int64_t eventStartTimeNs2 = conditionStarts2 - 4 * NS_PER_SEC;
David Chen2e414b92018-02-12 17:24:40 -0800312
313 int64_t metricId = 1;
314 Alert alert;
315 alert.set_id(101);
316 alert.set_metric_id(1);
317 alert.set_trigger_if_sum_gt(40 * NS_PER_SEC);
318 alert.set_num_buckets(2);
319 const int32_t refPeriodSec = 45;
320 alert.set_refractory_period_secs(refPeriodSec);
Yangster-mac932ecec2018-02-01 10:23:52 -0800321 sp<AlarmMonitor> alarmMonitor;
322 sp<DurationAnomalyTracker> anomalyTracker =
323 new DurationAnomalyTracker(alert, kConfigKey, alarmMonitor);
tsaichristine1449fa42020-01-02 12:12:05 -0800324 MaxDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, 1, false, bucketStartTimeNs,
325 bucketNum, bucketStartTimeNs, bucketSizeNs, true, false,
326 {anomalyTracker});
David Chen2e414b92018-02-12 17:24:40 -0800327
328 tracker.noteStart(key1, false, eventStartTimeNs, conditionKey1);
329 tracker.noteConditionChanged(key1, true, conditionStarts1);
330 tracker.noteConditionChanged(key1, false, conditionStops1);
331 tracker.noteStart(key2, true, eventStartTimeNs2, conditionKey2); // Condition is on already.
332 tracker.noteConditionChanged(key1, true, conditionStarts2);
Muhammad Qureshidff78d62020-05-11 13:37:43 -0700333 ASSERT_EQ(1U, anomalyTracker->mAlarms.size());
David Chen2e414b92018-02-12 17:24:40 -0800334 auto alarm = anomalyTracker->mAlarms.begin()->second;
Yangster-macb142cc82018-03-30 15:22:08 -0700335 int64_t anomalyFireTimeSec = alarm->timestampSec;
David Chen2e414b92018-02-12 17:24:40 -0800336 EXPECT_EQ(conditionStarts2 + 36 * NS_PER_SEC,
Yangster-macb142cc82018-03-30 15:22:08 -0700337 (long long)anomalyFireTimeSec * NS_PER_SEC);
Bookatz0dbc7a42018-03-30 16:21:17 -0700338
339 // Now we test the calculation now that there's a refractory period.
340 // At the correct time, declare the anomaly. This will set a refractory period. Make sure it
341 // gets correctly taken into account in future predictAnomalyTimestampNs calculations.
342 std::unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> firedAlarms({alarm});
343 anomalyTracker->informAlarmsFired(anomalyFireTimeSec * NS_PER_SEC, firedAlarms);
Muhammad Qureshidff78d62020-05-11 13:37:43 -0700344 ASSERT_EQ(0u, anomalyTracker->mAlarms.size());
Yangster-macb142cc82018-03-30 15:22:08 -0700345 int64_t refractoryPeriodEndsSec = anomalyFireTimeSec + refPeriodSec;
Bookatz0dbc7a42018-03-30 16:21:17 -0700346 EXPECT_EQ(anomalyTracker->getRefractoryPeriodEndsSec(eventKey), refractoryPeriodEndsSec);
347
348 // Now stop and start again. Make sure the new predictAnomalyTimestampNs takes into account
349 // the refractory period correctly.
Yangster-macb142cc82018-03-30 15:22:08 -0700350 int64_t eventStopTimeNs = anomalyFireTimeSec * NS_PER_SEC + 10;
Bookatz0dbc7a42018-03-30 16:21:17 -0700351 tracker.noteStop(key1, eventStopTimeNs, false);
352 tracker.noteStop(key2, eventStopTimeNs, false);
353 tracker.noteStart(key1, true, eventStopTimeNs + 1000000, conditionKey1);
354 // Anomaly is ongoing, but we're still in the refractory period.
Muhammad Qureshidff78d62020-05-11 13:37:43 -0700355 ASSERT_EQ(1U, anomalyTracker->mAlarms.size());
Bookatz0dbc7a42018-03-30 16:21:17 -0700356 alarm = anomalyTracker->mAlarms.begin()->second;
Yangster-macb142cc82018-03-30 15:22:08 -0700357 EXPECT_EQ(refractoryPeriodEndsSec, (long long)(alarm->timestampSec));
Bookatz0dbc7a42018-03-30 16:21:17 -0700358
359 // Makes sure it is correct after the refractory period is over.
360 tracker.noteStop(key1, eventStopTimeNs + 2000000, false);
Yangster-macb142cc82018-03-30 15:22:08 -0700361 int64_t justBeforeRefPeriodNs = (refractoryPeriodEndsSec - 2) * NS_PER_SEC;
Bookatz0dbc7a42018-03-30 16:21:17 -0700362 tracker.noteStart(key1, true, justBeforeRefPeriodNs, conditionKey1);
363 alarm = anomalyTracker->mAlarms.begin()->second;
364 EXPECT_EQ(justBeforeRefPeriodNs + 40 * NS_PER_SEC,
365 (unsigned long long)(alarm->timestampSec * NS_PER_SEC));
David Chen2e414b92018-02-12 17:24:40 -0800366}
367
368// Suppose that within one tracker there are two dimensions A and B.
369// Suppose A starts, then B starts, and then A stops. We still need to set an anomaly based on the
370// elapsed duration of B.
371TEST(MaxDurationTrackerTest, TestAnomalyPredictedTimestamp_UpdatedOnStop) {
David Chen2e414b92018-02-12 17:24:40 -0800372 sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
373
374 ConditionKey conditionKey1;
375 MetricDimensionKey eventKey = getMockedMetricDimensionKey(TagId, 2, "maps");
376 conditionKey1[StringToId("APP_BACKGROUND")] = conditionKey;
377 ConditionKey conditionKey2;
Yangster13fb7e42018-03-07 17:30:49 -0800378 conditionKey2[StringToId("APP_BACKGROUND")] = getMockedDimensionKey(TagId, 4, "2");
David Chen2e414b92018-02-12 17:24:40 -0800379
380 unordered_map<MetricDimensionKey, vector<DurationBucket>> buckets;
381
382 /**
383 * Suppose we have two sub-dimensions that we're taking the MAX over. In the first of these
384 * nested dimensions, are started for 8 seconds. When we stop, the other nested dimension has
385 * been started for 5 seconds. So we can only allow 35 more seconds from now.
386 */
Yangster-macb142cc82018-03-30 15:22:08 -0700387 int64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
388 int64_t bucketStartTimeNs = 10000000000;
389 int64_t bucketEndTimeNs = bucketStartTimeNs + bucketSizeNs;
390 int64_t bucketNum = 0;
391 int64_t eventStartTimeNs1 = bucketStartTimeNs + 5 * NS_PER_SEC; // Condition is off at start.
392 int64_t eventStopTimeNs1 = bucketStartTimeNs + 13 * NS_PER_SEC;
393 int64_t eventStartTimeNs2 = bucketStartTimeNs + 8 * NS_PER_SEC;
David Chen2e414b92018-02-12 17:24:40 -0800394
395 int64_t metricId = 1;
396 Alert alert;
397 alert.set_id(101);
398 alert.set_metric_id(1);
399 alert.set_trigger_if_sum_gt(40 * NS_PER_SEC);
400 alert.set_num_buckets(2);
401 const int32_t refPeriodSec = 45;
402 alert.set_refractory_period_secs(refPeriodSec);
Yangster-mac932ecec2018-02-01 10:23:52 -0800403 sp<AlarmMonitor> alarmMonitor;
404 sp<DurationAnomalyTracker> anomalyTracker =
405 new DurationAnomalyTracker(alert, kConfigKey, alarmMonitor);
tsaichristine1449fa42020-01-02 12:12:05 -0800406 MaxDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, 1, false, bucketStartTimeNs,
407 bucketNum, bucketStartTimeNs, bucketSizeNs, true, false,
408 {anomalyTracker});
David Chen2e414b92018-02-12 17:24:40 -0800409
410 tracker.noteStart(key1, true, eventStartTimeNs1, conditionKey1);
411 tracker.noteStart(key2, true, eventStartTimeNs2, conditionKey2);
412 tracker.noteStop(key1, eventStopTimeNs1, false);
Muhammad Qureshidff78d62020-05-11 13:37:43 -0700413 ASSERT_EQ(1U, anomalyTracker->mAlarms.size());
David Chen2e414b92018-02-12 17:24:40 -0800414 auto alarm = anomalyTracker->mAlarms.begin()->second;
415 EXPECT_EQ(eventStopTimeNs1 + 35 * NS_PER_SEC,
416 (unsigned long long)(alarm->timestampSec * NS_PER_SEC));
Yangster-mace2cd6d52017-11-09 20:38:30 -0800417}
Yao Chen5154a372017-10-30 22:57:06 -0700418
Yao Chen93fe3a32017-11-02 13:52:59 -0700419} // namespace statsd
420} // namespace os
421} // namespace android
Yao Chen5154a372017-10-30 22:57:06 -0700422#else
423GTEST_LOG_(INFO) << "This test does nothing.\n";
tsaichristine76853372019-08-06 17:17:03 -0700424#endif