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> |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 22 | #include <stdio.h> |
| 23 | #include <set> |
| 24 | #include <unordered_map> |
| 25 | #include <vector> |
| 26 | |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 27 | using namespace testing; |
| 28 | using android::sp; |
| 29 | using std::set; |
| 30 | using std::unordered_map; |
| 31 | using std::vector; |
| 32 | |
| 33 | #ifdef __ANDROID__ |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 34 | namespace android { |
| 35 | namespace os { |
| 36 | namespace statsd { |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 37 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 38 | const ConfigKey kConfigKey(0, 12345); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 39 | const int TagId = 1; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 40 | const int64_t metricId = 123; |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 41 | const HashableDimensionKey eventKey = getMockedDimensionKey(TagId, 0, "event"); |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 42 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 43 | const std::vector<HashableDimensionKey> kConditionKey1 = {getMockedDimensionKey(TagId, 1, "maps")}; |
| 44 | const HashableDimensionKey kEventKey1 = getMockedDimensionKey(TagId, 2, "maps"); |
| 45 | const HashableDimensionKey kEventKey2 = getMockedDimensionKey(TagId, 3, "maps"); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 46 | |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 47 | TEST(OringDurationTrackerTest, TestDurationOverlap) { |
| 48 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 49 | |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 50 | unordered_map<HashableDimensionKey, vector<DurationBucket>> buckets; |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 51 | |
| 52 | uint64_t bucketStartTimeNs = 10000000000; |
| 53 | uint64_t eventStartTimeNs = bucketStartTimeNs + 1; |
| 54 | uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 55 | uint64_t durationTimeNs = 2 * 1000; |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 56 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 57 | OringDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, 1, false, |
Yao Chen | d59a658 | 2018-01-08 11:17:11 -0800 | [diff] [blame] | 58 | bucketStartTimeNs, bucketSizeNs, false, {}); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 59 | |
Yao Chen | d59a658 | 2018-01-08 11:17:11 -0800 | [diff] [blame] | 60 | tracker.noteStart(kEventKey1, true, eventStartTimeNs, ConditionKey()); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 61 | EXPECT_EQ((long long)eventStartTimeNs, tracker.mLastStartTime); |
Yao Chen | d59a658 | 2018-01-08 11:17:11 -0800 | [diff] [blame] | 62 | tracker.noteStart(kEventKey1, true, eventStartTimeNs + 10, ConditionKey()); // overlapping wl |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 63 | EXPECT_EQ((long long)eventStartTimeNs, tracker.mLastStartTime); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 64 | |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 65 | tracker.noteStop(kEventKey1, eventStartTimeNs + durationTimeNs, false); |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 66 | tracker.flushIfNeeded(eventStartTimeNs + bucketSizeNs + 1, &buckets); |
| 67 | EXPECT_TRUE(buckets.find(eventKey) != buckets.end()); |
| 68 | |
| 69 | EXPECT_EQ(1u, buckets[eventKey].size()); |
| 70 | EXPECT_EQ(durationTimeNs, buckets[eventKey][0].mDuration); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 73 | TEST(OringDurationTrackerTest, TestDurationNested) { |
| 74 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 75 | |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 76 | unordered_map<HashableDimensionKey, vector<DurationBucket>> buckets; |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 77 | |
| 78 | uint64_t bucketStartTimeNs = 10000000000; |
| 79 | uint64_t eventStartTimeNs = bucketStartTimeNs + 1; |
| 80 | uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL; |
| 81 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 82 | OringDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, 1, true, bucketStartTimeNs, |
Yao Chen | d59a658 | 2018-01-08 11:17:11 -0800 | [diff] [blame] | 83 | bucketSizeNs, false, {}); |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 84 | |
Yao Chen | d59a658 | 2018-01-08 11:17:11 -0800 | [diff] [blame] | 85 | tracker.noteStart(kEventKey1, true, eventStartTimeNs, ConditionKey()); |
| 86 | tracker.noteStart(kEventKey1, true, eventStartTimeNs + 10, ConditionKey()); // overlapping wl |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 87 | |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 88 | tracker.noteStop(kEventKey1, eventStartTimeNs + 2000, false); |
| 89 | tracker.noteStop(kEventKey1, eventStartTimeNs + 2003, false); |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 90 | |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 91 | tracker.flushIfNeeded(bucketStartTimeNs + bucketSizeNs + 1, &buckets); |
| 92 | EXPECT_TRUE(buckets.find(eventKey) != buckets.end()); |
| 93 | EXPECT_EQ(1u, buckets[eventKey].size()); |
| 94 | EXPECT_EQ(2003ULL, buckets[eventKey][0].mDuration); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | TEST(OringDurationTrackerTest, TestStopAll) { |
| 98 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 99 | |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 100 | unordered_map<HashableDimensionKey, vector<DurationBucket>> buckets; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 101 | |
| 102 | uint64_t bucketStartTimeNs = 10000000000; |
| 103 | uint64_t eventStartTimeNs = bucketStartTimeNs + 1; |
| 104 | uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL; |
| 105 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 106 | OringDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, 1, true, bucketStartTimeNs, |
Yao Chen | d59a658 | 2018-01-08 11:17:11 -0800 | [diff] [blame] | 107 | bucketSizeNs, false, {}); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 108 | |
Yao Chen | d59a658 | 2018-01-08 11:17:11 -0800 | [diff] [blame] | 109 | tracker.noteStart(kEventKey1, true, eventStartTimeNs, ConditionKey()); |
| 110 | tracker.noteStart(kEventKey2, true, eventStartTimeNs + 10, ConditionKey()); // overlapping wl |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 111 | |
| 112 | tracker.noteStopAll(eventStartTimeNs + 2003); |
| 113 | |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 114 | tracker.flushIfNeeded(bucketStartTimeNs + bucketSizeNs + 1, &buckets); |
| 115 | EXPECT_TRUE(buckets.find(eventKey) != buckets.end()); |
| 116 | EXPECT_EQ(1u, buckets[eventKey].size()); |
| 117 | EXPECT_EQ(2003ULL, buckets[eventKey][0].mDuration); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | TEST(OringDurationTrackerTest, TestCrossBucketBoundary) { |
| 121 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 122 | |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 123 | unordered_map<HashableDimensionKey, vector<DurationBucket>> buckets; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 124 | |
| 125 | uint64_t bucketStartTimeNs = 10000000000; |
| 126 | uint64_t eventStartTimeNs = bucketStartTimeNs + 1; |
| 127 | uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL; |
| 128 | uint64_t durationTimeNs = 2 * 1000; |
| 129 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 130 | OringDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, 1, true, bucketStartTimeNs, |
Yao Chen | d59a658 | 2018-01-08 11:17:11 -0800 | [diff] [blame] | 131 | bucketSizeNs, false, {}); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 132 | |
Yao Chen | d59a658 | 2018-01-08 11:17:11 -0800 | [diff] [blame] | 133 | tracker.noteStart(kEventKey1, true, eventStartTimeNs, ConditionKey()); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 134 | EXPECT_EQ((long long)eventStartTimeNs, tracker.mLastStartTime); |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 135 | tracker.flushIfNeeded(eventStartTimeNs + 2 * bucketSizeNs, &buckets); |
Yao Chen | d59a658 | 2018-01-08 11:17:11 -0800 | [diff] [blame] | 136 | tracker.noteStart(kEventKey1, true, eventStartTimeNs + 2 * bucketSizeNs, ConditionKey()); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 137 | EXPECT_EQ((long long)(bucketStartTimeNs + 2 * bucketSizeNs), tracker.mLastStartTime); |
| 138 | |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 139 | EXPECT_EQ(2u, buckets[eventKey].size()); |
| 140 | EXPECT_EQ(bucketSizeNs - 1, buckets[eventKey][0].mDuration); |
| 141 | EXPECT_EQ(bucketSizeNs, buckets[eventKey][1].mDuration); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 142 | |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 143 | tracker.noteStop(kEventKey1, eventStartTimeNs + 2 * bucketSizeNs + 10, false); |
| 144 | tracker.noteStop(kEventKey1, eventStartTimeNs + 2 * bucketSizeNs + 12, false); |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 145 | tracker.flushIfNeeded(eventStartTimeNs + 2 * bucketSizeNs + 12, &buckets); |
| 146 | EXPECT_TRUE(buckets.find(eventKey) != buckets.end()); |
| 147 | EXPECT_EQ(2u, buckets[eventKey].size()); |
| 148 | EXPECT_EQ(bucketSizeNs - 1, buckets[eventKey][0].mDuration); |
| 149 | EXPECT_EQ(bucketSizeNs, buckets[eventKey][1].mDuration); |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 150 | } |
| 151 | |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 152 | TEST(OringDurationTrackerTest, TestDurationConditionChange) { |
| 153 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 154 | |
| 155 | ConditionKey key1; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 156 | key1[StringToId("APP_BACKGROUND")] = kConditionKey1; |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 157 | |
| 158 | EXPECT_CALL(*wizard, query(_, key1)) // #4 |
| 159 | .WillOnce(Return(ConditionState::kFalse)); |
| 160 | |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 161 | unordered_map<HashableDimensionKey, vector<DurationBucket>> buckets; |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 162 | |
| 163 | uint64_t bucketStartTimeNs = 10000000000; |
| 164 | uint64_t eventStartTimeNs = bucketStartTimeNs + 1; |
| 165 | uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 166 | uint64_t durationTimeNs = 2 * 1000; |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 167 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 168 | OringDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, 1, false, |
Yao Chen | d59a658 | 2018-01-08 11:17:11 -0800 | [diff] [blame] | 169 | bucketStartTimeNs, bucketSizeNs, true, {}); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 170 | |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 171 | tracker.noteStart(kEventKey1, true, eventStartTimeNs, key1); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 172 | |
Yao Chen | 09294ef | 2017-11-25 19:54:01 -0800 | [diff] [blame] | 173 | tracker.onSlicedConditionMayChange(eventStartTimeNs + 5); |
| 174 | |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 175 | tracker.noteStop(kEventKey1, eventStartTimeNs + durationTimeNs, false); |
Yao Chen | 09294ef | 2017-11-25 19:54:01 -0800 | [diff] [blame] | 176 | |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 177 | tracker.flushIfNeeded(bucketStartTimeNs + bucketSizeNs + 1, &buckets); |
| 178 | EXPECT_TRUE(buckets.find(eventKey) != buckets.end()); |
| 179 | EXPECT_EQ(1u, buckets[eventKey].size()); |
| 180 | EXPECT_EQ(5ULL, buckets[eventKey][0].mDuration); |
Yao Chen | 09294ef | 2017-11-25 19:54:01 -0800 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | TEST(OringDurationTrackerTest, TestDurationConditionChange2) { |
| 184 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 185 | |
| 186 | ConditionKey key1; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 187 | key1[StringToId("APP_BACKGROUND")] = kConditionKey1; |
Yao Chen | 09294ef | 2017-11-25 19:54:01 -0800 | [diff] [blame] | 188 | |
| 189 | EXPECT_CALL(*wizard, query(_, key1)) |
| 190 | .Times(2) |
| 191 | .WillOnce(Return(ConditionState::kFalse)) |
| 192 | .WillOnce(Return(ConditionState::kTrue)); |
| 193 | |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 194 | unordered_map<HashableDimensionKey, vector<DurationBucket>> buckets; |
Yao Chen | 09294ef | 2017-11-25 19:54:01 -0800 | [diff] [blame] | 195 | |
| 196 | uint64_t bucketStartTimeNs = 10000000000; |
| 197 | uint64_t eventStartTimeNs = bucketStartTimeNs + 1; |
| 198 | uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL; |
| 199 | uint64_t durationTimeNs = 2 * 1000; |
| 200 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 201 | OringDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, 1, false, |
Yao Chen | d59a658 | 2018-01-08 11:17:11 -0800 | [diff] [blame] | 202 | bucketStartTimeNs, bucketSizeNs, true, {}); |
Yao Chen | 09294ef | 2017-11-25 19:54:01 -0800 | [diff] [blame] | 203 | |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 204 | tracker.noteStart(kEventKey1, true, eventStartTimeNs, key1); |
Yao Chen | 09294ef | 2017-11-25 19:54:01 -0800 | [diff] [blame] | 205 | // condition to false; record duration 5n |
| 206 | tracker.onSlicedConditionMayChange(eventStartTimeNs + 5); |
| 207 | // condition to true. |
| 208 | tracker.onSlicedConditionMayChange(eventStartTimeNs + 1000); |
| 209 | // 2nd duration: 1000ns |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 210 | tracker.noteStop(kEventKey1, eventStartTimeNs + durationTimeNs, false); |
Yao Chen | 09294ef | 2017-11-25 19:54:01 -0800 | [diff] [blame] | 211 | |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 212 | tracker.flushIfNeeded(bucketStartTimeNs + bucketSizeNs + 1, &buckets); |
| 213 | EXPECT_TRUE(buckets.find(eventKey) != buckets.end()); |
| 214 | EXPECT_EQ(1u, buckets[eventKey].size()); |
| 215 | EXPECT_EQ(1005ULL, buckets[eventKey][0].mDuration); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 216 | } |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 217 | |
| 218 | TEST(OringDurationTrackerTest, TestDurationConditionChangeNested) { |
| 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 | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 223 | |
| 224 | EXPECT_CALL(*wizard, query(_, key1)) // #4 |
| 225 | .WillOnce(Return(ConditionState::kFalse)); |
| 226 | |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 227 | unordered_map<HashableDimensionKey, vector<DurationBucket>> buckets; |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 228 | |
| 229 | uint64_t bucketStartTimeNs = 10000000000; |
| 230 | uint64_t eventStartTimeNs = bucketStartTimeNs + 1; |
| 231 | uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL; |
| 232 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 233 | OringDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, 1, true, bucketStartTimeNs, |
Yao Chen | d59a658 | 2018-01-08 11:17:11 -0800 | [diff] [blame] | 234 | bucketSizeNs, true, {}); |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 235 | |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 236 | tracker.noteStart(kEventKey1, true, eventStartTimeNs, key1); |
| 237 | tracker.noteStart(kEventKey1, true, eventStartTimeNs + 2, key1); |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 238 | |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 239 | tracker.noteStop(kEventKey1, eventStartTimeNs + 3, false); |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 240 | |
| 241 | tracker.onSlicedConditionMayChange(eventStartTimeNs + 15); |
| 242 | |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 243 | tracker.noteStop(kEventKey1, eventStartTimeNs + 2003, false); |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 244 | |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 245 | tracker.flushIfNeeded(bucketStartTimeNs + bucketSizeNs + 1, &buckets); |
| 246 | EXPECT_TRUE(buckets.find(eventKey) != buckets.end()); |
| 247 | EXPECT_EQ(1u, buckets[eventKey].size()); |
| 248 | EXPECT_EQ(15ULL, buckets[eventKey][0].mDuration); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | TEST(OringDurationTrackerTest, TestPredictAnomalyTimestamp) { |
| 252 | Alert alert; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 253 | alert.set_id(101); |
| 254 | alert.set_metric_id(1); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 255 | alert.set_trigger_if_sum_gt(40 * NS_PER_SEC); |
Yangster-mac | a7fb12d | 2018-01-03 17:17:20 -0800 | [diff] [blame] | 256 | alert.set_num_buckets(2); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 257 | alert.set_refractory_period_secs(1); |
| 258 | |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 259 | unordered_map<HashableDimensionKey, vector<DurationBucket>> buckets; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 260 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
Yao Chen | d59a658 | 2018-01-08 11:17:11 -0800 | [diff] [blame] | 261 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 262 | uint64_t bucketStartTimeNs = 10 * NS_PER_SEC; |
| 263 | uint64_t eventStartTimeNs = bucketStartTimeNs + NS_PER_SEC + 1; |
| 264 | uint64_t bucketSizeNs = 30 * NS_PER_SEC; |
| 265 | |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 266 | sp<DurationAnomalyTracker> anomalyTracker = new DurationAnomalyTracker(alert, kConfigKey); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 267 | OringDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, 1, true, bucketStartTimeNs, |
Yao Chen | d59a658 | 2018-01-08 11:17:11 -0800 | [diff] [blame] | 268 | bucketSizeNs, true, {anomalyTracker}); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 269 | |
| 270 | // Nothing in the past bucket. |
Yao Chen | d59a658 | 2018-01-08 11:17:11 -0800 | [diff] [blame] | 271 | tracker.noteStart(DEFAULT_DIMENSION_KEY, true, eventStartTimeNs, ConditionKey()); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 272 | EXPECT_EQ((long long)(alert.trigger_if_sum_gt() + eventStartTimeNs), |
| 273 | tracker.predictAnomalyTimestampNs(*anomalyTracker, eventStartTimeNs)); |
| 274 | |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 275 | tracker.noteStop(DEFAULT_DIMENSION_KEY, eventStartTimeNs + 3, false); |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 276 | EXPECT_EQ(0u, buckets[eventKey].size()); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 277 | |
| 278 | uint64_t event1StartTimeNs = eventStartTimeNs + 10; |
Yao Chen | d59a658 | 2018-01-08 11:17:11 -0800 | [diff] [blame] | 279 | tracker.noteStart(kEventKey1, true, event1StartTimeNs, ConditionKey()); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 280 | // No past buckets. The anomaly will happen in bucket #0. |
| 281 | EXPECT_EQ((long long)(event1StartTimeNs + alert.trigger_if_sum_gt() - 3), |
| 282 | tracker.predictAnomalyTimestampNs(*anomalyTracker, event1StartTimeNs)); |
| 283 | |
| 284 | uint64_t event1StopTimeNs = eventStartTimeNs + bucketSizeNs + 10; |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 285 | tracker.flushIfNeeded(event1StopTimeNs, &buckets); |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 286 | tracker.noteStop(kEventKey1, event1StopTimeNs, false); |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 287 | |
| 288 | EXPECT_TRUE(buckets.find(eventKey) != buckets.end()); |
| 289 | EXPECT_EQ(1u, buckets[eventKey].size()); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 290 | EXPECT_EQ(3ULL + bucketStartTimeNs + bucketSizeNs - eventStartTimeNs - 10, |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 291 | buckets[eventKey][0].mDuration); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 292 | |
| 293 | const int64_t bucket0Duration = 3ULL + bucketStartTimeNs + bucketSizeNs - eventStartTimeNs - 10; |
| 294 | const int64_t bucket1Duration = eventStartTimeNs + 10 - bucketStartTimeNs; |
| 295 | |
| 296 | // One past buckets. The anomaly will happen in bucket #1. |
| 297 | uint64_t event2StartTimeNs = eventStartTimeNs + bucketSizeNs + 15; |
Yao Chen | d59a658 | 2018-01-08 11:17:11 -0800 | [diff] [blame] | 298 | tracker.noteStart(kEventKey1, true, event2StartTimeNs, ConditionKey()); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 299 | EXPECT_EQ((long long)(event2StartTimeNs + alert.trigger_if_sum_gt() - bucket0Duration - |
| 300 | bucket1Duration), |
| 301 | tracker.predictAnomalyTimestampNs(*anomalyTracker, event2StartTimeNs)); |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 302 | tracker.noteStop(kEventKey1, event2StartTimeNs + 1, false); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 303 | |
| 304 | // Only one past buckets is applicable. Bucket +0 should be trashed. The anomaly will happen in |
| 305 | // bucket #2. |
| 306 | uint64_t event3StartTimeNs = bucketStartTimeNs + 2 * bucketSizeNs - 9 * NS_PER_SEC; |
Yao Chen | d59a658 | 2018-01-08 11:17:11 -0800 | [diff] [blame] | 307 | tracker.noteStart(kEventKey1, true, event3StartTimeNs, ConditionKey()); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 308 | EXPECT_EQ((long long)(event3StartTimeNs + alert.trigger_if_sum_gt() - bucket1Duration - 1LL), |
| 309 | tracker.predictAnomalyTimestampNs(*anomalyTracker, event3StartTimeNs)); |
| 310 | } |
| 311 | |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 312 | TEST(OringDurationTrackerTest, TestAnomalyDetectionExpiredAlarm) { |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 313 | Alert alert; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 314 | alert.set_id(101); |
| 315 | alert.set_metric_id(1); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 316 | alert.set_trigger_if_sum_gt(40 * NS_PER_SEC); |
Yangster-mac | a7fb12d | 2018-01-03 17:17:20 -0800 | [diff] [blame] | 317 | alert.set_num_buckets(2); |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 318 | const int32_t refPeriodSec = 45; |
| 319 | alert.set_refractory_period_secs(refPeriodSec); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 320 | |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 321 | unordered_map<HashableDimensionKey, vector<DurationBucket>> buckets; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 322 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
Yao Chen | d59a658 | 2018-01-08 11:17:11 -0800 | [diff] [blame] | 323 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 324 | uint64_t bucketStartTimeNs = 10 * NS_PER_SEC; |
| 325 | uint64_t eventStartTimeNs = bucketStartTimeNs + NS_PER_SEC + 1; |
| 326 | uint64_t bucketSizeNs = 30 * NS_PER_SEC; |
| 327 | |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 328 | sp<DurationAnomalyTracker> anomalyTracker = new DurationAnomalyTracker(alert, kConfigKey); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 329 | OringDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, 1, true /*nesting*/, |
Yao Chen | d59a658 | 2018-01-08 11:17:11 -0800 | [diff] [blame] | 330 | bucketStartTimeNs, bucketSizeNs, false, {anomalyTracker}); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 331 | |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 332 | tracker.noteStart(kEventKey1, true, eventStartTimeNs, ConditionKey()); |
| 333 | tracker.noteStop(kEventKey1, eventStartTimeNs + 10, false); |
| 334 | EXPECT_EQ(anomalyTracker->getRefractoryPeriodEndsSec(eventKey), 0U); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 335 | EXPECT_TRUE(tracker.mStarted.empty()); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 336 | EXPECT_EQ(10LL, tracker.mDuration); |
| 337 | |
| 338 | EXPECT_EQ(0u, tracker.mStarted.size()); |
| 339 | |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 340 | tracker.noteStart(kEventKey1, true, eventStartTimeNs + 20, ConditionKey()); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 341 | EXPECT_EQ(1u, anomalyTracker->mAlarms.size()); |
| 342 | EXPECT_EQ((long long)(51ULL * NS_PER_SEC), |
| 343 | (long long)(anomalyTracker->mAlarms.begin()->second->timestampSec * NS_PER_SEC)); |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 344 | // The alarm is set to fire at 51s, and when it does, an anomaly would be declared. However, |
| 345 | // because this is a unit test, the alarm won't actually fire at all. Since the alarm fails |
| 346 | // 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] | 347 | tracker.flushIfNeeded(eventStartTimeNs + 2 * bucketSizeNs + 25, &buckets); |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 348 | tracker.noteStop(kEventKey1, eventStartTimeNs + 2 * bucketSizeNs + 25, false); |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 349 | EXPECT_EQ(anomalyTracker->getSumOverPastBuckets(eventKey), (long long)(bucketSizeNs)); |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 350 | EXPECT_EQ(anomalyTracker->getRefractoryPeriodEndsSec(eventKey), |
| 351 | (eventStartTimeNs + 2 * bucketSizeNs + 25) / NS_PER_SEC + refPeriodSec); |
| 352 | } |
| 353 | |
| 354 | TEST(OringDurationTrackerTest, TestAnomalyDetectionFiredAlarm) { |
| 355 | Alert alert; |
| 356 | alert.set_id(101); |
| 357 | alert.set_metric_id(1); |
| 358 | alert.set_trigger_if_sum_gt(40 * NS_PER_SEC); |
| 359 | alert.set_num_buckets(2); |
| 360 | const int32_t refPeriodSec = 45; |
| 361 | alert.set_refractory_period_secs(refPeriodSec); |
| 362 | |
| 363 | unordered_map<HashableDimensionKey, vector<DurationBucket>> buckets; |
| 364 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 365 | ConditionKey conkey; |
| 366 | conkey[StringToId("APP_BACKGROUND")] = kConditionKey1; |
| 367 | uint64_t bucketStartTimeNs = 10 * NS_PER_SEC; |
| 368 | uint64_t eventStartTimeNs = bucketStartTimeNs + NS_PER_SEC + 1; |
| 369 | uint64_t bucketSizeNs = 30 * NS_PER_SEC; |
| 370 | |
| 371 | sp<DurationAnomalyTracker> anomalyTracker = new DurationAnomalyTracker(alert, kConfigKey); |
| 372 | OringDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, 1, true /*nesting*/, |
| 373 | bucketStartTimeNs, bucketSizeNs, false, {anomalyTracker}); |
| 374 | |
| 375 | tracker.noteStart(kEventKey1, true, 15 * NS_PER_SEC, conkey); // start key1 |
| 376 | EXPECT_EQ(1u, anomalyTracker->mAlarms.size()); |
| 377 | sp<const AnomalyAlarm> alarm = anomalyTracker->mAlarms.begin()->second; |
| 378 | EXPECT_EQ((long long)(55ULL * NS_PER_SEC), (long long)(alarm->timestampSec * NS_PER_SEC)); |
| 379 | EXPECT_EQ(anomalyTracker->getRefractoryPeriodEndsSec(eventKey), 0U); |
| 380 | |
| 381 | tracker.noteStop(kEventKey1, 17 * NS_PER_SEC, false); // stop key1 (2 seconds later) |
| 382 | EXPECT_EQ(0u, anomalyTracker->mAlarms.size()); |
| 383 | EXPECT_EQ(anomalyTracker->getRefractoryPeriodEndsSec(eventKey), 0U); |
| 384 | |
| 385 | tracker.noteStart(kEventKey1, true, 22 * NS_PER_SEC, conkey); // start key1 again |
| 386 | EXPECT_EQ(1u, anomalyTracker->mAlarms.size()); |
| 387 | alarm = anomalyTracker->mAlarms.begin()->second; |
| 388 | EXPECT_EQ((long long)(60ULL * NS_PER_SEC), (long long)(alarm->timestampSec * NS_PER_SEC)); |
| 389 | EXPECT_EQ(anomalyTracker->getRefractoryPeriodEndsSec(eventKey), 0U); |
| 390 | |
| 391 | tracker.noteStart(kEventKey2, true, 32 * NS_PER_SEC, conkey); // start key2 |
| 392 | EXPECT_EQ(1u, anomalyTracker->mAlarms.size()); |
| 393 | alarm = anomalyTracker->mAlarms.begin()->second; |
| 394 | EXPECT_EQ((long long)(60ULL * NS_PER_SEC), (long long)(alarm->timestampSec * NS_PER_SEC)); |
| 395 | EXPECT_EQ(anomalyTracker->getRefractoryPeriodEndsSec(eventKey), 0U); |
| 396 | |
| 397 | tracker.noteStop(kEventKey1, 47 * NS_PER_SEC, false); // stop key1 |
| 398 | EXPECT_EQ(1u, anomalyTracker->mAlarms.size()); |
| 399 | alarm = anomalyTracker->mAlarms.begin()->second; |
| 400 | EXPECT_EQ((long long)(60ULL * NS_PER_SEC), (long long)(alarm->timestampSec * NS_PER_SEC)); |
| 401 | EXPECT_EQ(anomalyTracker->getRefractoryPeriodEndsSec(eventKey), 0U); |
| 402 | |
| 403 | // Now, at 60s, which is 38s after key1 started again, we have reached 40s of 'on' time. |
| 404 | std::unordered_set<sp<const AnomalyAlarm>, SpHash<AnomalyAlarm>> firedAlarms({alarm}); |
| 405 | anomalyTracker->informAlarmsFired(62 * NS_PER_SEC, firedAlarms); |
| 406 | EXPECT_EQ(0u, anomalyTracker->mAlarms.size()); |
| 407 | EXPECT_EQ(anomalyTracker->getRefractoryPeriodEndsSec(eventKey), 62U + refPeriodSec); |
| 408 | |
| 409 | tracker.noteStop(kEventKey2, 69 * NS_PER_SEC, false); // stop key2 |
| 410 | EXPECT_EQ(0u, anomalyTracker->mAlarms.size()); |
| 411 | EXPECT_EQ(anomalyTracker->getRefractoryPeriodEndsSec(eventKey), 62U + refPeriodSec); |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 412 | } |
| 413 | |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 414 | } // namespace statsd |
| 415 | } // namespace os |
| 416 | } // namespace android |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 417 | #else |
| 418 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 419 | #endif |