Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -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 | |
Chenjie Yu | 054ce9c | 2018-11-12 15:27:29 -0800 | [diff] [blame^] | 15 | #include "src/matchers/SimpleLogMatchingTracker.h" |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 16 | #include "src/metrics/ValueMetricProducer.h" |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 17 | #include "src/stats_log_util.h" |
Yao Chen | b356151 | 2017-11-21 18:07:17 -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" |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 20 | |
| 21 | #include <gmock/gmock.h> |
| 22 | #include <gtest/gtest.h> |
Bookatz | 6bf9825 | 2018-03-14 10:44:24 -0700 | [diff] [blame] | 23 | #include <math.h> |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 24 | #include <stdio.h> |
| 25 | #include <vector> |
| 26 | |
| 27 | using namespace testing; |
| 28 | using android::sp; |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 29 | using std::make_shared; |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 30 | using std::set; |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 31 | using std::shared_ptr; |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 32 | using std::unordered_map; |
| 33 | using std::vector; |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 34 | |
| 35 | #ifdef __ANDROID__ |
| 36 | |
| 37 | namespace android { |
| 38 | namespace os { |
| 39 | namespace statsd { |
| 40 | |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 41 | const ConfigKey kConfigKey(0, 12345); |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 42 | const int tagId = 1; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 43 | const int64_t metricId = 123; |
Chenjie Yu | 054ce9c | 2018-11-12 15:27:29 -0800 | [diff] [blame^] | 44 | const int64_t atomMatcherId = 678; |
| 45 | const int logEventMatcherIndex = 0; |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 46 | const int64_t bucketStartTimeNs = 10000000000; |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 47 | const int64_t bucketSizeNs = TimeUnitToBucketSizeInMillis(ONE_MINUTE) * 1000000LL; |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 48 | const int64_t bucket2StartTimeNs = bucketStartTimeNs + bucketSizeNs; |
| 49 | const int64_t bucket3StartTimeNs = bucketStartTimeNs + 2 * bucketSizeNs; |
| 50 | const int64_t bucket4StartTimeNs = bucketStartTimeNs + 3 * bucketSizeNs; |
Chenjie Yu | 6d370f4 | 2018-03-25 14:57:30 -0700 | [diff] [blame] | 51 | const int64_t bucket5StartTimeNs = bucketStartTimeNs + 4 * bucketSizeNs; |
| 52 | const int64_t bucket6StartTimeNs = bucketStartTimeNs + 5 * bucketSizeNs; |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 53 | double epsilon = 0.001; |
| 54 | |
| 55 | /* |
| 56 | * Tests that the first bucket works correctly |
| 57 | */ |
| 58 | TEST(ValueMetricProducerTest, TestCalcPreviousBucketEndTime) { |
| 59 | ValueMetric metric; |
| 60 | metric.set_id(metricId); |
| 61 | metric.set_bucket(ONE_MINUTE); |
| 62 | metric.mutable_value_field()->set_field(tagId); |
| 63 | metric.mutable_value_field()->add_child()->set_field(2); |
| 64 | |
| 65 | int64_t startTimeBase = 11; |
Chenjie Yu | 054ce9c | 2018-11-12 15:27:29 -0800 | [diff] [blame^] | 66 | UidMap uidMap; |
| 67 | SimpleAtomMatcher atomMatcher; |
| 68 | atomMatcher.set_atom_id(tagId); |
| 69 | sp<EventMatcherWizard> eventMatcherWizard = |
| 70 | new EventMatcherWizard({new SimpleLogMatchingTracker( |
| 71 | atomMatcherId, logEventMatcherIndex, atomMatcher, uidMap)}); |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 72 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 73 | sp<MockStatsPullerManager> pullerManager = new StrictMock<MockStatsPullerManager>(); |
| 74 | |
| 75 | // statsd started long ago. |
| 76 | // The metric starts in the middle of the bucket |
| 77 | ValueMetricProducer valueProducer(kConfigKey, metric, -1 /*-1 meaning no condition*/, wizard, |
Chenjie Yu | 054ce9c | 2018-11-12 15:27:29 -0800 | [diff] [blame^] | 78 | logEventMatcherIndex, eventMatcherWizard, -1, startTimeBase, |
| 79 | 22, pullerManager); |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 80 | |
| 81 | EXPECT_EQ(startTimeBase, valueProducer.calcPreviousBucketEndTime(60 * NS_PER_SEC + 10)); |
| 82 | EXPECT_EQ(startTimeBase, valueProducer.calcPreviousBucketEndTime(60 * NS_PER_SEC + 10)); |
| 83 | EXPECT_EQ(60 * NS_PER_SEC + startTimeBase, |
| 84 | valueProducer.calcPreviousBucketEndTime(2 * 60 * NS_PER_SEC)); |
| 85 | EXPECT_EQ(2 * 60 * NS_PER_SEC + startTimeBase, |
| 86 | valueProducer.calcPreviousBucketEndTime(3 * 60 * NS_PER_SEC)); |
| 87 | } |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 88 | |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 89 | /* |
Chenjie Yu | e1361ed | 2018-07-23 17:33:09 -0700 | [diff] [blame] | 90 | * Tests that the first bucket works correctly |
| 91 | */ |
| 92 | TEST(ValueMetricProducerTest, TestFirstBucket) { |
| 93 | ValueMetric metric; |
| 94 | metric.set_id(metricId); |
| 95 | metric.set_bucket(ONE_MINUTE); |
| 96 | metric.mutable_value_field()->set_field(tagId); |
| 97 | metric.mutable_value_field()->add_child()->set_field(2); |
| 98 | |
Chenjie Yu | 054ce9c | 2018-11-12 15:27:29 -0800 | [diff] [blame^] | 99 | UidMap uidMap; |
| 100 | SimpleAtomMatcher atomMatcher; |
| 101 | atomMatcher.set_atom_id(tagId); |
| 102 | sp<EventMatcherWizard> eventMatcherWizard = |
| 103 | new EventMatcherWizard({new SimpleLogMatchingTracker( |
| 104 | atomMatcherId, logEventMatcherIndex, atomMatcher, uidMap)}); |
Chenjie Yu | e1361ed | 2018-07-23 17:33:09 -0700 | [diff] [blame] | 105 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 106 | sp<MockStatsPullerManager> pullerManager = new StrictMock<MockStatsPullerManager>(); |
| 107 | |
| 108 | // statsd started long ago. |
| 109 | // The metric starts in the middle of the bucket |
| 110 | ValueMetricProducer valueProducer(kConfigKey, metric, -1 /*-1 meaning no condition*/, wizard, |
Chenjie Yu | 054ce9c | 2018-11-12 15:27:29 -0800 | [diff] [blame^] | 111 | logEventMatcherIndex, eventMatcherWizard, -1, 5, |
| 112 | 600 * NS_PER_SEC + NS_PER_SEC / 2, pullerManager); |
Chenjie Yu | e1361ed | 2018-07-23 17:33:09 -0700 | [diff] [blame] | 113 | |
| 114 | EXPECT_EQ(600500000000, valueProducer.mCurrentBucketStartTimeNs); |
| 115 | EXPECT_EQ(10, valueProducer.mCurrentBucketNum); |
| 116 | EXPECT_EQ(660000000005, valueProducer.getCurrentBucketEndTimeNs()); |
| 117 | } |
| 118 | |
| 119 | /* |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 120 | * Tests pulled atoms with no conditions |
| 121 | */ |
Chenjie Yu | e1361ed | 2018-07-23 17:33:09 -0700 | [diff] [blame] | 122 | TEST(ValueMetricProducerTest, TestPulledEventsNoCondition) { |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 123 | ValueMetric metric; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 124 | metric.set_id(metricId); |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 125 | metric.set_bucket(ONE_MINUTE); |
Yangster-mac | a7fb12d | 2018-01-03 17:17:20 -0800 | [diff] [blame] | 126 | metric.mutable_value_field()->set_field(tagId); |
| 127 | metric.mutable_value_field()->add_child()->set_field(2); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 128 | |
Chenjie Yu | 054ce9c | 2018-11-12 15:27:29 -0800 | [diff] [blame^] | 129 | UidMap uidMap; |
| 130 | SimpleAtomMatcher atomMatcher; |
| 131 | atomMatcher.set_atom_id(tagId); |
| 132 | sp<EventMatcherWizard> eventMatcherWizard = |
| 133 | new EventMatcherWizard({new SimpleLogMatchingTracker( |
| 134 | atomMatcherId, logEventMatcherIndex, atomMatcher, uidMap)}); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 135 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 136 | sp<MockStatsPullerManager> pullerManager = new StrictMock<MockStatsPullerManager>(); |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 137 | EXPECT_CALL(*pullerManager, RegisterReceiver(tagId, _, _, _)).WillOnce(Return()); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 138 | EXPECT_CALL(*pullerManager, UnRegisterReceiver(tagId, _)).WillOnce(Return()); |
Chenjie Yu | e1361ed | 2018-07-23 17:33:09 -0700 | [diff] [blame] | 139 | EXPECT_CALL(*pullerManager, Pull(tagId, _, _)) |
| 140 | .WillOnce(Invoke([](int tagId, int64_t timeNs, |
| 141 | vector<std::shared_ptr<LogEvent>>* data) { |
| 142 | data->clear(); |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 143 | shared_ptr<LogEvent> event = make_shared<LogEvent>(tagId, bucketStartTimeNs); |
Chenjie Yu | e1361ed | 2018-07-23 17:33:09 -0700 | [diff] [blame] | 144 | event->write(tagId); |
| 145 | event->write(3); |
| 146 | event->init(); |
| 147 | data->push_back(event); |
| 148 | return true; |
| 149 | })); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 150 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 151 | ValueMetricProducer valueProducer(kConfigKey, metric, -1 /*-1 meaning no condition*/, wizard, |
Chenjie Yu | 054ce9c | 2018-11-12 15:27:29 -0800 | [diff] [blame^] | 152 | logEventMatcherIndex, eventMatcherWizard, tagId, |
| 153 | bucketStartTimeNs, bucketStartTimeNs, pullerManager); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 154 | |
| 155 | vector<shared_ptr<LogEvent>> allData; |
| 156 | allData.clear(); |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 157 | shared_ptr<LogEvent> event = make_shared<LogEvent>(tagId, bucket2StartTimeNs + 1); |
| 158 | event->write(tagId); |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 159 | event->write(11); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 160 | event->init(); |
| 161 | allData.push_back(event); |
| 162 | |
| 163 | valueProducer.onDataPulled(allData); |
| 164 | // has one slice |
| 165 | EXPECT_EQ(1UL, valueProducer.mCurrentSlicedBucket.size()); |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 166 | ValueMetricProducer::Interval curInterval = valueProducer.mCurrentSlicedBucket.begin()->second[0]; |
yro | 59cc24d | 2018-02-13 20:17:32 -0800 | [diff] [blame] | 167 | |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 168 | EXPECT_EQ(true, curInterval.hasBase); |
| 169 | EXPECT_EQ(11, curInterval.base.long_value); |
| 170 | EXPECT_EQ(true, curInterval.hasValue); |
| 171 | EXPECT_EQ(8, curInterval.value.long_value); |
| 172 | EXPECT_EQ(0UL, valueProducer.mPastBuckets.size()); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 173 | |
| 174 | allData.clear(); |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 175 | event = make_shared<LogEvent>(tagId, bucket3StartTimeNs + 1); |
| 176 | event->write(tagId); |
| 177 | event->write(23); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 178 | event->init(); |
| 179 | allData.push_back(event); |
| 180 | valueProducer.onDataPulled(allData); |
| 181 | // has one slice |
| 182 | EXPECT_EQ(1UL, valueProducer.mCurrentSlicedBucket.size()); |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 183 | curInterval = valueProducer.mCurrentSlicedBucket.begin()->second[0]; |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 184 | |
| 185 | EXPECT_EQ(true, curInterval.hasBase); |
| 186 | EXPECT_EQ(23, curInterval.base.long_value); |
| 187 | EXPECT_EQ(true, curInterval.hasValue); |
| 188 | EXPECT_EQ(12, curInterval.value.long_value); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 189 | EXPECT_EQ(1UL, valueProducer.mPastBuckets.size()); |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 190 | EXPECT_EQ(1UL, valueProducer.mPastBuckets.begin()->second.size()); |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 191 | EXPECT_EQ(8, valueProducer.mPastBuckets.begin()->second.back().values[0].long_value); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 192 | |
| 193 | allData.clear(); |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 194 | event = make_shared<LogEvent>(tagId, bucket4StartTimeNs + 1); |
| 195 | event->write(tagId); |
| 196 | event->write(36); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 197 | event->init(); |
| 198 | allData.push_back(event); |
| 199 | valueProducer.onDataPulled(allData); |
| 200 | EXPECT_EQ(1UL, valueProducer.mCurrentSlicedBucket.size()); |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 201 | curInterval = valueProducer.mCurrentSlicedBucket.begin()->second[0]; |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 202 | |
| 203 | EXPECT_EQ(true, curInterval.hasBase); |
| 204 | EXPECT_EQ(36, curInterval.base.long_value); |
| 205 | EXPECT_EQ(true, curInterval.hasValue); |
| 206 | EXPECT_EQ(13, curInterval.value.long_value); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 207 | EXPECT_EQ(1UL, valueProducer.mPastBuckets.size()); |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 208 | EXPECT_EQ(2UL, valueProducer.mPastBuckets.begin()->second.size()); |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 209 | EXPECT_EQ(12, valueProducer.mPastBuckets.begin()->second.back().values[0].long_value); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | /* |
Chenjie Yu | 054ce9c | 2018-11-12 15:27:29 -0800 | [diff] [blame^] | 213 | * Tests pulled atoms with filtering |
| 214 | */ |
| 215 | TEST(ValueMetricProducerTest, TestPulledEventsWithFiltering) { |
| 216 | ValueMetric metric; |
| 217 | metric.set_id(metricId); |
| 218 | metric.set_bucket(ONE_MINUTE); |
| 219 | metric.mutable_value_field()->set_field(tagId); |
| 220 | metric.mutable_value_field()->add_child()->set_field(2); |
| 221 | |
| 222 | UidMap uidMap; |
| 223 | SimpleAtomMatcher atomMatcher; |
| 224 | atomMatcher.set_atom_id(tagId); |
| 225 | auto keyValue = atomMatcher.add_field_value_matcher(); |
| 226 | keyValue->set_field(1); |
| 227 | keyValue->set_eq_int(3); |
| 228 | sp<EventMatcherWizard> eventMatcherWizard = |
| 229 | new EventMatcherWizard({new SimpleLogMatchingTracker( |
| 230 | atomMatcherId, logEventMatcherIndex, atomMatcher, uidMap)}); |
| 231 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 232 | sp<MockStatsPullerManager> pullerManager = new StrictMock<MockStatsPullerManager>(); |
| 233 | EXPECT_CALL(*pullerManager, RegisterReceiver(tagId, _, _, _)).WillOnce(Return()); |
| 234 | EXPECT_CALL(*pullerManager, UnRegisterReceiver(tagId, _)).WillOnce(Return()); |
| 235 | EXPECT_CALL(*pullerManager, Pull(tagId, _, _)) |
| 236 | .WillOnce(Invoke([](int tagId, int64_t timeNs, |
| 237 | vector<std::shared_ptr<LogEvent>>* data) { |
| 238 | data->clear(); |
| 239 | shared_ptr<LogEvent> event = make_shared<LogEvent>(tagId, bucketStartTimeNs); |
| 240 | event->write(3); |
| 241 | event->write(3); |
| 242 | event->init(); |
| 243 | data->push_back(event); |
| 244 | return true; |
| 245 | })); |
| 246 | |
| 247 | ValueMetricProducer valueProducer(kConfigKey, metric, -1 /*-1 meaning no condition*/, wizard, |
| 248 | logEventMatcherIndex, eventMatcherWizard, tagId, |
| 249 | bucketStartTimeNs, bucketStartTimeNs, pullerManager); |
| 250 | |
| 251 | vector<shared_ptr<LogEvent>> allData; |
| 252 | allData.clear(); |
| 253 | shared_ptr<LogEvent> event = make_shared<LogEvent>(tagId, bucket2StartTimeNs + 1); |
| 254 | event->write(3); |
| 255 | event->write(11); |
| 256 | event->init(); |
| 257 | allData.push_back(event); |
| 258 | |
| 259 | valueProducer.onDataPulled(allData); |
| 260 | // has one slice |
| 261 | EXPECT_EQ(1UL, valueProducer.mCurrentSlicedBucket.size()); |
| 262 | ValueMetricProducer::Interval curInterval = |
| 263 | valueProducer.mCurrentSlicedBucket.begin()->second[0]; |
| 264 | |
| 265 | EXPECT_EQ(true, curInterval.hasBase); |
| 266 | EXPECT_EQ(11, curInterval.base.long_value); |
| 267 | EXPECT_EQ(true, curInterval.hasValue); |
| 268 | EXPECT_EQ(8, curInterval.value.long_value); |
| 269 | EXPECT_EQ(0UL, valueProducer.mPastBuckets.size()); |
| 270 | |
| 271 | allData.clear(); |
| 272 | event = make_shared<LogEvent>(tagId, bucket3StartTimeNs + 1); |
| 273 | event->write(4); |
| 274 | event->write(23); |
| 275 | event->init(); |
| 276 | allData.push_back(event); |
| 277 | valueProducer.onDataPulled(allData); |
| 278 | // has one slice |
| 279 | EXPECT_EQ(1UL, valueProducer.mCurrentSlicedBucket.size()); |
| 280 | curInterval = valueProducer.mCurrentSlicedBucket.begin()->second[0]; |
| 281 | |
| 282 | EXPECT_EQ(true, curInterval.hasBase); |
| 283 | EXPECT_EQ(11, curInterval.base.long_value); |
| 284 | // no events caused flush of bucket |
| 285 | EXPECT_EQ(true, curInterval.hasValue); |
| 286 | EXPECT_EQ(8, curInterval.value.long_value); |
| 287 | EXPECT_EQ(0UL, valueProducer.mPastBuckets.size()); |
| 288 | |
| 289 | allData.clear(); |
| 290 | event = make_shared<LogEvent>(tagId, bucket4StartTimeNs + 1); |
| 291 | event->write(3); |
| 292 | event->write(36); |
| 293 | event->init(); |
| 294 | allData.push_back(event); |
| 295 | valueProducer.onDataPulled(allData); |
| 296 | EXPECT_EQ(1UL, valueProducer.mCurrentSlicedBucket.size()); |
| 297 | curInterval = valueProducer.mCurrentSlicedBucket.begin()->second[0]; |
| 298 | |
| 299 | // the base was reset |
| 300 | EXPECT_EQ(true, curInterval.hasBase); |
| 301 | EXPECT_EQ(36, curInterval.base.long_value); |
| 302 | EXPECT_EQ(false, curInterval.hasValue); |
| 303 | EXPECT_EQ(1UL, valueProducer.mPastBuckets.size()); |
| 304 | EXPECT_EQ(1UL, valueProducer.mPastBuckets.begin()->second.size()); |
| 305 | EXPECT_EQ(8, valueProducer.mPastBuckets.begin()->second.back().values[0].long_value); |
| 306 | } |
| 307 | |
| 308 | /* |
Chenjie Yu | 021e253 | 2018-05-16 12:23:07 -0700 | [diff] [blame] | 309 | * Tests pulled atoms with no conditions and take absolute value after reset |
| 310 | */ |
| 311 | TEST(ValueMetricProducerTest, TestPulledEventsTakeAbsoluteValueOnReset) { |
| 312 | ValueMetric metric; |
| 313 | metric.set_id(metricId); |
| 314 | metric.set_bucket(ONE_MINUTE); |
| 315 | metric.mutable_value_field()->set_field(tagId); |
| 316 | metric.mutable_value_field()->add_child()->set_field(2); |
| 317 | metric.set_use_absolute_value_on_reset(true); |
| 318 | |
Chenjie Yu | 054ce9c | 2018-11-12 15:27:29 -0800 | [diff] [blame^] | 319 | UidMap uidMap; |
| 320 | SimpleAtomMatcher atomMatcher; |
| 321 | atomMatcher.set_atom_id(tagId); |
| 322 | sp<EventMatcherWizard> eventMatcherWizard = |
| 323 | new EventMatcherWizard({new SimpleLogMatchingTracker( |
| 324 | atomMatcherId, logEventMatcherIndex, atomMatcher, uidMap)}); |
Chenjie Yu | 021e253 | 2018-05-16 12:23:07 -0700 | [diff] [blame] | 325 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 326 | sp<MockStatsPullerManager> pullerManager = new StrictMock<MockStatsPullerManager>(); |
Chenjie Yu | 021e253 | 2018-05-16 12:23:07 -0700 | [diff] [blame] | 327 | EXPECT_CALL(*pullerManager, RegisterReceiver(tagId, _, _, _)).WillOnce(Return()); |
| 328 | EXPECT_CALL(*pullerManager, UnRegisterReceiver(tagId, _)).WillOnce(Return()); |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 329 | EXPECT_CALL(*pullerManager, Pull(tagId, _, _)).WillOnce(Return(true)); |
Chenjie Yu | 021e253 | 2018-05-16 12:23:07 -0700 | [diff] [blame] | 330 | |
| 331 | ValueMetricProducer valueProducer(kConfigKey, metric, -1 /*-1 meaning no condition*/, wizard, |
Chenjie Yu | 054ce9c | 2018-11-12 15:27:29 -0800 | [diff] [blame^] | 332 | logEventMatcherIndex, eventMatcherWizard, tagId, |
| 333 | bucketStartTimeNs, bucketStartTimeNs, pullerManager); |
Chenjie Yu | 021e253 | 2018-05-16 12:23:07 -0700 | [diff] [blame] | 334 | |
| 335 | vector<shared_ptr<LogEvent>> allData; |
| 336 | allData.clear(); |
| 337 | shared_ptr<LogEvent> event = make_shared<LogEvent>(tagId, bucket2StartTimeNs + 1); |
| 338 | event->write(tagId); |
| 339 | event->write(11); |
| 340 | event->init(); |
| 341 | allData.push_back(event); |
| 342 | |
| 343 | valueProducer.onDataPulled(allData); |
| 344 | // has one slice |
| 345 | EXPECT_EQ(1UL, valueProducer.mCurrentSlicedBucket.size()); |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 346 | ValueMetricProducer::Interval curInterval = valueProducer.mCurrentSlicedBucket.begin()->second[0]; |
Chenjie Yu | 021e253 | 2018-05-16 12:23:07 -0700 | [diff] [blame] | 347 | |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 348 | EXPECT_EQ(true, curInterval.hasBase); |
| 349 | EXPECT_EQ(11, curInterval.base.long_value); |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 350 | EXPECT_EQ(false, curInterval.hasValue); |
Chenjie Yu | 021e253 | 2018-05-16 12:23:07 -0700 | [diff] [blame] | 351 | EXPECT_EQ(0UL, valueProducer.mPastBuckets.size()); |
| 352 | |
| 353 | allData.clear(); |
| 354 | event = make_shared<LogEvent>(tagId, bucket3StartTimeNs + 1); |
| 355 | event->write(tagId); |
| 356 | event->write(10); |
| 357 | event->init(); |
| 358 | allData.push_back(event); |
| 359 | valueProducer.onDataPulled(allData); |
| 360 | // has one slice |
| 361 | EXPECT_EQ(1UL, valueProducer.mCurrentSlicedBucket.size()); |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 362 | curInterval = valueProducer.mCurrentSlicedBucket.begin()->second[0]; |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 363 | EXPECT_EQ(true, curInterval.hasBase); |
| 364 | EXPECT_EQ(10, curInterval.base.long_value); |
| 365 | EXPECT_EQ(true, curInterval.hasValue); |
| 366 | EXPECT_EQ(10, curInterval.value.long_value); |
| 367 | EXPECT_EQ(0UL, valueProducer.mPastBuckets.size()); |
Chenjie Yu | 021e253 | 2018-05-16 12:23:07 -0700 | [diff] [blame] | 368 | |
| 369 | allData.clear(); |
| 370 | event = make_shared<LogEvent>(tagId, bucket4StartTimeNs + 1); |
| 371 | event->write(tagId); |
| 372 | event->write(36); |
| 373 | event->init(); |
| 374 | allData.push_back(event); |
| 375 | valueProducer.onDataPulled(allData); |
| 376 | EXPECT_EQ(1UL, valueProducer.mCurrentSlicedBucket.size()); |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 377 | curInterval = valueProducer.mCurrentSlicedBucket.begin()->second[0]; |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 378 | EXPECT_EQ(true, curInterval.hasBase); |
| 379 | EXPECT_EQ(36, curInterval.base.long_value); |
| 380 | EXPECT_EQ(true, curInterval.hasValue); |
| 381 | EXPECT_EQ(26, curInterval.value.long_value); |
Chenjie Yu | 021e253 | 2018-05-16 12:23:07 -0700 | [diff] [blame] | 382 | EXPECT_EQ(1UL, valueProducer.mPastBuckets.size()); |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 383 | EXPECT_EQ(1UL, valueProducer.mPastBuckets.begin()->second.size()); |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 384 | EXPECT_EQ(10, valueProducer.mPastBuckets.begin()->second.back().values[0].long_value); |
Chenjie Yu | 021e253 | 2018-05-16 12:23:07 -0700 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | /* |
| 388 | * Tests pulled atoms with no conditions and take zero value after reset |
| 389 | */ |
| 390 | TEST(ValueMetricProducerTest, TestPulledEventsTakeZeroOnReset) { |
| 391 | ValueMetric metric; |
| 392 | metric.set_id(metricId); |
| 393 | metric.set_bucket(ONE_MINUTE); |
| 394 | metric.mutable_value_field()->set_field(tagId); |
| 395 | metric.mutable_value_field()->add_child()->set_field(2); |
| 396 | |
Chenjie Yu | 054ce9c | 2018-11-12 15:27:29 -0800 | [diff] [blame^] | 397 | UidMap uidMap; |
| 398 | SimpleAtomMatcher atomMatcher; |
| 399 | atomMatcher.set_atom_id(tagId); |
| 400 | sp<EventMatcherWizard> eventMatcherWizard = |
| 401 | new EventMatcherWizard({new SimpleLogMatchingTracker( |
| 402 | atomMatcherId, logEventMatcherIndex, atomMatcher, uidMap)}); |
Chenjie Yu | 021e253 | 2018-05-16 12:23:07 -0700 | [diff] [blame] | 403 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 404 | sp<MockStatsPullerManager> pullerManager = new StrictMock<MockStatsPullerManager>(); |
Chenjie Yu | 021e253 | 2018-05-16 12:23:07 -0700 | [diff] [blame] | 405 | EXPECT_CALL(*pullerManager, RegisterReceiver(tagId, _, _, _)).WillOnce(Return()); |
| 406 | EXPECT_CALL(*pullerManager, UnRegisterReceiver(tagId, _)).WillOnce(Return()); |
Chenjie Yu | e1361ed | 2018-07-23 17:33:09 -0700 | [diff] [blame] | 407 | EXPECT_CALL(*pullerManager, Pull(tagId, _, _)).WillOnce(Return(false)); |
Chenjie Yu | 021e253 | 2018-05-16 12:23:07 -0700 | [diff] [blame] | 408 | |
| 409 | ValueMetricProducer valueProducer(kConfigKey, metric, -1 /*-1 meaning no condition*/, wizard, |
Chenjie Yu | 054ce9c | 2018-11-12 15:27:29 -0800 | [diff] [blame^] | 410 | logEventMatcherIndex, eventMatcherWizard, tagId, |
| 411 | bucketStartTimeNs, bucketStartTimeNs, pullerManager); |
Chenjie Yu | 021e253 | 2018-05-16 12:23:07 -0700 | [diff] [blame] | 412 | |
| 413 | vector<shared_ptr<LogEvent>> allData; |
| 414 | allData.clear(); |
| 415 | shared_ptr<LogEvent> event = make_shared<LogEvent>(tagId, bucket2StartTimeNs + 1); |
| 416 | event->write(tagId); |
| 417 | event->write(11); |
| 418 | event->init(); |
| 419 | allData.push_back(event); |
| 420 | |
| 421 | valueProducer.onDataPulled(allData); |
| 422 | // has one slice |
| 423 | EXPECT_EQ(1UL, valueProducer.mCurrentSlicedBucket.size()); |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 424 | ValueMetricProducer::Interval curInterval = valueProducer.mCurrentSlicedBucket.begin()->second[0]; |
Chenjie Yu | 021e253 | 2018-05-16 12:23:07 -0700 | [diff] [blame] | 425 | |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 426 | EXPECT_EQ(true, curInterval.hasBase); |
| 427 | EXPECT_EQ(11, curInterval.base.long_value); |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 428 | EXPECT_EQ(false, curInterval.hasValue); |
Chenjie Yu | 021e253 | 2018-05-16 12:23:07 -0700 | [diff] [blame] | 429 | EXPECT_EQ(0UL, valueProducer.mPastBuckets.size()); |
| 430 | |
| 431 | allData.clear(); |
| 432 | event = make_shared<LogEvent>(tagId, bucket3StartTimeNs + 1); |
| 433 | event->write(tagId); |
| 434 | event->write(10); |
| 435 | event->init(); |
| 436 | allData.push_back(event); |
| 437 | valueProducer.onDataPulled(allData); |
| 438 | // has one slice |
| 439 | EXPECT_EQ(1UL, valueProducer.mCurrentSlicedBucket.size()); |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 440 | curInterval = valueProducer.mCurrentSlicedBucket.begin()->second[0]; |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 441 | EXPECT_EQ(true, curInterval.hasBase); |
| 442 | EXPECT_EQ(10, curInterval.base.long_value); |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 443 | EXPECT_EQ(false, curInterval.hasValue); |
Chenjie Yu | 021e253 | 2018-05-16 12:23:07 -0700 | [diff] [blame] | 444 | EXPECT_EQ(0UL, valueProducer.mPastBuckets.size()); |
| 445 | |
| 446 | allData.clear(); |
| 447 | event = make_shared<LogEvent>(tagId, bucket4StartTimeNs + 1); |
| 448 | event->write(tagId); |
| 449 | event->write(36); |
| 450 | event->init(); |
| 451 | allData.push_back(event); |
| 452 | valueProducer.onDataPulled(allData); |
| 453 | EXPECT_EQ(1UL, valueProducer.mCurrentSlicedBucket.size()); |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 454 | curInterval = valueProducer.mCurrentSlicedBucket.begin()->second[0]; |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 455 | EXPECT_EQ(true, curInterval.hasBase); |
| 456 | EXPECT_EQ(36, curInterval.base.long_value); |
| 457 | EXPECT_EQ(true, curInterval.hasValue); |
| 458 | EXPECT_EQ(26, curInterval.value.long_value); |
| 459 | EXPECT_EQ(0UL, valueProducer.mPastBuckets.size()); |
Chenjie Yu | 021e253 | 2018-05-16 12:23:07 -0700 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | /* |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 463 | * Test pulled event with non sliced condition. |
| 464 | */ |
| 465 | TEST(ValueMetricProducerTest, TestEventsWithNonSlicedCondition) { |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 466 | ValueMetric metric; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 467 | metric.set_id(metricId); |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 468 | metric.set_bucket(ONE_MINUTE); |
Yangster-mac | a7fb12d | 2018-01-03 17:17:20 -0800 | [diff] [blame] | 469 | metric.mutable_value_field()->set_field(tagId); |
| 470 | metric.mutable_value_field()->add_child()->set_field(2); |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 471 | metric.set_condition(StringToId("SCREEN_ON")); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 472 | |
Chenjie Yu | 054ce9c | 2018-11-12 15:27:29 -0800 | [diff] [blame^] | 473 | UidMap uidMap; |
| 474 | SimpleAtomMatcher atomMatcher; |
| 475 | atomMatcher.set_atom_id(tagId); |
| 476 | sp<EventMatcherWizard> eventMatcherWizard = |
| 477 | new EventMatcherWizard({new SimpleLogMatchingTracker( |
| 478 | atomMatcherId, logEventMatcherIndex, atomMatcher, uidMap)}); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 479 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 480 | sp<MockStatsPullerManager> pullerManager = new StrictMock<MockStatsPullerManager>(); |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 481 | EXPECT_CALL(*pullerManager, RegisterReceiver(tagId, _, _, _)).WillOnce(Return()); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 482 | EXPECT_CALL(*pullerManager, UnRegisterReceiver(tagId, _)).WillRepeatedly(Return()); |
| 483 | |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 484 | EXPECT_CALL(*pullerManager, Pull(tagId, _, _)) |
Chenjie Yu | e1361ed | 2018-07-23 17:33:09 -0700 | [diff] [blame] | 485 | .WillOnce(Invoke([](int tagId, int64_t timeNs, |
| 486 | vector<std::shared_ptr<LogEvent>>* data) { |
| 487 | data->clear(); |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 488 | shared_ptr<LogEvent> event = make_shared<LogEvent>(tagId, bucketStartTimeNs + 8); |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 489 | event->write(tagId); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 490 | event->write(100); |
| 491 | event->init(); |
| 492 | data->push_back(event); |
| 493 | return true; |
| 494 | })) |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 495 | .WillOnce(Invoke([](int tagId, int64_t timeNs, |
| 496 | vector<std::shared_ptr<LogEvent>>* data) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 497 | data->clear(); |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 498 | shared_ptr<LogEvent> event = make_shared<LogEvent>(tagId, bucket2StartTimeNs + 1); |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 499 | event->write(tagId); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 500 | event->write(120); |
| 501 | event->init(); |
| 502 | data->push_back(event); |
| 503 | return true; |
| 504 | })); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 505 | |
Chenjie Yu | 054ce9c | 2018-11-12 15:27:29 -0800 | [diff] [blame^] | 506 | ValueMetricProducer valueProducer(kConfigKey, metric, 1, wizard, logEventMatcherIndex, |
| 507 | eventMatcherWizard, tagId, bucketStartTimeNs, |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 508 | bucketStartTimeNs, pullerManager); |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 509 | valueProducer.onConditionChanged(true, bucketStartTimeNs + 8); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 510 | |
| 511 | // has one slice |
| 512 | EXPECT_EQ(1UL, valueProducer.mCurrentSlicedBucket.size()); |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 513 | ValueMetricProducer::Interval curInterval = valueProducer.mCurrentSlicedBucket.begin()->second[0]; |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 514 | // startUpdated:false sum:0 start:100 |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 515 | EXPECT_EQ(true, curInterval.hasBase); |
| 516 | EXPECT_EQ(100, curInterval.base.long_value); |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 517 | EXPECT_EQ(false, curInterval.hasValue); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 518 | EXPECT_EQ(0UL, valueProducer.mPastBuckets.size()); |
| 519 | |
| 520 | vector<shared_ptr<LogEvent>> allData; |
| 521 | allData.clear(); |
| 522 | shared_ptr<LogEvent> event = make_shared<LogEvent>(tagId, bucket2StartTimeNs + 1); |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 523 | event->write(1); |
| 524 | event->write(110); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 525 | event->init(); |
| 526 | allData.push_back(event); |
| 527 | valueProducer.onDataPulled(allData); |
| 528 | |
| 529 | // has one slice |
| 530 | EXPECT_EQ(1UL, valueProducer.mCurrentSlicedBucket.size()); |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 531 | curInterval = valueProducer.mCurrentSlicedBucket.begin()->second[0]; |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 532 | EXPECT_EQ(true, curInterval.hasBase); |
| 533 | EXPECT_EQ(110, curInterval.base.long_value); |
| 534 | EXPECT_EQ(true, curInterval.hasValue); |
| 535 | EXPECT_EQ(10, curInterval.value.long_value); |
| 536 | EXPECT_EQ(0UL, valueProducer.mPastBuckets.size()); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 537 | |
| 538 | valueProducer.onConditionChanged(false, bucket2StartTimeNs + 1); |
| 539 | |
| 540 | // has one slice |
| 541 | EXPECT_EQ(1UL, valueProducer.mCurrentSlicedBucket.size()); |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 542 | curInterval = valueProducer.mCurrentSlicedBucket.begin()->second[0]; |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 543 | EXPECT_EQ(true, curInterval.hasValue); |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 544 | EXPECT_EQ(10, curInterval.value.long_value); |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 545 | EXPECT_EQ(false, curInterval.hasBase); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 546 | } |
| 547 | |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 548 | TEST(ValueMetricProducerTest, TestPushedEventsWithUpgrade) { |
| 549 | ValueMetric metric; |
| 550 | metric.set_id(metricId); |
| 551 | metric.set_bucket(ONE_MINUTE); |
| 552 | metric.mutable_value_field()->set_field(tagId); |
| 553 | metric.mutable_value_field()->add_child()->set_field(2); |
| 554 | |
Chenjie Yu | 054ce9c | 2018-11-12 15:27:29 -0800 | [diff] [blame^] | 555 | UidMap uidMap; |
| 556 | SimpleAtomMatcher atomMatcher; |
| 557 | atomMatcher.set_atom_id(tagId); |
| 558 | sp<EventMatcherWizard> eventMatcherWizard = |
| 559 | new EventMatcherWizard({new SimpleLogMatchingTracker( |
| 560 | atomMatcherId, logEventMatcherIndex, atomMatcher, uidMap)}); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 561 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 562 | sp<MockStatsPullerManager> pullerManager = new StrictMock<MockStatsPullerManager>(); |
Chenjie Yu | 054ce9c | 2018-11-12 15:27:29 -0800 | [diff] [blame^] | 563 | ValueMetricProducer valueProducer(kConfigKey, metric, -1, wizard, logEventMatcherIndex, |
| 564 | eventMatcherWizard, -1, bucketStartTimeNs, bucketStartTimeNs, |
| 565 | pullerManager); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 566 | |
| 567 | shared_ptr<LogEvent> event1 = make_shared<LogEvent>(tagId, bucketStartTimeNs + 10); |
| 568 | event1->write(1); |
| 569 | event1->write(10); |
| 570 | event1->init(); |
| 571 | valueProducer.onMatchedLogEvent(1 /*log matcher index*/, *event1); |
| 572 | EXPECT_EQ(1UL, valueProducer.mCurrentSlicedBucket.size()); |
| 573 | |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 574 | valueProducer.notifyAppUpgrade(bucketStartTimeNs + 150, "ANY.APP", 1, 1); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 575 | EXPECT_EQ(1UL, valueProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY].size()); |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 576 | EXPECT_EQ(bucketStartTimeNs + 150, valueProducer.mCurrentBucketStartTimeNs); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 577 | |
| 578 | shared_ptr<LogEvent> event2 = make_shared<LogEvent>(tagId, bucketStartTimeNs + 59 * NS_PER_SEC); |
| 579 | event2->write(1); |
| 580 | event2->write(10); |
| 581 | event2->init(); |
| 582 | valueProducer.onMatchedLogEvent(1 /*log matcher index*/, *event2); |
| 583 | EXPECT_EQ(1UL, valueProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY].size()); |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 584 | EXPECT_EQ(bucketStartTimeNs + 150, valueProducer.mCurrentBucketStartTimeNs); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 585 | |
| 586 | // Next value should create a new bucket. |
| 587 | shared_ptr<LogEvent> event3 = make_shared<LogEvent>(tagId, bucketStartTimeNs + 65 * NS_PER_SEC); |
| 588 | event3->write(1); |
| 589 | event3->write(10); |
| 590 | event3->init(); |
| 591 | valueProducer.onMatchedLogEvent(1 /*log matcher index*/, *event3); |
| 592 | EXPECT_EQ(2UL, valueProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY].size()); |
Yangster-mac | b142cc8 | 2018-03-30 15:22:08 -0700 | [diff] [blame] | 593 | EXPECT_EQ(bucketStartTimeNs + bucketSizeNs, valueProducer.mCurrentBucketStartTimeNs); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | TEST(ValueMetricProducerTest, TestPulledValueWithUpgrade) { |
| 597 | ValueMetric metric; |
| 598 | metric.set_id(metricId); |
| 599 | metric.set_bucket(ONE_MINUTE); |
| 600 | metric.mutable_value_field()->set_field(tagId); |
| 601 | metric.mutable_value_field()->add_child()->set_field(2); |
| 602 | |
Chenjie Yu | 054ce9c | 2018-11-12 15:27:29 -0800 | [diff] [blame^] | 603 | UidMap uidMap; |
| 604 | SimpleAtomMatcher atomMatcher; |
| 605 | atomMatcher.set_atom_id(tagId); |
| 606 | sp<EventMatcherWizard> eventMatcherWizard = |
| 607 | new EventMatcherWizard({new SimpleLogMatchingTracker( |
| 608 | atomMatcherId, logEventMatcherIndex, atomMatcher, uidMap)}); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 609 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 610 | sp<MockStatsPullerManager> pullerManager = new StrictMock<MockStatsPullerManager>(); |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 611 | EXPECT_CALL(*pullerManager, RegisterReceiver(tagId, _, _, _)).WillOnce(Return()); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 612 | EXPECT_CALL(*pullerManager, UnRegisterReceiver(tagId, _)).WillOnce(Return()); |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 613 | EXPECT_CALL(*pullerManager, Pull(tagId, _, _)) |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 614 | .WillOnce(Return(true)) |
Chenjie Yu | 1a0a941 | 2018-03-28 10:07:22 -0700 | [diff] [blame] | 615 | .WillOnce(Invoke([](int tagId, int64_t timeNs, |
| 616 | vector<std::shared_ptr<LogEvent>>* data) { |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 617 | data->clear(); |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 618 | shared_ptr<LogEvent> event = make_shared<LogEvent>(tagId, bucket2StartTimeNs + 149); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 619 | event->write(tagId); |
| 620 | event->write(120); |
| 621 | event->init(); |
| 622 | data->push_back(event); |
| 623 | return true; |
| 624 | })); |
Chenjie Yu | 054ce9c | 2018-11-12 15:27:29 -0800 | [diff] [blame^] | 625 | ValueMetricProducer valueProducer(kConfigKey, metric, -1, wizard, logEventMatcherIndex, |
| 626 | eventMatcherWizard, tagId, bucketStartTimeNs, |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 627 | bucketStartTimeNs, pullerManager); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 628 | |
| 629 | vector<shared_ptr<LogEvent>> allData; |
| 630 | allData.clear(); |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 631 | shared_ptr<LogEvent> event = make_shared<LogEvent>(tagId, bucket2StartTimeNs + 1); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 632 | event->write(tagId); |
| 633 | event->write(100); |
| 634 | event->init(); |
| 635 | allData.push_back(event); |
| 636 | |
| 637 | valueProducer.onDataPulled(allData); |
| 638 | EXPECT_EQ(1UL, valueProducer.mCurrentSlicedBucket.size()); |
| 639 | |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 640 | valueProducer.notifyAppUpgrade(bucket2StartTimeNs + 150, "ANY.APP", 1, 1); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 641 | EXPECT_EQ(1UL, valueProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY].size()); |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 642 | EXPECT_EQ(bucket2StartTimeNs + 150, valueProducer.mCurrentBucketStartTimeNs); |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 643 | EXPECT_EQ(20L, valueProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY][0].values[0].long_value); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 644 | |
| 645 | allData.clear(); |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 646 | event = make_shared<LogEvent>(tagId, bucket3StartTimeNs + 1); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 647 | event->write(tagId); |
| 648 | event->write(150); |
| 649 | event->init(); |
| 650 | allData.push_back(event); |
| 651 | valueProducer.onDataPulled(allData); |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 652 | EXPECT_EQ(1UL, valueProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY].size()); |
| 653 | EXPECT_EQ(bucket2StartTimeNs + 150, valueProducer.mCurrentBucketStartTimeNs); |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 654 | EXPECT_EQ(20L, valueProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY][0].values[0].long_value); |
David Chen | 27785a8 | 2018-01-19 17:06:45 -0800 | [diff] [blame] | 655 | } |
| 656 | |
David Chen | 092a5a9 | 2018-05-15 17:50:32 -0700 | [diff] [blame] | 657 | TEST(ValueMetricProducerTest, TestPulledValueWithUpgradeWhileConditionFalse) { |
| 658 | ValueMetric metric; |
| 659 | metric.set_id(metricId); |
| 660 | metric.set_bucket(ONE_MINUTE); |
| 661 | metric.mutable_value_field()->set_field(tagId); |
| 662 | metric.mutable_value_field()->add_child()->set_field(2); |
| 663 | metric.set_condition(StringToId("SCREEN_ON")); |
| 664 | |
Chenjie Yu | 054ce9c | 2018-11-12 15:27:29 -0800 | [diff] [blame^] | 665 | UidMap uidMap; |
| 666 | SimpleAtomMatcher atomMatcher; |
| 667 | atomMatcher.set_atom_id(tagId); |
| 668 | sp<EventMatcherWizard> eventMatcherWizard = |
| 669 | new EventMatcherWizard({new SimpleLogMatchingTracker( |
| 670 | atomMatcherId, logEventMatcherIndex, atomMatcher, uidMap)}); |
David Chen | 092a5a9 | 2018-05-15 17:50:32 -0700 | [diff] [blame] | 671 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 672 | sp<MockStatsPullerManager> pullerManager = new StrictMock<MockStatsPullerManager>(); |
David Chen | 092a5a9 | 2018-05-15 17:50:32 -0700 | [diff] [blame] | 673 | EXPECT_CALL(*pullerManager, RegisterReceiver(tagId, _, _, _)).WillOnce(Return()); |
| 674 | EXPECT_CALL(*pullerManager, UnRegisterReceiver(tagId, _)).WillOnce(Return()); |
| 675 | EXPECT_CALL(*pullerManager, Pull(tagId, _, _)) |
| 676 | .WillOnce(Invoke([](int tagId, int64_t timeNs, |
| 677 | vector<std::shared_ptr<LogEvent>>* data) { |
| 678 | data->clear(); |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 679 | shared_ptr<LogEvent> event = make_shared<LogEvent>(tagId, bucketStartTimeNs + 1); |
David Chen | 092a5a9 | 2018-05-15 17:50:32 -0700 | [diff] [blame] | 680 | event->write(tagId); |
| 681 | event->write(100); |
| 682 | event->init(); |
| 683 | data->push_back(event); |
| 684 | return true; |
| 685 | })) |
| 686 | .WillOnce(Invoke([](int tagId, int64_t timeNs, |
| 687 | vector<std::shared_ptr<LogEvent>>* data) { |
| 688 | data->clear(); |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 689 | shared_ptr<LogEvent> event = make_shared<LogEvent>(tagId, bucket2StartTimeNs - 100); |
David Chen | 092a5a9 | 2018-05-15 17:50:32 -0700 | [diff] [blame] | 690 | event->write(tagId); |
| 691 | event->write(120); |
| 692 | event->init(); |
| 693 | data->push_back(event); |
| 694 | return true; |
| 695 | })); |
Chenjie Yu | 054ce9c | 2018-11-12 15:27:29 -0800 | [diff] [blame^] | 696 | ValueMetricProducer valueProducer(kConfigKey, metric, 1, wizard, logEventMatcherIndex, |
| 697 | eventMatcherWizard, tagId, bucketStartTimeNs, |
David Chen | 092a5a9 | 2018-05-15 17:50:32 -0700 | [diff] [blame] | 698 | bucketStartTimeNs, pullerManager); |
David Chen | 092a5a9 | 2018-05-15 17:50:32 -0700 | [diff] [blame] | 699 | valueProducer.onConditionChanged(true, bucketStartTimeNs + 1); |
| 700 | |
| 701 | valueProducer.onConditionChanged(false, bucket2StartTimeNs-100); |
| 702 | EXPECT_FALSE(valueProducer.mCondition); |
| 703 | |
| 704 | valueProducer.notifyAppUpgrade(bucket2StartTimeNs-50, "ANY.APP", 1, 1); |
| 705 | // Expect one full buckets already done and starting a partial bucket. |
| 706 | EXPECT_EQ(bucket2StartTimeNs-50, valueProducer.mCurrentBucketStartTimeNs); |
| 707 | EXPECT_EQ(1UL, valueProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY].size()); |
| 708 | EXPECT_EQ(bucketStartTimeNs, valueProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY][0].mBucketStartNs); |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 709 | EXPECT_EQ(20L, valueProducer.mPastBuckets[DEFAULT_METRIC_DIMENSION_KEY][0].values[0].long_value); |
David Chen | 092a5a9 | 2018-05-15 17:50:32 -0700 | [diff] [blame] | 710 | EXPECT_FALSE(valueProducer.mCondition); |
| 711 | } |
| 712 | |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 713 | TEST(ValueMetricProducerTest, TestPushedEventsWithoutCondition) { |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 714 | ValueMetric metric; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 715 | metric.set_id(metricId); |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 716 | metric.set_bucket(ONE_MINUTE); |
Yangster-mac | a7fb12d | 2018-01-03 17:17:20 -0800 | [diff] [blame] | 717 | metric.mutable_value_field()->set_field(tagId); |
| 718 | metric.mutable_value_field()->add_child()->set_field(2); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 719 | |
Chenjie Yu | 054ce9c | 2018-11-12 15:27:29 -0800 | [diff] [blame^] | 720 | UidMap uidMap; |
| 721 | SimpleAtomMatcher atomMatcher; |
| 722 | atomMatcher.set_atom_id(tagId); |
| 723 | sp<EventMatcherWizard> eventMatcherWizard = |
| 724 | new EventMatcherWizard({new SimpleLogMatchingTracker( |
| 725 | atomMatcherId, logEventMatcherIndex, atomMatcher, uidMap)}); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 726 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 727 | sp<MockStatsPullerManager> pullerManager = new StrictMock<MockStatsPullerManager>(); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 728 | |
Chenjie Yu | 054ce9c | 2018-11-12 15:27:29 -0800 | [diff] [blame^] | 729 | ValueMetricProducer valueProducer(kConfigKey, metric, -1, wizard, logEventMatcherIndex, |
| 730 | eventMatcherWizard, -1, bucketStartTimeNs, bucketStartTimeNs, |
| 731 | pullerManager); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 732 | |
| 733 | shared_ptr<LogEvent> event1 = make_shared<LogEvent>(tagId, bucketStartTimeNs + 10); |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 734 | event1->write(1); |
| 735 | event1->write(10); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 736 | event1->init(); |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 737 | shared_ptr<LogEvent> event2 = make_shared<LogEvent>(tagId, bucketStartTimeNs + 20); |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 738 | event2->write(1); |
| 739 | event2->write(20); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 740 | event2->init(); |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 741 | valueProducer.onMatchedLogEvent(1 /*log matcher index*/, *event1); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 742 | // has one slice |
| 743 | EXPECT_EQ(1UL, valueProducer.mCurrentSlicedBucket.size()); |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 744 | ValueMetricProducer::Interval curInterval = valueProducer.mCurrentSlicedBucket.begin()->second[0]; |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 745 | EXPECT_EQ(10, curInterval.value.long_value); |
| 746 | EXPECT_EQ(true, curInterval.hasValue); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 747 | |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame] | 748 | valueProducer.onMatchedLogEvent(1 /*log matcher index*/, *event2); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 749 | |
| 750 | // has one slice |
| 751 | EXPECT_EQ(1UL, valueProducer.mCurrentSlicedBucket.size()); |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 752 | curInterval = valueProducer.mCurrentSlicedBucket.begin()->second[0]; |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 753 | EXPECT_EQ(30, curInterval.value.long_value); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 754 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 755 | valueProducer.flushIfNeededLocked(bucket3StartTimeNs); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 756 | EXPECT_EQ(1UL, valueProducer.mPastBuckets.size()); |
| 757 | EXPECT_EQ(1UL, valueProducer.mPastBuckets.begin()->second.size()); |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 758 | EXPECT_EQ(30, valueProducer.mPastBuckets.begin()->second.back().values[0].long_value); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 759 | } |
| 760 | |
Chenjie Yu | 021e253 | 2018-05-16 12:23:07 -0700 | [diff] [blame] | 761 | TEST(ValueMetricProducerTest, TestPushedEventsWithCondition) { |
| 762 | ValueMetric metric; |
| 763 | metric.set_id(metricId); |
| 764 | metric.set_bucket(ONE_MINUTE); |
| 765 | metric.mutable_value_field()->set_field(tagId); |
| 766 | metric.mutable_value_field()->add_child()->set_field(2); |
| 767 | |
Chenjie Yu | 054ce9c | 2018-11-12 15:27:29 -0800 | [diff] [blame^] | 768 | UidMap uidMap; |
| 769 | SimpleAtomMatcher atomMatcher; |
| 770 | atomMatcher.set_atom_id(tagId); |
| 771 | sp<EventMatcherWizard> eventMatcherWizard = |
| 772 | new EventMatcherWizard({new SimpleLogMatchingTracker( |
| 773 | atomMatcherId, logEventMatcherIndex, atomMatcher, uidMap)}); |
Chenjie Yu | 021e253 | 2018-05-16 12:23:07 -0700 | [diff] [blame] | 774 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 775 | sp<MockStatsPullerManager> pullerManager = new StrictMock<MockStatsPullerManager>(); |
Chenjie Yu | 021e253 | 2018-05-16 12:23:07 -0700 | [diff] [blame] | 776 | |
Chenjie Yu | 054ce9c | 2018-11-12 15:27:29 -0800 | [diff] [blame^] | 777 | ValueMetricProducer valueProducer(kConfigKey, metric, 1, wizard, logEventMatcherIndex, |
| 778 | eventMatcherWizard, -1, bucketStartTimeNs, bucketStartTimeNs, |
| 779 | pullerManager); |
Chenjie Yu | 021e253 | 2018-05-16 12:23:07 -0700 | [diff] [blame] | 780 | |
| 781 | shared_ptr<LogEvent> event1 = make_shared<LogEvent>(tagId, bucketStartTimeNs + 10); |
| 782 | event1->write(1); |
| 783 | event1->write(10); |
| 784 | event1->init(); |
| 785 | valueProducer.onMatchedLogEvent(1 /*log matcher index*/, *event1); |
| 786 | // has 1 slice |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 787 | EXPECT_EQ(0UL, valueProducer.mCurrentSlicedBucket.size()); |
Chenjie Yu | 021e253 | 2018-05-16 12:23:07 -0700 | [diff] [blame] | 788 | |
| 789 | valueProducer.onConditionChangedLocked(true, bucketStartTimeNs + 15); |
| 790 | shared_ptr<LogEvent> event2 = make_shared<LogEvent>(tagId, bucketStartTimeNs + 20); |
| 791 | event2->write(1); |
| 792 | event2->write(20); |
| 793 | event2->init(); |
| 794 | valueProducer.onMatchedLogEvent(1 /*log matcher index*/, *event2); |
| 795 | |
| 796 | // has one slice |
| 797 | EXPECT_EQ(1UL, valueProducer.mCurrentSlicedBucket.size()); |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 798 | ValueMetricProducer::Interval curInterval = valueProducer.mCurrentSlicedBucket.begin()->second[0]; |
| 799 | curInterval = valueProducer.mCurrentSlicedBucket.begin()->second[0]; |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 800 | EXPECT_EQ(20, curInterval.value.long_value); |
Chenjie Yu | 021e253 | 2018-05-16 12:23:07 -0700 | [diff] [blame] | 801 | |
| 802 | shared_ptr<LogEvent> event3 = make_shared<LogEvent>(tagId, bucketStartTimeNs + 30); |
| 803 | event3->write(1); |
| 804 | event3->write(30); |
| 805 | event3->init(); |
| 806 | valueProducer.onMatchedLogEvent(1 /*log matcher index*/, *event3); |
| 807 | |
| 808 | // has one slice |
| 809 | EXPECT_EQ(1UL, valueProducer.mCurrentSlicedBucket.size()); |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 810 | curInterval = valueProducer.mCurrentSlicedBucket.begin()->second[0]; |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 811 | EXPECT_EQ(50, curInterval.value.long_value); |
Chenjie Yu | 021e253 | 2018-05-16 12:23:07 -0700 | [diff] [blame] | 812 | |
| 813 | valueProducer.onConditionChangedLocked(false, bucketStartTimeNs + 35); |
| 814 | shared_ptr<LogEvent> event4 = make_shared<LogEvent>(tagId, bucketStartTimeNs + 40); |
| 815 | event4->write(1); |
| 816 | event4->write(40); |
| 817 | event4->init(); |
| 818 | valueProducer.onMatchedLogEvent(1 /*log matcher index*/, *event4); |
| 819 | |
| 820 | // has one slice |
| 821 | EXPECT_EQ(1UL, valueProducer.mCurrentSlicedBucket.size()); |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 822 | curInterval = valueProducer.mCurrentSlicedBucket.begin()->second[0]; |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 823 | EXPECT_EQ(50, curInterval.value.long_value); |
Chenjie Yu | 021e253 | 2018-05-16 12:23:07 -0700 | [diff] [blame] | 824 | |
| 825 | valueProducer.flushIfNeededLocked(bucket3StartTimeNs); |
| 826 | EXPECT_EQ(1UL, valueProducer.mPastBuckets.size()); |
| 827 | EXPECT_EQ(1UL, valueProducer.mPastBuckets.begin()->second.size()); |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 828 | EXPECT_EQ(50, valueProducer.mPastBuckets.begin()->second.back().values[0].long_value); |
Chenjie Yu | 021e253 | 2018-05-16 12:23:07 -0700 | [diff] [blame] | 829 | } |
| 830 | |
Bookatz | de1b5562 | 2017-12-14 18:38:27 -0800 | [diff] [blame] | 831 | TEST(ValueMetricProducerTest, TestAnomalyDetection) { |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 832 | sp<AlarmMonitor> alarmMonitor; |
Bookatz | de1b5562 | 2017-12-14 18:38:27 -0800 | [diff] [blame] | 833 | Alert alert; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 834 | alert.set_id(101); |
| 835 | alert.set_metric_id(metricId); |
Bookatz | de1b5562 | 2017-12-14 18:38:27 -0800 | [diff] [blame] | 836 | alert.set_trigger_if_sum_gt(130); |
Yangster-mac | a7fb12d | 2018-01-03 17:17:20 -0800 | [diff] [blame] | 837 | alert.set_num_buckets(2); |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 838 | const int32_t refPeriodSec = 3; |
| 839 | alert.set_refractory_period_secs(refPeriodSec); |
Bookatz | de1b5562 | 2017-12-14 18:38:27 -0800 | [diff] [blame] | 840 | |
| 841 | ValueMetric metric; |
Yangster-mac | 94e197c | 2018-01-02 16:03:03 -0800 | [diff] [blame] | 842 | metric.set_id(metricId); |
Yangster-mac | b814481 | 2018-01-04 10:56:23 -0800 | [diff] [blame] | 843 | metric.set_bucket(ONE_MINUTE); |
Yangster-mac | a7fb12d | 2018-01-03 17:17:20 -0800 | [diff] [blame] | 844 | metric.mutable_value_field()->set_field(tagId); |
| 845 | metric.mutable_value_field()->add_child()->set_field(2); |
Bookatz | de1b5562 | 2017-12-14 18:38:27 -0800 | [diff] [blame] | 846 | |
Chenjie Yu | 054ce9c | 2018-11-12 15:27:29 -0800 | [diff] [blame^] | 847 | UidMap uidMap; |
| 848 | SimpleAtomMatcher atomMatcher; |
| 849 | atomMatcher.set_atom_id(tagId); |
| 850 | sp<EventMatcherWizard> eventMatcherWizard = |
| 851 | new EventMatcherWizard({new SimpleLogMatchingTracker( |
| 852 | atomMatcherId, logEventMatcherIndex, atomMatcher, uidMap)}); |
Bookatz | de1b5562 | 2017-12-14 18:38:27 -0800 | [diff] [blame] | 853 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 854 | sp<MockStatsPullerManager> pullerManager = new StrictMock<MockStatsPullerManager>(); |
Bookatz | de1b5562 | 2017-12-14 18:38:27 -0800 | [diff] [blame] | 855 | ValueMetricProducer valueProducer(kConfigKey, metric, -1 /*-1 meaning no condition*/, wizard, |
Chenjie Yu | 054ce9c | 2018-11-12 15:27:29 -0800 | [diff] [blame^] | 856 | logEventMatcherIndex, eventMatcherWizard, -1 /*not pulled*/, |
| 857 | bucketStartTimeNs, bucketStartTimeNs, pullerManager); |
yro | 59cc24d | 2018-02-13 20:17:32 -0800 | [diff] [blame] | 858 | |
Yangster-mac | 932ecec | 2018-02-01 10:23:52 -0800 | [diff] [blame] | 859 | sp<AnomalyTracker> anomalyTracker = valueProducer.addAnomalyTracker(alert, alarmMonitor); |
Bookatz | de1b5562 | 2017-12-14 18:38:27 -0800 | [diff] [blame] | 860 | |
| 861 | |
| 862 | shared_ptr<LogEvent> event1 |
| 863 | = make_shared<LogEvent>(tagId, bucketStartTimeNs + 1 * NS_PER_SEC); |
| 864 | event1->write(161); |
| 865 | event1->write(10); // value of interest |
| 866 | event1->init(); |
| 867 | shared_ptr<LogEvent> event2 |
| 868 | = make_shared<LogEvent>(tagId, bucketStartTimeNs + 2 + NS_PER_SEC); |
| 869 | event2->write(162); |
| 870 | event2->write(20); // value of interest |
| 871 | event2->init(); |
| 872 | shared_ptr<LogEvent> event3 |
| 873 | = make_shared<LogEvent>(tagId, bucketStartTimeNs + 2 * bucketSizeNs + 1 * NS_PER_SEC); |
| 874 | event3->write(163); |
| 875 | event3->write(130); // value of interest |
| 876 | event3->init(); |
| 877 | shared_ptr<LogEvent> event4 |
| 878 | = make_shared<LogEvent>(tagId, bucketStartTimeNs + 3 * bucketSizeNs + 1 * NS_PER_SEC); |
| 879 | event4->write(35); |
| 880 | event4->write(1); // value of interest |
| 881 | event4->init(); |
| 882 | shared_ptr<LogEvent> event5 |
| 883 | = make_shared<LogEvent>(tagId, bucketStartTimeNs + 3 * bucketSizeNs + 2 * NS_PER_SEC); |
| 884 | event5->write(45); |
| 885 | event5->write(150); // value of interest |
| 886 | event5->init(); |
| 887 | shared_ptr<LogEvent> event6 |
| 888 | = make_shared<LogEvent>(tagId, bucketStartTimeNs + 3 * bucketSizeNs + 10 * NS_PER_SEC); |
| 889 | event6->write(25); |
| 890 | event6->write(160); // value of interest |
| 891 | event6->init(); |
| 892 | |
| 893 | // Two events in bucket #0. |
| 894 | valueProducer.onMatchedLogEvent(1 /*log matcher index*/, *event1); |
| 895 | valueProducer.onMatchedLogEvent(1 /*log matcher index*/, *event2); |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 896 | // Value sum == 30 <= 130. |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 897 | EXPECT_EQ(anomalyTracker->getRefractoryPeriodEndsSec(DEFAULT_METRIC_DIMENSION_KEY), 0U); |
Bookatz | de1b5562 | 2017-12-14 18:38:27 -0800 | [diff] [blame] | 898 | |
| 899 | // One event in bucket #2. No alarm as bucket #0 is trashed out. |
| 900 | valueProducer.onMatchedLogEvent(1 /*log matcher index*/, *event3); |
Bookatz | 1bf9438 | 2018-01-04 11:43:20 -0800 | [diff] [blame] | 901 | // Value sum == 130 <= 130. |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 902 | EXPECT_EQ(anomalyTracker->getRefractoryPeriodEndsSec(DEFAULT_METRIC_DIMENSION_KEY), 0U); |
Bookatz | de1b5562 | 2017-12-14 18:38:27 -0800 | [diff] [blame] | 903 | |
| 904 | // Three events in bucket #3. |
| 905 | valueProducer.onMatchedLogEvent(1 /*log matcher index*/, *event4); |
| 906 | // Anomaly at event 4 since Value sum == 131 > 130! |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 907 | EXPECT_EQ(anomalyTracker->getRefractoryPeriodEndsSec(DEFAULT_METRIC_DIMENSION_KEY), |
Bookatz | 6bf9825 | 2018-03-14 10:44:24 -0700 | [diff] [blame] | 908 | std::ceil(1.0 * event4->GetElapsedTimestampNs() / NS_PER_SEC + refPeriodSec)); |
Bookatz | de1b5562 | 2017-12-14 18:38:27 -0800 | [diff] [blame] | 909 | valueProducer.onMatchedLogEvent(1 /*log matcher index*/, *event5); |
| 910 | // Event 5 is within 3 sec refractory period. Thus last alarm timestamp is still event4. |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 911 | EXPECT_EQ(anomalyTracker->getRefractoryPeriodEndsSec(DEFAULT_METRIC_DIMENSION_KEY), |
Bookatz | 6bf9825 | 2018-03-14 10:44:24 -0700 | [diff] [blame] | 912 | std::ceil(1.0 * event4->GetElapsedTimestampNs() / NS_PER_SEC + refPeriodSec)); |
Bookatz | de1b5562 | 2017-12-14 18:38:27 -0800 | [diff] [blame] | 913 | |
| 914 | valueProducer.onMatchedLogEvent(1 /*log matcher index*/, *event6); |
| 915 | // Anomaly at event 6 since Value sum == 160 > 130 and after refractory period. |
Yangster-mac | 9369446 | 2018-01-22 20:49:31 -0800 | [diff] [blame] | 916 | EXPECT_EQ(anomalyTracker->getRefractoryPeriodEndsSec(DEFAULT_METRIC_DIMENSION_KEY), |
Bookatz | 6bf9825 | 2018-03-14 10:44:24 -0700 | [diff] [blame] | 917 | std::ceil(1.0 * event6->GetElapsedTimestampNs() / NS_PER_SEC + refPeriodSec)); |
Bookatz | de1b5562 | 2017-12-14 18:38:27 -0800 | [diff] [blame] | 918 | } |
| 919 | |
Chenjie Yu | 6d370f4 | 2018-03-25 14:57:30 -0700 | [diff] [blame] | 920 | // Test value metric no condition, the pull on bucket boundary come in time and too late |
| 921 | TEST(ValueMetricProducerTest, TestBucketBoundaryNoCondition) { |
| 922 | ValueMetric metric; |
| 923 | metric.set_id(metricId); |
| 924 | metric.set_bucket(ONE_MINUTE); |
| 925 | metric.mutable_value_field()->set_field(tagId); |
| 926 | metric.mutable_value_field()->add_child()->set_field(2); |
| 927 | |
Chenjie Yu | 054ce9c | 2018-11-12 15:27:29 -0800 | [diff] [blame^] | 928 | UidMap uidMap; |
| 929 | SimpleAtomMatcher atomMatcher; |
| 930 | atomMatcher.set_atom_id(tagId); |
| 931 | sp<EventMatcherWizard> eventMatcherWizard = |
| 932 | new EventMatcherWizard({new SimpleLogMatchingTracker( |
| 933 | atomMatcherId, logEventMatcherIndex, atomMatcher, uidMap)}); |
Chenjie Yu | 6d370f4 | 2018-03-25 14:57:30 -0700 | [diff] [blame] | 934 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 935 | sp<MockStatsPullerManager> pullerManager = new StrictMock<MockStatsPullerManager>(); |
Chenjie Yu | 6d370f4 | 2018-03-25 14:57:30 -0700 | [diff] [blame] | 936 | EXPECT_CALL(*pullerManager, RegisterReceiver(tagId, _, _, _)).WillOnce(Return()); |
| 937 | EXPECT_CALL(*pullerManager, UnRegisterReceiver(tagId, _)).WillOnce(Return()); |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 938 | EXPECT_CALL(*pullerManager, Pull(tagId, _, _)).WillOnce(Return(true)); |
Chenjie Yu | 6d370f4 | 2018-03-25 14:57:30 -0700 | [diff] [blame] | 939 | |
| 940 | ValueMetricProducer valueProducer(kConfigKey, metric, -1 /*-1 meaning no condition*/, wizard, |
Chenjie Yu | 054ce9c | 2018-11-12 15:27:29 -0800 | [diff] [blame^] | 941 | logEventMatcherIndex, eventMatcherWizard, tagId, |
| 942 | bucketStartTimeNs, bucketStartTimeNs, pullerManager); |
Chenjie Yu | 6d370f4 | 2018-03-25 14:57:30 -0700 | [diff] [blame] | 943 | |
| 944 | vector<shared_ptr<LogEvent>> allData; |
| 945 | // pull 1 |
| 946 | allData.clear(); |
| 947 | shared_ptr<LogEvent> event = make_shared<LogEvent>(tagId, bucket2StartTimeNs + 1); |
| 948 | event->write(tagId); |
| 949 | event->write(11); |
| 950 | event->init(); |
| 951 | allData.push_back(event); |
| 952 | |
| 953 | valueProducer.onDataPulled(allData); |
| 954 | // has one slice |
| 955 | EXPECT_EQ(1UL, valueProducer.mCurrentSlicedBucket.size()); |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 956 | ValueMetricProducer::Interval curInterval = valueProducer.mCurrentSlicedBucket.begin()->second[0]; |
Chenjie Yu | 6d370f4 | 2018-03-25 14:57:30 -0700 | [diff] [blame] | 957 | |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 958 | // startUpdated:true sum:0 start:11 |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 959 | EXPECT_EQ(true, curInterval.hasBase); |
| 960 | EXPECT_EQ(11, curInterval.base.long_value); |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 961 | EXPECT_EQ(false, curInterval.hasValue); |
Chenjie Yu | ae63b0a | 2018-04-10 14:59:31 -0700 | [diff] [blame] | 962 | EXPECT_EQ(0UL, valueProducer.mPastBuckets.size()); |
Chenjie Yu | 6d370f4 | 2018-03-25 14:57:30 -0700 | [diff] [blame] | 963 | |
| 964 | // pull 2 at correct time |
| 965 | allData.clear(); |
| 966 | event = make_shared<LogEvent>(tagId, bucket3StartTimeNs + 1); |
| 967 | event->write(tagId); |
| 968 | event->write(23); |
| 969 | event->init(); |
| 970 | allData.push_back(event); |
| 971 | valueProducer.onDataPulled(allData); |
| 972 | // has one slice |
| 973 | EXPECT_EQ(1UL, valueProducer.mCurrentSlicedBucket.size()); |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 974 | curInterval = valueProducer.mCurrentSlicedBucket.begin()->second[0]; |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 975 | // tartUpdated:false sum:12 |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 976 | EXPECT_EQ(true, curInterval.hasBase); |
| 977 | EXPECT_EQ(23, curInterval.base.long_value); |
| 978 | EXPECT_EQ(true, curInterval.hasValue); |
| 979 | EXPECT_EQ(12, curInterval.value.long_value); |
| 980 | EXPECT_EQ(0UL, valueProducer.mPastBuckets.size()); |
Chenjie Yu | 6d370f4 | 2018-03-25 14:57:30 -0700 | [diff] [blame] | 981 | |
| 982 | // pull 3 come late. |
| 983 | // The previous bucket gets closed with error. (Has start value 23, no ending) |
| 984 | // Another bucket gets closed with error. (No start, but ending with 36) |
| 985 | // The new bucket is back to normal. |
| 986 | allData.clear(); |
| 987 | event = make_shared<LogEvent>(tagId, bucket6StartTimeNs + 1); |
| 988 | event->write(tagId); |
| 989 | event->write(36); |
| 990 | event->init(); |
| 991 | allData.push_back(event); |
| 992 | valueProducer.onDataPulled(allData); |
| 993 | EXPECT_EQ(1UL, valueProducer.mCurrentSlicedBucket.size()); |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 994 | curInterval = valueProducer.mCurrentSlicedBucket.begin()->second[0]; |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 995 | // startUpdated:false sum:12 |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 996 | EXPECT_EQ(true, curInterval.hasBase); |
| 997 | EXPECT_EQ(36, curInterval.base.long_value); |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 998 | EXPECT_EQ(false, curInterval.hasValue); |
Chenjie Yu | 6d370f4 | 2018-03-25 14:57:30 -0700 | [diff] [blame] | 999 | EXPECT_EQ(1UL, valueProducer.mPastBuckets.size()); |
Chenjie Yu | ae63b0a | 2018-04-10 14:59:31 -0700 | [diff] [blame] | 1000 | EXPECT_EQ(1UL, valueProducer.mPastBuckets.begin()->second.size()); |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 1001 | EXPECT_EQ(12, valueProducer.mPastBuckets.begin()->second.back().values[0].long_value); |
Chenjie Yu | 6d370f4 | 2018-03-25 14:57:30 -0700 | [diff] [blame] | 1002 | } |
| 1003 | |
| 1004 | /* |
| 1005 | * Test pulled event with non sliced condition. The pull on boundary come late because the alarm |
| 1006 | * was delivered late. |
| 1007 | */ |
| 1008 | TEST(ValueMetricProducerTest, TestBucketBoundaryWithCondition) { |
| 1009 | ValueMetric metric; |
| 1010 | metric.set_id(metricId); |
| 1011 | metric.set_bucket(ONE_MINUTE); |
| 1012 | metric.mutable_value_field()->set_field(tagId); |
| 1013 | metric.mutable_value_field()->add_child()->set_field(2); |
| 1014 | metric.set_condition(StringToId("SCREEN_ON")); |
| 1015 | |
Chenjie Yu | 054ce9c | 2018-11-12 15:27:29 -0800 | [diff] [blame^] | 1016 | UidMap uidMap; |
| 1017 | SimpleAtomMatcher atomMatcher; |
| 1018 | atomMatcher.set_atom_id(tagId); |
| 1019 | sp<EventMatcherWizard> eventMatcherWizard = |
| 1020 | new EventMatcherWizard({new SimpleLogMatchingTracker( |
| 1021 | atomMatcherId, logEventMatcherIndex, atomMatcher, uidMap)}); |
Chenjie Yu | 6d370f4 | 2018-03-25 14:57:30 -0700 | [diff] [blame] | 1022 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 1023 | sp<MockStatsPullerManager> pullerManager = new StrictMock<MockStatsPullerManager>(); |
Chenjie Yu | 6d370f4 | 2018-03-25 14:57:30 -0700 | [diff] [blame] | 1024 | EXPECT_CALL(*pullerManager, RegisterReceiver(tagId, _, _, _)).WillOnce(Return()); |
| 1025 | EXPECT_CALL(*pullerManager, UnRegisterReceiver(tagId, _)).WillRepeatedly(Return()); |
| 1026 | |
| 1027 | EXPECT_CALL(*pullerManager, Pull(tagId, _, _)) |
| 1028 | // condition becomes true |
| 1029 | .WillOnce(Invoke([](int tagId, int64_t timeNs, |
| 1030 | vector<std::shared_ptr<LogEvent>>* data) { |
| 1031 | data->clear(); |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 1032 | shared_ptr<LogEvent> event = make_shared<LogEvent>(tagId, bucketStartTimeNs + 8); |
Chenjie Yu | 6d370f4 | 2018-03-25 14:57:30 -0700 | [diff] [blame] | 1033 | event->write(tagId); |
| 1034 | event->write(100); |
| 1035 | event->init(); |
| 1036 | data->push_back(event); |
| 1037 | return true; |
| 1038 | })) |
| 1039 | // condition becomes false |
| 1040 | .WillOnce(Invoke([](int tagId, int64_t timeNs, |
| 1041 | vector<std::shared_ptr<LogEvent>>* data) { |
| 1042 | data->clear(); |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 1043 | shared_ptr<LogEvent> event = make_shared<LogEvent>(tagId, bucket2StartTimeNs + 1); |
Chenjie Yu | 6d370f4 | 2018-03-25 14:57:30 -0700 | [diff] [blame] | 1044 | event->write(tagId); |
| 1045 | event->write(120); |
| 1046 | event->init(); |
| 1047 | data->push_back(event); |
| 1048 | return true; |
| 1049 | })); |
| 1050 | |
Chenjie Yu | 054ce9c | 2018-11-12 15:27:29 -0800 | [diff] [blame^] | 1051 | ValueMetricProducer valueProducer(kConfigKey, metric, 1, wizard, logEventMatcherIndex, |
| 1052 | eventMatcherWizard, tagId, bucketStartTimeNs, |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 1053 | bucketStartTimeNs, pullerManager); |
Chenjie Yu | 6d370f4 | 2018-03-25 14:57:30 -0700 | [diff] [blame] | 1054 | valueProducer.onConditionChanged(true, bucketStartTimeNs + 8); |
| 1055 | |
| 1056 | // has one slice |
| 1057 | EXPECT_EQ(1UL, valueProducer.mCurrentSlicedBucket.size()); |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 1058 | ValueMetricProducer::Interval curInterval = valueProducer.mCurrentSlicedBucket.begin()->second[0]; |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 1059 | EXPECT_EQ(true, curInterval.hasBase); |
| 1060 | EXPECT_EQ(100, curInterval.base.long_value); |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 1061 | EXPECT_EQ(false, curInterval.hasValue); |
Chenjie Yu | 6d370f4 | 2018-03-25 14:57:30 -0700 | [diff] [blame] | 1062 | EXPECT_EQ(0UL, valueProducer.mPastBuckets.size()); |
| 1063 | |
| 1064 | // pull on bucket boundary come late, condition change happens before it |
| 1065 | valueProducer.onConditionChanged(false, bucket2StartTimeNs + 1); |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 1066 | curInterval = valueProducer.mCurrentSlicedBucket.begin()->second[0]; |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 1067 | EXPECT_EQ(false, curInterval.hasBase); |
| 1068 | EXPECT_EQ(true, curInterval.hasValue); |
| 1069 | EXPECT_EQ(20, curInterval.value.long_value); |
Chenjie Yu | ae63b0a | 2018-04-10 14:59:31 -0700 | [diff] [blame] | 1070 | EXPECT_EQ(0UL, valueProducer.mPastBuckets.size()); |
Chenjie Yu | 6d370f4 | 2018-03-25 14:57:30 -0700 | [diff] [blame] | 1071 | |
| 1072 | // Now the alarm is delivered. |
| 1073 | // since the condition turned to off before this pull finish, it has no effect |
| 1074 | vector<shared_ptr<LogEvent>> allData; |
| 1075 | allData.clear(); |
| 1076 | shared_ptr<LogEvent> event = make_shared<LogEvent>(tagId, bucket2StartTimeNs + 30); |
| 1077 | event->write(1); |
| 1078 | event->write(110); |
| 1079 | event->init(); |
| 1080 | allData.push_back(event); |
| 1081 | valueProducer.onDataPulled(allData); |
| 1082 | |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 1083 | curInterval = valueProducer.mCurrentSlicedBucket.begin()->second[0]; |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 1084 | EXPECT_EQ(false, curInterval.hasBase); |
| 1085 | EXPECT_EQ(true, curInterval.hasValue); |
| 1086 | EXPECT_EQ(20, curInterval.value.long_value); |
Chenjie Yu | ae63b0a | 2018-04-10 14:59:31 -0700 | [diff] [blame] | 1087 | EXPECT_EQ(0UL, valueProducer.mPastBuckets.size()); |
Chenjie Yu | 6d370f4 | 2018-03-25 14:57:30 -0700 | [diff] [blame] | 1088 | } |
| 1089 | |
| 1090 | /* |
| 1091 | * Test pulled event with non sliced condition. The pull on boundary come late, after the condition |
| 1092 | * change to false, and then true again. This is due to alarm delivered late. |
| 1093 | */ |
| 1094 | TEST(ValueMetricProducerTest, TestBucketBoundaryWithCondition2) { |
| 1095 | ValueMetric metric; |
| 1096 | metric.set_id(metricId); |
| 1097 | metric.set_bucket(ONE_MINUTE); |
| 1098 | metric.mutable_value_field()->set_field(tagId); |
| 1099 | metric.mutable_value_field()->add_child()->set_field(2); |
| 1100 | metric.set_condition(StringToId("SCREEN_ON")); |
| 1101 | |
Chenjie Yu | 054ce9c | 2018-11-12 15:27:29 -0800 | [diff] [blame^] | 1102 | UidMap uidMap; |
| 1103 | SimpleAtomMatcher atomMatcher; |
| 1104 | atomMatcher.set_atom_id(tagId); |
| 1105 | sp<EventMatcherWizard> eventMatcherWizard = |
| 1106 | new EventMatcherWizard({new SimpleLogMatchingTracker( |
| 1107 | atomMatcherId, logEventMatcherIndex, atomMatcher, uidMap)}); |
Chenjie Yu | 6d370f4 | 2018-03-25 14:57:30 -0700 | [diff] [blame] | 1108 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
Chenjie Yu | e221920 | 2018-06-08 10:07:51 -0700 | [diff] [blame] | 1109 | sp<MockStatsPullerManager> pullerManager = new StrictMock<MockStatsPullerManager>(); |
Chenjie Yu | 6d370f4 | 2018-03-25 14:57:30 -0700 | [diff] [blame] | 1110 | EXPECT_CALL(*pullerManager, RegisterReceiver(tagId, _, _, _)).WillRepeatedly(Return()); |
| 1111 | EXPECT_CALL(*pullerManager, UnRegisterReceiver(tagId, _)).WillRepeatedly(Return()); |
| 1112 | |
| 1113 | EXPECT_CALL(*pullerManager, Pull(tagId, _, _)) |
| 1114 | // condition becomes true |
| 1115 | .WillOnce(Invoke([](int tagId, int64_t timeNs, |
| 1116 | vector<std::shared_ptr<LogEvent>>* data) { |
| 1117 | data->clear(); |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 1118 | shared_ptr<LogEvent> event = make_shared<LogEvent>(tagId, bucketStartTimeNs + 8); |
Chenjie Yu | 6d370f4 | 2018-03-25 14:57:30 -0700 | [diff] [blame] | 1119 | event->write(tagId); |
| 1120 | event->write(100); |
| 1121 | event->init(); |
| 1122 | data->push_back(event); |
| 1123 | return true; |
| 1124 | })) |
| 1125 | // condition becomes false |
| 1126 | .WillOnce(Invoke([](int tagId, int64_t timeNs, |
| 1127 | vector<std::shared_ptr<LogEvent>>* data) { |
| 1128 | data->clear(); |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 1129 | shared_ptr<LogEvent> event = make_shared<LogEvent>(tagId, bucket2StartTimeNs + 1); |
Chenjie Yu | 6d370f4 | 2018-03-25 14:57:30 -0700 | [diff] [blame] | 1130 | event->write(tagId); |
| 1131 | event->write(120); |
| 1132 | event->init(); |
| 1133 | data->push_back(event); |
| 1134 | return true; |
| 1135 | })) |
| 1136 | // condition becomes true again |
| 1137 | .WillOnce(Invoke([](int tagId, int64_t timeNs, |
| 1138 | vector<std::shared_ptr<LogEvent>>* data) { |
| 1139 | data->clear(); |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 1140 | shared_ptr<LogEvent> event = make_shared<LogEvent>(tagId, bucket2StartTimeNs + 25); |
Chenjie Yu | 6d370f4 | 2018-03-25 14:57:30 -0700 | [diff] [blame] | 1141 | event->write(tagId); |
| 1142 | event->write(130); |
| 1143 | event->init(); |
| 1144 | data->push_back(event); |
| 1145 | return true; |
| 1146 | })); |
| 1147 | |
Chenjie Yu | 054ce9c | 2018-11-12 15:27:29 -0800 | [diff] [blame^] | 1148 | ValueMetricProducer valueProducer(kConfigKey, metric, 1, wizard, logEventMatcherIndex, |
| 1149 | eventMatcherWizard, tagId, bucketStartTimeNs, |
Yangster-mac | 15f6bbc | 2018-04-08 11:52:26 -0700 | [diff] [blame] | 1150 | bucketStartTimeNs, pullerManager); |
Chenjie Yu | 6d370f4 | 2018-03-25 14:57:30 -0700 | [diff] [blame] | 1151 | valueProducer.onConditionChanged(true, bucketStartTimeNs + 8); |
| 1152 | |
| 1153 | // has one slice |
| 1154 | EXPECT_EQ(1UL, valueProducer.mCurrentSlicedBucket.size()); |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 1155 | ValueMetricProducer::Interval curInterval = valueProducer.mCurrentSlicedBucket.begin()->second[0]; |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 1156 | // startUpdated:false sum:0 start:100 |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 1157 | EXPECT_EQ(true, curInterval.hasBase); |
| 1158 | EXPECT_EQ(100, curInterval.base.long_value); |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 1159 | EXPECT_EQ(false, curInterval.hasValue); |
Chenjie Yu | 6d370f4 | 2018-03-25 14:57:30 -0700 | [diff] [blame] | 1160 | EXPECT_EQ(0UL, valueProducer.mPastBuckets.size()); |
| 1161 | |
| 1162 | // pull on bucket boundary come late, condition change happens before it |
| 1163 | valueProducer.onConditionChanged(false, bucket2StartTimeNs + 1); |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 1164 | curInterval = valueProducer.mCurrentSlicedBucket.begin()->second[0]; |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 1165 | EXPECT_EQ(false, curInterval.hasBase); |
| 1166 | EXPECT_EQ(true, curInterval.hasValue); |
| 1167 | EXPECT_EQ(20, curInterval.value.long_value); |
Chenjie Yu | ae63b0a | 2018-04-10 14:59:31 -0700 | [diff] [blame] | 1168 | EXPECT_EQ(0UL, valueProducer.mPastBuckets.size()); |
Chenjie Yu | 6d370f4 | 2018-03-25 14:57:30 -0700 | [diff] [blame] | 1169 | |
| 1170 | // condition changed to true again, before the pull alarm is delivered |
| 1171 | valueProducer.onConditionChanged(true, bucket2StartTimeNs + 25); |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 1172 | curInterval = valueProducer.mCurrentSlicedBucket.begin()->second[0]; |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 1173 | EXPECT_EQ(true, curInterval.hasBase); |
| 1174 | EXPECT_EQ(130, curInterval.base.long_value); |
| 1175 | EXPECT_EQ(true, curInterval.hasValue); |
| 1176 | EXPECT_EQ(20, curInterval.value.long_value); |
Chenjie Yu | ae63b0a | 2018-04-10 14:59:31 -0700 | [diff] [blame] | 1177 | EXPECT_EQ(0UL, valueProducer.mPastBuckets.size()); |
Chenjie Yu | 6d370f4 | 2018-03-25 14:57:30 -0700 | [diff] [blame] | 1178 | |
| 1179 | // Now the alarm is delivered, but it is considered late, it has no effect |
| 1180 | vector<shared_ptr<LogEvent>> allData; |
| 1181 | allData.clear(); |
| 1182 | shared_ptr<LogEvent> event = make_shared<LogEvent>(tagId, bucket2StartTimeNs + 50); |
| 1183 | event->write(1); |
| 1184 | event->write(110); |
| 1185 | event->init(); |
| 1186 | allData.push_back(event); |
| 1187 | valueProducer.onDataPulled(allData); |
| 1188 | |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 1189 | curInterval = valueProducer.mCurrentSlicedBucket.begin()->second[0]; |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 1190 | EXPECT_EQ(true, curInterval.hasBase); |
| 1191 | EXPECT_EQ(130, curInterval.base.long_value); |
| 1192 | EXPECT_EQ(true, curInterval.hasValue); |
| 1193 | EXPECT_EQ(20, curInterval.value.long_value); |
Chenjie Yu | ae63b0a | 2018-04-10 14:59:31 -0700 | [diff] [blame] | 1194 | EXPECT_EQ(0UL, valueProducer.mPastBuckets.size()); |
Chenjie Yu | 6d370f4 | 2018-03-25 14:57:30 -0700 | [diff] [blame] | 1195 | } |
| 1196 | |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 1197 | TEST(ValueMetricProducerTest, TestPushedAggregateMin) { |
| 1198 | ValueMetric metric; |
| 1199 | metric.set_id(metricId); |
| 1200 | metric.set_bucket(ONE_MINUTE); |
| 1201 | metric.mutable_value_field()->set_field(tagId); |
| 1202 | metric.mutable_value_field()->add_child()->set_field(2); |
| 1203 | metric.set_aggregation_type(ValueMetric::MIN); |
| 1204 | |
Chenjie Yu | 054ce9c | 2018-11-12 15:27:29 -0800 | [diff] [blame^] | 1205 | UidMap uidMap; |
| 1206 | SimpleAtomMatcher atomMatcher; |
| 1207 | atomMatcher.set_atom_id(tagId); |
| 1208 | sp<EventMatcherWizard> eventMatcherWizard = |
| 1209 | new EventMatcherWizard({new SimpleLogMatchingTracker( |
| 1210 | atomMatcherId, logEventMatcherIndex, atomMatcher, uidMap)}); |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 1211 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 1212 | sp<MockStatsPullerManager> pullerManager = new StrictMock<MockStatsPullerManager>(); |
| 1213 | |
Chenjie Yu | 054ce9c | 2018-11-12 15:27:29 -0800 | [diff] [blame^] | 1214 | ValueMetricProducer valueProducer(kConfigKey, metric, -1, wizard, logEventMatcherIndex, |
| 1215 | eventMatcherWizard, -1, bucketStartTimeNs, bucketStartTimeNs, |
| 1216 | pullerManager); |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 1217 | |
| 1218 | shared_ptr<LogEvent> event1 = make_shared<LogEvent>(tagId, bucketStartTimeNs + 10); |
| 1219 | event1->write(1); |
| 1220 | event1->write(10); |
| 1221 | event1->init(); |
| 1222 | shared_ptr<LogEvent> event2 = make_shared<LogEvent>(tagId, bucketStartTimeNs + 20); |
| 1223 | event2->write(1); |
| 1224 | event2->write(20); |
| 1225 | event2->init(); |
| 1226 | valueProducer.onMatchedLogEvent(1 /*log matcher index*/, *event1); |
| 1227 | // has one slice |
| 1228 | EXPECT_EQ(1UL, valueProducer.mCurrentSlicedBucket.size()); |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 1229 | ValueMetricProducer::Interval curInterval = valueProducer.mCurrentSlicedBucket.begin()->second[0]; |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 1230 | EXPECT_EQ(10, curInterval.value.long_value); |
| 1231 | EXPECT_EQ(true, curInterval.hasValue); |
| 1232 | |
| 1233 | valueProducer.onMatchedLogEvent(1 /*log matcher index*/, *event2); |
| 1234 | |
| 1235 | // has one slice |
| 1236 | EXPECT_EQ(1UL, valueProducer.mCurrentSlicedBucket.size()); |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 1237 | curInterval = valueProducer.mCurrentSlicedBucket.begin()->second[0]; |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 1238 | EXPECT_EQ(10, curInterval.value.long_value); |
| 1239 | |
| 1240 | valueProducer.flushIfNeededLocked(bucket3StartTimeNs); |
| 1241 | EXPECT_EQ(1UL, valueProducer.mPastBuckets.size()); |
| 1242 | EXPECT_EQ(1UL, valueProducer.mPastBuckets.begin()->second.size()); |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 1243 | EXPECT_EQ(10, valueProducer.mPastBuckets.begin()->second.back().values[0].long_value); |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 1244 | } |
| 1245 | |
| 1246 | TEST(ValueMetricProducerTest, TestPushedAggregateMax) { |
| 1247 | ValueMetric metric; |
| 1248 | metric.set_id(metricId); |
| 1249 | metric.set_bucket(ONE_MINUTE); |
| 1250 | metric.mutable_value_field()->set_field(tagId); |
| 1251 | metric.mutable_value_field()->add_child()->set_field(2); |
| 1252 | metric.set_aggregation_type(ValueMetric::MAX); |
| 1253 | |
Chenjie Yu | 054ce9c | 2018-11-12 15:27:29 -0800 | [diff] [blame^] | 1254 | UidMap uidMap; |
| 1255 | SimpleAtomMatcher atomMatcher; |
| 1256 | atomMatcher.set_atom_id(tagId); |
| 1257 | sp<EventMatcherWizard> eventMatcherWizard = |
| 1258 | new EventMatcherWizard({new SimpleLogMatchingTracker( |
| 1259 | atomMatcherId, logEventMatcherIndex, atomMatcher, uidMap)}); |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 1260 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 1261 | sp<MockStatsPullerManager> pullerManager = new StrictMock<MockStatsPullerManager>(); |
| 1262 | |
Chenjie Yu | 054ce9c | 2018-11-12 15:27:29 -0800 | [diff] [blame^] | 1263 | ValueMetricProducer valueProducer(kConfigKey, metric, -1, wizard, logEventMatcherIndex, |
| 1264 | eventMatcherWizard, -1, bucketStartTimeNs, bucketStartTimeNs, |
| 1265 | pullerManager); |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 1266 | |
| 1267 | shared_ptr<LogEvent> event1 = make_shared<LogEvent>(tagId, bucketStartTimeNs + 10); |
| 1268 | event1->write(1); |
| 1269 | event1->write(10); |
| 1270 | event1->init(); |
| 1271 | shared_ptr<LogEvent> event2 = make_shared<LogEvent>(tagId, bucketStartTimeNs + 20); |
| 1272 | event2->write(1); |
| 1273 | event2->write(20); |
| 1274 | event2->init(); |
| 1275 | valueProducer.onMatchedLogEvent(1 /*log matcher index*/, *event1); |
| 1276 | // has one slice |
| 1277 | EXPECT_EQ(1UL, valueProducer.mCurrentSlicedBucket.size()); |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 1278 | ValueMetricProducer::Interval curInterval = valueProducer.mCurrentSlicedBucket.begin()->second[0]; |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 1279 | EXPECT_EQ(10, curInterval.value.long_value); |
| 1280 | EXPECT_EQ(true, curInterval.hasValue); |
| 1281 | |
| 1282 | valueProducer.onMatchedLogEvent(1 /*log matcher index*/, *event2); |
| 1283 | |
| 1284 | // has one slice |
| 1285 | EXPECT_EQ(1UL, valueProducer.mCurrentSlicedBucket.size()); |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 1286 | curInterval = valueProducer.mCurrentSlicedBucket.begin()->second[0]; |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 1287 | EXPECT_EQ(20, curInterval.value.long_value); |
| 1288 | |
| 1289 | valueProducer.flushIfNeededLocked(bucket3StartTimeNs); |
| 1290 | EXPECT_EQ(1UL, valueProducer.mPastBuckets.size()); |
| 1291 | EXPECT_EQ(1UL, valueProducer.mPastBuckets.begin()->second.size()); |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 1292 | EXPECT_EQ(20, valueProducer.mPastBuckets.begin()->second.back().values[0].long_value); |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | TEST(ValueMetricProducerTest, TestPushedAggregateAvg) { |
| 1296 | ValueMetric metric; |
| 1297 | metric.set_id(metricId); |
| 1298 | metric.set_bucket(ONE_MINUTE); |
| 1299 | metric.mutable_value_field()->set_field(tagId); |
| 1300 | metric.mutable_value_field()->add_child()->set_field(2); |
| 1301 | metric.set_aggregation_type(ValueMetric::AVG); |
| 1302 | |
Chenjie Yu | 054ce9c | 2018-11-12 15:27:29 -0800 | [diff] [blame^] | 1303 | UidMap uidMap; |
| 1304 | SimpleAtomMatcher atomMatcher; |
| 1305 | atomMatcher.set_atom_id(tagId); |
| 1306 | sp<EventMatcherWizard> eventMatcherWizard = |
| 1307 | new EventMatcherWizard({new SimpleLogMatchingTracker( |
| 1308 | atomMatcherId, logEventMatcherIndex, atomMatcher, uidMap)}); |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 1309 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 1310 | sp<MockStatsPullerManager> pullerManager = new StrictMock<MockStatsPullerManager>(); |
| 1311 | |
Chenjie Yu | 054ce9c | 2018-11-12 15:27:29 -0800 | [diff] [blame^] | 1312 | ValueMetricProducer valueProducer(kConfigKey, metric, -1, wizard, logEventMatcherIndex, |
| 1313 | eventMatcherWizard, -1, bucketStartTimeNs, bucketStartTimeNs, |
| 1314 | pullerManager); |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 1315 | |
| 1316 | shared_ptr<LogEvent> event1 = make_shared<LogEvent>(tagId, bucketStartTimeNs + 10); |
| 1317 | event1->write(1); |
| 1318 | event1->write(10); |
| 1319 | event1->init(); |
| 1320 | shared_ptr<LogEvent> event2 = make_shared<LogEvent>(tagId, bucketStartTimeNs + 20); |
| 1321 | event2->write(1); |
| 1322 | event2->write(15); |
| 1323 | event2->init(); |
| 1324 | valueProducer.onMatchedLogEvent(1 /*log matcher index*/, *event1); |
| 1325 | // has one slice |
| 1326 | EXPECT_EQ(1UL, valueProducer.mCurrentSlicedBucket.size()); |
| 1327 | ValueMetricProducer::Interval curInterval; |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 1328 | curInterval = valueProducer.mCurrentSlicedBucket.begin()->second[0]; |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 1329 | EXPECT_EQ(10, curInterval.value.long_value); |
| 1330 | EXPECT_EQ(true, curInterval.hasValue); |
| 1331 | EXPECT_EQ(1, curInterval.sampleSize); |
| 1332 | |
| 1333 | valueProducer.onMatchedLogEvent(1 /*log matcher index*/, *event2); |
| 1334 | |
| 1335 | // has one slice |
| 1336 | EXPECT_EQ(1UL, valueProducer.mCurrentSlicedBucket.size()); |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 1337 | curInterval = valueProducer.mCurrentSlicedBucket.begin()->second[0]; |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 1338 | EXPECT_EQ(25, curInterval.value.long_value); |
| 1339 | EXPECT_EQ(2, curInterval.sampleSize); |
| 1340 | |
| 1341 | valueProducer.flushIfNeededLocked(bucket3StartTimeNs); |
| 1342 | EXPECT_EQ(1UL, valueProducer.mPastBuckets.size()); |
| 1343 | EXPECT_EQ(1UL, valueProducer.mPastBuckets.begin()->second.size()); |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 1344 | EXPECT_TRUE(std::abs(valueProducer.mPastBuckets.begin()->second.back().values[0].double_value - 12.5) < epsilon); |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 1345 | } |
| 1346 | |
| 1347 | TEST(ValueMetricProducerTest, TestPushedAggregateSum) { |
| 1348 | ValueMetric metric; |
| 1349 | metric.set_id(metricId); |
| 1350 | metric.set_bucket(ONE_MINUTE); |
| 1351 | metric.mutable_value_field()->set_field(tagId); |
| 1352 | metric.mutable_value_field()->add_child()->set_field(2); |
| 1353 | metric.set_aggregation_type(ValueMetric::SUM); |
| 1354 | |
Chenjie Yu | 054ce9c | 2018-11-12 15:27:29 -0800 | [diff] [blame^] | 1355 | UidMap uidMap; |
| 1356 | SimpleAtomMatcher atomMatcher; |
| 1357 | atomMatcher.set_atom_id(tagId); |
| 1358 | sp<EventMatcherWizard> eventMatcherWizard = |
| 1359 | new EventMatcherWizard({new SimpleLogMatchingTracker( |
| 1360 | atomMatcherId, logEventMatcherIndex, atomMatcher, uidMap)}); |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 1361 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 1362 | sp<MockStatsPullerManager> pullerManager = new StrictMock<MockStatsPullerManager>(); |
| 1363 | |
Chenjie Yu | 054ce9c | 2018-11-12 15:27:29 -0800 | [diff] [blame^] | 1364 | ValueMetricProducer valueProducer(kConfigKey, metric, -1, wizard, logEventMatcherIndex, |
| 1365 | eventMatcherWizard, -1, bucketStartTimeNs, bucketStartTimeNs, |
| 1366 | pullerManager); |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 1367 | |
| 1368 | shared_ptr<LogEvent> event1 = make_shared<LogEvent>(tagId, bucketStartTimeNs + 10); |
| 1369 | event1->write(1); |
| 1370 | event1->write(10); |
| 1371 | event1->init(); |
| 1372 | shared_ptr<LogEvent> event2 = make_shared<LogEvent>(tagId, bucketStartTimeNs + 20); |
| 1373 | event2->write(1); |
| 1374 | event2->write(15); |
| 1375 | event2->init(); |
| 1376 | valueProducer.onMatchedLogEvent(1 /*log matcher index*/, *event1); |
| 1377 | // has one slice |
| 1378 | EXPECT_EQ(1UL, valueProducer.mCurrentSlicedBucket.size()); |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 1379 | ValueMetricProducer::Interval curInterval = valueProducer.mCurrentSlicedBucket.begin()->second[0]; |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 1380 | EXPECT_EQ(10, curInterval.value.long_value); |
| 1381 | EXPECT_EQ(true, curInterval.hasValue); |
| 1382 | |
| 1383 | valueProducer.onMatchedLogEvent(1 /*log matcher index*/, *event2); |
| 1384 | |
| 1385 | // has one slice |
| 1386 | EXPECT_EQ(1UL, valueProducer.mCurrentSlicedBucket.size()); |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 1387 | curInterval = valueProducer.mCurrentSlicedBucket.begin()->second[0]; |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 1388 | EXPECT_EQ(25, curInterval.value.long_value); |
| 1389 | |
| 1390 | valueProducer.flushIfNeededLocked(bucket3StartTimeNs); |
| 1391 | EXPECT_EQ(1UL, valueProducer.mPastBuckets.size()); |
| 1392 | EXPECT_EQ(1UL, valueProducer.mPastBuckets.begin()->second.size()); |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 1393 | EXPECT_EQ(25, valueProducer.mPastBuckets.begin()->second.back().values[0].long_value); |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 1394 | } |
| 1395 | |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 1396 | TEST(ValueMetricProducerTest, TestSkipZeroDiffOutput) { |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 1397 | ValueMetric metric; |
| 1398 | metric.set_id(metricId); |
| 1399 | metric.set_bucket(ONE_MINUTE); |
| 1400 | metric.mutable_value_field()->set_field(tagId); |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 1401 | metric.mutable_value_field()->add_child()->set_field(2); |
| 1402 | metric.set_aggregation_type(ValueMetric::MIN); |
| 1403 | metric.set_use_diff(true); |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 1404 | |
Chenjie Yu | 054ce9c | 2018-11-12 15:27:29 -0800 | [diff] [blame^] | 1405 | UidMap uidMap; |
| 1406 | SimpleAtomMatcher atomMatcher; |
| 1407 | atomMatcher.set_atom_id(tagId); |
| 1408 | sp<EventMatcherWizard> eventMatcherWizard = |
| 1409 | new EventMatcherWizard({new SimpleLogMatchingTracker( |
| 1410 | atomMatcherId, logEventMatcherIndex, atomMatcher, uidMap)}); |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 1411 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 1412 | sp<MockStatsPullerManager> pullerManager = new StrictMock<MockStatsPullerManager>(); |
| 1413 | |
Chenjie Yu | 054ce9c | 2018-11-12 15:27:29 -0800 | [diff] [blame^] | 1414 | ValueMetricProducer valueProducer(kConfigKey, metric, -1, wizard, logEventMatcherIndex, |
| 1415 | eventMatcherWizard, -1, bucketStartTimeNs, bucketStartTimeNs, |
| 1416 | pullerManager); |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 1417 | |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 1418 | shared_ptr<LogEvent> event1 = make_shared<LogEvent>(tagId, bucketStartTimeNs + 10); |
| 1419 | event1->write(1); |
| 1420 | event1->write(10); |
| 1421 | event1->init(); |
| 1422 | shared_ptr<LogEvent> event2 = make_shared<LogEvent>(tagId, bucketStartTimeNs + 15); |
| 1423 | event2->write(1); |
| 1424 | event2->write(15); |
| 1425 | event2->init(); |
| 1426 | valueProducer.onMatchedLogEvent(1 /*log matcher index*/, *event1); |
| 1427 | // has one slice |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 1428 | EXPECT_EQ(1UL, valueProducer.mCurrentSlicedBucket.size()); |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 1429 | ValueMetricProducer::Interval curInterval = valueProducer.mCurrentSlicedBucket.begin()->second[0]; |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 1430 | EXPECT_EQ(true, curInterval.hasBase); |
| 1431 | EXPECT_EQ(10, curInterval.base.long_value); |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 1432 | EXPECT_EQ(false, curInterval.hasValue); |
| 1433 | |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 1434 | valueProducer.onMatchedLogEvent(1 /*log matcher index*/, *event2); |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 1435 | |
| 1436 | // has one slice |
| 1437 | EXPECT_EQ(1UL, valueProducer.mCurrentSlicedBucket.size()); |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 1438 | curInterval = valueProducer.mCurrentSlicedBucket.begin()->second[0]; |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 1439 | EXPECT_EQ(true, curInterval.hasValue); |
| 1440 | EXPECT_EQ(5, curInterval.value.long_value); |
| 1441 | |
| 1442 | // no change in data. |
| 1443 | shared_ptr<LogEvent> event3 = make_shared<LogEvent>(tagId, bucket2StartTimeNs + 10); |
| 1444 | event3->write(1); |
| 1445 | event3->write(15); |
| 1446 | event3->init(); |
| 1447 | valueProducer.onMatchedLogEvent(1 /*log matcher index*/, *event3); |
| 1448 | EXPECT_EQ(1UL, valueProducer.mCurrentSlicedBucket.size()); |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 1449 | curInterval = valueProducer.mCurrentSlicedBucket.begin()->second[0]; |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 1450 | EXPECT_EQ(true, curInterval.hasBase); |
| 1451 | EXPECT_EQ(15, curInterval.base.long_value); |
| 1452 | EXPECT_EQ(true, curInterval.hasValue); |
| 1453 | |
| 1454 | shared_ptr<LogEvent> event4 = make_shared<LogEvent>(tagId, bucket2StartTimeNs + 15); |
| 1455 | event4->write(1); |
| 1456 | event4->write(15); |
| 1457 | event4->init(); |
| 1458 | valueProducer.onMatchedLogEvent(1 /*log matcher index*/, *event4); |
| 1459 | EXPECT_EQ(1UL, valueProducer.mCurrentSlicedBucket.size()); |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 1460 | curInterval = valueProducer.mCurrentSlicedBucket.begin()->second[0]; |
Chenjie Yu | c715b9e | 2018-10-19 07:52:12 -0700 | [diff] [blame] | 1461 | EXPECT_EQ(true, curInterval.hasBase); |
| 1462 | EXPECT_EQ(15, curInterval.base.long_value); |
| 1463 | EXPECT_EQ(true, curInterval.hasValue); |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 1464 | |
| 1465 | valueProducer.flushIfNeededLocked(bucket3StartTimeNs); |
| 1466 | EXPECT_EQ(1UL, valueProducer.mPastBuckets.size()); |
| 1467 | EXPECT_EQ(1UL, valueProducer.mPastBuckets.begin()->second.size()); |
Chenjie Yu | 32717c3 | 2018-10-20 23:54:48 -0700 | [diff] [blame] | 1468 | EXPECT_EQ(5, valueProducer.mPastBuckets.begin()->second.back().values[0].long_value); |
Chenjie Yu | a0f0224 | 2018-07-06 16:14:34 -0700 | [diff] [blame] | 1469 | } |
| 1470 | |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 1471 | } // namespace statsd |
| 1472 | } // namespace os |
| 1473 | } // namespace android |
| 1474 | #else |
| 1475 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 1476 | #endif |