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