Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [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 | |
| 15 | #include "src/metrics/DurationMetricProducer.h" |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 16 | |
| 17 | #include <gmock/gmock.h> |
| 18 | #include <gtest/gtest.h> |
| 19 | #include <stdio.h> |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 20 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 21 | #include <set> |
| 22 | #include <unordered_map> |
| 23 | #include <vector> |
| 24 | |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 25 | #include "metrics_test_helper.h" |
| 26 | #include "src/condition/ConditionWizard.h" |
| 27 | #include "src/stats_log_util.h" |
| 28 | #include "stats_event.h" |
tsaichristine | 8dca82e | 2020-04-07 09:40:03 -0700 | [diff] [blame] | 29 | #include "tests/statsd_test_util.h" |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 30 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 31 | using namespace android::os::statsd; |
| 32 | using namespace testing; |
| 33 | using android::sp; |
| 34 | using std::set; |
| 35 | using std::unordered_map; |
| 36 | using std::vector; |
| 37 | |
| 38 | #ifdef __ANDROID__ |
| 39 | |
| 40 | namespace android { |
| 41 | namespace os { |
| 42 | namespace statsd { |
| 43 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 44 | |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 45 | namespace { |
| 46 | |
Tej Singh | e678cb7 | 2020-04-14 16:23:30 -0700 | [diff] [blame] | 47 | const ConfigKey kConfigKey(0, 12345); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 48 | void makeLogEvent(LogEvent* logEvent, int64_t timestampNs, int atomId) { |
| 49 | AStatsEvent* statsEvent = AStatsEvent_obtain(); |
| 50 | AStatsEvent_setAtomId(statsEvent, atomId); |
| 51 | AStatsEvent_overwriteTimestamp(statsEvent, timestampNs); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 52 | |
tsaichristine | 8dca82e | 2020-04-07 09:40:03 -0700 | [diff] [blame] | 53 | parseStatsEventToLogEvent(statsEvent, logEvent); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | } // namespace |
| 57 | |
Tej Singh | e678cb7 | 2020-04-14 16:23:30 -0700 | [diff] [blame] | 58 | // Setup for parameterized tests. |
| 59 | class DurationMetricProducerTest_PartialBucket : public TestWithParam<BucketSplitEvent> {}; |
| 60 | |
| 61 | INSTANTIATE_TEST_SUITE_P(DurationMetricProducerTest_PartialBucket, |
| 62 | DurationMetricProducerTest_PartialBucket, |
| 63 | testing::Values(APP_UPGRADE, BOOT_COMPLETE)); |
| 64 | |
Chenjie Yu | e1361ed | 2018-07-23 17:33:09 -0700 | [diff] [blame] | 65 | TEST(DurationMetricTrackerTest, TestFirstBucket) { |
| 66 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 67 | DurationMetric metric; |
| 68 | metric.set_id(1); |
| 69 | metric.set_bucket(ONE_MINUTE); |
| 70 | metric.set_aggregation_type(DurationMetric_AggregationType_SUM); |
| 71 | |
| 72 | FieldMatcher dimensions; |
| 73 | DurationMetricProducer durationProducer( |
| 74 | kConfigKey, metric, -1 /*no condition*/, 1 /* start index */, 2 /* stop index */, |
| 75 | 3 /* stop_all index */, false /*nesting*/, wizard, dimensions, 5, 600 * NS_PER_SEC + NS_PER_SEC/2); |
| 76 | |
| 77 | EXPECT_EQ(600500000000, durationProducer.mCurrentBucketStartTimeNs); |
| 78 | EXPECT_EQ(10, durationProducer.mCurrentBucketNum); |
| 79 | EXPECT_EQ(660000000005, durationProducer.getCurrentBucketEndTimeNs()); |
| 80 | } |
| 81 | |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 82 | TEST(DurationMetricTrackerTest, TestNoCondition) { |
| 83 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 84 | int64_t bucketStartTimeNs = 10000000000; |
| 85 | int64_t bucketSizeNs = TimeUnitToBucketSizeInMillis(ONE_MINUTE) * 1000000LL; |
| 86 | |
| 87 | DurationMetric metric; |
| 88 | metric.set_id(1); |
| 89 | metric.set_bucket(ONE_MINUTE); |
| 90 | metric.set_aggregation_type(DurationMetric_AggregationType_SUM); |
| 91 | |
| 92 | int tagId = 1; |
| 93 | LogEvent event1(/*uid=*/0, /*pid=*/0); |
| 94 | makeLogEvent(&event1, bucketStartTimeNs + 1, tagId); |
| 95 | LogEvent event2(/*uid=*/0, /*pid=*/0); |
| 96 | makeLogEvent(&event2, bucketStartTimeNs + bucketSizeNs + 2, tagId); |
| 97 | |
| 98 | FieldMatcher dimensions; |
| 99 | DurationMetricProducer durationProducer(kConfigKey, metric, -1 /*no condition*/, |
| 100 | 1 /* start index */, 2 /* stop index */, |
| 101 | 3 /* stop_all index */, false /*nesting*/, wizard, |
| 102 | dimensions, bucketStartTimeNs, bucketStartTimeNs); |
| 103 | |
| 104 | durationProducer.onMatchedLogEvent(1 /* start index*/, event1); |
| 105 | durationProducer.onMatchedLogEvent(2 /* stop index*/, event2); |
| 106 | durationProducer.flushIfNeededLocked(bucketStartTimeNs + 2 * bucketSizeNs + 1); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 107 | ASSERT_EQ(1UL, durationProducer.mPastBuckets.size()); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 108 | EXPECT_TRUE(durationProducer.mPastBuckets.find(DEFAULT_METRIC_DIMENSION_KEY) != |
| 109 | durationProducer.mPastBuckets.end()); |
| 110 | const auto& buckets = durationProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY]; |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 111 | ASSERT_EQ(2UL, buckets.size()); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 112 | EXPECT_EQ(bucketStartTimeNs, buckets[0].mBucketStartNs); |
| 113 | EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, buckets[0].mBucketEndNs); |
| 114 | EXPECT_EQ(bucketSizeNs - 1LL, buckets[0].mDuration); |
| 115 | EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, buckets[1].mBucketStartNs); |
| 116 | EXPECT_EQ(bucketStartTimeNs + 2 * bucketSizeNs, buckets[1].mBucketEndNs); |
| 117 | EXPECT_EQ(2LL, buckets[1].mDuration); |
| 118 | } |
| 119 | |
| 120 | TEST(DurationMetricTrackerTest, TestNonSlicedCondition) { |
| 121 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 122 | int64_t bucketStartTimeNs = 10000000000; |
| 123 | int64_t bucketSizeNs = TimeUnitToBucketSizeInMillis(ONE_MINUTE) * 1000000LL; |
| 124 | |
| 125 | DurationMetric metric; |
| 126 | metric.set_id(1); |
| 127 | metric.set_bucket(ONE_MINUTE); |
| 128 | metric.set_aggregation_type(DurationMetric_AggregationType_SUM); |
| 129 | |
| 130 | int tagId = 1; |
| 131 | LogEvent event1(/*uid=*/0, /*pid=*/0); |
| 132 | makeLogEvent(&event1, bucketStartTimeNs + 1, tagId); |
| 133 | LogEvent event2(/*uid=*/0, /*pid=*/0); |
| 134 | makeLogEvent(&event2, bucketStartTimeNs + 2, tagId); |
| 135 | LogEvent event3(/*uid=*/0, /*pid=*/0); |
| 136 | makeLogEvent(&event3, bucketStartTimeNs + bucketSizeNs + 1, tagId); |
| 137 | LogEvent event4(/*uid=*/0, /*pid=*/0); |
| 138 | makeLogEvent(&event4, bucketStartTimeNs + bucketSizeNs + 3, tagId); |
| 139 | |
| 140 | FieldMatcher dimensions; |
| 141 | DurationMetricProducer durationProducer(kConfigKey, metric, 0 /* condition index */, |
| 142 | 1 /* start index */, 2 /* stop index */, |
| 143 | 3 /* stop_all index */, false /*nesting*/, wizard, |
| 144 | dimensions, bucketStartTimeNs, bucketStartTimeNs); |
| 145 | durationProducer.mCondition = ConditionState::kFalse; |
| 146 | |
| 147 | EXPECT_FALSE(durationProducer.mCondition); |
| 148 | EXPECT_FALSE(durationProducer.isConditionSliced()); |
| 149 | |
| 150 | durationProducer.onMatchedLogEvent(1 /* start index*/, event1); |
| 151 | durationProducer.onMatchedLogEvent(2 /* stop index*/, event2); |
| 152 | durationProducer.flushIfNeededLocked(bucketStartTimeNs + bucketSizeNs + 1); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 153 | ASSERT_EQ(0UL, durationProducer.mPastBuckets.size()); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 154 | |
| 155 | durationProducer.onMatchedLogEvent(1 /* start index*/, event3); |
| 156 | durationProducer.onConditionChanged(true /* condition */, bucketStartTimeNs + bucketSizeNs + 2); |
| 157 | durationProducer.onMatchedLogEvent(2 /* stop index*/, event4); |
| 158 | durationProducer.flushIfNeededLocked(bucketStartTimeNs + 2 * bucketSizeNs + 1); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 159 | ASSERT_EQ(1UL, durationProducer.mPastBuckets.size()); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 160 | EXPECT_TRUE(durationProducer.mPastBuckets.find(DEFAULT_METRIC_DIMENSION_KEY) != |
| 161 | durationProducer.mPastBuckets.end()); |
| 162 | const auto& buckets2 = durationProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY]; |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 163 | ASSERT_EQ(1UL, buckets2.size()); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 164 | EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, buckets2[0].mBucketStartNs); |
| 165 | EXPECT_EQ(bucketStartTimeNs + 2 * bucketSizeNs, buckets2[0].mBucketEndNs); |
| 166 | EXPECT_EQ(1LL, buckets2[0].mDuration); |
| 167 | } |
| 168 | |
| 169 | TEST(DurationMetricTrackerTest, TestNonSlicedConditionUnknownState) { |
| 170 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 171 | int64_t bucketStartTimeNs = 10000000000; |
| 172 | int64_t bucketSizeNs = TimeUnitToBucketSizeInMillis(ONE_MINUTE) * 1000000LL; |
| 173 | |
| 174 | DurationMetric metric; |
| 175 | metric.set_id(1); |
| 176 | metric.set_bucket(ONE_MINUTE); |
| 177 | metric.set_aggregation_type(DurationMetric_AggregationType_SUM); |
| 178 | |
| 179 | int tagId = 1; |
| 180 | LogEvent event1(/*uid=*/0, /*pid=*/0); |
| 181 | makeLogEvent(&event1, bucketStartTimeNs + 1, tagId); |
| 182 | LogEvent event2(/*uid=*/0, /*pid=*/0); |
| 183 | makeLogEvent(&event2, bucketStartTimeNs + 2, tagId); |
| 184 | LogEvent event3(/*uid=*/0, /*pid=*/0); |
| 185 | makeLogEvent(&event3, bucketStartTimeNs + bucketSizeNs + 1, tagId); |
| 186 | LogEvent event4(/*uid=*/0, /*pid=*/0); |
| 187 | makeLogEvent(&event4, bucketStartTimeNs + bucketSizeNs + 3, tagId); |
| 188 | |
| 189 | FieldMatcher dimensions; |
| 190 | DurationMetricProducer durationProducer(kConfigKey, metric, 0 /* condition index */, |
| 191 | 1 /* start index */, 2 /* stop index */, |
| 192 | 3 /* stop_all index */, false /*nesting*/, wizard, |
| 193 | dimensions, bucketStartTimeNs, bucketStartTimeNs); |
| 194 | |
| 195 | EXPECT_EQ(ConditionState::kUnknown, durationProducer.mCondition); |
| 196 | EXPECT_FALSE(durationProducer.isConditionSliced()); |
| 197 | |
| 198 | durationProducer.onMatchedLogEvent(1 /* start index*/, event1); |
| 199 | durationProducer.onMatchedLogEvent(2 /* stop index*/, event2); |
| 200 | durationProducer.flushIfNeededLocked(bucketStartTimeNs + bucketSizeNs + 1); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 201 | ASSERT_EQ(0UL, durationProducer.mPastBuckets.size()); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 202 | |
| 203 | durationProducer.onMatchedLogEvent(1 /* start index*/, event3); |
| 204 | durationProducer.onConditionChanged(true /* condition */, bucketStartTimeNs + bucketSizeNs + 2); |
| 205 | durationProducer.onMatchedLogEvent(2 /* stop index*/, event4); |
| 206 | durationProducer.flushIfNeededLocked(bucketStartTimeNs + 2 * bucketSizeNs + 1); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 207 | ASSERT_EQ(1UL, durationProducer.mPastBuckets.size()); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 208 | const auto& buckets2 = durationProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY]; |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 209 | ASSERT_EQ(1UL, buckets2.size()); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 210 | EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, buckets2[0].mBucketStartNs); |
| 211 | EXPECT_EQ(bucketStartTimeNs + 2 * bucketSizeNs, buckets2[0].mBucketEndNs); |
| 212 | EXPECT_EQ(1LL, buckets2[0].mDuration); |
| 213 | } |
| 214 | |
Tej Singh | e678cb7 | 2020-04-14 16:23:30 -0700 | [diff] [blame] | 215 | TEST_P(DurationMetricProducerTest_PartialBucket, TestSumDuration) { |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 216 | /** |
| 217 | * The duration starts from the first bucket, through the two partial buckets (10-70sec), |
| 218 | * another bucket, and ends at the beginning of the next full bucket. |
| 219 | * Expected buckets: |
| 220 | * - [10,25]: 14 secs |
| 221 | * - [25,70]: All 45 secs |
| 222 | * - [70,130]: All 60 secs |
| 223 | * - [130, 210]: Only 5 secs (event ended at 135sec) |
| 224 | */ |
| 225 | int64_t bucketStartTimeNs = 10000000000; |
| 226 | int64_t bucketSizeNs = TimeUnitToBucketSizeInMillis(ONE_MINUTE) * 1000000LL; |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 227 | int tagId = 1; |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 228 | |
| 229 | DurationMetric metric; |
| 230 | metric.set_id(1); |
| 231 | metric.set_bucket(ONE_MINUTE); |
| 232 | metric.set_aggregation_type(DurationMetric_AggregationType_SUM); |
| 233 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 234 | FieldMatcher dimensions; |
| 235 | DurationMetricProducer durationProducer(kConfigKey, metric, -1 /* no condition */, |
| 236 | 1 /* start index */, 2 /* stop index */, |
| 237 | 3 /* stop_all index */, false /*nesting*/, wizard, |
| 238 | dimensions, bucketStartTimeNs, bucketStartTimeNs); |
| 239 | |
Tej Singh | e678cb7 | 2020-04-14 16:23:30 -0700 | [diff] [blame] | 240 | int64_t startTimeNs = bucketStartTimeNs + 1 * NS_PER_SEC; |
| 241 | LogEvent event1(/*uid=*/0, /*pid=*/0); |
| 242 | makeLogEvent(&event1, startTimeNs, tagId); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 243 | durationProducer.onMatchedLogEvent(1 /* start index*/, event1); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 244 | ASSERT_EQ(0UL, durationProducer.mPastBuckets.size()); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 245 | EXPECT_EQ(bucketStartTimeNs, durationProducer.mCurrentBucketStartTimeNs); |
| 246 | |
Tej Singh | e678cb7 | 2020-04-14 16:23:30 -0700 | [diff] [blame] | 247 | int64_t partialBucketSplitTimeNs = bucketStartTimeNs + 15 * NS_PER_SEC; |
| 248 | switch (GetParam()) { |
| 249 | case APP_UPGRADE: |
| 250 | durationProducer.notifyAppUpgrade(partialBucketSplitTimeNs); |
| 251 | break; |
| 252 | case BOOT_COMPLETE: |
| 253 | durationProducer.onStatsdInitCompleted(partialBucketSplitTimeNs); |
| 254 | break; |
| 255 | } |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 256 | ASSERT_EQ(1UL, durationProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY].size()); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 257 | std::vector<DurationBucket> buckets = |
| 258 | durationProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY]; |
| 259 | EXPECT_EQ(bucketStartTimeNs, buckets[0].mBucketStartNs); |
Tej Singh | e678cb7 | 2020-04-14 16:23:30 -0700 | [diff] [blame] | 260 | EXPECT_EQ(partialBucketSplitTimeNs, buckets[0].mBucketEndNs); |
| 261 | EXPECT_EQ(partialBucketSplitTimeNs - startTimeNs, buckets[0].mDuration); |
| 262 | EXPECT_EQ(partialBucketSplitTimeNs, durationProducer.mCurrentBucketStartTimeNs); |
| 263 | EXPECT_EQ(0, durationProducer.getCurrentBucketNum()); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 264 | |
| 265 | // We skip ahead one bucket, so we fill in the first two partial buckets and one full bucket. |
Tej Singh | e678cb7 | 2020-04-14 16:23:30 -0700 | [diff] [blame] | 266 | int64_t endTimeNs = startTimeNs + 125 * NS_PER_SEC; |
| 267 | LogEvent event2(/*uid=*/0, /*pid=*/0); |
| 268 | makeLogEvent(&event2, endTimeNs, tagId); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 269 | durationProducer.onMatchedLogEvent(2 /* stop index*/, event2); |
| 270 | buckets = durationProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY]; |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 271 | ASSERT_EQ(3UL, buckets.size()); |
Tej Singh | e678cb7 | 2020-04-14 16:23:30 -0700 | [diff] [blame] | 272 | EXPECT_EQ(partialBucketSplitTimeNs, buckets[1].mBucketStartNs); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 273 | EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, buckets[1].mBucketEndNs); |
Tej Singh | e678cb7 | 2020-04-14 16:23:30 -0700 | [diff] [blame] | 274 | EXPECT_EQ(bucketStartTimeNs + bucketSizeNs - partialBucketSplitTimeNs, buckets[1].mDuration); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 275 | EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, buckets[2].mBucketStartNs); |
| 276 | EXPECT_EQ(bucketStartTimeNs + 2 * bucketSizeNs, buckets[2].mBucketEndNs); |
| 277 | EXPECT_EQ(bucketSizeNs, buckets[2].mDuration); |
| 278 | } |
| 279 | |
Tej Singh | e678cb7 | 2020-04-14 16:23:30 -0700 | [diff] [blame] | 280 | TEST_P(DurationMetricProducerTest_PartialBucket, TestSumDurationWithSplitInFollowingBucket) { |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 281 | /** |
| 282 | * Expected buckets (start at 11s, upgrade at 75s, end at 135s): |
| 283 | * - [10,70]: 59 secs |
| 284 | * - [70,75]: 5 sec |
| 285 | * - [75,130]: 55 secs |
| 286 | */ |
| 287 | int64_t bucketStartTimeNs = 10000000000; |
| 288 | int64_t bucketSizeNs = TimeUnitToBucketSizeInMillis(ONE_MINUTE) * 1000000LL; |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 289 | int tagId = 1; |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 290 | |
| 291 | DurationMetric metric; |
| 292 | metric.set_id(1); |
| 293 | metric.set_bucket(ONE_MINUTE); |
| 294 | metric.set_aggregation_type(DurationMetric_AggregationType_SUM); |
| 295 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 296 | FieldMatcher dimensions; |
| 297 | DurationMetricProducer durationProducer(kConfigKey, metric, -1 /* no condition */, |
| 298 | 1 /* start index */, 2 /* stop index */, |
| 299 | 3 /* stop_all index */, false /*nesting*/, wizard, |
| 300 | dimensions, bucketStartTimeNs, bucketStartTimeNs); |
| 301 | |
Tej Singh | e678cb7 | 2020-04-14 16:23:30 -0700 | [diff] [blame] | 302 | int64_t startTimeNs = bucketStartTimeNs + 1 * NS_PER_SEC; |
| 303 | LogEvent event1(/*uid=*/0, /*pid=*/0); |
| 304 | makeLogEvent(&event1, startTimeNs, tagId); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 305 | durationProducer.onMatchedLogEvent(1 /* start index*/, event1); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 306 | ASSERT_EQ(0UL, durationProducer.mPastBuckets.size()); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 307 | EXPECT_EQ(bucketStartTimeNs, durationProducer.mCurrentBucketStartTimeNs); |
| 308 | |
Tej Singh | e678cb7 | 2020-04-14 16:23:30 -0700 | [diff] [blame] | 309 | int64_t partialBucketSplitTimeNs = bucketStartTimeNs + 65 * NS_PER_SEC; |
| 310 | switch (GetParam()) { |
| 311 | case APP_UPGRADE: |
| 312 | durationProducer.notifyAppUpgrade(partialBucketSplitTimeNs); |
| 313 | break; |
| 314 | case BOOT_COMPLETE: |
| 315 | durationProducer.onStatsdInitCompleted(partialBucketSplitTimeNs); |
| 316 | break; |
| 317 | } |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 318 | ASSERT_EQ(2UL, durationProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY].size()); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 319 | std::vector<DurationBucket> buckets = |
| 320 | durationProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY]; |
| 321 | EXPECT_EQ(bucketStartTimeNs, buckets[0].mBucketStartNs); |
| 322 | EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, buckets[0].mBucketEndNs); |
| 323 | EXPECT_EQ(bucketStartTimeNs + bucketSizeNs - startTimeNs, buckets[0].mDuration); |
| 324 | EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, buckets[1].mBucketStartNs); |
Tej Singh | e678cb7 | 2020-04-14 16:23:30 -0700 | [diff] [blame] | 325 | EXPECT_EQ(partialBucketSplitTimeNs, buckets[1].mBucketEndNs); |
| 326 | EXPECT_EQ(partialBucketSplitTimeNs - (bucketStartTimeNs + bucketSizeNs), buckets[1].mDuration); |
| 327 | EXPECT_EQ(partialBucketSplitTimeNs, durationProducer.mCurrentBucketStartTimeNs); |
| 328 | EXPECT_EQ(1, durationProducer.getCurrentBucketNum()); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 329 | |
| 330 | // We skip ahead one bucket, so we fill in the first two partial buckets and one full bucket. |
Tej Singh | e678cb7 | 2020-04-14 16:23:30 -0700 | [diff] [blame] | 331 | int64_t endTimeNs = startTimeNs + 125 * NS_PER_SEC; |
| 332 | LogEvent event2(/*uid=*/0, /*pid=*/0); |
| 333 | makeLogEvent(&event2, endTimeNs, tagId); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 334 | durationProducer.onMatchedLogEvent(2 /* stop index*/, event2); |
| 335 | buckets = durationProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY]; |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 336 | ASSERT_EQ(3UL, buckets.size()); |
Tej Singh | e678cb7 | 2020-04-14 16:23:30 -0700 | [diff] [blame] | 337 | EXPECT_EQ(partialBucketSplitTimeNs, buckets[2].mBucketStartNs); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 338 | EXPECT_EQ(bucketStartTimeNs + 2 * bucketSizeNs, buckets[2].mBucketEndNs); |
Tej Singh | e678cb7 | 2020-04-14 16:23:30 -0700 | [diff] [blame] | 339 | EXPECT_EQ(bucketStartTimeNs + 2 * bucketSizeNs - partialBucketSplitTimeNs, |
| 340 | buckets[2].mDuration); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 341 | } |
| 342 | |
Tej Singh | e678cb7 | 2020-04-14 16:23:30 -0700 | [diff] [blame] | 343 | TEST_P(DurationMetricProducerTest_PartialBucket, TestSumDurationAnomaly) { |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 344 | sp<AlarmMonitor> alarmMonitor; |
| 345 | int64_t bucketStartTimeNs = 10000000000; |
| 346 | int64_t bucketSizeNs = TimeUnitToBucketSizeInMillis(ONE_MINUTE) * 1000000LL; |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 347 | int tagId = 1; |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 348 | |
| 349 | // Setup metric with alert. |
| 350 | DurationMetric metric; |
| 351 | metric.set_id(1); |
| 352 | metric.set_bucket(ONE_MINUTE); |
| 353 | metric.set_aggregation_type(DurationMetric_AggregationType_SUM); |
| 354 | Alert alert; |
| 355 | alert.set_num_buckets(3); |
| 356 | alert.set_trigger_if_sum_gt(2); |
| 357 | |
| 358 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 359 | FieldMatcher dimensions; |
| 360 | DurationMetricProducer durationProducer(kConfigKey, metric, -1 /* no condition */, |
| 361 | 1 /* start index */, 2 /* stop index */, |
| 362 | 3 /* stop_all index */, false /*nesting*/, wizard, |
| 363 | dimensions, bucketStartTimeNs, bucketStartTimeNs); |
| 364 | |
| 365 | sp<AnomalyTracker> anomalyTracker = durationProducer.addAnomalyTracker(alert, alarmMonitor); |
| 366 | EXPECT_TRUE(anomalyTracker != nullptr); |
| 367 | |
Tej Singh | e678cb7 | 2020-04-14 16:23:30 -0700 | [diff] [blame] | 368 | int64_t startTimeNs = bucketStartTimeNs + 1; |
| 369 | LogEvent event1(/*uid=*/0, /*pid=*/0); |
| 370 | makeLogEvent(&event1, startTimeNs, tagId); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 371 | durationProducer.onMatchedLogEvent(1 /* start index*/, event1); |
Tej Singh | e678cb7 | 2020-04-14 16:23:30 -0700 | [diff] [blame] | 372 | |
| 373 | int64_t partialBucketSplitTimeNs = bucketStartTimeNs + 15 * NS_PER_SEC; |
| 374 | switch (GetParam()) { |
| 375 | case APP_UPGRADE: |
| 376 | durationProducer.notifyAppUpgrade(partialBucketSplitTimeNs); |
| 377 | break; |
| 378 | case BOOT_COMPLETE: |
| 379 | durationProducer.onStatsdInitCompleted(partialBucketSplitTimeNs); |
| 380 | break; |
| 381 | } |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 382 | |
| 383 | // We skip ahead one bucket, so we fill in the first two partial buckets and one full bucket. |
Tej Singh | e678cb7 | 2020-04-14 16:23:30 -0700 | [diff] [blame] | 384 | int64_t endTimeNs = startTimeNs + 65 * NS_PER_SEC; |
| 385 | LogEvent event2(/*uid=*/0, /*pid=*/0); |
| 386 | makeLogEvent(&event2, endTimeNs, tagId); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 387 | durationProducer.onMatchedLogEvent(2 /* stop index*/, event2); |
Tej Singh | e678cb7 | 2020-04-14 16:23:30 -0700 | [diff] [blame] | 388 | |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 389 | EXPECT_EQ(bucketStartTimeNs + bucketSizeNs - startTimeNs, |
| 390 | anomalyTracker->getSumOverPastBuckets(DEFAULT_METRIC_DIMENSION_KEY)); |
| 391 | } |
| 392 | |
Tej Singh | e678cb7 | 2020-04-14 16:23:30 -0700 | [diff] [blame] | 393 | TEST_P(DurationMetricProducerTest_PartialBucket, TestMaxDuration) { |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 394 | int64_t bucketStartTimeNs = 10000000000; |
| 395 | int64_t bucketSizeNs = TimeUnitToBucketSizeInMillis(ONE_MINUTE) * 1000000LL; |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 396 | int tagId = 1; |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 397 | |
| 398 | DurationMetric metric; |
| 399 | metric.set_id(1); |
| 400 | metric.set_bucket(ONE_MINUTE); |
| 401 | metric.set_aggregation_type(DurationMetric_AggregationType_MAX_SPARSE); |
| 402 | |
| 403 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 404 | FieldMatcher dimensions; |
| 405 | DurationMetricProducer durationProducer(kConfigKey, metric, -1 /* no condition */, |
| 406 | 1 /* start index */, 2 /* stop index */, |
| 407 | 3 /* stop_all index */, false /*nesting*/, wizard, |
| 408 | dimensions, bucketStartTimeNs, bucketStartTimeNs); |
| 409 | |
Tej Singh | e678cb7 | 2020-04-14 16:23:30 -0700 | [diff] [blame] | 410 | int64_t startTimeNs = bucketStartTimeNs + 1; |
| 411 | LogEvent event1(/*uid=*/0, /*pid=*/0); |
| 412 | makeLogEvent(&event1, startTimeNs, tagId); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 413 | durationProducer.onMatchedLogEvent(1 /* start index*/, event1); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 414 | ASSERT_EQ(0UL, durationProducer.mPastBuckets.size()); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 415 | EXPECT_EQ(bucketStartTimeNs, durationProducer.mCurrentBucketStartTimeNs); |
| 416 | |
Tej Singh | e678cb7 | 2020-04-14 16:23:30 -0700 | [diff] [blame] | 417 | int64_t partialBucketSplitTimeNs = bucketStartTimeNs + 15 * NS_PER_SEC; |
| 418 | switch (GetParam()) { |
| 419 | case APP_UPGRADE: |
| 420 | durationProducer.notifyAppUpgrade(partialBucketSplitTimeNs); |
| 421 | break; |
| 422 | case BOOT_COMPLETE: |
| 423 | durationProducer.onStatsdInitCompleted(partialBucketSplitTimeNs); |
| 424 | break; |
| 425 | } |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 426 | ASSERT_EQ(0UL, durationProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY].size()); |
Tej Singh | e678cb7 | 2020-04-14 16:23:30 -0700 | [diff] [blame] | 427 | EXPECT_EQ(partialBucketSplitTimeNs, durationProducer.mCurrentBucketStartTimeNs); |
| 428 | EXPECT_EQ(0, durationProducer.getCurrentBucketNum()); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 429 | |
| 430 | // We skip ahead one bucket, so we fill in the first two partial buckets and one full bucket. |
Tej Singh | e678cb7 | 2020-04-14 16:23:30 -0700 | [diff] [blame] | 431 | int64_t endTimeNs = startTimeNs + 125 * NS_PER_SEC; |
| 432 | LogEvent event2(/*uid=*/0, /*pid=*/0); |
| 433 | makeLogEvent(&event2, endTimeNs, tagId); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 434 | durationProducer.onMatchedLogEvent(2 /* stop index*/, event2); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 435 | ASSERT_EQ(0UL, durationProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY].size()); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 436 | |
| 437 | durationProducer.flushIfNeededLocked(bucketStartTimeNs + 3 * bucketSizeNs + 1); |
| 438 | std::vector<DurationBucket> buckets = |
| 439 | durationProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY]; |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 440 | ASSERT_EQ(1UL, buckets.size()); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 441 | EXPECT_EQ(bucketStartTimeNs + 2 * bucketSizeNs, buckets[0].mBucketStartNs); |
| 442 | EXPECT_EQ(bucketStartTimeNs + 3 * bucketSizeNs, buckets[0].mBucketEndNs); |
| 443 | EXPECT_EQ(endTimeNs - startTimeNs, buckets[0].mDuration); |
| 444 | } |
| 445 | |
Tej Singh | e678cb7 | 2020-04-14 16:23:30 -0700 | [diff] [blame] | 446 | TEST_P(DurationMetricProducerTest_PartialBucket, TestMaxDurationWithSplitInNextBucket) { |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 447 | int64_t bucketStartTimeNs = 10000000000; |
| 448 | int64_t bucketSizeNs = TimeUnitToBucketSizeInMillis(ONE_MINUTE) * 1000000LL; |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 449 | int tagId = 1; |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 450 | |
| 451 | DurationMetric metric; |
| 452 | metric.set_id(1); |
| 453 | metric.set_bucket(ONE_MINUTE); |
| 454 | metric.set_aggregation_type(DurationMetric_AggregationType_MAX_SPARSE); |
| 455 | |
| 456 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 457 | FieldMatcher dimensions; |
| 458 | DurationMetricProducer durationProducer(kConfigKey, metric, -1 /* no condition */, |
| 459 | 1 /* start index */, 2 /* stop index */, |
| 460 | 3 /* stop_all index */, false /*nesting*/, wizard, |
| 461 | dimensions, bucketStartTimeNs, bucketStartTimeNs); |
| 462 | |
Tej Singh | e678cb7 | 2020-04-14 16:23:30 -0700 | [diff] [blame] | 463 | int64_t startTimeNs = bucketStartTimeNs + 1; |
| 464 | LogEvent event1(/*uid=*/0, /*pid=*/0); |
| 465 | makeLogEvent(&event1, startTimeNs, tagId); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 466 | durationProducer.onMatchedLogEvent(1 /* start index*/, event1); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 467 | ASSERT_EQ(0UL, durationProducer.mPastBuckets.size()); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 468 | EXPECT_EQ(bucketStartTimeNs, durationProducer.mCurrentBucketStartTimeNs); |
| 469 | |
Tej Singh | e678cb7 | 2020-04-14 16:23:30 -0700 | [diff] [blame] | 470 | int64_t partialBucketSplitTimeNs = bucketStartTimeNs + 65 * NS_PER_SEC; |
| 471 | switch (GetParam()) { |
| 472 | case APP_UPGRADE: |
| 473 | durationProducer.notifyAppUpgrade(partialBucketSplitTimeNs); |
| 474 | break; |
| 475 | case BOOT_COMPLETE: |
| 476 | durationProducer.onStatsdInitCompleted(partialBucketSplitTimeNs); |
| 477 | break; |
| 478 | } |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 479 | ASSERT_EQ(0UL, durationProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY].size()); |
Tej Singh | e678cb7 | 2020-04-14 16:23:30 -0700 | [diff] [blame] | 480 | EXPECT_EQ(partialBucketSplitTimeNs, durationProducer.mCurrentBucketStartTimeNs); |
| 481 | EXPECT_EQ(1, durationProducer.getCurrentBucketNum()); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 482 | |
| 483 | // Stop occurs in the same partial bucket as created for the app upgrade. |
Tej Singh | e678cb7 | 2020-04-14 16:23:30 -0700 | [diff] [blame] | 484 | int64_t endTimeNs = startTimeNs + 115 * NS_PER_SEC; |
| 485 | LogEvent event2(/*uid=*/0, /*pid=*/0); |
| 486 | makeLogEvent(&event2, endTimeNs, tagId); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 487 | durationProducer.onMatchedLogEvent(2 /* stop index*/, event2); |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 488 | ASSERT_EQ(0UL, durationProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY].size()); |
Tej Singh | e678cb7 | 2020-04-14 16:23:30 -0700 | [diff] [blame] | 489 | EXPECT_EQ(partialBucketSplitTimeNs, durationProducer.mCurrentBucketStartTimeNs); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 490 | |
| 491 | durationProducer.flushIfNeededLocked(bucketStartTimeNs + 2 * bucketSizeNs + 1); |
| 492 | std::vector<DurationBucket> buckets = |
| 493 | durationProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY]; |
Muhammad Qureshi | dff78d6 | 2020-05-11 13:37:43 -0700 | [diff] [blame] | 494 | ASSERT_EQ(1UL, buckets.size()); |
Tej Singh | e678cb7 | 2020-04-14 16:23:30 -0700 | [diff] [blame] | 495 | EXPECT_EQ(partialBucketSplitTimeNs, buckets[0].mBucketStartNs); |
tsaichristine | 7747d37 | 2020-02-28 17:36:59 -0800 | [diff] [blame] | 496 | EXPECT_EQ(bucketStartTimeNs + 2 * bucketSizeNs, buckets[0].mBucketEndNs); |
| 497 | EXPECT_EQ(endTimeNs - startTimeNs, buckets[0].mDuration); |
| 498 | } |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 499 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 500 | } // namespace statsd |
| 501 | } // namespace os |
| 502 | } // namespace android |
| 503 | #else |
| 504 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 505 | #endif |