Yang Lu | 3eba621 | 2017-10-25 19:54:45 -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 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 15 | #include "src/anomaly/AnomalyTracker.h" |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 16 | #include "../metrics/metrics_test_helper.h" |
Yang Lu | 3eba621 | 2017-10-25 19:54:45 -0700 | [diff] [blame] | 17 | |
| 18 | #include <gtest/gtest.h> |
| 19 | #include <stdio.h> |
| 20 | #include <vector> |
| 21 | |
| 22 | using namespace testing; |
| 23 | using android::sp; |
| 24 | using std::set; |
| 25 | using std::unordered_map; |
| 26 | using std::vector; |
| 27 | |
| 28 | #ifdef __ANDROID__ |
| 29 | |
| 30 | namespace android { |
| 31 | namespace os { |
| 32 | namespace statsd { |
| 33 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 34 | const ConfigKey kConfigKey(0, 12345); |
Bookatz | 8f2f3d8 | 2017-12-07 13:53:21 -0800 | [diff] [blame] | 35 | |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 36 | HashableDimensionKey getMockDimensionKey(int key, string value) { |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 37 | DimensionsValue dimensionsValue; |
| 38 | dimensionsValue.set_field(key); |
| 39 | dimensionsValue.set_value_str(value); |
| 40 | return HashableDimensionKey(dimensionsValue); |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | void AddValueToBucket(const std::vector<std::pair<HashableDimensionKey, long>>& key_value_pair_list, |
Yang Lu | 3eba621 | 2017-10-25 19:54:45 -0700 | [diff] [blame] | 44 | std::shared_ptr<DimToValMap> bucket) { |
| 45 | for (auto itr = key_value_pair_list.begin(); itr != key_value_pair_list.end(); itr++) { |
| 46 | (*bucket)[itr->first] += itr->second; |
| 47 | } |
| 48 | } |
| 49 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 50 | std::shared_ptr<DimToValMap> MockBucket( |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 51 | const std::vector<std::pair<HashableDimensionKey, long>>& key_value_pair_list) { |
Yang Lu | 3eba621 | 2017-10-25 19:54:45 -0700 | [diff] [blame] | 52 | std::shared_ptr<DimToValMap> bucket = std::make_shared<DimToValMap>(); |
| 53 | AddValueToBucket(key_value_pair_list, bucket); |
| 54 | return bucket; |
| 55 | } |
| 56 | |
| 57 | TEST(AnomalyTrackerTest, TestConsecutiveBuckets) { |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 58 | const int64_t bucketSizeNs = 30 * NS_PER_SEC; |
Yang Lu | 3eba621 | 2017-10-25 19:54:45 -0700 | [diff] [blame] | 59 | Alert alert; |
Yangster-mac | a7fb12d | 2018-01-03 17:17:20 -0800 | [diff] [blame] | 60 | alert.set_num_buckets(3); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 61 | alert.set_refractory_period_secs(2 * bucketSizeNs / NS_PER_SEC); |
Yang Lu | 3eba621 | 2017-10-25 19:54:45 -0700 | [diff] [blame] | 62 | alert.set_trigger_if_sum_gt(2); |
| 63 | |
Bookatz | 8f2f3d8 | 2017-12-07 13:53:21 -0800 | [diff] [blame] | 64 | AnomalyTracker anomalyTracker(alert, kConfigKey); |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 65 | HashableDimensionKey keyA = getMockDimensionKey(1, "a"); |
| 66 | HashableDimensionKey keyB = getMockDimensionKey(1, "b"); |
| 67 | HashableDimensionKey keyC = getMockDimensionKey(1, "c"); |
Yang Lu | 3eba621 | 2017-10-25 19:54:45 -0700 | [diff] [blame] | 68 | |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 69 | std::shared_ptr<DimToValMap> bucket0 = MockBucket({{keyA, 1}, {keyB, 2}, {keyC, 1}}); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 70 | int64_t eventTimestamp0 = 10; |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 71 | std::shared_ptr<DimToValMap> bucket1 = MockBucket({{keyA, 1}}); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 72 | int64_t eventTimestamp1 = bucketSizeNs + 11; |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 73 | std::shared_ptr<DimToValMap> bucket2 = MockBucket({{keyB, 1}}); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 74 | int64_t eventTimestamp2 = 2 * bucketSizeNs + 12; |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 75 | std::shared_ptr<DimToValMap> bucket3 = MockBucket({{keyA, 2}}); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 76 | int64_t eventTimestamp3 = 3 * bucketSizeNs + 13; |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 77 | std::shared_ptr<DimToValMap> bucket4 = MockBucket({{keyB, 1}}); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 78 | int64_t eventTimestamp4 = 4 * bucketSizeNs + 14; |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 79 | std::shared_ptr<DimToValMap> bucket5 = MockBucket({{keyA, 2}}); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 80 | int64_t eventTimestamp5 = 5 * bucketSizeNs + 15; |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 81 | std::shared_ptr<DimToValMap> bucket6 = MockBucket({{keyA, 2}}); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 82 | int64_t eventTimestamp6 = 6 * bucketSizeNs + 16; |
Yang Lu | 3eba621 | 2017-10-25 19:54:45 -0700 | [diff] [blame] | 83 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 84 | EXPECT_EQ(anomalyTracker.mSumOverPastBuckets.size(), 0u); |
| 85 | EXPECT_EQ(anomalyTracker.mMostRecentBucketNum, -1LL); |
| 86 | EXPECT_FALSE(anomalyTracker.detectAnomaly(0, *bucket0)); |
| 87 | anomalyTracker.detectAndDeclareAnomaly(eventTimestamp0, 0, *bucket0); |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 88 | EXPECT_EQ(anomalyTracker.mLastAnomalyTimestampNs, -1L); |
Yang Lu | 3eba621 | 2017-10-25 19:54:45 -0700 | [diff] [blame] | 89 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 90 | // Adds past bucket #0 |
| 91 | anomalyTracker.addPastBucket(bucket0, 0); |
| 92 | EXPECT_EQ(anomalyTracker.mSumOverPastBuckets.size(), 3u); |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 93 | EXPECT_EQ(anomalyTracker.getSumOverPastBuckets(keyA), 1LL); |
| 94 | EXPECT_EQ(anomalyTracker.getSumOverPastBuckets(keyB), 2LL); |
| 95 | EXPECT_EQ(anomalyTracker.getSumOverPastBuckets(keyC), 1LL); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 96 | EXPECT_EQ(anomalyTracker.mMostRecentBucketNum, 0LL); |
| 97 | EXPECT_FALSE(anomalyTracker.detectAnomaly(1, *bucket1)); |
| 98 | anomalyTracker.detectAndDeclareAnomaly(eventTimestamp1, 1, *bucket1); |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 99 | EXPECT_EQ(anomalyTracker.mLastAnomalyTimestampNs, -1L); |
Yang Lu | 3eba621 | 2017-10-25 19:54:45 -0700 | [diff] [blame] | 100 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 101 | // Adds past bucket #0 again. The sum does not change. |
| 102 | anomalyTracker.addPastBucket(bucket0, 0); |
| 103 | EXPECT_EQ(anomalyTracker.mSumOverPastBuckets.size(), 3u); |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 104 | EXPECT_EQ(anomalyTracker.getSumOverPastBuckets(keyA), 1LL); |
| 105 | EXPECT_EQ(anomalyTracker.getSumOverPastBuckets(keyB), 2LL); |
| 106 | EXPECT_EQ(anomalyTracker.getSumOverPastBuckets(keyC), 1LL); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 107 | EXPECT_EQ(anomalyTracker.mMostRecentBucketNum, 0LL); |
| 108 | EXPECT_FALSE(anomalyTracker.detectAnomaly(1, *bucket1)); |
| 109 | anomalyTracker.detectAndDeclareAnomaly(eventTimestamp1 + 1, 1, *bucket1); |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 110 | EXPECT_EQ(anomalyTracker.mLastAnomalyTimestampNs, -1L); |
Yang Lu | 3eba621 | 2017-10-25 19:54:45 -0700 | [diff] [blame] | 111 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 112 | // Adds past bucket #1. |
| 113 | anomalyTracker.addPastBucket(bucket1, 1); |
| 114 | EXPECT_EQ(anomalyTracker.mMostRecentBucketNum, 1L); |
| 115 | EXPECT_EQ(anomalyTracker.mSumOverPastBuckets.size(), 3UL); |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 116 | EXPECT_EQ(anomalyTracker.getSumOverPastBuckets(keyA), 2LL); |
| 117 | EXPECT_EQ(anomalyTracker.getSumOverPastBuckets(keyB), 2LL); |
| 118 | EXPECT_EQ(anomalyTracker.getSumOverPastBuckets(keyC), 1LL); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 119 | EXPECT_TRUE(anomalyTracker.detectAnomaly(2, *bucket2)); |
| 120 | anomalyTracker.detectAndDeclareAnomaly(eventTimestamp2, 2, *bucket2); |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 121 | EXPECT_EQ(anomalyTracker.mLastAnomalyTimestampNs, eventTimestamp2); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 122 | |
| 123 | // Adds past bucket #1 again. Nothing changes. |
| 124 | anomalyTracker.addPastBucket(bucket1, 1); |
| 125 | EXPECT_EQ(anomalyTracker.mMostRecentBucketNum, 1L); |
| 126 | EXPECT_EQ(anomalyTracker.mSumOverPastBuckets.size(), 3UL); |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 127 | EXPECT_EQ(anomalyTracker.getSumOverPastBuckets(keyA), 2LL); |
| 128 | EXPECT_EQ(anomalyTracker.getSumOverPastBuckets(keyB), 2LL); |
| 129 | EXPECT_EQ(anomalyTracker.getSumOverPastBuckets(keyC), 1LL); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 130 | EXPECT_TRUE(anomalyTracker.detectAnomaly(2, *bucket2)); |
| 131 | anomalyTracker.detectAndDeclareAnomaly(eventTimestamp2 + 1, 2, *bucket2); |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 132 | EXPECT_EQ(anomalyTracker.mLastAnomalyTimestampNs, eventTimestamp2); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 133 | |
| 134 | // Adds past bucket #2. |
| 135 | anomalyTracker.addPastBucket(bucket2, 2); |
| 136 | EXPECT_EQ(anomalyTracker.mMostRecentBucketNum, 2L); |
| 137 | EXPECT_EQ(anomalyTracker.mSumOverPastBuckets.size(), 2UL); |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 138 | EXPECT_EQ(anomalyTracker.getSumOverPastBuckets(keyA), 1LL); |
| 139 | EXPECT_EQ(anomalyTracker.getSumOverPastBuckets(keyB), 1LL); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 140 | EXPECT_TRUE(anomalyTracker.detectAnomaly(3, *bucket3)); |
| 141 | anomalyTracker.detectAndDeclareAnomaly(eventTimestamp3, 3, *bucket3); |
Yang Lu | 3eba621 | 2017-10-25 19:54:45 -0700 | [diff] [blame] | 142 | // Within refractory period. |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 143 | EXPECT_EQ(anomalyTracker.mLastAnomalyTimestampNs, eventTimestamp2); |
Yang Lu | 3eba621 | 2017-10-25 19:54:45 -0700 | [diff] [blame] | 144 | |
| 145 | // Adds bucket #3. |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 146 | anomalyTracker.addPastBucket(bucket3, 3L); |
| 147 | EXPECT_EQ(anomalyTracker.mMostRecentBucketNum, 3L); |
| 148 | EXPECT_EQ(anomalyTracker.mSumOverPastBuckets.size(), 2UL); |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 149 | EXPECT_EQ(anomalyTracker.getSumOverPastBuckets(keyA), 2LL); |
| 150 | EXPECT_EQ(anomalyTracker.getSumOverPastBuckets(keyB), 1LL); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 151 | EXPECT_FALSE(anomalyTracker.detectAnomaly(4, *bucket4)); |
| 152 | anomalyTracker.detectAndDeclareAnomaly(eventTimestamp4, 4, *bucket4); |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 153 | EXPECT_EQ(anomalyTracker.mLastAnomalyTimestampNs, eventTimestamp2); |
Yang Lu | 3eba621 | 2017-10-25 19:54:45 -0700 | [diff] [blame] | 154 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 155 | // Adds bucket #4. |
| 156 | anomalyTracker.addPastBucket(bucket4, 4); |
| 157 | EXPECT_EQ(anomalyTracker.mMostRecentBucketNum, 4L); |
| 158 | EXPECT_EQ(anomalyTracker.mSumOverPastBuckets.size(), 2UL); |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 159 | EXPECT_EQ(anomalyTracker.getSumOverPastBuckets(keyA), 2LL); |
| 160 | EXPECT_EQ(anomalyTracker.getSumOverPastBuckets(keyB), 1LL); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 161 | EXPECT_TRUE(anomalyTracker.detectAnomaly(5, *bucket5)); |
| 162 | anomalyTracker.detectAndDeclareAnomaly(eventTimestamp5, 5, *bucket5); |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 163 | EXPECT_EQ(anomalyTracker.mLastAnomalyTimestampNs, eventTimestamp5); |
Yang Lu | 3eba621 | 2017-10-25 19:54:45 -0700 | [diff] [blame] | 164 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 165 | // Adds bucket #5. |
| 166 | anomalyTracker.addPastBucket(bucket5, 5); |
| 167 | EXPECT_EQ(anomalyTracker.mMostRecentBucketNum, 5L); |
| 168 | EXPECT_EQ(anomalyTracker.mSumOverPastBuckets.size(), 2UL); |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 169 | EXPECT_EQ(anomalyTracker.getSumOverPastBuckets(keyA), 2LL); |
| 170 | EXPECT_EQ(anomalyTracker.getSumOverPastBuckets(keyB), 1LL); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 171 | EXPECT_TRUE(anomalyTracker.detectAnomaly(6, *bucket6)); |
Yang Lu | 3eba621 | 2017-10-25 19:54:45 -0700 | [diff] [blame] | 172 | // Within refractory period. |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 173 | anomalyTracker.detectAndDeclareAnomaly(eventTimestamp6, 6, *bucket6); |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 174 | EXPECT_EQ(anomalyTracker.mLastAnomalyTimestampNs, eventTimestamp5); |
Yang Lu | 3eba621 | 2017-10-25 19:54:45 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | TEST(AnomalyTrackerTest, TestSparseBuckets) { |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 178 | const int64_t bucketSizeNs = 30 * NS_PER_SEC; |
Yang Lu | 3eba621 | 2017-10-25 19:54:45 -0700 | [diff] [blame] | 179 | Alert alert; |
Yangster-mac | a7fb12d | 2018-01-03 17:17:20 -0800 | [diff] [blame] | 180 | alert.set_num_buckets(3); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 181 | alert.set_refractory_period_secs(2 * bucketSizeNs / NS_PER_SEC); |
Yang Lu | 3eba621 | 2017-10-25 19:54:45 -0700 | [diff] [blame] | 182 | alert.set_trigger_if_sum_gt(2); |
| 183 | |
Bookatz | 8f2f3d8 | 2017-12-07 13:53:21 -0800 | [diff] [blame] | 184 | AnomalyTracker anomalyTracker(alert, kConfigKey); |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 185 | HashableDimensionKey keyA = getMockDimensionKey(1, "a"); |
| 186 | HashableDimensionKey keyB = getMockDimensionKey(1, "b"); |
| 187 | HashableDimensionKey keyC = getMockDimensionKey(1, "c"); |
| 188 | HashableDimensionKey keyD = getMockDimensionKey(1, "d"); |
| 189 | HashableDimensionKey keyE = getMockDimensionKey(1, "e"); |
Yang Lu | 3eba621 | 2017-10-25 19:54:45 -0700 | [diff] [blame] | 190 | |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 191 | std::shared_ptr<DimToValMap> bucket9 = MockBucket({{keyA, 1}, {keyB, 2}, {keyC, 1}}); |
| 192 | std::shared_ptr<DimToValMap> bucket16 = MockBucket({{keyB, 4}}); |
| 193 | std::shared_ptr<DimToValMap> bucket18 = MockBucket({{keyB, 1}, {keyC, 1}}); |
| 194 | std::shared_ptr<DimToValMap> bucket20 = MockBucket({{keyB, 3}, {keyC, 1}}); |
| 195 | std::shared_ptr<DimToValMap> bucket25 = MockBucket({{keyD, 1}}); |
| 196 | std::shared_ptr<DimToValMap> bucket28 = MockBucket({{keyE, 2}}); |
Yang Lu | 3eba621 | 2017-10-25 19:54:45 -0700 | [diff] [blame] | 197 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 198 | int64_t eventTimestamp1 = bucketSizeNs * 8 + 1; |
| 199 | int64_t eventTimestamp2 = bucketSizeNs * 15 + 11; |
| 200 | int64_t eventTimestamp3 = bucketSizeNs * 17 + 1; |
| 201 | int64_t eventTimestamp4 = bucketSizeNs * 19 + 2; |
| 202 | int64_t eventTimestamp5 = bucketSizeNs * 24 + 3; |
| 203 | int64_t eventTimestamp6 = bucketSizeNs * 27 + 3; |
Yang Lu | 3eba621 | 2017-10-25 19:54:45 -0700 | [diff] [blame] | 204 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 205 | EXPECT_EQ(anomalyTracker.mMostRecentBucketNum, -1LL); |
| 206 | EXPECT_EQ(anomalyTracker.mSumOverPastBuckets.size(), 0UL); |
| 207 | EXPECT_FALSE(anomalyTracker.detectAnomaly(9, *bucket9)); |
| 208 | anomalyTracker.detectAndDeclareAnomaly(eventTimestamp1, 9, *bucket9); |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 209 | EXPECT_EQ(anomalyTracker.mLastAnomalyTimestampNs, -1); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 210 | |
| 211 | // Add past bucket #9 |
| 212 | anomalyTracker.addPastBucket(bucket9, 9); |
| 213 | EXPECT_EQ(anomalyTracker.mMostRecentBucketNum, 9L); |
| 214 | EXPECT_EQ(anomalyTracker.mSumOverPastBuckets.size(), 3UL); |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 215 | EXPECT_EQ(anomalyTracker.getSumOverPastBuckets(keyA), 1LL); |
| 216 | EXPECT_EQ(anomalyTracker.getSumOverPastBuckets(keyB), 2LL); |
| 217 | EXPECT_EQ(anomalyTracker.getSumOverPastBuckets(keyC), 1LL); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 218 | EXPECT_TRUE(anomalyTracker.detectAnomaly(16, *bucket16)); |
| 219 | EXPECT_EQ(anomalyTracker.mSumOverPastBuckets.size(), 0UL); |
| 220 | EXPECT_EQ(anomalyTracker.mMostRecentBucketNum, 15L); |
| 221 | anomalyTracker.detectAndDeclareAnomaly(eventTimestamp2, 16, *bucket16); |
| 222 | EXPECT_EQ(anomalyTracker.mSumOverPastBuckets.size(), 0UL); |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 223 | EXPECT_EQ(anomalyTracker.mLastAnomalyTimestampNs, eventTimestamp2); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 224 | EXPECT_EQ(anomalyTracker.mMostRecentBucketNum, 15L); |
| 225 | |
| 226 | // Add past bucket #16 |
| 227 | anomalyTracker.addPastBucket(bucket16, 16); |
| 228 | EXPECT_EQ(anomalyTracker.mMostRecentBucketNum, 16L); |
| 229 | EXPECT_EQ(anomalyTracker.mSumOverPastBuckets.size(), 1UL); |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 230 | EXPECT_EQ(anomalyTracker.getSumOverPastBuckets(keyB), 4LL); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 231 | EXPECT_TRUE(anomalyTracker.detectAnomaly(18, *bucket18)); |
| 232 | EXPECT_EQ(anomalyTracker.mSumOverPastBuckets.size(), 1UL); |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 233 | EXPECT_EQ(anomalyTracker.getSumOverPastBuckets(keyB), 4LL); |
Yang Lu | 3eba621 | 2017-10-25 19:54:45 -0700 | [diff] [blame] | 234 | // Within refractory period. |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 235 | anomalyTracker.detectAndDeclareAnomaly(eventTimestamp3, 18, *bucket18); |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 236 | EXPECT_EQ(anomalyTracker.mLastAnomalyTimestampNs, eventTimestamp2); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 237 | EXPECT_EQ(anomalyTracker.mSumOverPastBuckets.size(), 1UL); |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 238 | EXPECT_EQ(anomalyTracker.getSumOverPastBuckets(keyB), 4LL); |
Yang Lu | 3eba621 | 2017-10-25 19:54:45 -0700 | [diff] [blame] | 239 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 240 | // Add past bucket #18 |
| 241 | anomalyTracker.addPastBucket(bucket18, 18); |
| 242 | EXPECT_EQ(anomalyTracker.mMostRecentBucketNum, 18L); |
| 243 | EXPECT_EQ(anomalyTracker.mSumOverPastBuckets.size(), 2UL); |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 244 | EXPECT_EQ(anomalyTracker.getSumOverPastBuckets(keyB), 1LL); |
| 245 | EXPECT_EQ(anomalyTracker.getSumOverPastBuckets(keyC), 1LL); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 246 | EXPECT_TRUE(anomalyTracker.detectAnomaly(20, *bucket20)); |
| 247 | EXPECT_EQ(anomalyTracker.mMostRecentBucketNum, 19L); |
| 248 | EXPECT_EQ(anomalyTracker.mSumOverPastBuckets.size(), 2UL); |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 249 | EXPECT_EQ(anomalyTracker.getSumOverPastBuckets(keyB), 1LL); |
| 250 | EXPECT_EQ(anomalyTracker.getSumOverPastBuckets(keyC), 1LL); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 251 | anomalyTracker.detectAndDeclareAnomaly(eventTimestamp4, 20, *bucket20); |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 252 | EXPECT_EQ(anomalyTracker.mLastAnomalyTimestampNs, eventTimestamp4); |
Yang Lu | 3eba621 | 2017-10-25 19:54:45 -0700 | [diff] [blame] | 253 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 254 | // Add bucket #18 again. Nothing changes. |
| 255 | anomalyTracker.addPastBucket(bucket18, 18); |
| 256 | EXPECT_EQ(anomalyTracker.mMostRecentBucketNum, 19L); |
| 257 | EXPECT_EQ(anomalyTracker.mSumOverPastBuckets.size(), 2UL); |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 258 | EXPECT_EQ(anomalyTracker.getSumOverPastBuckets(keyB), 1LL); |
| 259 | EXPECT_EQ(anomalyTracker.getSumOverPastBuckets(keyC), 1LL); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 260 | EXPECT_TRUE(anomalyTracker.detectAnomaly(20, *bucket20)); |
| 261 | EXPECT_EQ(anomalyTracker.mSumOverPastBuckets.size(), 2UL); |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 262 | EXPECT_EQ(anomalyTracker.getSumOverPastBuckets(keyB), 1LL); |
| 263 | EXPECT_EQ(anomalyTracker.getSumOverPastBuckets(keyC), 1LL); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 264 | anomalyTracker.detectAndDeclareAnomaly(eventTimestamp4 + 1, 20, *bucket20); |
| 265 | // Within refractory period. |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 266 | EXPECT_EQ(anomalyTracker.mLastAnomalyTimestampNs, eventTimestamp4); |
Yang Lu | 3eba621 | 2017-10-25 19:54:45 -0700 | [diff] [blame] | 267 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 268 | // Add past bucket #20 |
| 269 | anomalyTracker.addPastBucket(bucket20, 20); |
| 270 | EXPECT_EQ(anomalyTracker.mMostRecentBucketNum, 20L); |
| 271 | EXPECT_EQ(anomalyTracker.mSumOverPastBuckets.size(), 2UL); |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 272 | EXPECT_EQ(anomalyTracker.getSumOverPastBuckets(keyB), 3LL); |
| 273 | EXPECT_EQ(anomalyTracker.getSumOverPastBuckets(keyC), 1LL); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 274 | EXPECT_FALSE(anomalyTracker.detectAnomaly(25, *bucket25)); |
| 275 | EXPECT_EQ(anomalyTracker.mMostRecentBucketNum, 24L); |
| 276 | EXPECT_EQ(anomalyTracker.mSumOverPastBuckets.size(), 0UL); |
| 277 | anomalyTracker.detectAndDeclareAnomaly(eventTimestamp5, 25, *bucket25); |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 278 | EXPECT_EQ(anomalyTracker.mLastAnomalyTimestampNs, eventTimestamp4); |
Yang Lu | 3eba621 | 2017-10-25 19:54:45 -0700 | [diff] [blame] | 279 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 280 | // Add past bucket #25 |
| 281 | anomalyTracker.addPastBucket(bucket25, 25); |
| 282 | EXPECT_EQ(anomalyTracker.mMostRecentBucketNum, 25L); |
| 283 | EXPECT_EQ(anomalyTracker.mSumOverPastBuckets.size(), 1UL); |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 284 | EXPECT_EQ(anomalyTracker.getSumOverPastBuckets(keyD), 1LL); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 285 | EXPECT_FALSE(anomalyTracker.detectAnomaly(28, *bucket28)); |
| 286 | EXPECT_EQ(anomalyTracker.mMostRecentBucketNum, 27L); |
| 287 | EXPECT_EQ(anomalyTracker.mSumOverPastBuckets.size(), 0UL); |
| 288 | anomalyTracker.detectAndDeclareAnomaly(eventTimestamp6, 28, *bucket28); |
| 289 | EXPECT_EQ(anomalyTracker.mSumOverPastBuckets.size(), 0UL); |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 290 | EXPECT_EQ(anomalyTracker.mLastAnomalyTimestampNs, eventTimestamp4); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 291 | |
| 292 | // Updates current bucket #28. |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 293 | (*bucket28)[keyE] = 5; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 294 | EXPECT_TRUE(anomalyTracker.detectAnomaly(28, *bucket28)); |
| 295 | EXPECT_EQ(anomalyTracker.mMostRecentBucketNum, 27L); |
| 296 | EXPECT_EQ(anomalyTracker.mSumOverPastBuckets.size(), 0UL); |
| 297 | anomalyTracker.detectAndDeclareAnomaly(eventTimestamp6 + 7, 28, *bucket28); |
| 298 | EXPECT_EQ(anomalyTracker.mSumOverPastBuckets.size(), 0UL); |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 299 | EXPECT_EQ(anomalyTracker.mLastAnomalyTimestampNs, eventTimestamp6 + 7); |
Yang Lu | 3eba621 | 2017-10-25 19:54:45 -0700 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | } // namespace statsd |
| 303 | } // namespace os |
| 304 | } // namespace android |
| 305 | #else |
| 306 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 307 | #endif |