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 | |
| 41 | const HashableDimensionKey eventKey = getMockedDimensionKey(0, "1"); |
| 42 | const HashableDimensionKey conditionKey = getMockedDimensionKey(4, "1"); |
| 43 | const HashableDimensionKey key1 = getMockedDimensionKey(1, "1"); |
| 44 | const HashableDimensionKey key2 = getMockedDimensionKey(1, "2"); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 45 | |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 46 | TEST(MaxDurationTrackerTest, TestSimpleMaxDuration) { |
| 47 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 48 | |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 49 | unordered_map<HashableDimensionKey, vector<DurationBucket>> buckets; |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 50 | ConditionKey conditionKey1; |
| 51 | conditionKey1["condition"] = conditionKey; |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 52 | |
| 53 | uint64_t bucketStartTimeNs = 10000000000; |
| 54 | uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL; |
| 55 | |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 56 | MaxDurationTracker tracker(kConfigKey, "metric", eventKey, wizard, -1, false, bucketStartTimeNs, |
| 57 | bucketSizeNs, {}); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 58 | |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 59 | tracker.noteStart(key1, true, bucketStartTimeNs, conditionKey1); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 60 | // Event starts again. This would not change anything as it already starts. |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 61 | tracker.noteStart(key1, true, bucketStartTimeNs + 3, conditionKey1); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 62 | // Stopped. |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 63 | tracker.noteStop(key1, bucketStartTimeNs + 10, false); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 64 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 65 | // Another event starts in this bucket. |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 66 | tracker.noteStart(key2, true, bucketStartTimeNs + 20, conditionKey1); |
| 67 | tracker.noteStop(key2, bucketStartTimeNs + 40, false /*stop all*/); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 68 | |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 69 | tracker.flushIfNeeded(bucketStartTimeNs + bucketSizeNs + 1, &buckets); |
| 70 | EXPECT_TRUE(buckets.find(eventKey) != buckets.end()); |
| 71 | EXPECT_EQ(1u, buckets[eventKey].size()); |
| 72 | EXPECT_EQ(20ULL, buckets[eventKey][0].mDuration); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | TEST(MaxDurationTrackerTest, TestStopAll) { |
| 76 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 77 | |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 78 | unordered_map<HashableDimensionKey, vector<DurationBucket>> buckets; |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 79 | ConditionKey conditionKey1; |
| 80 | conditionKey1["condition"] = conditionKey; |
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 | |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 85 | MaxDurationTracker tracker(kConfigKey, "metric", eventKey, wizard, -1, false, bucketStartTimeNs, |
| 86 | bucketSizeNs, {}); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 87 | |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 88 | tracker.noteStart(key1, true, bucketStartTimeNs + 1, conditionKey1); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 89 | |
| 90 | // Another event starts in this bucket. |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 91 | tracker.noteStart(key2, true, bucketStartTimeNs + 20, conditionKey1); |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 92 | tracker.flushIfNeeded(bucketStartTimeNs + bucketSizeNs + 40, &buckets); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 93 | tracker.noteStopAll(bucketStartTimeNs + bucketSizeNs + 40); |
| 94 | EXPECT_TRUE(tracker.mInfos.empty()); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 95 | |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 96 | EXPECT_TRUE(buckets.find(eventKey) != buckets.end()); |
| 97 | EXPECT_EQ(1u, buckets[eventKey].size()); |
| 98 | EXPECT_EQ(bucketSizeNs - 1, buckets[eventKey][0].mDuration); |
| 99 | |
| 100 | tracker.flushIfNeeded(bucketStartTimeNs + 3 * bucketSizeNs + 40, &buckets); |
| 101 | EXPECT_EQ(2u, buckets[eventKey].size()); |
| 102 | EXPECT_EQ(bucketSizeNs - 1, buckets[eventKey][0].mDuration); |
| 103 | EXPECT_EQ(40ULL, buckets[eventKey][1].mDuration); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | TEST(MaxDurationTrackerTest, TestCrossBucketBoundary) { |
| 107 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 108 | |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 109 | unordered_map<HashableDimensionKey, vector<DurationBucket>> buckets; |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 110 | ConditionKey conditionKey1; |
| 111 | conditionKey1["condition"] = conditionKey; |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 112 | |
| 113 | uint64_t bucketStartTimeNs = 10000000000; |
| 114 | uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL; |
| 115 | |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 116 | MaxDurationTracker tracker(kConfigKey, "metric", eventKey, wizard, -1, false, bucketStartTimeNs, |
| 117 | bucketSizeNs, {}); |
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 | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 120 | tracker.noteStart(DEFAULT_DIMENSION_KEY, true, bucketStartTimeNs + 1, conditionKey1); |
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, |
| 124 | conditionKey1); |
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 | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 141 | ConditionKey conditionKey1; |
| 142 | conditionKey1["condition"] = conditionKey; |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 143 | |
| 144 | uint64_t bucketStartTimeNs = 10000000000; |
| 145 | uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL; |
| 146 | |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 147 | MaxDurationTracker tracker(kConfigKey, "metric", eventKey, wizard, -1, true, bucketStartTimeNs, |
| 148 | bucketSizeNs, {}); |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 149 | |
| 150 | // 2 starts |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 151 | tracker.noteStart(DEFAULT_DIMENSION_KEY, true, bucketStartTimeNs + 1, conditionKey1); |
| 152 | tracker.noteStart(DEFAULT_DIMENSION_KEY, true, bucketStartTimeNs + 10, conditionKey1); |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 153 | // one stop |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 154 | tracker.noteStop(DEFAULT_DIMENSION_KEY, bucketStartTimeNs + 20, false /*stop all*/); |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 155 | |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 156 | tracker.flushIfNeeded(bucketStartTimeNs + (2 * bucketSizeNs) + 1, &buckets); |
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 | EXPECT_TRUE(buckets.find(eventKey) != buckets.end()); |
| 159 | EXPECT_EQ(2u, buckets[eventKey].size()); |
| 160 | EXPECT_EQ(bucketSizeNs - 1, buckets[eventKey][0].mDuration); |
| 161 | EXPECT_EQ(bucketSizeNs, buckets[eventKey][1].mDuration); |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 162 | |
| 163 | // real stop now. |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 164 | tracker.noteStop(DEFAULT_DIMENSION_KEY, bucketStartTimeNs + (2 * bucketSizeNs) + 5, false); |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 165 | tracker.flushIfNeeded(bucketStartTimeNs + (3 * bucketSizeNs) + 1, &buckets); |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 166 | |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 167 | EXPECT_EQ(3u, buckets[eventKey].size()); |
| 168 | EXPECT_EQ(bucketSizeNs - 1, buckets[eventKey][0].mDuration); |
| 169 | EXPECT_EQ(bucketSizeNs, buckets[eventKey][1].mDuration); |
| 170 | EXPECT_EQ(5ULL, buckets[eventKey][2].mDuration); |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 171 | } |
| 172 | |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 173 | TEST(MaxDurationTrackerTest, TestMaxDurationWithCondition) { |
| 174 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 175 | |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 176 | ConditionKey conditionKey1; |
| 177 | HashableDimensionKey eventKey = getMockedDimensionKey(2, "maps"); |
| 178 | conditionKey1["APP_BACKGROUND"] = conditionKey; |
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 | EXPECT_CALL(*wizard, query(_, conditionKey1)) // #4 |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 181 | .WillOnce(Return(ConditionState::kFalse)); |
| 182 | |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 183 | unordered_map<HashableDimensionKey, vector<DurationBucket>> buckets; |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 184 | |
| 185 | uint64_t bucketStartTimeNs = 10000000000; |
| 186 | uint64_t eventStartTimeNs = bucketStartTimeNs + 1; |
| 187 | uint64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL; |
| 188 | int64_t durationTimeNs = 2 * 1000; |
| 189 | |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 190 | MaxDurationTracker tracker(kConfigKey, "metric", eventKey, wizard, 1, false, bucketStartTimeNs, |
| 191 | bucketSizeNs, {}); |
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) { |
| 207 | Alert alert; |
| 208 | alert.set_name("alert"); |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 209 | alert.set_metric_name("metric"); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 210 | alert.set_trigger_if_sum_gt(32 * NS_PER_SEC); |
| 211 | alert.set_number_of_buckets(2); |
| 212 | alert.set_refractory_period_secs(1); |
| 213 | |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 214 | unordered_map<HashableDimensionKey, vector<DurationBucket>> buckets; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 215 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 216 | ConditionKey conditionKey1; |
| 217 | conditionKey1["APP_BACKGROUND"] = conditionKey; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 218 | uint64_t bucketStartTimeNs = 10 * NS_PER_SEC; |
| 219 | uint64_t eventStartTimeNs = bucketStartTimeNs + NS_PER_SEC + 1; |
| 220 | uint64_t bucketSizeNs = 30 * NS_PER_SEC; |
| 221 | |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame^] | 222 | sp<DurationAnomalyTracker> anomalyTracker = new DurationAnomalyTracker(alert, kConfigKey); |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 223 | MaxDurationTracker tracker(kConfigKey, "metric", eventKey, wizard, -1, true, bucketStartTimeNs, |
| 224 | bucketSizeNs, {anomalyTracker}); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 225 | |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 226 | tracker.noteStart(key1, true, eventStartTimeNs, conditionKey1); |
| 227 | tracker.noteStop(key1, eventStartTimeNs + 10, false); |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame^] | 228 | EXPECT_EQ(anomalyTracker->mLastAnomalyTimestampNs, -1); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 229 | EXPECT_EQ(10LL, tracker.mDuration); |
| 230 | |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 231 | tracker.noteStart(key2, true, eventStartTimeNs + 20, conditionKey1); |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 232 | tracker.flushIfNeeded(eventStartTimeNs + 2 * bucketSizeNs + 3 * NS_PER_SEC, &buckets); |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 233 | tracker.noteStop(key2, eventStartTimeNs + 2 * bucketSizeNs + 3 * NS_PER_SEC, false); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 234 | EXPECT_EQ((long long)(4 * NS_PER_SEC + 1LL), tracker.mDuration); |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame^] | 235 | EXPECT_EQ(anomalyTracker->mLastAnomalyTimestampNs, |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 236 | (long long)(eventStartTimeNs + 2 * bucketSizeNs + 3 * NS_PER_SEC)); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 239 | } // namespace statsd |
| 240 | } // namespace os |
| 241 | } // namespace android |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 242 | #else |
| 243 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 244 | #endif |