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