Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -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 | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 15 | #include "src/metrics/CountMetricProducer.h" |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 16 | #include "src/stats_log_util.h" |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 17 | #include "metrics_test_helper.h" |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 18 | #include "tests/statsd_test_util.h" |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 19 | |
| 20 | #include <gmock/gmock.h> |
| 21 | #include <gtest/gtest.h> |
| 22 | #include <stdio.h> |
| 23 | #include <vector> |
| 24 | |
| 25 | using namespace testing; |
| 26 | using android::sp; |
| 27 | using std::set; |
| 28 | using std::unordered_map; |
| 29 | using std::vector; |
| 30 | |
| 31 | #ifdef __ANDROID__ |
| 32 | |
| 33 | namespace android { |
| 34 | namespace os { |
| 35 | namespace statsd { |
| 36 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 37 | const ConfigKey kConfigKey(0, 12345); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 38 | |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 39 | TEST(CountMetricProducerTest, TestNonDimensionalEvents) { |
| 40 | int64_t bucketStartTimeNs = 10000000000; |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 41 | int64_t bucketSizeNs = TimeUnitToBucketSizeInMillis(ONE_MINUTE) * 1000000LL; |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 42 | int64_t bucket2StartTimeNs = bucketStartTimeNs + bucketSizeNs; |
| 43 | int64_t bucket3StartTimeNs = bucketStartTimeNs + 2 * bucketSizeNs; |
| 44 | int tagId = 1; |
| 45 | |
| 46 | CountMetric metric; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 47 | metric.set_id(1); |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 48 | metric.set_bucket(ONE_MINUTE); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 49 | |
| 50 | LogEvent event1(tagId, bucketStartTimeNs + 1); |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 51 | event1.init(); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 52 | LogEvent event2(tagId, bucketStartTimeNs + 2); |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 53 | event2.init(); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 54 | |
| 55 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 56 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 57 | CountMetricProducer countProducer(kConfigKey, metric, -1 /*-1 meaning no condition*/, wizard, |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 58 | bucketStartTimeNs); |
yro | 59cc24d | 2018-02-13 20:17:32 -0800 | [diff] [blame] | 59 | countProducer.setBucketSize(60 * NS_PER_SEC); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 60 | |
| 61 | // 2 events in bucket 1. |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 62 | countProducer.onMatchedLogEvent(1 /*log matcher index*/, event1); |
| 63 | countProducer.onMatchedLogEvent(1 /*log matcher index*/, event2); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 64 | |
| 65 | // Flushes at event #2. |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 66 | countProducer.flushIfNeededLocked(bucketStartTimeNs + 2); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 67 | EXPECT_EQ(0UL, countProducer.mPastBuckets.size()); |
| 68 | |
| 69 | // Flushes. |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 70 | countProducer.flushIfNeededLocked(bucketStartTimeNs + bucketSizeNs + 1); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 71 | EXPECT_EQ(1UL, countProducer.mPastBuckets.size()); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 72 | EXPECT_TRUE(countProducer.mPastBuckets.find(DEFAULT_METRIC_DIMENSION_KEY) != |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 73 | countProducer.mPastBuckets.end()); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 74 | const auto& buckets = countProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY]; |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 75 | EXPECT_EQ(1UL, buckets.size()); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 76 | EXPECT_EQ(bucketStartTimeNs, buckets[0].mBucketStartNs); |
| 77 | EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, buckets[0].mBucketEndNs); |
| 78 | EXPECT_EQ(2LL, buckets[0].mCount); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 79 | |
| 80 | // 1 matched event happens in bucket 2. |
| 81 | LogEvent event3(tagId, bucketStartTimeNs + bucketSizeNs + 2); |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 82 | event3.init(); |
| 83 | |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 84 | countProducer.onMatchedLogEvent(1 /*log matcher index*/, event3); |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 85 | countProducer.flushIfNeededLocked(bucketStartTimeNs + 2 * bucketSizeNs + 1); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 86 | EXPECT_EQ(1UL, countProducer.mPastBuckets.size()); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 87 | EXPECT_TRUE(countProducer.mPastBuckets.find(DEFAULT_METRIC_DIMENSION_KEY) != |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 88 | countProducer.mPastBuckets.end()); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 89 | EXPECT_EQ(2UL, countProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY].size()); |
| 90 | const auto& bucketInfo2 = countProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY][1]; |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 91 | EXPECT_EQ(bucket2StartTimeNs, bucketInfo2.mBucketStartNs); |
| 92 | EXPECT_EQ(bucket2StartTimeNs + bucketSizeNs, bucketInfo2.mBucketEndNs); |
| 93 | EXPECT_EQ(1LL, bucketInfo2.mCount); |
| 94 | |
| 95 | // nothing happens in bucket 3. we should not record anything for bucket 3. |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 96 | countProducer.flushIfNeededLocked(bucketStartTimeNs + 3 * bucketSizeNs + 1); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 97 | EXPECT_EQ(1UL, countProducer.mPastBuckets.size()); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 98 | EXPECT_TRUE(countProducer.mPastBuckets.find(DEFAULT_METRIC_DIMENSION_KEY) != |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 99 | countProducer.mPastBuckets.end()); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 100 | const auto& buckets3 = countProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY]; |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 101 | EXPECT_EQ(2UL, buckets3.size()); |
| 102 | } |
| 103 | |
| 104 | TEST(CountMetricProducerTest, TestEventsWithNonSlicedCondition) { |
| 105 | int64_t bucketStartTimeNs = 10000000000; |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 106 | int64_t bucketSizeNs = TimeUnitToBucketSizeInMillis(ONE_MINUTE) * 1000000LL; |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 107 | |
| 108 | CountMetric metric; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 109 | metric.set_id(1); |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 110 | metric.set_bucket(ONE_MINUTE); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 111 | metric.set_condition(StringToId("SCREEN_ON")); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 112 | |
| 113 | LogEvent event1(1, bucketStartTimeNs + 1); |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 114 | event1.init(); |
| 115 | |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 116 | LogEvent event2(1, bucketStartTimeNs + 10); |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 117 | event2.init(); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 118 | |
| 119 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 120 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 121 | CountMetricProducer countProducer(kConfigKey, metric, 1, wizard, bucketStartTimeNs); |
yro | 59cc24d | 2018-02-13 20:17:32 -0800 | [diff] [blame] | 122 | countProducer.setBucketSize(60 * NS_PER_SEC); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 123 | |
| 124 | countProducer.onConditionChanged(true, bucketStartTimeNs); |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 125 | countProducer.onMatchedLogEvent(1 /*matcher index*/, event1); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 126 | EXPECT_EQ(0UL, countProducer.mPastBuckets.size()); |
| 127 | |
| 128 | countProducer.onConditionChanged(false /*new condition*/, bucketStartTimeNs + 2); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 129 | // Upon this match event, the matched event1 is flushed. |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 130 | countProducer.onMatchedLogEvent(1 /*matcher index*/, event2); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 131 | EXPECT_EQ(0UL, countProducer.mPastBuckets.size()); |
| 132 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 133 | countProducer.flushIfNeededLocked(bucketStartTimeNs + bucketSizeNs + 1); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 134 | EXPECT_EQ(1UL, countProducer.mPastBuckets.size()); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 135 | EXPECT_TRUE(countProducer.mPastBuckets.find(DEFAULT_METRIC_DIMENSION_KEY) != |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 136 | countProducer.mPastBuckets.end()); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 137 | { |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 138 | const auto& buckets = countProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY]; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 139 | EXPECT_EQ(1UL, buckets.size()); |
| 140 | const auto& bucketInfo = buckets[0]; |
| 141 | EXPECT_EQ(bucketStartTimeNs, bucketInfo.mBucketStartNs); |
| 142 | EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, bucketInfo.mBucketEndNs); |
| 143 | EXPECT_EQ(1LL, bucketInfo.mCount); |
| 144 | } |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | TEST(CountMetricProducerTest, TestEventsWithSlicedCondition) { |
| 148 | int64_t bucketStartTimeNs = 10000000000; |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 149 | int64_t bucketSizeNs = TimeUnitToBucketSizeInMillis(ONE_MINUTE) * 1000000LL; |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 150 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 151 | int tagId = 1; |
| 152 | int conditionTagId = 2; |
| 153 | |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 154 | CountMetric metric; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 155 | metric.set_id(1); |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 156 | metric.set_bucket(ONE_MINUTE); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 157 | metric.set_condition(StringToId("APP_IN_BACKGROUND_PER_UID_AND_SCREEN_ON")); |
Stefan Lafon | a5b5191 | 2017-12-05 21:43:52 -0800 | [diff] [blame] | 158 | MetricConditionLink* link = metric.add_links(); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 159 | link->set_condition(StringToId("APP_IN_BACKGROUND_PER_UID")); |
Yangster-mac | 7ba8fc3 | 2018-01-24 16:16:46 -0800 | [diff] [blame] | 160 | buildSimpleAtomFieldMatcher(tagId, 1, link->mutable_fields_in_what()); |
| 161 | buildSimpleAtomFieldMatcher(conditionTagId, 2, link->mutable_fields_in_condition()); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 162 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 163 | LogEvent event1(tagId, bucketStartTimeNs + 1); |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 164 | event1.write("111"); // uid |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 165 | event1.init(); |
| 166 | ConditionKey key1; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 167 | key1[StringToId("APP_IN_BACKGROUND_PER_UID")] = |
| 168 | {getMockedDimensionKey(conditionTagId, 2, "111")}; |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 169 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 170 | LogEvent event2(tagId, bucketStartTimeNs + 10); |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 171 | event2.write("222"); // uid |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 172 | event2.init(); |
| 173 | ConditionKey key2; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 174 | key2[StringToId("APP_IN_BACKGROUND_PER_UID")] = |
| 175 | {getMockedDimensionKey(conditionTagId, 2, "222")}; |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 176 | |
| 177 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 178 | EXPECT_CALL(*wizard, query(_, key1, _, _, _, _)).WillOnce(Return(ConditionState::kFalse)); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 179 | |
Yangster | 13fb7e4 | 2018-03-07 17:30:49 -0800 | [diff] [blame] | 180 | EXPECT_CALL(*wizard, query(_, key2, _, _, _, _)).WillOnce(Return(ConditionState::kTrue)); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 181 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 182 | CountMetricProducer countProducer(kConfigKey, metric, 1 /*condition tracker index*/, wizard, |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 183 | bucketStartTimeNs); |
yro | 59cc24d | 2018-02-13 20:17:32 -0800 | [diff] [blame] | 184 | countProducer.setBucketSize(60 * NS_PER_SEC); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 185 | |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 186 | countProducer.onMatchedLogEvent(1 /*log matcher index*/, event1); |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 187 | countProducer.flushIfNeededLocked(bucketStartTimeNs + 1); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 188 | EXPECT_EQ(0UL, countProducer.mPastBuckets.size()); |
| 189 | |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 190 | countProducer.onMatchedLogEvent(1 /*log matcher index*/, event2); |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 191 | countProducer.flushIfNeededLocked(bucketStartTimeNs + bucketSizeNs + 1); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 192 | EXPECT_EQ(1UL, countProducer.mPastBuckets.size()); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 193 | EXPECT_TRUE(countProducer.mPastBuckets.find(DEFAULT_METRIC_DIMENSION_KEY) != |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 194 | countProducer.mPastBuckets.end()); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 195 | const auto& buckets = countProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY]; |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 196 | EXPECT_EQ(1UL, buckets.size()); |
| 197 | const auto& bucketInfo = buckets[0]; |
| 198 | EXPECT_EQ(bucketStartTimeNs, bucketInfo.mBucketStartNs); |
| 199 | EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, bucketInfo.mBucketEndNs); |
| 200 | EXPECT_EQ(1LL, bucketInfo.mCount); |
| 201 | } |
| 202 | |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 203 | TEST(CountMetricProducerTest, TestEventWithAppUpgrade) { |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 204 | sp<AlarmMonitor> alarmMonitor; |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 205 | uint64_t bucketStartTimeNs = 10000000000; |
| 206 | uint64_t bucketSizeNs = TimeUnitToBucketSizeInMillis(ONE_MINUTE) * 1000000LL; |
| 207 | uint64_t eventUpgradeTimeNs = bucketStartTimeNs + 15 * NS_PER_SEC; |
| 208 | |
| 209 | int tagId = 1; |
| 210 | int conditionTagId = 2; |
| 211 | |
| 212 | CountMetric metric; |
| 213 | metric.set_id(1); |
| 214 | metric.set_bucket(ONE_MINUTE); |
| 215 | Alert alert; |
| 216 | alert.set_num_buckets(3); |
| 217 | alert.set_trigger_if_sum_gt(2); |
| 218 | LogEvent event1(tagId, bucketStartTimeNs + 1); |
| 219 | event1.write("111"); // uid |
| 220 | event1.init(); |
| 221 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 222 | CountMetricProducer countProducer(kConfigKey, metric, -1 /* no condition */, wizard, |
| 223 | bucketStartTimeNs); |
yro | 59cc24d | 2018-02-13 20:17:32 -0800 | [diff] [blame] | 224 | countProducer.setBucketSize(60 * NS_PER_SEC); |
| 225 | |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 226 | sp<AnomalyTracker> anomalyTracker = countProducer.addAnomalyTracker(alert, alarmMonitor); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 227 | EXPECT_TRUE(anomalyTracker != nullptr); |
| 228 | |
| 229 | // Bucket is flushed yet. |
| 230 | countProducer.onMatchedLogEvent(1 /*log matcher index*/, event1); |
| 231 | EXPECT_EQ(0UL, countProducer.mPastBuckets.size()); |
| 232 | EXPECT_EQ(0, anomalyTracker->getSumOverPastBuckets(DEFAULT_METRIC_DIMENSION_KEY)); |
| 233 | |
| 234 | // App upgrade forces bucket flush. |
| 235 | // Check that there's a past bucket and the bucket end is not adjusted. |
| 236 | countProducer.notifyAppUpgrade(eventUpgradeTimeNs, "ANY.APP", 1, 1); |
| 237 | EXPECT_EQ(1UL, countProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY].size()); |
| 238 | EXPECT_EQ((long long)bucketStartTimeNs, |
| 239 | countProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY][0].mBucketStartNs); |
| 240 | EXPECT_EQ((long long)eventUpgradeTimeNs, |
| 241 | countProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY][0].mBucketEndNs); |
| 242 | EXPECT_EQ(eventUpgradeTimeNs, countProducer.mCurrentBucketStartTimeNs); |
| 243 | // Anomaly tracker only contains full buckets. |
| 244 | EXPECT_EQ(0, anomalyTracker->getSumOverPastBuckets(DEFAULT_METRIC_DIMENSION_KEY)); |
| 245 | |
| 246 | uint64_t lastEndTimeNs = countProducer.getCurrentBucketEndTimeNs(); |
| 247 | // Next event occurs in same bucket as partial bucket created. |
| 248 | LogEvent event2(tagId, bucketStartTimeNs + 59 * NS_PER_SEC + 10); |
| 249 | event2.write("222"); // uid |
| 250 | event2.init(); |
| 251 | countProducer.onMatchedLogEvent(1 /*log matcher index*/, event2); |
| 252 | EXPECT_EQ(1UL, countProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY].size()); |
| 253 | EXPECT_EQ(eventUpgradeTimeNs, countProducer.mCurrentBucketStartTimeNs); |
| 254 | EXPECT_EQ(0, anomalyTracker->getSumOverPastBuckets(DEFAULT_METRIC_DIMENSION_KEY)); |
| 255 | |
| 256 | // Third event in following bucket. |
| 257 | LogEvent event3(tagId, bucketStartTimeNs + 62 * NS_PER_SEC + 10); |
| 258 | event3.write("333"); // uid |
| 259 | event3.init(); |
| 260 | countProducer.onMatchedLogEvent(1 /*log matcher index*/, event3); |
| 261 | EXPECT_EQ(2UL, countProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY].size()); |
| 262 | EXPECT_EQ(lastEndTimeNs, countProducer.mCurrentBucketStartTimeNs); |
| 263 | EXPECT_EQ(2, anomalyTracker->getSumOverPastBuckets(DEFAULT_METRIC_DIMENSION_KEY)); |
| 264 | } |
| 265 | |
| 266 | TEST(CountMetricProducerTest, TestEventWithAppUpgradeInNextBucket) { |
| 267 | uint64_t bucketStartTimeNs = 10000000000; |
| 268 | uint64_t bucketSizeNs = TimeUnitToBucketSizeInMillis(ONE_MINUTE) * 1000000LL; |
| 269 | uint64_t eventUpgradeTimeNs = bucketStartTimeNs + 65 * NS_PER_SEC; |
| 270 | |
| 271 | int tagId = 1; |
| 272 | int conditionTagId = 2; |
| 273 | |
| 274 | CountMetric metric; |
| 275 | metric.set_id(1); |
| 276 | metric.set_bucket(ONE_MINUTE); |
| 277 | LogEvent event1(tagId, bucketStartTimeNs + 1); |
| 278 | event1.write("111"); // uid |
| 279 | event1.init(); |
| 280 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 281 | CountMetricProducer countProducer(kConfigKey, metric, -1 /* no condition */, wizard, |
| 282 | bucketStartTimeNs); |
yro | 59cc24d | 2018-02-13 20:17:32 -0800 | [diff] [blame] | 283 | countProducer.setBucketSize(60 * NS_PER_SEC); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 284 | |
| 285 | // Bucket is flushed yet. |
| 286 | countProducer.onMatchedLogEvent(1 /*log matcher index*/, event1); |
| 287 | EXPECT_EQ(0UL, countProducer.mPastBuckets.size()); |
| 288 | |
| 289 | // App upgrade forces bucket flush. |
| 290 | // Check that there's a past bucket and the bucket end is not adjusted. |
| 291 | countProducer.notifyAppUpgrade(eventUpgradeTimeNs, "ANY.APP", 1, 1); |
| 292 | EXPECT_EQ(1UL, countProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY].size()); |
| 293 | EXPECT_EQ((int64_t)bucketStartTimeNs, |
| 294 | countProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY][0].mBucketStartNs); |
| 295 | EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, |
| 296 | (uint64_t)countProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY][0].mBucketEndNs); |
| 297 | EXPECT_EQ(eventUpgradeTimeNs, countProducer.mCurrentBucketStartTimeNs); |
| 298 | |
| 299 | // Next event occurs in same bucket as partial bucket created. |
| 300 | LogEvent event2(tagId, bucketStartTimeNs + 70 * NS_PER_SEC + 10); |
| 301 | event2.write("222"); // uid |
| 302 | event2.init(); |
| 303 | countProducer.onMatchedLogEvent(1 /*log matcher index*/, event2); |
| 304 | EXPECT_EQ(1UL, countProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY].size()); |
| 305 | |
| 306 | // Third event in following bucket. |
| 307 | LogEvent event3(tagId, bucketStartTimeNs + 121 * NS_PER_SEC + 10); |
| 308 | event3.write("333"); // uid |
| 309 | event3.init(); |
| 310 | countProducer.onMatchedLogEvent(1 /*log matcher index*/, event3); |
| 311 | EXPECT_EQ(2UL, countProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY].size()); |
| 312 | EXPECT_EQ((int64_t)eventUpgradeTimeNs, |
| 313 | countProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY][1].mBucketStartNs); |
| 314 | EXPECT_EQ(bucketStartTimeNs + 2 * bucketSizeNs, |
| 315 | (uint64_t)countProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY][1].mBucketEndNs); |
| 316 | } |
| 317 | |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 318 | TEST(CountMetricProducerTest, TestAnomalyDetectionUnSliced) { |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 319 | sp<AlarmMonitor> alarmMonitor; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 320 | Alert alert; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 321 | alert.set_id(11); |
| 322 | alert.set_metric_id(1); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 323 | alert.set_trigger_if_sum_gt(2); |
Yangster-mac | a7fb12d | 2018-01-03 17:17:20 -0800 | [diff] [blame] | 324 | alert.set_num_buckets(2); |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 325 | const int32_t refPeriodSec = 1; |
| 326 | alert.set_refractory_period_secs(refPeriodSec); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 327 | |
| 328 | int64_t bucketStartTimeNs = 10000000000; |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 329 | int64_t bucketSizeNs = TimeUnitToBucketSizeInMillis(ONE_MINUTE) * 1000000LL; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 330 | int64_t bucket2StartTimeNs = bucketStartTimeNs + bucketSizeNs; |
| 331 | int64_t bucket3StartTimeNs = bucketStartTimeNs + 2 * bucketSizeNs; |
| 332 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 333 | CountMetric metric; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 334 | metric.set_id(1); |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 335 | metric.set_bucket(ONE_MINUTE); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 336 | |
| 337 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 338 | CountMetricProducer countProducer(kConfigKey, metric, -1 /*-1 meaning no condition*/, wizard, |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 339 | bucketStartTimeNs); |
yro | 59cc24d | 2018-02-13 20:17:32 -0800 | [diff] [blame] | 340 | countProducer.setBucketSize(60 * NS_PER_SEC); |
| 341 | |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 342 | sp<AnomalyTracker> anomalyTracker = countProducer.addAnomalyTracker(alert, alarmMonitor); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 343 | |
| 344 | int tagId = 1; |
| 345 | LogEvent event1(tagId, bucketStartTimeNs + 1); |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 346 | event1.init(); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 347 | LogEvent event2(tagId, bucketStartTimeNs + 2); |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 348 | event2.init(); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 349 | LogEvent event3(tagId, bucketStartTimeNs + 2 * bucketSizeNs + 1); |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 350 | event3.init(); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 351 | LogEvent event4(tagId, bucketStartTimeNs + 3 * bucketSizeNs + 1); |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 352 | event4.init(); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 353 | LogEvent event5(tagId, bucketStartTimeNs + 3 * bucketSizeNs + 2); |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 354 | event5.init(); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 355 | LogEvent event6(tagId, bucketStartTimeNs + 3 * bucketSizeNs + 3); |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 356 | event6.init(); |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 357 | LogEvent event7(tagId, bucketStartTimeNs + 3 * bucketSizeNs + 2 * NS_PER_SEC); |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 358 | event7.init(); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 359 | |
| 360 | // Two events in bucket #0. |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 361 | countProducer.onMatchedLogEvent(1 /*log matcher index*/, event1); |
| 362 | countProducer.onMatchedLogEvent(1 /*log matcher index*/, event2); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 363 | |
| 364 | EXPECT_EQ(1UL, countProducer.mCurrentSlicedCounter->size()); |
| 365 | EXPECT_EQ(2L, countProducer.mCurrentSlicedCounter->begin()->second); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 366 | EXPECT_EQ(anomalyTracker->getRefractoryPeriodEndsSec(DEFAULT_METRIC_DIMENSION_KEY), 0U); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 367 | |
| 368 | // One event in bucket #2. No alarm as bucket #0 is trashed out. |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 369 | countProducer.onMatchedLogEvent(1 /*log matcher index*/, event3); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 370 | EXPECT_EQ(1UL, countProducer.mCurrentSlicedCounter->size()); |
| 371 | EXPECT_EQ(1L, countProducer.mCurrentSlicedCounter->begin()->second); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 372 | EXPECT_EQ(anomalyTracker->getRefractoryPeriodEndsSec(DEFAULT_METRIC_DIMENSION_KEY), 0U); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 373 | |
| 374 | // Two events in bucket #3. |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 375 | countProducer.onMatchedLogEvent(1 /*log matcher index*/, event4); |
| 376 | countProducer.onMatchedLogEvent(1 /*log matcher index*/, event5); |
| 377 | countProducer.onMatchedLogEvent(1 /*log matcher index*/, event6); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 378 | EXPECT_EQ(1UL, countProducer.mCurrentSlicedCounter->size()); |
| 379 | EXPECT_EQ(3L, countProducer.mCurrentSlicedCounter->begin()->second); |
| 380 | // Anomaly at event 6 is within refractory period. The alarm is at event 5 timestamp not event 6 |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 381 | EXPECT_EQ(anomalyTracker->getRefractoryPeriodEndsSec(DEFAULT_METRIC_DIMENSION_KEY), |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 382 | event5.GetElapsedTimestampNs() / NS_PER_SEC + refPeriodSec); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 383 | |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 384 | countProducer.onMatchedLogEvent(1 /*log matcher index*/, event7); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 385 | EXPECT_EQ(1UL, countProducer.mCurrentSlicedCounter->size()); |
| 386 | EXPECT_EQ(4L, countProducer.mCurrentSlicedCounter->begin()->second); |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 387 | EXPECT_EQ(anomalyTracker->getRefractoryPeriodEndsSec(DEFAULT_METRIC_DIMENSION_KEY), |
Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 388 | event7.GetElapsedTimestampNs() / NS_PER_SEC + refPeriodSec); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 389 | } |
| 390 | |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 391 | } // namespace statsd |
| 392 | } // namespace os |
| 393 | } // namespace android |
| 394 | #else |
| 395 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 396 | #endif |