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