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 | |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 44 | const HashableDimensionKey eventKey = getMockedDimensionKey(TagId, 0, "1"); |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 45 | const HashableDimensionKey conditionKey = getMockedDimensionKey(TagId, 4, "1"); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 46 | const HashableDimensionKey key1 = getMockedDimensionKey(TagId, 1, "1"); |
| 47 | const HashableDimensionKey key2 = getMockedDimensionKey(TagId, 1, "2"); |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 48 | const int64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL; |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 49 | |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 50 | TEST(MaxDurationTrackerTest, TestSimpleMaxDuration) { |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 51 | const MetricDimensionKey eventKey = getMockedMetricDimensionKey(TagId, 0, "1"); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 52 | const HashableDimensionKey key1 = getMockedDimensionKey(TagId, 1, "1"); |
| 53 | const HashableDimensionKey key2 = getMockedDimensionKey(TagId, 1, "2"); |
| 54 | |
Yao Chen | 8a8d16c | 2018-02-08 14:50:40 -0800 | [diff] [blame] | 55 | |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 56 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 57 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 58 | unordered_map<MetricDimensionKey, vector<DurationBucket>> buckets; |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 59 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 60 | int64_t bucketStartTimeNs = 10000000000; |
| 61 | int64_t bucketEndTimeNs = bucketStartTimeNs + bucketSizeNs; |
| 62 | int64_t bucketNum = 0; |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 63 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 64 | int64_t metricId = 1; |
tsaichristine | 1449fa4 | 2020-01-02 12:12:05 -0800 | [diff] [blame] | 65 | MaxDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, -1, false, bucketStartTimeNs, |
| 66 | bucketNum, bucketStartTimeNs, bucketSizeNs, false, false, {}); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 67 | |
Yao Chen | d59a658 | 2018-01-08 11:17:11 -0800 | [diff] [blame] | 68 | tracker.noteStart(key1, true, bucketStartTimeNs, ConditionKey()); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 69 | // Event starts again. This would not change anything as it already starts. |
Yao Chen | d59a658 | 2018-01-08 11:17:11 -0800 | [diff] [blame] | 70 | tracker.noteStart(key1, true, bucketStartTimeNs + 3, ConditionKey()); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 71 | // Stopped. |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 72 | tracker.noteStop(key1, bucketStartTimeNs + 10, false); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 73 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 74 | // Another event starts in this bucket. |
Yao Chen | d59a658 | 2018-01-08 11:17:11 -0800 | [diff] [blame] | 75 | tracker.noteStart(key2, true, bucketStartTimeNs + 20, ConditionKey()); |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 76 | tracker.noteStop(key2, bucketStartTimeNs + 40, false /*stop all*/); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 77 | |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 78 | tracker.flushIfNeeded(bucketStartTimeNs + bucketSizeNs + 1, &buckets); |
| 79 | EXPECT_TRUE(buckets.find(eventKey) != buckets.end()); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 80 | ASSERT_EQ(1u, buckets[eventKey].size()); |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 81 | EXPECT_EQ(20LL, buckets[eventKey][0].mDuration); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | TEST(MaxDurationTrackerTest, TestStopAll) { |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 85 | const MetricDimensionKey eventKey = getMockedMetricDimensionKey(TagId, 0, "1"); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 86 | const HashableDimensionKey key1 = getMockedDimensionKey(TagId, 1, "1"); |
| 87 | const HashableDimensionKey key2 = getMockedDimensionKey(TagId, 1, "2"); |
| 88 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 89 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 90 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 91 | unordered_map<MetricDimensionKey, vector<DurationBucket>> buckets; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 92 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 93 | int64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL; |
| 94 | int64_t bucketStartTimeNs = 10000000000; |
| 95 | int64_t bucketEndTimeNs = bucketStartTimeNs + bucketSizeNs; |
| 96 | int64_t bucketNum = 0; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 97 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 98 | int64_t metricId = 1; |
tsaichristine | 1449fa4 | 2020-01-02 12:12:05 -0800 | [diff] [blame] | 99 | MaxDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, -1, false, bucketStartTimeNs, |
| 100 | bucketNum, bucketStartTimeNs, bucketSizeNs, false, false, {}); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 101 | |
Yao Chen | d59a658 | 2018-01-08 11:17:11 -0800 | [diff] [blame] | 102 | tracker.noteStart(key1, true, bucketStartTimeNs + 1, ConditionKey()); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 103 | |
| 104 | // Another event starts in this bucket. |
Yao Chen | d59a658 | 2018-01-08 11:17:11 -0800 | [diff] [blame] | 105 | tracker.noteStart(key2, true, bucketStartTimeNs + 20, ConditionKey()); |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 106 | tracker.flushIfNeeded(bucketStartTimeNs + bucketSizeNs + 40, &buckets); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 107 | tracker.noteStopAll(bucketStartTimeNs + bucketSizeNs + 40); |
| 108 | EXPECT_TRUE(tracker.mInfos.empty()); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 109 | EXPECT_TRUE(buckets.find(eventKey) == buckets.end()); |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 110 | |
| 111 | tracker.flushIfNeeded(bucketStartTimeNs + 3 * bucketSizeNs + 40, &buckets); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 112 | EXPECT_TRUE(buckets.find(eventKey) != buckets.end()); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 113 | ASSERT_EQ(1u, buckets[eventKey].size()); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 114 | EXPECT_EQ(bucketSizeNs + 40 - 1, buckets[eventKey][0].mDuration); |
| 115 | EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, buckets[eventKey][0].mBucketStartNs); |
| 116 | EXPECT_EQ(bucketStartTimeNs + 2 * bucketSizeNs, buckets[eventKey][0].mBucketEndNs); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | TEST(MaxDurationTrackerTest, TestCrossBucketBoundary) { |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 120 | const MetricDimensionKey eventKey = getMockedMetricDimensionKey(TagId, 0, "1"); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 121 | const HashableDimensionKey key1 = getMockedDimensionKey(TagId, 1, "1"); |
| 122 | const HashableDimensionKey key2 = getMockedDimensionKey(TagId, 1, "2"); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 123 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 124 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 125 | unordered_map<MetricDimensionKey, vector<DurationBucket>> buckets; |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 126 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 127 | int64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL; |
| 128 | int64_t bucketStartTimeNs = 10000000000; |
| 129 | int64_t bucketEndTimeNs = bucketStartTimeNs + bucketSizeNs; |
| 130 | int64_t bucketNum = 0; |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 131 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 132 | int64_t metricId = 1; |
tsaichristine | 1449fa4 | 2020-01-02 12:12:05 -0800 | [diff] [blame] | 133 | MaxDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, -1, false, bucketStartTimeNs, |
| 134 | bucketNum, bucketStartTimeNs, bucketSizeNs, false, false, {}); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 135 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 136 | // The event starts. |
Yao Chen | d59a658 | 2018-01-08 11:17:11 -0800 | [diff] [blame] | 137 | tracker.noteStart(DEFAULT_DIMENSION_KEY, true, bucketStartTimeNs + 1, ConditionKey()); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 138 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 139 | // Starts again. Does not DEFAULT_DIMENSION_KEY anything. |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 140 | tracker.noteStart(DEFAULT_DIMENSION_KEY, true, bucketStartTimeNs + bucketSizeNs + 1, |
Yao Chen | d59a658 | 2018-01-08 11:17:11 -0800 | [diff] [blame] | 141 | ConditionKey()); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 142 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 143 | // The event stops at early 4th bucket. |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 144 | // Notestop is called from DurationMetricProducer's onMatchedLogEvent, which calls |
| 145 | // flushIfneeded. |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 146 | tracker.flushIfNeeded(bucketStartTimeNs + (3 * bucketSizeNs) + 20, &buckets); |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 147 | tracker.noteStop(DEFAULT_DIMENSION_KEY, bucketStartTimeNs + (3 * bucketSizeNs) + 20, |
| 148 | false /*stop all*/); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 149 | EXPECT_TRUE(buckets.find(eventKey) == buckets.end()); |
| 150 | |
| 151 | tracker.flushIfNeeded(bucketStartTimeNs + 4 * bucketSizeNs, &buckets); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 152 | ASSERT_EQ(1u, buckets[eventKey].size()); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 153 | EXPECT_EQ((3 * bucketSizeNs) + 20 - 1, buckets[eventKey][0].mDuration); |
| 154 | EXPECT_EQ(bucketStartTimeNs + 3 * bucketSizeNs, buckets[eventKey][0].mBucketStartNs); |
| 155 | EXPECT_EQ(bucketStartTimeNs + 4 * bucketSizeNs, buckets[eventKey][0].mBucketEndNs); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 158 | TEST(MaxDurationTrackerTest, TestCrossBucketBoundary_nested) { |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 159 | const MetricDimensionKey eventKey = getMockedMetricDimensionKey(TagId, 0, "1"); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 160 | const HashableDimensionKey key1 = getMockedDimensionKey(TagId, 1, "1"); |
| 161 | const HashableDimensionKey key2 = getMockedDimensionKey(TagId, 1, "2"); |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 162 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 163 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 164 | unordered_map<MetricDimensionKey, vector<DurationBucket>> buckets; |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 165 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 166 | int64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL; |
| 167 | int64_t bucketStartTimeNs = 10000000000; |
| 168 | int64_t bucketEndTimeNs = bucketStartTimeNs + bucketSizeNs; |
| 169 | int64_t bucketNum = 0; |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 170 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 171 | int64_t metricId = 1; |
tsaichristine | 1449fa4 | 2020-01-02 12:12:05 -0800 | [diff] [blame] | 172 | MaxDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, -1, true, bucketStartTimeNs, |
| 173 | bucketNum, bucketStartTimeNs, bucketSizeNs, false, false, {}); |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 174 | |
| 175 | // 2 starts |
Yao Chen | d59a658 | 2018-01-08 11:17:11 -0800 | [diff] [blame] | 176 | tracker.noteStart(DEFAULT_DIMENSION_KEY, true, bucketStartTimeNs + 1, ConditionKey()); |
| 177 | tracker.noteStart(DEFAULT_DIMENSION_KEY, true, bucketStartTimeNs + 10, ConditionKey()); |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 178 | // one stop |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 179 | tracker.noteStop(DEFAULT_DIMENSION_KEY, bucketStartTimeNs + 20, false /*stop all*/); |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 180 | |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 181 | tracker.flushIfNeeded(bucketStartTimeNs + (2 * bucketSizeNs) + 1, &buckets); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 182 | // Because of nesting, still not stopped. |
| 183 | EXPECT_TRUE(buckets.find(eventKey) == buckets.end()); |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 184 | |
| 185 | // real stop now. |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 186 | tracker.noteStop(DEFAULT_DIMENSION_KEY, |
| 187 | bucketStartTimeNs + (2 * bucketSizeNs) + 5, false); |
Yao Chen | f60e0ba | 2017-11-29 15:06:41 -0800 | [diff] [blame] | 188 | tracker.flushIfNeeded(bucketStartTimeNs + (3 * bucketSizeNs) + 1, &buckets); |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 189 | |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 190 | ASSERT_EQ(1u, buckets[eventKey].size()); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 191 | EXPECT_EQ(2 * bucketSizeNs + 5 - 1, buckets[eventKey][0].mDuration); |
Yao Chen | 0ea1990 | 2017-11-15 15:44:45 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 194 | TEST(MaxDurationTrackerTest, TestMaxDurationWithCondition) { |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 195 | const HashableDimensionKey conditionDimKey = key1; |
David Chen | 2e414b9 | 2018-02-12 17:24:40 -0800 | [diff] [blame] | 196 | |
David Chen | 2e414b9 | 2018-02-12 17:24:40 -0800 | [diff] [blame] | 197 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 198 | |
| 199 | ConditionKey conditionKey1; |
| 200 | MetricDimensionKey eventKey = getMockedMetricDimensionKey(TagId, 1, "1"); |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 201 | conditionKey1[StringToId("APP_BACKGROUND")] = conditionDimKey; |
David Chen | 2e414b9 | 2018-02-12 17:24:40 -0800 | [diff] [blame] | 202 | |
| 203 | /** |
| 204 | Start in first bucket, stop in second bucket. Condition turns on and off in the first bucket |
| 205 | and again turns on and off in the second bucket. |
| 206 | */ |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 207 | int64_t bucketStartTimeNs = 10000000000; |
| 208 | int64_t bucketEndTimeNs = bucketStartTimeNs + bucketSizeNs; |
| 209 | int64_t eventStartTimeNs = bucketStartTimeNs + 1 * NS_PER_SEC; |
| 210 | int64_t conditionStarts1 = bucketStartTimeNs + 11 * NS_PER_SEC; |
| 211 | int64_t conditionStops1 = bucketStartTimeNs + 14 * NS_PER_SEC; |
| 212 | int64_t conditionStarts2 = bucketStartTimeNs + bucketSizeNs + 5 * NS_PER_SEC; |
| 213 | int64_t conditionStops2 = conditionStarts2 + 10 * NS_PER_SEC; |
| 214 | int64_t eventStopTimeNs = conditionStops2 + 8 * NS_PER_SEC; |
David Chen | 2e414b9 | 2018-02-12 17:24:40 -0800 | [diff] [blame] | 215 | |
| 216 | int64_t metricId = 1; |
tsaichristine | 1449fa4 | 2020-01-02 12:12:05 -0800 | [diff] [blame] | 217 | MaxDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, 1, false, bucketStartTimeNs, |
| 218 | 0, bucketStartTimeNs, bucketSizeNs, true, false, {}); |
David Chen | 2e414b9 | 2018-02-12 17:24:40 -0800 | [diff] [blame] | 219 | EXPECT_TRUE(tracker.mAnomalyTrackers.empty()); |
| 220 | |
| 221 | tracker.noteStart(key1, false, eventStartTimeNs, conditionKey1); |
| 222 | tracker.noteConditionChanged(key1, true, conditionStarts1); |
| 223 | tracker.noteConditionChanged(key1, false, conditionStops1); |
| 224 | unordered_map<MetricDimensionKey, vector<DurationBucket>> buckets; |
| 225 | tracker.flushIfNeeded(bucketStartTimeNs + bucketSizeNs + 1, &buckets); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 226 | ASSERT_EQ(0U, buckets.size()); |
David Chen | 2e414b9 | 2018-02-12 17:24:40 -0800 | [diff] [blame] | 227 | |
| 228 | tracker.noteConditionChanged(key1, true, conditionStarts2); |
| 229 | tracker.noteConditionChanged(key1, false, conditionStops2); |
| 230 | tracker.noteStop(key1, eventStopTimeNs, false); |
| 231 | tracker.flushIfNeeded(bucketStartTimeNs + 2 * bucketSizeNs + 1, &buckets); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 232 | ASSERT_EQ(1U, buckets.size()); |
David Chen | 2e414b9 | 2018-02-12 17:24:40 -0800 | [diff] [blame] | 233 | vector<DurationBucket> item = buckets.begin()->second; |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 234 | ASSERT_EQ(1UL, item.size()); |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 235 | EXPECT_EQ((int64_t)(13LL * NS_PER_SEC), item[0].mDuration); |
David Chen | 2e414b9 | 2018-02-12 17:24:40 -0800 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | TEST(MaxDurationTrackerTest, TestAnomalyDetection) { |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 239 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 240 | |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 241 | ConditionKey conditionKey1; |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 242 | MetricDimensionKey eventKey = getMockedMetricDimensionKey(TagId, 2, "maps"); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 243 | conditionKey1[StringToId("APP_BACKGROUND")] = conditionKey; |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 244 | |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 245 | unordered_map<MetricDimensionKey, vector<DurationBucket>> buckets; |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 246 | |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 247 | int64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL; |
| 248 | int64_t bucketStartTimeNs = 10000000000; |
| 249 | int64_t bucketEndTimeNs = bucketStartTimeNs + bucketSizeNs; |
| 250 | int64_t bucketNum = 0; |
| 251 | int64_t eventStartTimeNs = 13000000000; |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 252 | int64_t durationTimeNs = 2 * 1000; |
| 253 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 254 | int64_t metricId = 1; |
David Chen | 2e414b9 | 2018-02-12 17:24:40 -0800 | [diff] [blame] | 255 | Alert alert; |
| 256 | alert.set_id(101); |
| 257 | alert.set_metric_id(1); |
| 258 | alert.set_trigger_if_sum_gt(40 * NS_PER_SEC); |
| 259 | alert.set_num_buckets(2); |
| 260 | const int32_t refPeriodSec = 45; |
| 261 | alert.set_refractory_period_secs(refPeriodSec); |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 262 | sp<AlarmMonitor> alarmMonitor; |
| 263 | sp<DurationAnomalyTracker> anomalyTracker = |
| 264 | new DurationAnomalyTracker(alert, kConfigKey, alarmMonitor); |
tsaichristine | 1449fa4 | 2020-01-02 12:12:05 -0800 | [diff] [blame] | 265 | MaxDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, 1, false, bucketStartTimeNs, |
| 266 | bucketNum, bucketStartTimeNs, bucketSizeNs, true, false, |
| 267 | {anomalyTracker}); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 268 | |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 269 | tracker.noteStart(key1, true, eventStartTimeNs, conditionKey1); |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 270 | sp<const InternalAlarm> alarm = anomalyTracker->mAlarms.begin()->second; |
David Chen | 2e414b9 | 2018-02-12 17:24:40 -0800 | [diff] [blame] | 271 | EXPECT_EQ((long long)(53ULL * NS_PER_SEC), (long long)(alarm->timestampSec * NS_PER_SEC)); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 272 | |
David Chen | 2e414b9 | 2018-02-12 17:24:40 -0800 | [diff] [blame] | 273 | // Remove the anomaly alarm when the duration is no longer fully met. |
| 274 | tracker.noteConditionChanged(key1, false, eventStartTimeNs + 15 * NS_PER_SEC); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 275 | ASSERT_EQ(0U, anomalyTracker->mAlarms.size()); |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 276 | |
David Chen | 2e414b9 | 2018-02-12 17:24:40 -0800 | [diff] [blame] | 277 | // Since the condition was off for 10 seconds, the anomaly should trigger 10 sec later. |
| 278 | tracker.noteConditionChanged(key1, true, eventStartTimeNs + 25 * NS_PER_SEC); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 279 | ASSERT_EQ(1U, anomalyTracker->mAlarms.size()); |
David Chen | 2e414b9 | 2018-02-12 17:24:40 -0800 | [diff] [blame] | 280 | alarm = anomalyTracker->mAlarms.begin()->second; |
| 281 | EXPECT_EQ((long long)(63ULL * NS_PER_SEC), (long long)(alarm->timestampSec * NS_PER_SEC)); |
| 282 | } |
Yao Chen | 09294ef | 2017-11-25 19:54:01 -0800 | [diff] [blame] | 283 | |
David Chen | 2e414b9 | 2018-02-12 17:24:40 -0800 | [diff] [blame] | 284 | // This tests that we correctly compute the predicted time of an anomaly assuming that the current |
| 285 | // state continues forward as-is. |
| 286 | TEST(MaxDurationTrackerTest, TestAnomalyPredictedTimestamp) { |
David Chen | 2e414b9 | 2018-02-12 17:24:40 -0800 | [diff] [blame] | 287 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 288 | |
| 289 | ConditionKey conditionKey1; |
| 290 | MetricDimensionKey eventKey = getMockedMetricDimensionKey(TagId, 2, "maps"); |
| 291 | conditionKey1[StringToId("APP_BACKGROUND")] = conditionKey; |
| 292 | ConditionKey conditionKey2; |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 293 | conditionKey2[StringToId("APP_BACKGROUND")] = getMockedDimensionKey(TagId, 4, "2"); |
David Chen | 2e414b9 | 2018-02-12 17:24:40 -0800 | [diff] [blame] | 294 | |
| 295 | unordered_map<MetricDimensionKey, vector<DurationBucket>> buckets; |
| 296 | |
| 297 | /** |
| 298 | * Suppose we have two sub-dimensions that we're taking the MAX over. In the first of these |
| 299 | * nested dimensions, we enter the pause state after 3 seconds. When we resume, the second |
| 300 | * dimension has already been running for 4 seconds. Thus, we have 40-4=36 seconds remaining |
| 301 | * before we trigger the anomaly. |
| 302 | */ |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 303 | int64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL; |
| 304 | int64_t bucketStartTimeNs = 10000000000; |
| 305 | int64_t bucketEndTimeNs = bucketStartTimeNs + bucketSizeNs; |
| 306 | int64_t bucketNum = 0; |
| 307 | int64_t eventStartTimeNs = bucketStartTimeNs + 5 * NS_PER_SEC; // Condition is off at start. |
| 308 | int64_t conditionStarts1 = bucketStartTimeNs + 11 * NS_PER_SEC; |
| 309 | int64_t conditionStops1 = bucketStartTimeNs + 14 * NS_PER_SEC; |
| 310 | int64_t conditionStarts2 = bucketStartTimeNs + 20 * NS_PER_SEC; |
| 311 | int64_t eventStartTimeNs2 = conditionStarts2 - 4 * NS_PER_SEC; |
David Chen | 2e414b9 | 2018-02-12 17:24:40 -0800 | [diff] [blame] | 312 | |
| 313 | int64_t metricId = 1; |
| 314 | Alert alert; |
| 315 | alert.set_id(101); |
| 316 | alert.set_metric_id(1); |
| 317 | alert.set_trigger_if_sum_gt(40 * NS_PER_SEC); |
| 318 | alert.set_num_buckets(2); |
| 319 | const int32_t refPeriodSec = 45; |
| 320 | alert.set_refractory_period_secs(refPeriodSec); |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 321 | sp<AlarmMonitor> alarmMonitor; |
| 322 | sp<DurationAnomalyTracker> anomalyTracker = |
| 323 | new DurationAnomalyTracker(alert, kConfigKey, alarmMonitor); |
tsaichristine | 1449fa4 | 2020-01-02 12:12:05 -0800 | [diff] [blame] | 324 | MaxDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, 1, false, bucketStartTimeNs, |
| 325 | bucketNum, bucketStartTimeNs, bucketSizeNs, true, false, |
| 326 | {anomalyTracker}); |
David Chen | 2e414b9 | 2018-02-12 17:24:40 -0800 | [diff] [blame] | 327 | |
| 328 | tracker.noteStart(key1, false, eventStartTimeNs, conditionKey1); |
| 329 | tracker.noteConditionChanged(key1, true, conditionStarts1); |
| 330 | tracker.noteConditionChanged(key1, false, conditionStops1); |
| 331 | tracker.noteStart(key2, true, eventStartTimeNs2, conditionKey2); // Condition is on already. |
| 332 | tracker.noteConditionChanged(key1, true, conditionStarts2); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 333 | ASSERT_EQ(1U, anomalyTracker->mAlarms.size()); |
David Chen | 2e414b9 | 2018-02-12 17:24:40 -0800 | [diff] [blame] | 334 | auto alarm = anomalyTracker->mAlarms.begin()->second; |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 335 | int64_t anomalyFireTimeSec = alarm->timestampSec; |
David Chen | 2e414b9 | 2018-02-12 17:24:40 -0800 | [diff] [blame] | 336 | EXPECT_EQ(conditionStarts2 + 36 * NS_PER_SEC, |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 337 | (long long)anomalyFireTimeSec * NS_PER_SEC); |
Bookatz | 0dbc7a4 | 2018-03-30 16:21:17 -0700 | [diff] [blame] | 338 | |
| 339 | // Now we test the calculation now that there's a refractory period. |
| 340 | // At the correct time, declare the anomaly. This will set a refractory period. Make sure it |
| 341 | // gets correctly taken into account in future predictAnomalyTimestampNs calculations. |
| 342 | std::unordered_set<sp<const InternalAlarm>, SpHash<InternalAlarm>> firedAlarms({alarm}); |
| 343 | anomalyTracker->informAlarmsFired(anomalyFireTimeSec * NS_PER_SEC, firedAlarms); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 344 | ASSERT_EQ(0u, anomalyTracker->mAlarms.size()); |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 345 | int64_t refractoryPeriodEndsSec = anomalyFireTimeSec + refPeriodSec; |
Bookatz | 0dbc7a4 | 2018-03-30 16:21:17 -0700 | [diff] [blame] | 346 | EXPECT_EQ(anomalyTracker->getRefractoryPeriodEndsSec(eventKey), refractoryPeriodEndsSec); |
| 347 | |
| 348 | // Now stop and start again. Make sure the new predictAnomalyTimestampNs takes into account |
| 349 | // the refractory period correctly. |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 350 | int64_t eventStopTimeNs = anomalyFireTimeSec * NS_PER_SEC + 10; |
Bookatz | 0dbc7a4 | 2018-03-30 16:21:17 -0700 | [diff] [blame] | 351 | tracker.noteStop(key1, eventStopTimeNs, false); |
| 352 | tracker.noteStop(key2, eventStopTimeNs, false); |
| 353 | tracker.noteStart(key1, true, eventStopTimeNs + 1000000, conditionKey1); |
| 354 | // Anomaly is ongoing, but we're still in the refractory period. |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 355 | ASSERT_EQ(1U, anomalyTracker->mAlarms.size()); |
Bookatz | 0dbc7a4 | 2018-03-30 16:21:17 -0700 | [diff] [blame] | 356 | alarm = anomalyTracker->mAlarms.begin()->second; |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 357 | EXPECT_EQ(refractoryPeriodEndsSec, (long long)(alarm->timestampSec)); |
Bookatz | 0dbc7a4 | 2018-03-30 16:21:17 -0700 | [diff] [blame] | 358 | |
| 359 | // Makes sure it is correct after the refractory period is over. |
| 360 | tracker.noteStop(key1, eventStopTimeNs + 2000000, false); |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 361 | int64_t justBeforeRefPeriodNs = (refractoryPeriodEndsSec - 2) * NS_PER_SEC; |
Bookatz | 0dbc7a4 | 2018-03-30 16:21:17 -0700 | [diff] [blame] | 362 | tracker.noteStart(key1, true, justBeforeRefPeriodNs, conditionKey1); |
| 363 | alarm = anomalyTracker->mAlarms.begin()->second; |
| 364 | EXPECT_EQ(justBeforeRefPeriodNs + 40 * NS_PER_SEC, |
| 365 | (unsigned long long)(alarm->timestampSec * NS_PER_SEC)); |
David Chen | 2e414b9 | 2018-02-12 17:24:40 -0800 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | // Suppose that within one tracker there are two dimensions A and B. |
| 369 | // Suppose A starts, then B starts, and then A stops. We still need to set an anomaly based on the |
| 370 | // elapsed duration of B. |
| 371 | TEST(MaxDurationTrackerTest, TestAnomalyPredictedTimestamp_UpdatedOnStop) { |
David Chen | 2e414b9 | 2018-02-12 17:24:40 -0800 | [diff] [blame] | 372 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 373 | |
| 374 | ConditionKey conditionKey1; |
| 375 | MetricDimensionKey eventKey = getMockedMetricDimensionKey(TagId, 2, "maps"); |
| 376 | conditionKey1[StringToId("APP_BACKGROUND")] = conditionKey; |
| 377 | ConditionKey conditionKey2; |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 378 | conditionKey2[StringToId("APP_BACKGROUND")] = getMockedDimensionKey(TagId, 4, "2"); |
David Chen | 2e414b9 | 2018-02-12 17:24:40 -0800 | [diff] [blame] | 379 | |
| 380 | unordered_map<MetricDimensionKey, vector<DurationBucket>> buckets; |
| 381 | |
| 382 | /** |
| 383 | * Suppose we have two sub-dimensions that we're taking the MAX over. In the first of these |
| 384 | * nested dimensions, are started for 8 seconds. When we stop, the other nested dimension has |
| 385 | * been started for 5 seconds. So we can only allow 35 more seconds from now. |
| 386 | */ |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 387 | int64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL; |
| 388 | int64_t bucketStartTimeNs = 10000000000; |
| 389 | int64_t bucketEndTimeNs = bucketStartTimeNs + bucketSizeNs; |
| 390 | int64_t bucketNum = 0; |
| 391 | int64_t eventStartTimeNs1 = bucketStartTimeNs + 5 * NS_PER_SEC; // Condition is off at start. |
| 392 | int64_t eventStopTimeNs1 = bucketStartTimeNs + 13 * NS_PER_SEC; |
| 393 | int64_t eventStartTimeNs2 = bucketStartTimeNs + 8 * NS_PER_SEC; |
David Chen | 2e414b9 | 2018-02-12 17:24:40 -0800 | [diff] [blame] | 394 | |
| 395 | int64_t metricId = 1; |
| 396 | Alert alert; |
| 397 | alert.set_id(101); |
| 398 | alert.set_metric_id(1); |
| 399 | alert.set_trigger_if_sum_gt(40 * NS_PER_SEC); |
| 400 | alert.set_num_buckets(2); |
| 401 | const int32_t refPeriodSec = 45; |
| 402 | alert.set_refractory_period_secs(refPeriodSec); |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 403 | sp<AlarmMonitor> alarmMonitor; |
| 404 | sp<DurationAnomalyTracker> anomalyTracker = |
| 405 | new DurationAnomalyTracker(alert, kConfigKey, alarmMonitor); |
tsaichristine | 1449fa4 | 2020-01-02 12:12:05 -0800 | [diff] [blame] | 406 | MaxDurationTracker tracker(kConfigKey, metricId, eventKey, wizard, 1, false, bucketStartTimeNs, |
| 407 | bucketNum, bucketStartTimeNs, bucketSizeNs, true, false, |
| 408 | {anomalyTracker}); |
David Chen | 2e414b9 | 2018-02-12 17:24:40 -0800 | [diff] [blame] | 409 | |
| 410 | tracker.noteStart(key1, true, eventStartTimeNs1, conditionKey1); |
| 411 | tracker.noteStart(key2, true, eventStartTimeNs2, conditionKey2); |
| 412 | tracker.noteStop(key1, eventStopTimeNs1, false); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 413 | ASSERT_EQ(1U, anomalyTracker->mAlarms.size()); |
David Chen | 2e414b9 | 2018-02-12 17:24:40 -0800 | [diff] [blame] | 414 | auto alarm = anomalyTracker->mAlarms.begin()->second; |
| 415 | EXPECT_EQ(eventStopTimeNs1 + 35 * NS_PER_SEC, |
| 416 | (unsigned long long)(alarm->timestampSec * NS_PER_SEC)); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 417 | } |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 418 | |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 419 | } // namespace statsd |
| 420 | } // namespace os |
| 421 | } // namespace android |
Yao Chen | 5154a37 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 422 | #else |
| 423 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
tsaichristine | 7685337 | 2019-08-06 17:17:03 -0700 | [diff] [blame] | 424 | #endif |