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 | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 16 | #include "src/dimension.h" |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 17 | #include "src/stats_log_util.h" |
Yao Chen | d5aa01b3 | 2017-12-19 16:46:36 -0800 | [diff] [blame] | 18 | #include "metrics_test_helper.h" |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 19 | #include "tests/statsd_test_util.h" |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 20 | |
| 21 | #include <gmock/gmock.h> |
| 22 | #include <gtest/gtest.h> |
| 23 | #include <stdio.h> |
| 24 | #include <vector> |
| 25 | |
| 26 | using namespace testing; |
| 27 | using android::sp; |
| 28 | using std::set; |
| 29 | using std::unordered_map; |
| 30 | using std::vector; |
| 31 | |
| 32 | #ifdef __ANDROID__ |
| 33 | |
| 34 | namespace android { |
| 35 | namespace os { |
| 36 | namespace statsd { |
| 37 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 38 | const ConfigKey kConfigKey(0, 12345); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 39 | |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 40 | TEST(CountMetricProducerTest, TestNonDimensionalEvents) { |
| 41 | int64_t bucketStartTimeNs = 10000000000; |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 42 | int64_t bucketSizeNs = TimeUnitToBucketSizeInMillis(ONE_MINUTE) * 1000000LL; |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 43 | int64_t bucket2StartTimeNs = bucketStartTimeNs + bucketSizeNs; |
| 44 | int64_t bucket3StartTimeNs = bucketStartTimeNs + 2 * bucketSizeNs; |
| 45 | int tagId = 1; |
| 46 | |
| 47 | CountMetric metric; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 48 | metric.set_id(1); |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 49 | metric.set_bucket(ONE_MINUTE); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 50 | |
| 51 | LogEvent event1(tagId, bucketStartTimeNs + 1); |
| 52 | LogEvent event2(tagId, bucketStartTimeNs + 2); |
| 53 | |
| 54 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 55 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 56 | CountMetricProducer countProducer(kConfigKey, metric, -1 /*-1 meaning no condition*/, wizard, |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 57 | bucketStartTimeNs); |
| 58 | |
| 59 | // 2 events in bucket 1. |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 60 | countProducer.onMatchedLogEvent(1 /*log matcher index*/, event1); |
| 61 | countProducer.onMatchedLogEvent(1 /*log matcher index*/, event2); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 62 | |
| 63 | // Flushes at event #2. |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 64 | countProducer.flushIfNeededLocked(bucketStartTimeNs + 2); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 65 | EXPECT_EQ(0UL, countProducer.mPastBuckets.size()); |
| 66 | |
| 67 | // Flushes. |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 68 | countProducer.flushIfNeededLocked(bucketStartTimeNs + bucketSizeNs + 1); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 69 | EXPECT_EQ(1UL, countProducer.mPastBuckets.size()); |
| 70 | EXPECT_TRUE(countProducer.mPastBuckets.find(DEFAULT_DIMENSION_KEY) != |
| 71 | countProducer.mPastBuckets.end()); |
| 72 | const auto& buckets = countProducer.mPastBuckets[DEFAULT_DIMENSION_KEY]; |
| 73 | EXPECT_EQ(1UL, buckets.size()); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 74 | EXPECT_EQ(bucketStartTimeNs, buckets[0].mBucketStartNs); |
| 75 | EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, buckets[0].mBucketEndNs); |
| 76 | EXPECT_EQ(2LL, buckets[0].mCount); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 77 | |
| 78 | // 1 matched event happens in bucket 2. |
| 79 | LogEvent event3(tagId, bucketStartTimeNs + bucketSizeNs + 2); |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 80 | countProducer.onMatchedLogEvent(1 /*log matcher index*/, event3); |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 81 | countProducer.flushIfNeededLocked(bucketStartTimeNs + 2 * bucketSizeNs + 1); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 82 | EXPECT_EQ(1UL, countProducer.mPastBuckets.size()); |
| 83 | EXPECT_TRUE(countProducer.mPastBuckets.find(DEFAULT_DIMENSION_KEY) != |
| 84 | countProducer.mPastBuckets.end()); |
| 85 | EXPECT_EQ(2UL, countProducer.mPastBuckets[DEFAULT_DIMENSION_KEY].size()); |
| 86 | const auto& bucketInfo2 = countProducer.mPastBuckets[DEFAULT_DIMENSION_KEY][1]; |
| 87 | EXPECT_EQ(bucket2StartTimeNs, bucketInfo2.mBucketStartNs); |
| 88 | EXPECT_EQ(bucket2StartTimeNs + bucketSizeNs, bucketInfo2.mBucketEndNs); |
| 89 | EXPECT_EQ(1LL, bucketInfo2.mCount); |
| 90 | |
| 91 | // nothing happens in bucket 3. we should not record anything for bucket 3. |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 92 | countProducer.flushIfNeededLocked(bucketStartTimeNs + 3 * bucketSizeNs + 1); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 93 | EXPECT_EQ(1UL, countProducer.mPastBuckets.size()); |
| 94 | EXPECT_TRUE(countProducer.mPastBuckets.find(DEFAULT_DIMENSION_KEY) != |
| 95 | countProducer.mPastBuckets.end()); |
| 96 | const auto& buckets3 = countProducer.mPastBuckets[DEFAULT_DIMENSION_KEY]; |
| 97 | EXPECT_EQ(2UL, buckets3.size()); |
| 98 | } |
| 99 | |
| 100 | TEST(CountMetricProducerTest, TestEventsWithNonSlicedCondition) { |
| 101 | int64_t bucketStartTimeNs = 10000000000; |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 102 | int64_t bucketSizeNs = TimeUnitToBucketSizeInMillis(ONE_MINUTE) * 1000000LL; |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 103 | |
| 104 | CountMetric metric; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 105 | metric.set_id(1); |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 106 | metric.set_bucket(ONE_MINUTE); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 107 | metric.set_condition(StringToId("SCREEN_ON")); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 108 | |
| 109 | LogEvent event1(1, bucketStartTimeNs + 1); |
| 110 | LogEvent event2(1, bucketStartTimeNs + 10); |
| 111 | |
| 112 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 113 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 114 | CountMetricProducer countProducer(kConfigKey, metric, 1, wizard, bucketStartTimeNs); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 115 | |
| 116 | countProducer.onConditionChanged(true, bucketStartTimeNs); |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 117 | countProducer.onMatchedLogEvent(1 /*matcher index*/, event1); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 118 | EXPECT_EQ(0UL, countProducer.mPastBuckets.size()); |
| 119 | |
| 120 | countProducer.onConditionChanged(false /*new condition*/, bucketStartTimeNs + 2); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 121 | // Upon this match event, the matched event1 is flushed. |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 122 | countProducer.onMatchedLogEvent(1 /*matcher index*/, event2); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 123 | EXPECT_EQ(0UL, countProducer.mPastBuckets.size()); |
| 124 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 125 | countProducer.flushIfNeededLocked(bucketStartTimeNs + bucketSizeNs + 1); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 126 | EXPECT_EQ(1UL, countProducer.mPastBuckets.size()); |
| 127 | EXPECT_TRUE(countProducer.mPastBuckets.find(DEFAULT_DIMENSION_KEY) != |
| 128 | countProducer.mPastBuckets.end()); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 129 | { |
| 130 | const auto& buckets = countProducer.mPastBuckets[DEFAULT_DIMENSION_KEY]; |
| 131 | EXPECT_EQ(1UL, buckets.size()); |
| 132 | const auto& bucketInfo = buckets[0]; |
| 133 | EXPECT_EQ(bucketStartTimeNs, bucketInfo.mBucketStartNs); |
| 134 | EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, bucketInfo.mBucketEndNs); |
| 135 | EXPECT_EQ(1LL, bucketInfo.mCount); |
| 136 | } |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | TEST(CountMetricProducerTest, TestEventsWithSlicedCondition) { |
| 140 | int64_t bucketStartTimeNs = 10000000000; |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 141 | int64_t bucketSizeNs = TimeUnitToBucketSizeInMillis(ONE_MINUTE) * 1000000LL; |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 142 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 143 | int tagId = 1; |
| 144 | int conditionTagId = 2; |
| 145 | |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 146 | CountMetric metric; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 147 | metric.set_id(1); |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 148 | metric.set_bucket(ONE_MINUTE); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 149 | metric.set_condition(StringToId("APP_IN_BACKGROUND_PER_UID_AND_SCREEN_ON")); |
Stefan Lafon | a5b5191 | 2017-12-05 21:43:52 -0800 | [diff] [blame] | 150 | MetricConditionLink* link = metric.add_links(); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 151 | link->set_condition(StringToId("APP_IN_BACKGROUND_PER_UID")); |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 152 | *link->mutable_dimensions_in_what() = buildSimpleAtomFieldMatcher(tagId, 1); |
| 153 | *link->mutable_dimensions_in_condition() = buildSimpleAtomFieldMatcher(conditionTagId, 2); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 154 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 155 | LogEvent event1(tagId, bucketStartTimeNs + 1); |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 156 | event1.write("111"); // uid |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 157 | event1.init(); |
| 158 | ConditionKey key1; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 159 | key1[StringToId("APP_IN_BACKGROUND_PER_UID")] = |
| 160 | {getMockedDimensionKey(conditionTagId, 2, "111")}; |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 161 | |
Yangster-mac | 2087716 | 2017-12-22 17:19:39 -0800 | [diff] [blame] | 162 | LogEvent event2(tagId, bucketStartTimeNs + 10); |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 163 | event2.write("222"); // uid |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 164 | event2.init(); |
| 165 | ConditionKey key2; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 166 | key2[StringToId("APP_IN_BACKGROUND_PER_UID")] = |
| 167 | {getMockedDimensionKey(conditionTagId, 2, "222")}; |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 168 | |
| 169 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 170 | EXPECT_CALL(*wizard, query(_, key1)).WillOnce(Return(ConditionState::kFalse)); |
| 171 | |
| 172 | EXPECT_CALL(*wizard, query(_, key2)).WillOnce(Return(ConditionState::kTrue)); |
| 173 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 174 | CountMetricProducer countProducer(kConfigKey, metric, 1 /*condition tracker index*/, wizard, |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 175 | bucketStartTimeNs); |
| 176 | |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 177 | countProducer.onMatchedLogEvent(1 /*log matcher index*/, event1); |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 178 | countProducer.flushIfNeededLocked(bucketStartTimeNs + 1); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 179 | EXPECT_EQ(0UL, countProducer.mPastBuckets.size()); |
| 180 | |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 181 | countProducer.onMatchedLogEvent(1 /*log matcher index*/, event2); |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 182 | countProducer.flushIfNeededLocked(bucketStartTimeNs + bucketSizeNs + 1); |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 183 | EXPECT_EQ(1UL, countProducer.mPastBuckets.size()); |
| 184 | EXPECT_TRUE(countProducer.mPastBuckets.find(DEFAULT_DIMENSION_KEY) != |
| 185 | countProducer.mPastBuckets.end()); |
| 186 | const auto& buckets = countProducer.mPastBuckets[DEFAULT_DIMENSION_KEY]; |
| 187 | EXPECT_EQ(1UL, buckets.size()); |
| 188 | const auto& bucketInfo = buckets[0]; |
| 189 | EXPECT_EQ(bucketStartTimeNs, bucketInfo.mBucketStartNs); |
| 190 | EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, bucketInfo.mBucketEndNs); |
| 191 | EXPECT_EQ(1LL, bucketInfo.mCount); |
| 192 | } |
| 193 | |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 194 | TEST(CountMetricProducerTest, TestAnomalyDetectionUnSliced) { |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 195 | Alert alert; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 196 | alert.set_id(11); |
| 197 | alert.set_metric_id(1); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 198 | alert.set_trigger_if_sum_gt(2); |
Yangster-mac | a7fb12d | 2018-01-03 17:17:20 -0800 | [diff] [blame] | 199 | alert.set_num_buckets(2); |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 200 | const int32_t refPeriodSec = 1; |
| 201 | alert.set_refractory_period_secs(refPeriodSec); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 202 | |
| 203 | int64_t bucketStartTimeNs = 10000000000; |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 204 | int64_t bucketSizeNs = TimeUnitToBucketSizeInMillis(ONE_MINUTE) * 1000000LL; |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 205 | int64_t bucket2StartTimeNs = bucketStartTimeNs + bucketSizeNs; |
| 206 | int64_t bucket3StartTimeNs = bucketStartTimeNs + 2 * bucketSizeNs; |
| 207 | |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 208 | CountMetric metric; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 209 | metric.set_id(1); |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 210 | metric.set_bucket(ONE_MINUTE); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 211 | |
| 212 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 213 | CountMetricProducer countProducer(kConfigKey, metric, -1 /*-1 meaning no condition*/, wizard, |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 214 | bucketStartTimeNs); |
Bookatz | 857aaa5 | 2017-12-19 15:29:06 -0800 | [diff] [blame] | 215 | sp<AnomalyTracker> anomalyTracker = countProducer.addAnomalyTracker(alert); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 216 | |
| 217 | int tagId = 1; |
| 218 | LogEvent event1(tagId, bucketStartTimeNs + 1); |
| 219 | LogEvent event2(tagId, bucketStartTimeNs + 2); |
| 220 | LogEvent event3(tagId, bucketStartTimeNs + 2 * bucketSizeNs + 1); |
| 221 | LogEvent event4(tagId, bucketStartTimeNs + 3 * bucketSizeNs + 1); |
| 222 | LogEvent event5(tagId, bucketStartTimeNs + 3 * bucketSizeNs + 2); |
| 223 | LogEvent event6(tagId, bucketStartTimeNs + 3 * bucketSizeNs + 3); |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 224 | LogEvent event7(tagId, bucketStartTimeNs + 3 * bucketSizeNs + 2 * NS_PER_SEC); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 225 | |
| 226 | // Two events in bucket #0. |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 227 | countProducer.onMatchedLogEvent(1 /*log matcher index*/, event1); |
| 228 | countProducer.onMatchedLogEvent(1 /*log matcher index*/, event2); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 229 | |
| 230 | EXPECT_EQ(1UL, countProducer.mCurrentSlicedCounter->size()); |
| 231 | EXPECT_EQ(2L, countProducer.mCurrentSlicedCounter->begin()->second); |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 232 | EXPECT_EQ(anomalyTracker->getRefractoryPeriodEndsSec(DEFAULT_DIMENSION_KEY), 0U); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 233 | |
| 234 | // 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] | 235 | countProducer.onMatchedLogEvent(1 /*log matcher index*/, event3); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 236 | EXPECT_EQ(1UL, countProducer.mCurrentSlicedCounter->size()); |
| 237 | EXPECT_EQ(1L, countProducer.mCurrentSlicedCounter->begin()->second); |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 238 | EXPECT_EQ(anomalyTracker->getRefractoryPeriodEndsSec(DEFAULT_DIMENSION_KEY), 0U); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 239 | |
| 240 | // Two events in bucket #3. |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 241 | countProducer.onMatchedLogEvent(1 /*log matcher index*/, event4); |
| 242 | countProducer.onMatchedLogEvent(1 /*log matcher index*/, event5); |
| 243 | countProducer.onMatchedLogEvent(1 /*log matcher index*/, event6); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 244 | EXPECT_EQ(1UL, countProducer.mCurrentSlicedCounter->size()); |
| 245 | EXPECT_EQ(3L, countProducer.mCurrentSlicedCounter->begin()->second); |
| 246 | // Anomaly at event 6 is within refractory period. The alarm is at event 5 timestamp not event 6 |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 247 | EXPECT_EQ(anomalyTracker->getRefractoryPeriodEndsSec(DEFAULT_DIMENSION_KEY), |
| 248 | event5.GetTimestampNs() / NS_PER_SEC + refPeriodSec); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 249 | |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 250 | countProducer.onMatchedLogEvent(1 /*log matcher index*/, event7); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 251 | EXPECT_EQ(1UL, countProducer.mCurrentSlicedCounter->size()); |
| 252 | EXPECT_EQ(4L, countProducer.mCurrentSlicedCounter->begin()->second); |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 253 | EXPECT_EQ(anomalyTracker->getRefractoryPeriodEndsSec(DEFAULT_DIMENSION_KEY), |
| 254 | event7.GetTimestampNs() / NS_PER_SEC + refPeriodSec); |
Yangster-mac | e2cd6d5 | 2017-11-09 20:38:30 -0800 | [diff] [blame] | 255 | } |
| 256 | |
Yao Chen | 93fe3a3 | 2017-11-02 13:52:59 -0700 | [diff] [blame] | 257 | } // namespace statsd |
| 258 | } // namespace os |
| 259 | } // namespace android |
| 260 | #else |
| 261 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 262 | #endif |