Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 1 | // 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 Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 15 | #include "src/metrics/duration_helper/OringDurationTracker.h" |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 16 | #include "src/condition/ConditionWizard.h" |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 17 | #include "metrics_test_helper.h" |
| 18 | #include "tests/statsd_test_util.h" |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 19 | |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 20 | #include <gmock/gmock.h> |
| 21 | #include <gtest/gtest.h> |
Bookatz | 6bf9825 | 2018-03-14 10:44:24 -0700 | [diff] [blame] | 22 | #include <math.h> |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 23 | #include <stdio.h> |
| 24 | #include <set> |
| 25 | #include <unordered_map> |
| 26 | #include <vector> |
| 27 | |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 28 | using namespace testing; |
| 29 | using android::sp; |
| 30 | using std::set; |
| 31 | using std::unordered_map; |
| 32 | using std::vector; |
| 33 | |
| 34 | #ifdef __ANDROID__ |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 35 | namespace android { |
| 36 | namespace os { |
| 37 | namespace statsd { |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 38 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 39 | const ConfigKey kConfigKey(0, 12345); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 40 | const int TagId = 1; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 41 | const int64_t metricId = 123; |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 42 | const HashableDimensionKey eventKey = getMockedDimensionKey(TagId, 0, "event"); |
| 43 | |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 44 | const HashableDimensionKey kConditionKey1 = getMockedDimensionKey(TagId, 1, "maps"); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 45 | const HashableDimensionKey kEventKey1 = getMockedDimensionKey(TagId, 2, "maps"); |
| 46 | const HashableDimensionKey kEventKey2 = getMockedDimensionKey(TagId, 3, "maps"); |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 47 | const int64_t bucketSizeNs = 30 * NS_PER_SEC; |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 48 | |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 49 | TEST(OringDurationTrackerTest, TestDurationOverlap) { |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 50 | const MetricDimensionKey eventKey = getMockedMetricDimensionKey(TagId, 0, "event"); |
| 51 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 52 | const HashableDimensionKey kEventKey1 = getMockedDimensionKey(TagId, 2, "maps"); |
| 53 | const HashableDimensionKey kEventKey2 = getMockedDimensionKey(TagId, 3, "maps"); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 54 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 55 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 56 | unordered_map<MetricDimensionKey, vector<DurationBucket>> buckets; |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 57 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 58 | int64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL; |
| 59 | int64_t bucketStartTimeNs = 10000000000; |
| 60 | int64_t bucketNum = 0; |
| 61 | int64_t eventStartTimeNs = bucketStartTimeNs + 1; |
| 62 | int64_t durationTimeNs = 2 * 1000; |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 63 | |
tsaichristine | 1449fa4 | 2020-01-02 12:12:05 -0800 | [diff] [blame] | 64 | OringDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, 1, false, |
| 65 | bucketStartTimeNs, bucketNum, bucketStartTimeNs, bucketSizeNs, |
| 66 | false, false, {}); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 67 | |
Yao Chen | d59a658 | 2018-01-08 11:17:11 -0800 | [diff] [blame] | 68 | tracker.noteStart(kEventKey1, true, eventStartTimeNs, ConditionKey()); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 69 | EXPECT_EQ((long long)eventStartTimeNs, tracker.mLastStartTime); |
Yao Chen | d59a658 | 2018-01-08 11:17:11 -0800 | [diff] [blame] | 70 | tracker.noteStart(kEventKey1, true, eventStartTimeNs + 10, ConditionKey()); // overlapping wl |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 71 | EXPECT_EQ((long long)eventStartTimeNs, tracker.mLastStartTime); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 72 | |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 73 | tracker.noteStop(kEventKey1, eventStartTimeNs + durationTimeNs, false); |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 74 | tracker.flushIfNeeded(eventStartTimeNs + bucketSizeNs + 1, &buckets); |
| 75 | EXPECT_TRUE(buckets.find(eventKey) != buckets.end()); |
| 76 | |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 77 | ASSERT_EQ(1u, buckets[eventKey].size()); |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 78 | EXPECT_EQ(durationTimeNs, buckets[eventKey][0].mDuration); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 81 | TEST(OringDurationTrackerTest, TestDurationNested) { |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 82 | const MetricDimensionKey eventKey = getMockedMetricDimensionKey(TagId, 0, "event"); |
| 83 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 84 | const HashableDimensionKey kEventKey1 = getMockedDimensionKey(TagId, 2, "maps"); |
| 85 | const HashableDimensionKey kEventKey2 = getMockedDimensionKey(TagId, 3, "maps"); |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 86 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 87 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 88 | unordered_map<MetricDimensionKey, vector<DurationBucket>> buckets; |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 89 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 90 | int64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL; |
| 91 | int64_t bucketStartTimeNs = 10000000000; |
| 92 | int64_t bucketNum = 0; |
| 93 | int64_t eventStartTimeNs = bucketStartTimeNs + 1; |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 94 | |
tsaichristine | 1449fa4 | 2020-01-02 12:12:05 -0800 | [diff] [blame] | 95 | OringDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, 1, true, bucketStartTimeNs, |
| 96 | bucketNum, bucketStartTimeNs, bucketSizeNs, false, false, {}); |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 97 | |
Yao Chen | d59a658 | 2018-01-08 11:17:11 -0800 | [diff] [blame] | 98 | tracker.noteStart(kEventKey1, true, eventStartTimeNs, ConditionKey()); |
| 99 | tracker.noteStart(kEventKey1, true, eventStartTimeNs + 10, ConditionKey()); // overlapping wl |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 100 | |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 101 | tracker.noteStop(kEventKey1, eventStartTimeNs + 2000, false); |
| 102 | tracker.noteStop(kEventKey1, eventStartTimeNs + 2003, false); |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 103 | |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 104 | tracker.flushIfNeeded(bucketStartTimeNs + bucketSizeNs + 1, &buckets); |
| 105 | EXPECT_TRUE(buckets.find(eventKey) != buckets.end()); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 106 | ASSERT_EQ(1u, buckets[eventKey].size()); |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 107 | EXPECT_EQ(2003LL, buckets[eventKey][0].mDuration); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | TEST(OringDurationTrackerTest, TestStopAll) { |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 111 | const MetricDimensionKey eventKey = getMockedMetricDimensionKey(TagId, 0, "event"); |
| 112 | |
| 113 | const std::vector<HashableDimensionKey> kConditionKey1 = |
| 114 | {getMockedDimensionKey(TagId, 1, "maps")}; |
| 115 | const HashableDimensionKey kEventKey1 = getMockedDimensionKey(TagId, 2, "maps"); |
| 116 | const HashableDimensionKey kEventKey2 = getMockedDimensionKey(TagId, 3, "maps"); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 117 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 118 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 119 | unordered_map<MetricDimensionKey, vector<DurationBucket>> buckets; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 120 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 121 | int64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL; |
| 122 | int64_t bucketStartTimeNs = 10000000000; |
| 123 | int64_t bucketNum = 0; |
| 124 | int64_t eventStartTimeNs = bucketStartTimeNs + 1; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 125 | |
tsaichristine | 1449fa4 | 2020-01-02 12:12:05 -0800 | [diff] [blame] | 126 | OringDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, 1, true, bucketStartTimeNs, |
| 127 | bucketNum, bucketStartTimeNs, bucketSizeNs, false, false, {}); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 128 | |
Yao Chen | d59a658 | 2018-01-08 11:17:11 -0800 | [diff] [blame] | 129 | tracker.noteStart(kEventKey1, true, eventStartTimeNs, ConditionKey()); |
| 130 | tracker.noteStart(kEventKey2, true, eventStartTimeNs + 10, ConditionKey()); // overlapping wl |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 131 | |
| 132 | tracker.noteStopAll(eventStartTimeNs + 2003); |
| 133 | |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 134 | tracker.flushIfNeeded(bucketStartTimeNs + bucketSizeNs + 1, &buckets); |
| 135 | EXPECT_TRUE(buckets.find(eventKey) != buckets.end()); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 136 | ASSERT_EQ(1u, buckets[eventKey].size()); |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 137 | EXPECT_EQ(2003LL, buckets[eventKey][0].mDuration); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | TEST(OringDurationTrackerTest, TestCrossBucketBoundary) { |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 141 | const MetricDimensionKey eventKey = getMockedMetricDimensionKey(TagId, 0, "event"); |
| 142 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 143 | const HashableDimensionKey kEventKey1 = getMockedDimensionKey(TagId, 2, "maps"); |
| 144 | const HashableDimensionKey kEventKey2 = getMockedDimensionKey(TagId, 3, "maps"); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 145 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 146 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 147 | unordered_map<MetricDimensionKey, vector<DurationBucket>> buckets; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 148 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 149 | int64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL; |
| 150 | int64_t bucketStartTimeNs = 10000000000; |
| 151 | int64_t bucketNum = 0; |
| 152 | int64_t eventStartTimeNs = bucketStartTimeNs + 1; |
| 153 | int64_t durationTimeNs = 2 * 1000; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 154 | |
tsaichristine | 1449fa4 | 2020-01-02 12:12:05 -0800 | [diff] [blame] | 155 | OringDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, 1, true, bucketStartTimeNs, |
| 156 | bucketNum, bucketStartTimeNs, bucketSizeNs, false, false, {}); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 157 | |
Yao Chen | d59a658 | 2018-01-08 11:17:11 -0800 | [diff] [blame] | 158 | tracker.noteStart(kEventKey1, true, eventStartTimeNs, ConditionKey()); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 159 | EXPECT_EQ((long long)eventStartTimeNs, tracker.mLastStartTime); |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 160 | tracker.flushIfNeeded(eventStartTimeNs + 2 * bucketSizeNs, &buckets); |
Yao Chen | d59a658 | 2018-01-08 11:17:11 -0800 | [diff] [blame] | 161 | tracker.noteStart(kEventKey1, true, eventStartTimeNs + 2 * bucketSizeNs, ConditionKey()); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 162 | EXPECT_EQ((long long)(bucketStartTimeNs + 2 * bucketSizeNs), tracker.mLastStartTime); |
| 163 | |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 164 | ASSERT_EQ(2u, buckets[eventKey].size()); |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 165 | EXPECT_EQ(bucketSizeNs - 1, buckets[eventKey][0].mDuration); |
| 166 | EXPECT_EQ(bucketSizeNs, buckets[eventKey][1].mDuration); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 167 | |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 168 | tracker.noteStop(kEventKey1, eventStartTimeNs + 2 * bucketSizeNs + 10, false); |
| 169 | tracker.noteStop(kEventKey1, eventStartTimeNs + 2 * bucketSizeNs + 12, false); |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 170 | tracker.flushIfNeeded(eventStartTimeNs + 2 * bucketSizeNs + 12, &buckets); |
| 171 | EXPECT_TRUE(buckets.find(eventKey) != buckets.end()); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 172 | ASSERT_EQ(2u, buckets[eventKey].size()); |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 173 | EXPECT_EQ(bucketSizeNs - 1, buckets[eventKey][0].mDuration); |
| 174 | EXPECT_EQ(bucketSizeNs, buckets[eventKey][1].mDuration); |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 175 | } |
| 176 | |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 177 | TEST(OringDurationTrackerTest, TestDurationConditionChange) { |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 178 | const MetricDimensionKey eventKey = getMockedMetricDimensionKey(TagId, 0, "event"); |
| 179 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 180 | const HashableDimensionKey kEventKey1 = getMockedDimensionKey(TagId, 2, "maps"); |
| 181 | const HashableDimensionKey kEventKey2 = getMockedDimensionKey(TagId, 3, "maps"); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 182 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 183 | |
| 184 | ConditionKey key1; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 185 | key1[StringToId("APP_BACKGROUND")] = kConditionKey1; |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 186 | |
tsaichristine | 7685337 | 2019-08-06 17:17:03 -0700 | [diff] [blame] | 187 | EXPECT_CALL(*wizard, query(_, key1, _)) // #4 |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 188 | .WillOnce(Return(ConditionState::kFalse)); |
| 189 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 190 | unordered_map<MetricDimensionKey, vector<DurationBucket>> buckets; |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 191 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 192 | int64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL; |
| 193 | int64_t bucketStartTimeNs = 10000000000; |
| 194 | int64_t bucketNum = 0; |
| 195 | int64_t eventStartTimeNs = bucketStartTimeNs + 1; |
| 196 | int64_t durationTimeNs = 2 * 1000; |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 197 | |
tsaichristine | 1449fa4 | 2020-01-02 12:12:05 -0800 | [diff] [blame] | 198 | OringDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, 1, false, |
| 199 | bucketStartTimeNs, bucketNum, bucketStartTimeNs, bucketSizeNs, |
| 200 | true, false, {}); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 201 | |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 202 | tracker.noteStart(kEventKey1, true, eventStartTimeNs, key1); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 203 | |
Yao Chen | 427d372 | 2018-03-22 15:21:52 -0700 | [diff] [blame] | 204 | tracker.onSlicedConditionMayChange(true, eventStartTimeNs + 5); |
Yao Chen | 09294ef | 2017-11-25 19:54:01 -0800 | [diff] [blame] | 205 | |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 206 | tracker.noteStop(kEventKey1, eventStartTimeNs + durationTimeNs, false); |
Yao Chen | 09294ef | 2017-11-25 19:54:01 -0800 | [diff] [blame] | 207 | |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 208 | tracker.flushIfNeeded(bucketStartTimeNs + bucketSizeNs + 1, &buckets); |
| 209 | EXPECT_TRUE(buckets.find(eventKey) != buckets.end()); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 210 | ASSERT_EQ(1u, buckets[eventKey].size()); |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 211 | EXPECT_EQ(5LL, buckets[eventKey][0].mDuration); |
Yao Chen | 09294ef | 2017-11-25 19:54:01 -0800 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | TEST(OringDurationTrackerTest, TestDurationConditionChange2) { |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 215 | const MetricDimensionKey eventKey = getMockedMetricDimensionKey(TagId, 0, "event"); |
| 216 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 217 | const HashableDimensionKey kEventKey1 = getMockedDimensionKey(TagId, 2, "maps"); |
| 218 | const HashableDimensionKey kEventKey2 = getMockedDimensionKey(TagId, 3, "maps"); |
Yao Chen | 09294ef | 2017-11-25 19:54:01 -0800 | [diff] [blame] | 219 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 220 | |
| 221 | ConditionKey key1; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 222 | key1[StringToId("APP_BACKGROUND")] = kConditionKey1; |
Yao Chen | 09294ef | 2017-11-25 19:54:01 -0800 | [diff] [blame] | 223 | |
tsaichristine | 7685337 | 2019-08-06 17:17:03 -0700 | [diff] [blame] | 224 | EXPECT_CALL(*wizard, query(_, key1, _)) |
Yao Chen | 09294ef | 2017-11-25 19:54:01 -0800 | [diff] [blame] | 225 | .Times(2) |
| 226 | .WillOnce(Return(ConditionState::kFalse)) |
| 227 | .WillOnce(Return(ConditionState::kTrue)); |
| 228 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 229 | unordered_map<MetricDimensionKey, vector<DurationBucket>> buckets; |
Yao Chen | 09294ef | 2017-11-25 19:54:01 -0800 | [diff] [blame] | 230 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 231 | int64_t bucketStartTimeNs = 10000000000; |
| 232 | int64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL; |
| 233 | int64_t bucketNum = 0; |
| 234 | int64_t eventStartTimeNs = bucketStartTimeNs + 1; |
| 235 | int64_t durationTimeNs = 2 * 1000; |
Yao Chen | 09294ef | 2017-11-25 19:54:01 -0800 | [diff] [blame] | 236 | |
tsaichristine | 1449fa4 | 2020-01-02 12:12:05 -0800 | [diff] [blame] | 237 | OringDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, 1, false, |
| 238 | bucketStartTimeNs, bucketNum, bucketStartTimeNs, bucketSizeNs, |
| 239 | true, false, {}); |
Yao Chen | 09294ef | 2017-11-25 19:54:01 -0800 | [diff] [blame] | 240 | |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 241 | tracker.noteStart(kEventKey1, true, eventStartTimeNs, key1); |
Yao Chen | 09294ef | 2017-11-25 19:54:01 -0800 | [diff] [blame] | 242 | // condition to false; record duration 5n |
Yao Chen | 427d372 | 2018-03-22 15:21:52 -0700 | [diff] [blame] | 243 | tracker.onSlicedConditionMayChange(true, eventStartTimeNs + 5); |
Yao Chen | 09294ef | 2017-11-25 19:54:01 -0800 | [diff] [blame] | 244 | // condition to true. |
Yao Chen | 427d372 | 2018-03-22 15:21:52 -0700 | [diff] [blame] | 245 | tracker.onSlicedConditionMayChange(true, eventStartTimeNs + 1000); |
Yao Chen | 09294ef | 2017-11-25 19:54:01 -0800 | [diff] [blame] | 246 | // 2nd duration: 1000ns |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 247 | tracker.noteStop(kEventKey1, eventStartTimeNs + durationTimeNs, false); |
Yao Chen | 09294ef | 2017-11-25 19:54:01 -0800 | [diff] [blame] | 248 | |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 249 | tracker.flushIfNeeded(bucketStartTimeNs + bucketSizeNs + 1, &buckets); |
| 250 | EXPECT_TRUE(buckets.find(eventKey) != buckets.end()); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 251 | ASSERT_EQ(1u, buckets[eventKey].size()); |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 252 | EXPECT_EQ(1005LL, buckets[eventKey][0].mDuration); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 253 | } |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 254 | |
| 255 | TEST(OringDurationTrackerTest, TestDurationConditionChangeNested) { |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 256 | const MetricDimensionKey eventKey = getMockedMetricDimensionKey(TagId, 0, "event"); |
| 257 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 258 | const HashableDimensionKey kEventKey1 = getMockedDimensionKey(TagId, 2, "maps"); |
| 259 | const HashableDimensionKey kEventKey2 = getMockedDimensionKey(TagId, 3, "maps"); |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 260 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 261 | |
| 262 | ConditionKey key1; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 263 | key1[StringToId("APP_BACKGROUND")] = kConditionKey1; |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 264 | |
tsaichristine | 7685337 | 2019-08-06 17:17:03 -0700 | [diff] [blame] | 265 | EXPECT_CALL(*wizard, query(_, key1, _)) // #4 |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 266 | .WillOnce(Return(ConditionState::kFalse)); |
| 267 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 268 | unordered_map<MetricDimensionKey, vector<DurationBucket>> buckets; |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 269 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 270 | int64_t bucketStartTimeNs = 10000000000; |
| 271 | int64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL; |
| 272 | int64_t bucketNum = 0; |
| 273 | int64_t eventStartTimeNs = bucketStartTimeNs + 1; |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 274 | |
tsaichristine | 1449fa4 | 2020-01-02 12:12:05 -0800 | [diff] [blame] | 275 | OringDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, 1, true, bucketStartTimeNs, |
| 276 | bucketNum, bucketStartTimeNs, bucketSizeNs, true, false, {}); |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 277 | |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 278 | tracker.noteStart(kEventKey1, true, eventStartTimeNs, key1); |
| 279 | tracker.noteStart(kEventKey1, true, eventStartTimeNs + 2, key1); |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 280 | |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 281 | tracker.noteStop(kEventKey1, eventStartTimeNs + 3, false); |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 282 | |
Yao Chen | 427d372 | 2018-03-22 15:21:52 -0700 | [diff] [blame] | 283 | tracker.onSlicedConditionMayChange(true, eventStartTimeNs + 15); |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 284 | |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 285 | tracker.noteStop(kEventKey1, eventStartTimeNs + 2003, false); |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 286 | |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 287 | tracker.flushIfNeeded(bucketStartTimeNs + bucketSizeNs + 1, &buckets); |
| 288 | EXPECT_TRUE(buckets.find(eventKey) != buckets.end()); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 289 | ASSERT_EQ(1u, buckets[eventKey].size()); |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 290 | EXPECT_EQ(15LL, buckets[eventKey][0].mDuration); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | TEST(OringDurationTrackerTest, TestPredictAnomalyTimestamp) { |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 294 | const MetricDimensionKey eventKey = getMockedMetricDimensionKey(TagId, 0, "event"); |
| 295 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 296 | const HashableDimensionKey kEventKey1 = getMockedDimensionKey(TagId, 2, "maps"); |
| 297 | const HashableDimensionKey kEventKey2 = getMockedDimensionKey(TagId, 3, "maps"); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 298 | Alert alert; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 299 | alert.set_id(101); |
| 300 | alert.set_metric_id(1); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 301 | alert.set_trigger_if_sum_gt(40 * NS_PER_SEC); |
Yangster-mac | a7fb12d | 2018-01-03 17:17:20 -0800 | [diff] [blame] | 302 | alert.set_num_buckets(2); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 303 | alert.set_refractory_period_secs(1); |
| 304 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 305 | unordered_map<MetricDimensionKey, vector<DurationBucket>> buckets; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 306 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
Yao Chen | d59a658 | 2018-01-08 11:17:11 -0800 | [diff] [blame] | 307 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 308 | int64_t bucketStartTimeNs = 10 * NS_PER_SEC; |
| 309 | int64_t bucketNum = 0; |
| 310 | int64_t eventStartTimeNs = bucketStartTimeNs + NS_PER_SEC + 1; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 311 | |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 312 | sp<AlarmMonitor> alarmMonitor; |
| 313 | sp<DurationAnomalyTracker> anomalyTracker = |
| 314 | new DurationAnomalyTracker(alert, kConfigKey, alarmMonitor); |
tsaichristine | 1449fa4 | 2020-01-02 12:12:05 -0800 | [diff] [blame] | 315 | OringDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, 1, true, bucketStartTimeNs, |
| 316 | bucketNum, bucketStartTimeNs, bucketSizeNs, true, false, |
| 317 | {anomalyTracker}); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 318 | |
| 319 | // Nothing in the past bucket. |
Yao Chen | d59a658 | 2018-01-08 11:17:11 -0800 | [diff] [blame] | 320 | tracker.noteStart(DEFAULT_DIMENSION_KEY, true, eventStartTimeNs, ConditionKey()); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 321 | EXPECT_EQ((long long)(alert.trigger_if_sum_gt() + eventStartTimeNs), |
| 322 | tracker.predictAnomalyTimestampNs(*anomalyTracker, eventStartTimeNs)); |
| 323 | |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 324 | tracker.noteStop(DEFAULT_DIMENSION_KEY, eventStartTimeNs + 3, false); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 325 | ASSERT_EQ(0u, buckets[eventKey].size()); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 326 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 327 | int64_t event1StartTimeNs = eventStartTimeNs + 10; |
Yao Chen | d59a658 | 2018-01-08 11:17:11 -0800 | [diff] [blame] | 328 | tracker.noteStart(kEventKey1, true, event1StartTimeNs, ConditionKey()); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 329 | // No past buckets. The anomaly will happen in bucket #0. |
| 330 | EXPECT_EQ((long long)(event1StartTimeNs + alert.trigger_if_sum_gt() - 3), |
| 331 | tracker.predictAnomalyTimestampNs(*anomalyTracker, event1StartTimeNs)); |
| 332 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 333 | int64_t event1StopTimeNs = eventStartTimeNs + bucketSizeNs + 10; |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 334 | tracker.flushIfNeeded(event1StopTimeNs, &buckets); |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 335 | tracker.noteStop(kEventKey1, event1StopTimeNs, false); |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 336 | |
| 337 | EXPECT_TRUE(buckets.find(eventKey) != buckets.end()); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 338 | ASSERT_EQ(1u, buckets[eventKey].size()); |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 339 | EXPECT_EQ(3LL + bucketStartTimeNs + bucketSizeNs - eventStartTimeNs - 10, |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 340 | buckets[eventKey][0].mDuration); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 341 | |
| 342 | const int64_t bucket0Duration = 3ULL + bucketStartTimeNs + bucketSizeNs - eventStartTimeNs - 10; |
| 343 | const int64_t bucket1Duration = eventStartTimeNs + 10 - bucketStartTimeNs; |
| 344 | |
| 345 | // One past buckets. The anomaly will happen in bucket #1. |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 346 | int64_t event2StartTimeNs = eventStartTimeNs + bucketSizeNs + 15; |
Yao Chen | d59a658 | 2018-01-08 11:17:11 -0800 | [diff] [blame] | 347 | tracker.noteStart(kEventKey1, true, event2StartTimeNs, ConditionKey()); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 348 | EXPECT_EQ((long long)(event2StartTimeNs + alert.trigger_if_sum_gt() - bucket0Duration - |
| 349 | bucket1Duration), |
| 350 | tracker.predictAnomalyTimestampNs(*anomalyTracker, event2StartTimeNs)); |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 351 | tracker.noteStop(kEventKey1, event2StartTimeNs + 1, false); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 352 | |
| 353 | // Only one past buckets is applicable. Bucket +0 should be trashed. The anomaly will happen in |
| 354 | // bucket #2. |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 355 | int64_t event3StartTimeNs = bucketStartTimeNs + 2 * bucketSizeNs - 9 * NS_PER_SEC; |
Yao Chen | d59a658 | 2018-01-08 11:17:11 -0800 | [diff] [blame] | 356 | tracker.noteStart(kEventKey1, true, event3StartTimeNs, ConditionKey()); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 357 | EXPECT_EQ((long long)(event3StartTimeNs + alert.trigger_if_sum_gt() - bucket1Duration - 1LL), |
| 358 | tracker.predictAnomalyTimestampNs(*anomalyTracker, event3StartTimeNs)); |
| 359 | } |
| 360 | |
Yangster-mac | be10ddf | 2018-03-13 15:39:51 -0700 | [diff] [blame] | 361 | TEST(OringDurationTrackerTest, TestPredictAnomalyTimestamp2) { |
Yangster-mac | be10ddf | 2018-03-13 15:39:51 -0700 | [diff] [blame] | 362 | Alert alert; |
| 363 | alert.set_id(101); |
| 364 | alert.set_metric_id(1); |
| 365 | alert.set_trigger_if_sum_gt(5 * NS_PER_SEC); |
| 366 | alert.set_num_buckets(1); |
| 367 | alert.set_refractory_period_secs(20); |
| 368 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 369 | int64_t bucketStartTimeNs = 10 * NS_PER_SEC; |
| 370 | int64_t bucketNum = 0; |
Yangster-mac | be10ddf | 2018-03-13 15:39:51 -0700 | [diff] [blame] | 371 | |
| 372 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 373 | sp<AlarmMonitor> alarmMonitor; |
| 374 | sp<DurationAnomalyTracker> anomalyTracker = |
| 375 | new DurationAnomalyTracker(alert, kConfigKey, alarmMonitor); |
| 376 | OringDurationTracker tracker(kConfigKey, metricId, DEFAULT_METRIC_DIMENSION_KEY, wizard, 1, |
tsaichristine | 7685337 | 2019-08-06 17:17:03 -0700 | [diff] [blame] | 377 | |
Yangster-mac | be10ddf | 2018-03-13 15:39:51 -0700 | [diff] [blame] | 378 | true, bucketStartTimeNs, bucketNum, bucketStartTimeNs, |
| 379 | bucketSizeNs, true, false, {anomalyTracker}); |
| 380 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 381 | int64_t eventStartTimeNs = bucketStartTimeNs + 9 * NS_PER_SEC; |
Yangster-mac | be10ddf | 2018-03-13 15:39:51 -0700 | [diff] [blame] | 382 | tracker.noteStart(DEFAULT_DIMENSION_KEY, true, eventStartTimeNs, ConditionKey()); |
| 383 | // Anomaly happens in the bucket #1. |
| 384 | EXPECT_EQ((long long)(bucketStartTimeNs + 14 * NS_PER_SEC), |
| 385 | tracker.predictAnomalyTimestampNs(*anomalyTracker, eventStartTimeNs)); |
| 386 | |
| 387 | tracker.noteStop(DEFAULT_DIMENSION_KEY, bucketStartTimeNs + 14 * NS_PER_SEC, false); |
| 388 | |
| 389 | EXPECT_EQ((long long)(bucketStartTimeNs + 34 * NS_PER_SEC) / NS_PER_SEC, |
| 390 | anomalyTracker->getRefractoryPeriodEndsSec(DEFAULT_METRIC_DIMENSION_KEY)); |
| 391 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 392 | int64_t event2StartTimeNs = bucketStartTimeNs + 22 * NS_PER_SEC; |
Yangster-mac | be10ddf | 2018-03-13 15:39:51 -0700 | [diff] [blame] | 393 | EXPECT_EQ((long long)(bucketStartTimeNs + 34 * NS_PER_SEC) / NS_PER_SEC, |
| 394 | anomalyTracker->getRefractoryPeriodEndsSec(DEFAULT_METRIC_DIMENSION_KEY)); |
| 395 | EXPECT_EQ((long long)(bucketStartTimeNs + 35 * NS_PER_SEC), |
| 396 | tracker.predictAnomalyTimestampNs(*anomalyTracker, event2StartTimeNs)); |
| 397 | } |
| 398 | |
| 399 | TEST(OringDurationTrackerTest, TestPredictAnomalyTimestamp3) { |
| 400 | // Test the cases where the refractory period is smaller than the bucket size, longer than |
| 401 | // the bucket size, and longer than 2x of the anomaly detection window. |
| 402 | for (int j = 0; j < 3; j++) { |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 403 | int64_t thresholdNs = j * bucketSizeNs + 5 * NS_PER_SEC; |
Yangster-mac | be10ddf | 2018-03-13 15:39:51 -0700 | [diff] [blame] | 404 | for (int i = 0; i <= 7; ++i) { |
tsaichristine | 7685337 | 2019-08-06 17:17:03 -0700 | [diff] [blame] | 405 | |
Yangster-mac | be10ddf | 2018-03-13 15:39:51 -0700 | [diff] [blame] | 406 | Alert alert; |
| 407 | alert.set_id(101); |
| 408 | alert.set_metric_id(1); |
| 409 | alert.set_trigger_if_sum_gt(thresholdNs); |
| 410 | alert.set_num_buckets(3); |
| 411 | alert.set_refractory_period_secs( |
| 412 | bucketSizeNs / NS_PER_SEC / 2 + i * bucketSizeNs / NS_PER_SEC); |
| 413 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 414 | int64_t bucketStartTimeNs = 10 * NS_PER_SEC; |
| 415 | int64_t bucketNum = 101; |
Yangster-mac | be10ddf | 2018-03-13 15:39:51 -0700 | [diff] [blame] | 416 | |
| 417 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 418 | sp<AlarmMonitor> alarmMonitor; |
| 419 | sp<DurationAnomalyTracker> anomalyTracker = |
| 420 | new DurationAnomalyTracker(alert, kConfigKey, alarmMonitor); |
tsaichristine | 1449fa4 | 2020-01-02 12:12:05 -0800 | [diff] [blame] | 421 | OringDurationTracker tracker(kConfigKey, metricId, DEFAULT_METRIC_DIMENSION_KEY, wizard, |
| 422 | 1, true, bucketStartTimeNs, bucketNum, bucketStartTimeNs, |
Yangster-mac | be10ddf | 2018-03-13 15:39:51 -0700 | [diff] [blame] | 423 | bucketSizeNs, true, false, {anomalyTracker}); |
| 424 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 425 | int64_t eventStartTimeNs = bucketStartTimeNs + 9 * NS_PER_SEC; |
Yangster-mac | be10ddf | 2018-03-13 15:39:51 -0700 | [diff] [blame] | 426 | tracker.noteStart(DEFAULT_DIMENSION_KEY, true, eventStartTimeNs, ConditionKey()); |
| 427 | EXPECT_EQ((long long)(eventStartTimeNs + thresholdNs), |
| 428 | tracker.predictAnomalyTimestampNs(*anomalyTracker, eventStartTimeNs)); |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 429 | int64_t eventStopTimeNs = eventStartTimeNs + thresholdNs + NS_PER_SEC; |
Yangster-mac | be10ddf | 2018-03-13 15:39:51 -0700 | [diff] [blame] | 430 | tracker.noteStop(DEFAULT_DIMENSION_KEY, eventStopTimeNs, false); |
| 431 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 432 | int64_t refractoryPeriodEndSec = |
Yangster-mac | be10ddf | 2018-03-13 15:39:51 -0700 | [diff] [blame] | 433 | anomalyTracker->getRefractoryPeriodEndsSec(DEFAULT_METRIC_DIMENSION_KEY); |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 434 | EXPECT_EQ(eventStopTimeNs / (int64_t)NS_PER_SEC + alert.refractory_period_secs(), |
Yangster-mac | be10ddf | 2018-03-13 15:39:51 -0700 | [diff] [blame] | 435 | refractoryPeriodEndSec); |
| 436 | |
| 437 | // Acquire and release a wakelock in the next bucket. |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 438 | int64_t event2StartTimeNs = eventStopTimeNs + bucketSizeNs; |
Yangster-mac | be10ddf | 2018-03-13 15:39:51 -0700 | [diff] [blame] | 439 | tracker.noteStart(DEFAULT_DIMENSION_KEY, true, event2StartTimeNs, ConditionKey()); |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 440 | int64_t event2StopTimeNs = event2StartTimeNs + 4 * NS_PER_SEC; |
Yangster-mac | be10ddf | 2018-03-13 15:39:51 -0700 | [diff] [blame] | 441 | tracker.noteStop(DEFAULT_DIMENSION_KEY, event2StopTimeNs, false); |
| 442 | |
| 443 | // Test the alarm prediction works well when seeing another wakelock start event. |
| 444 | for (int k = 0; k <= 2; ++k) { |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 445 | int64_t event3StartTimeNs = event2StopTimeNs + NS_PER_SEC + k * bucketSizeNs; |
| 446 | int64_t alarmTimestampNs = |
Yangster-mac | be10ddf | 2018-03-13 15:39:51 -0700 | [diff] [blame] | 447 | tracker.predictAnomalyTimestampNs(*anomalyTracker, event3StartTimeNs); |
| 448 | EXPECT_GT(alarmTimestampNs, 0u); |
| 449 | EXPECT_GE(alarmTimestampNs, event3StartTimeNs); |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 450 | EXPECT_GE(alarmTimestampNs, refractoryPeriodEndSec *(int64_t) NS_PER_SEC); |
Yangster-mac | be10ddf | 2018-03-13 15:39:51 -0700 | [diff] [blame] | 451 | } |
| 452 | } |
| 453 | } |
| 454 | } |
| 455 | |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 456 | TEST(OringDurationTrackerTest, TestAnomalyDetectionExpiredAlarm) { |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 457 | const MetricDimensionKey eventKey = getMockedMetricDimensionKey(TagId, 0, "event"); |
| 458 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 459 | const HashableDimensionKey kEventKey1 = getMockedDimensionKey(TagId, 2, "maps"); |
| 460 | const HashableDimensionKey kEventKey2 = getMockedDimensionKey(TagId, 3, "maps"); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 461 | Alert alert; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 462 | alert.set_id(101); |
| 463 | alert.set_metric_id(1); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 464 | alert.set_trigger_if_sum_gt(40 * NS_PER_SEC); |
Yangster-mac | a7fb12d | 2018-01-03 17:17:20 -0800 | [diff] [blame] | 465 | alert.set_num_buckets(2); |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 466 | const int32_t refPeriodSec = 45; |
| 467 | alert.set_refractory_period_secs(refPeriodSec); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 468 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 469 | unordered_map<MetricDimensionKey, vector<DurationBucket>> buckets; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 470 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
Yao Chen | d59a658 | 2018-01-08 11:17:11 -0800 | [diff] [blame] | 471 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 472 | int64_t bucketStartTimeNs = 10 * NS_PER_SEC; |
| 473 | int64_t bucketNum = 0; |
| 474 | int64_t eventStartTimeNs = bucketStartTimeNs + NS_PER_SEC + 1; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 475 | |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 476 | sp<AlarmMonitor> alarmMonitor; |
| 477 | sp<DurationAnomalyTracker> anomalyTracker = |
| 478 | new DurationAnomalyTracker(alert, kConfigKey, alarmMonitor); |
tsaichristine | 1449fa4 | 2020-01-02 12:12:05 -0800 | [diff] [blame] | 479 | OringDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, 1, true /*nesting*/, |
| 480 | bucketStartTimeNs, bucketNum, bucketStartTimeNs, bucketSizeNs, |
| 481 | false, false, {anomalyTracker}); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 482 | |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 483 | tracker.noteStart(kEventKey1, true, eventStartTimeNs, ConditionKey()); |
| 484 | tracker.noteStop(kEventKey1, eventStartTimeNs + 10, false); |
| 485 | EXPECT_EQ(anomalyTracker->getRefractoryPeriodEndsSec(eventKey), 0U); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 486 | EXPECT_TRUE(tracker.mStarted.empty()); |
tsaichristine | 1449fa4 | 2020-01-02 12:12:05 -0800 | [diff] [blame] | 487 | EXPECT_EQ(10LL, tracker.mStateKeyDurationMap[DEFAULT_DIMENSION_KEY].mDuration); // 10ns |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 488 | |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 489 | ASSERT_EQ(0u, tracker.mStarted.size()); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 490 | |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 491 | tracker.noteStart(kEventKey1, true, eventStartTimeNs + 20, ConditionKey()); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 492 | ASSERT_EQ(1u, anomalyTracker->mAlarms.size()); |
Bookatz | 14e0d85 | 2018-02-13 15:55:14 -0800 | [diff] [blame] | 493 | EXPECT_EQ((long long)(52ULL * NS_PER_SEC), // (10s + 1s + 1ns + 20ns) - 10ns + 40s, rounded up |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 494 | (long long)(anomalyTracker->mAlarms.begin()->second->timestampSec * NS_PER_SEC)); |
Bookatz | 14e0d85 | 2018-02-13 15:55:14 -0800 | [diff] [blame] | 495 | // The alarm is set to fire at 52s, and when it does, an anomaly would be declared. However, |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 496 | // because this is a unit test, the alarm won't actually fire at all. Since the alarm fails |
| 497 | // to fire in time, the anomaly is instead caught when noteStop is called, at around 71s. |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 498 | tracker.flushIfNeeded(eventStartTimeNs + 2 * bucketSizeNs + 25, &buckets); |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 499 | tracker.noteStop(kEventKey1, eventStartTimeNs + 2 * bucketSizeNs + 25, false); |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 500 | EXPECT_EQ(anomalyTracker->getSumOverPastBuckets(eventKey), (long long)(bucketSizeNs)); |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 501 | EXPECT_EQ(anomalyTracker->getRefractoryPeriodEndsSec(eventKey), |
Bookatz | 6bf9825 | 2018-03-14 10:44:24 -0700 | [diff] [blame] | 502 | std::ceil((eventStartTimeNs + 2 * bucketSizeNs + 25.0) / NS_PER_SEC + refPeriodSec)); |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | TEST(OringDurationTrackerTest, TestAnomalyDetectionFiredAlarm) { |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 506 | const MetricDimensionKey eventKey = getMockedMetricDimensionKey(TagId, 0, "event"); |
| 507 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 508 | const HashableDimensionKey kEventKey1 = getMockedDimensionKey(TagId, 2, "maps"); |
| 509 | const HashableDimensionKey kEventKey2 = getMockedDimensionKey(TagId, 3, "maps"); |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 510 | Alert alert; |
| 511 | alert.set_id(101); |
| 512 | alert.set_metric_id(1); |
| 513 | alert.set_trigger_if_sum_gt(40 * NS_PER_SEC); |
| 514 | alert.set_num_buckets(2); |
| 515 | const int32_t refPeriodSec = 45; |
| 516 | alert.set_refractory_period_secs(refPeriodSec); |
| 517 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 518 | unordered_map<MetricDimensionKey, vector<DurationBucket>> buckets; |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 519 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 520 | ConditionKey conkey; |
| 521 | conkey[StringToId("APP_BACKGROUND")] = kConditionKey1; |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 522 | int64_t bucketStartTimeNs = 10 * NS_PER_SEC; |
| 523 | int64_t bucketSizeNs = 30 * NS_PER_SEC; |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 524 | |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 525 | sp<AlarmMonitor> alarmMonitor; |
| 526 | sp<DurationAnomalyTracker> anomalyTracker = |
| 527 | new DurationAnomalyTracker(alert, kConfigKey, alarmMonitor); |
tsaichristine | 1449fa4 | 2020-01-02 12:12:05 -0800 | [diff] [blame] | 528 | OringDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, 1, true /*nesting*/, |
| 529 | bucketStartTimeNs, 0, bucketStartTimeNs, bucketSizeNs, false, |
| 530 | false, {anomalyTracker}); |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 531 | |
tsaichristine | 1449fa4 | 2020-01-02 12:12:05 -0800 | [diff] [blame] | 532 | tracker.noteStart(kEventKey1, true, 15 * NS_PER_SEC, conkey); // start key1 |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 533 | ASSERT_EQ(1u, anomalyTracker->mAlarms.size()); |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 534 | sp<const InternalAlarm> alarm = anomalyTracker->mAlarms.begin()->second; |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 535 | EXPECT_EQ((long long)(55ULL * NS_PER_SEC), (long long)(alarm->timestampSec * NS_PER_SEC)); |
| 536 | EXPECT_EQ(anomalyTracker->getRefractoryPeriodEndsSec(eventKey), 0U); |
| 537 | |
| 538 | tracker.noteStop(kEventKey1, 17 * NS_PER_SEC, false); // stop key1 (2 seconds later) |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 539 | ASSERT_EQ(0u, anomalyTracker->mAlarms.size()); |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 540 | EXPECT_EQ(anomalyTracker->getRefractoryPeriodEndsSec(eventKey), 0U); |
| 541 | |
tsaichristine | 1449fa4 | 2020-01-02 12:12:05 -0800 | [diff] [blame] | 542 | tracker.noteStart(kEventKey1, true, 22 * NS_PER_SEC, conkey); // start key1 again |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 543 | ASSERT_EQ(1u, anomalyTracker->mAlarms.size()); |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 544 | alarm = anomalyTracker->mAlarms.begin()->second; |
| 545 | EXPECT_EQ((long long)(60ULL * NS_PER_SEC), (long long)(alarm->timestampSec * NS_PER_SEC)); |
| 546 | EXPECT_EQ(anomalyTracker->getRefractoryPeriodEndsSec(eventKey), 0U); |
| 547 | |
tsaichristine | 1449fa4 | 2020-01-02 12:12:05 -0800 | [diff] [blame] | 548 | tracker.noteStart(kEventKey2, true, 32 * NS_PER_SEC, conkey); // start key2 |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 549 | ASSERT_EQ(1u, anomalyTracker->mAlarms.size()); |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 550 | alarm = anomalyTracker->mAlarms.begin()->second; |
| 551 | EXPECT_EQ((long long)(60ULL * NS_PER_SEC), (long long)(alarm->timestampSec * NS_PER_SEC)); |
| 552 | EXPECT_EQ(anomalyTracker->getRefractoryPeriodEndsSec(eventKey), 0U); |
| 553 | |
| 554 | tracker.noteStop(kEventKey1, 47 * NS_PER_SEC, false); // stop key1 |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 555 | ASSERT_EQ(1u, anomalyTracker->mAlarms.size()); |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 556 | alarm = anomalyTracker->mAlarms.begin()->second; |
| 557 | EXPECT_EQ((long long)(60ULL * NS_PER_SEC), (long long)(alarm->timestampSec * NS_PER_SEC)); |
| 558 | EXPECT_EQ(anomalyTracker->getRefractoryPeriodEndsSec(eventKey), 0U); |
| 559 | |
| 560 | // Now, at 60s, which is 38s after key1 started again, we have reached 40s of 'on' time. |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 561 | std::unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> firedAlarms({alarm}); |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 562 | anomalyTracker->informAlarmsFired(62 * NS_PER_SEC, firedAlarms); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 563 | ASSERT_EQ(0u, anomalyTracker->mAlarms.size()); |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 564 | EXPECT_EQ(anomalyTracker->getRefractoryPeriodEndsSec(eventKey), 62U + refPeriodSec); |
| 565 | |
| 566 | tracker.noteStop(kEventKey2, 69 * NS_PER_SEC, false); // stop key2 |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 567 | ASSERT_EQ(0u, anomalyTracker->mAlarms.size()); |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 568 | EXPECT_EQ(anomalyTracker->getRefractoryPeriodEndsSec(eventKey), 62U + refPeriodSec); |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 569 | } |
| 570 | |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 571 | } // namespace statsd |
| 572 | } // namespace os |
| 573 | } // namespace android |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 574 | #else |
| 575 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
tsaichristine | 7685337 | 2019-08-06 17:17:03 -0700 | [diff] [blame] | 576 | #endif |