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/MaxDurationTracker.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 | |
| 27 | using namespace android::os::statsd; |
| 28 | using namespace testing; |
| 29 | using android::sp; |
| 30 | using std::set; |
| 31 | using std::unordered_map; |
| 32 | using std::vector; |
| 33 | |
| 34 | #ifdef __ANDROID__ |
| 35 | |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 36 | namespace android { |
| 37 | namespace os { |
| 38 | namespace statsd { |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 39 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 40 | const ConfigKey kConfigKey(0, 12345); |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 41 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 42 | const int TagId = 1; |
| 43 | |
| 44 | const HashableDimensionKey eventKey = getMockedDimensionKey(TagId, 0, "1"); |
| 45 | const std::vector<HashableDimensionKey> conditionKey = {getMockedDimensionKey(TagId, 4, "1")}; |
| 46 | const HashableDimensionKey key1 = getMockedDimensionKey(TagId, 1, "1"); |
| 47 | const HashableDimensionKey key2 = getMockedDimensionKey(TagId, 1, "2"); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 48 | |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 49 | TEST(MaxDurationTrackerTest, TestSimpleMaxDuration) { |
| 50 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 51 | |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 52 | unordered_map<HashableDimensionKey, vector<DurationBucket>> buckets; |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 53 | |
| 54 | uint64_t bucketStartTimeNs = 10000000000; |
| 55 | uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL; |
| 56 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 57 | int64_t metricId = 1; |
| 58 | MaxDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, -1, false, bucketStartTimeNs, |
Yao Chen | d59a658 | 2018-01-08 11:17:11 -0800 | [diff] [blame] | 59 | bucketSizeNs, false, {}); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 60 | |
Yao Chen | d59a658 | 2018-01-08 11:17:11 -0800 | [diff] [blame] | 61 | tracker.noteStart(key1, true, bucketStartTimeNs, ConditionKey()); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 62 | // Event starts again. This would not change anything as it already starts. |
Yao Chen | d59a658 | 2018-01-08 11:17:11 -0800 | [diff] [blame] | 63 | tracker.noteStart(key1, true, bucketStartTimeNs + 3, ConditionKey()); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 64 | // Stopped. |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 65 | tracker.noteStop(key1, bucketStartTimeNs + 10, false); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 66 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 67 | // Another event starts in this bucket. |
Yao Chen | d59a658 | 2018-01-08 11:17:11 -0800 | [diff] [blame] | 68 | tracker.noteStart(key2, true, bucketStartTimeNs + 20, ConditionKey()); |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 69 | tracker.noteStop(key2, bucketStartTimeNs + 40, false /*stop all*/); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 70 | |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 71 | tracker.flushIfNeeded(bucketStartTimeNs + bucketSizeNs + 1, &buckets); |
| 72 | EXPECT_TRUE(buckets.find(eventKey) != buckets.end()); |
| 73 | EXPECT_EQ(1u, buckets[eventKey].size()); |
| 74 | EXPECT_EQ(20ULL, buckets[eventKey][0].mDuration); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | TEST(MaxDurationTrackerTest, TestStopAll) { |
| 78 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 79 | |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 80 | unordered_map<HashableDimensionKey, vector<DurationBucket>> buckets; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 81 | |
| 82 | uint64_t bucketStartTimeNs = 10000000000; |
| 83 | uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL; |
| 84 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 85 | int64_t metricId = 1; |
| 86 | MaxDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, -1, false, bucketStartTimeNs, |
Yao Chen | d59a658 | 2018-01-08 11:17:11 -0800 | [diff] [blame] | 87 | bucketSizeNs, false, {}); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 88 | |
Yao Chen | d59a658 | 2018-01-08 11:17:11 -0800 | [diff] [blame] | 89 | tracker.noteStart(key1, true, bucketStartTimeNs + 1, ConditionKey()); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 90 | |
| 91 | // Another event starts in this bucket. |
Yao Chen | d59a658 | 2018-01-08 11:17:11 -0800 | [diff] [blame] | 92 | tracker.noteStart(key2, true, bucketStartTimeNs + 20, ConditionKey()); |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 93 | tracker.flushIfNeeded(bucketStartTimeNs + bucketSizeNs + 40, &buckets); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 94 | tracker.noteStopAll(bucketStartTimeNs + bucketSizeNs + 40); |
| 95 | EXPECT_TRUE(tracker.mInfos.empty()); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 96 | |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 97 | EXPECT_TRUE(buckets.find(eventKey) != buckets.end()); |
| 98 | EXPECT_EQ(1u, buckets[eventKey].size()); |
| 99 | EXPECT_EQ(bucketSizeNs - 1, buckets[eventKey][0].mDuration); |
| 100 | |
| 101 | tracker.flushIfNeeded(bucketStartTimeNs + 3 * bucketSizeNs + 40, &buckets); |
| 102 | EXPECT_EQ(2u, buckets[eventKey].size()); |
| 103 | EXPECT_EQ(bucketSizeNs - 1, buckets[eventKey][0].mDuration); |
| 104 | EXPECT_EQ(40ULL, buckets[eventKey][1].mDuration); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | TEST(MaxDurationTrackerTest, TestCrossBucketBoundary) { |
| 108 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 109 | |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 110 | unordered_map<HashableDimensionKey, vector<DurationBucket>> buckets; |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 111 | |
| 112 | uint64_t bucketStartTimeNs = 10000000000; |
| 113 | uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL; |
| 114 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 115 | int64_t metricId = 1; |
| 116 | MaxDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, -1, false, bucketStartTimeNs, |
Yao Chen | d59a658 | 2018-01-08 11:17:11 -0800 | [diff] [blame] | 117 | bucketSizeNs, false, {}); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 118 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 119 | // The event starts. |
Yao Chen | d59a658 | 2018-01-08 11:17:11 -0800 | [diff] [blame] | 120 | tracker.noteStart(DEFAULT_DIMENSION_KEY, true, bucketStartTimeNs + 1, ConditionKey()); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 121 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 122 | // Starts again. Does not change anything. |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 123 | tracker.noteStart(DEFAULT_DIMENSION_KEY, true, bucketStartTimeNs + bucketSizeNs + 1, |
Yao Chen | d59a658 | 2018-01-08 11:17:11 -0800 | [diff] [blame] | 124 | ConditionKey()); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 125 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 126 | // The event stops at early 4th bucket. |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 127 | tracker.flushIfNeeded(bucketStartTimeNs + (3 * bucketSizeNs) + 20, &buckets); |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 128 | tracker.noteStop(DEFAULT_DIMENSION_KEY, bucketStartTimeNs + (3 * bucketSizeNs) + 20, |
| 129 | false /*stop all*/); |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 130 | EXPECT_TRUE(buckets.find(eventKey) != buckets.end()); |
| 131 | EXPECT_EQ(3u, buckets[eventKey].size()); |
| 132 | EXPECT_EQ((unsigned long long)(bucketSizeNs - 1), buckets[eventKey][0].mDuration); |
| 133 | EXPECT_EQ((unsigned long long)bucketSizeNs, buckets[eventKey][1].mDuration); |
| 134 | EXPECT_EQ((unsigned long long)bucketSizeNs, buckets[eventKey][2].mDuration); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 137 | TEST(MaxDurationTrackerTest, TestCrossBucketBoundary_nested) { |
| 138 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 139 | |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 140 | unordered_map<HashableDimensionKey, vector<DurationBucket>> buckets; |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 141 | |
| 142 | uint64_t bucketStartTimeNs = 10000000000; |
| 143 | uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL; |
| 144 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 145 | int64_t metricId = 1; |
| 146 | MaxDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, -1, true, bucketStartTimeNs, |
Yao Chen | d59a658 | 2018-01-08 11:17:11 -0800 | [diff] [blame] | 147 | bucketSizeNs, false, {}); |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 148 | |
| 149 | // 2 starts |
Yao Chen | d59a658 | 2018-01-08 11:17:11 -0800 | [diff] [blame] | 150 | tracker.noteStart(DEFAULT_DIMENSION_KEY, true, bucketStartTimeNs + 1, ConditionKey()); |
| 151 | tracker.noteStart(DEFAULT_DIMENSION_KEY, true, bucketStartTimeNs + 10, ConditionKey()); |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 152 | // one stop |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 153 | tracker.noteStop(DEFAULT_DIMENSION_KEY, bucketStartTimeNs + 20, false /*stop all*/); |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 154 | |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 155 | tracker.flushIfNeeded(bucketStartTimeNs + (2 * bucketSizeNs) + 1, &buckets); |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 156 | |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 157 | EXPECT_TRUE(buckets.find(eventKey) != buckets.end()); |
| 158 | EXPECT_EQ(2u, buckets[eventKey].size()); |
| 159 | EXPECT_EQ(bucketSizeNs - 1, buckets[eventKey][0].mDuration); |
| 160 | EXPECT_EQ(bucketSizeNs, buckets[eventKey][1].mDuration); |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 161 | |
| 162 | // real stop now. |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 163 | tracker.noteStop(DEFAULT_DIMENSION_KEY, bucketStartTimeNs + (2 * bucketSizeNs) + 5, false); |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 164 | tracker.flushIfNeeded(bucketStartTimeNs + (3 * bucketSizeNs) + 1, &buckets); |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 165 | |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 166 | EXPECT_EQ(3u, buckets[eventKey].size()); |
| 167 | EXPECT_EQ(bucketSizeNs - 1, buckets[eventKey][0].mDuration); |
| 168 | EXPECT_EQ(bucketSizeNs, buckets[eventKey][1].mDuration); |
| 169 | EXPECT_EQ(5ULL, buckets[eventKey][2].mDuration); |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 170 | } |
| 171 | |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 172 | TEST(MaxDurationTrackerTest, TestMaxDurationWithCondition) { |
| 173 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 174 | |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 175 | ConditionKey conditionKey1; |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 176 | HashableDimensionKey eventKey = getMockedDimensionKey(TagId, 2, "maps"); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 177 | conditionKey1[StringToId("APP_BACKGROUND")] = conditionKey; |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 178 | |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 179 | EXPECT_CALL(*wizard, query(_, conditionKey1)) // #4 |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 180 | .WillOnce(Return(ConditionState::kFalse)); |
| 181 | |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 182 | unordered_map<HashableDimensionKey, vector<DurationBucket>> buckets; |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 183 | |
| 184 | uint64_t bucketStartTimeNs = 10000000000; |
| 185 | uint64_t eventStartTimeNs = bucketStartTimeNs + 1; |
| 186 | uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL; |
| 187 | int64_t durationTimeNs = 2 * 1000; |
| 188 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 189 | int64_t metricId = 1; |
| 190 | MaxDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, 1, false, bucketStartTimeNs, |
Yao Chen | d59a658 | 2018-01-08 11:17:11 -0800 | [diff] [blame] | 191 | bucketSizeNs, true, {}); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 192 | EXPECT_TRUE(tracker.mAnomalyTrackers.empty()); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 193 | |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 194 | tracker.noteStart(key1, true, eventStartTimeNs, conditionKey1); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 195 | |
Yao Chen | 09294ef | 2017-11-25 19:54:01 -0800 | [diff] [blame] | 196 | tracker.onSlicedConditionMayChange(eventStartTimeNs + 5); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 197 | |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 198 | tracker.noteStop(key1, eventStartTimeNs + durationTimeNs, false); |
Yao Chen | 09294ef | 2017-11-25 19:54:01 -0800 | [diff] [blame] | 199 | |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 200 | tracker.flushIfNeeded(bucketStartTimeNs + bucketSizeNs + 1, &buckets); |
| 201 | EXPECT_TRUE(buckets.find(eventKey) != buckets.end()); |
| 202 | EXPECT_EQ(1u, buckets[eventKey].size()); |
| 203 | EXPECT_EQ(5ULL, buckets[eventKey][0].mDuration); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 204 | } |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 205 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 206 | TEST(MaxDurationTrackerTest, TestAnomalyDetection) { |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 207 | int64_t metricId = 1; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 208 | Alert alert; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 209 | alert.set_id(101); |
| 210 | alert.set_metric_id(metricId); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 211 | alert.set_trigger_if_sum_gt(32 * NS_PER_SEC); |
Yangster-mac | a7fb12d | 2018-01-03 17:17:20 -0800 | [diff] [blame] | 212 | alert.set_num_buckets(2); |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 213 | const int32_t refPeriodSec = 1; |
| 214 | alert.set_refractory_period_secs(refPeriodSec); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 215 | |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 216 | unordered_map<HashableDimensionKey, vector<DurationBucket>> buckets; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 217 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
Yao Chen | d59a658 | 2018-01-08 11:17:11 -0800 | [diff] [blame] | 218 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 219 | uint64_t bucketStartTimeNs = 10 * NS_PER_SEC; |
| 220 | uint64_t eventStartTimeNs = bucketStartTimeNs + NS_PER_SEC + 1; |
| 221 | uint64_t bucketSizeNs = 30 * NS_PER_SEC; |
| 222 | |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 223 | sp<DurationAnomalyTracker> anomalyTracker = new DurationAnomalyTracker(alert, kConfigKey); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 224 | MaxDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, -1, true, bucketStartTimeNs, |
Yao Chen | d59a658 | 2018-01-08 11:17:11 -0800 | [diff] [blame] | 225 | bucketSizeNs, false, {anomalyTracker}); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 226 | |
Yao Chen | d59a658 | 2018-01-08 11:17:11 -0800 | [diff] [blame] | 227 | tracker.noteStart(key1, true, eventStartTimeNs, ConditionKey()); |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 228 | tracker.noteStop(key1, eventStartTimeNs + 10, false); |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 229 | EXPECT_EQ(anomalyTracker->getRefractoryPeriodEndsSec(eventKey), 0U); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 230 | EXPECT_EQ(10LL, tracker.mDuration); |
| 231 | |
Yao Chen | d59a658 | 2018-01-08 11:17:11 -0800 | [diff] [blame] | 232 | tracker.noteStart(key2, true, eventStartTimeNs + 20, ConditionKey()); |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 233 | tracker.flushIfNeeded(eventStartTimeNs + 2 * bucketSizeNs + 3 * NS_PER_SEC, &buckets); |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 234 | tracker.noteStop(key2, eventStartTimeNs + 2 * bucketSizeNs + 3 * NS_PER_SEC, false); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 235 | EXPECT_EQ((long long)(4 * NS_PER_SEC + 1LL), tracker.mDuration); |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 236 | |
| 237 | EXPECT_EQ(anomalyTracker->getRefractoryPeriodEndsSec(eventKey), |
| 238 | (eventStartTimeNs + 2 * bucketSizeNs) / NS_PER_SEC + 3 + refPeriodSec); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 241 | } // namespace statsd |
| 242 | } // namespace os |
| 243 | } // namespace android |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 244 | #else |
| 245 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 246 | #endif |