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