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" |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 16 | #include "metrics_test_helper.h" |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 17 | |
| 18 | #include <gmock/gmock.h> |
| 19 | #include <gtest/gtest.h> |
| 20 | #include <stdio.h> |
| 21 | #include <vector> |
| 22 | |
| 23 | using namespace testing; |
| 24 | using android::sp; |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 25 | using std::make_shared; |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 26 | using std::set; |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 27 | using std::shared_ptr; |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 28 | using std::unordered_map; |
| 29 | using std::vector; |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 30 | |
| 31 | #ifdef __ANDROID__ |
| 32 | |
| 33 | namespace android { |
| 34 | namespace os { |
| 35 | namespace statsd { |
| 36 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 37 | const ConfigKey kConfigKey(0, "test"); |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame^] | 38 | const int tagId = 1; |
| 39 | const string metricName = "test_metric"; |
| 40 | const int64_t bucketStartTimeNs = 10000000000; |
| 41 | const int64_t bucketSizeNs = 60 * 1000 * 1000 * 1000LL; |
| 42 | const int64_t bucket2StartTimeNs = bucketStartTimeNs + bucketSizeNs; |
| 43 | const int64_t bucket3StartTimeNs = bucketStartTimeNs + 2 * bucketSizeNs; |
| 44 | const int64_t bucket4StartTimeNs = bucketStartTimeNs + 3 * bucketSizeNs; |
| 45 | |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 46 | /* |
| 47 | * Tests pulled atoms with no conditions |
| 48 | */ |
| 49 | TEST(ValueMetricProducerTest, TestNonDimensionalEvents) { |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 50 | ValueMetric metric; |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame^] | 51 | metric.set_name(metricName); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 52 | metric.mutable_bucket()->set_bucket_size_millis(bucketSizeNs / 1000000); |
| 53 | metric.set_value_field(2); |
| 54 | |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 55 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
| 56 | // TODO: pending refactor of StatsPullerManager |
| 57 | // For now we still need this so that it doesn't do real pulling. |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 58 | shared_ptr<MockStatsPullerManager> pullerManager = |
| 59 | make_shared<StrictMock<MockStatsPullerManager>>(); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 60 | EXPECT_CALL(*pullerManager, RegisterReceiver(tagId, _, _)).WillOnce(Return()); |
| 61 | EXPECT_CALL(*pullerManager, UnRegisterReceiver(tagId, _)).WillOnce(Return()); |
| 62 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 63 | ValueMetricProducer valueProducer(kConfigKey, metric, -1 /*-1 meaning no condition*/, wizard, |
| 64 | tagId, bucketStartTimeNs, pullerManager); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 65 | |
| 66 | vector<shared_ptr<LogEvent>> allData; |
| 67 | allData.clear(); |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame^] | 68 | shared_ptr<LogEvent> event = make_shared<LogEvent>(tagId, bucket2StartTimeNs + 1); |
| 69 | event->write(tagId); |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 70 | event->write(11); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 71 | event->init(); |
| 72 | allData.push_back(event); |
| 73 | |
| 74 | valueProducer.onDataPulled(allData); |
| 75 | // has one slice |
| 76 | EXPECT_EQ(1UL, valueProducer.mCurrentSlicedBucket.size()); |
| 77 | ValueMetricProducer::Interval curInterval = valueProducer.mCurrentSlicedBucket.begin()->second; |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame^] | 78 | // startUpdated:true tainted:0 sum:0 start:11 |
| 79 | EXPECT_EQ(true, curInterval.startUpdated); |
| 80 | EXPECT_EQ(0, curInterval.tainted); |
| 81 | EXPECT_EQ(0, curInterval.sum); |
| 82 | EXPECT_EQ(11, curInterval.start); |
| 83 | EXPECT_EQ(1UL, valueProducer.mPastBuckets.size()); |
| 84 | EXPECT_EQ(0, valueProducer.mPastBuckets.begin()->second.back().mValue); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 85 | |
| 86 | allData.clear(); |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame^] | 87 | event = make_shared<LogEvent>(tagId, bucket3StartTimeNs + 1); |
| 88 | event->write(tagId); |
| 89 | event->write(23); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 90 | event->init(); |
| 91 | allData.push_back(event); |
| 92 | valueProducer.onDataPulled(allData); |
| 93 | // has one slice |
| 94 | EXPECT_EQ(1UL, valueProducer.mCurrentSlicedBucket.size()); |
| 95 | curInterval = valueProducer.mCurrentSlicedBucket.begin()->second; |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame^] | 96 | // tartUpdated:false tainted:0 sum:12 |
| 97 | EXPECT_EQ(true, curInterval.startUpdated); |
| 98 | EXPECT_EQ(0, curInterval.tainted); |
| 99 | EXPECT_EQ(0, curInterval.sum); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 100 | EXPECT_EQ(1UL, valueProducer.mPastBuckets.size()); |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame^] | 101 | EXPECT_EQ(2UL, valueProducer.mPastBuckets.begin()->second.size()); |
| 102 | EXPECT_EQ(12, valueProducer.mPastBuckets.begin()->second.back().mValue); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 103 | |
| 104 | allData.clear(); |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame^] | 105 | event = make_shared<LogEvent>(tagId, bucket4StartTimeNs + 1); |
| 106 | event->write(tagId); |
| 107 | event->write(36); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 108 | event->init(); |
| 109 | allData.push_back(event); |
| 110 | valueProducer.onDataPulled(allData); |
| 111 | EXPECT_EQ(1UL, valueProducer.mCurrentSlicedBucket.size()); |
| 112 | curInterval = valueProducer.mCurrentSlicedBucket.begin()->second; |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame^] | 113 | // startUpdated:false tainted:0 sum:12 |
| 114 | EXPECT_EQ(true, curInterval.startUpdated); |
| 115 | EXPECT_EQ(0, curInterval.tainted); |
| 116 | EXPECT_EQ(0, curInterval.sum); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 117 | EXPECT_EQ(1UL, valueProducer.mPastBuckets.size()); |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame^] | 118 | EXPECT_EQ(3UL, valueProducer.mPastBuckets.begin()->second.size()); |
| 119 | EXPECT_EQ(13, valueProducer.mPastBuckets.begin()->second.back().mValue); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /* |
| 123 | * Test pulled event with non sliced condition. |
| 124 | */ |
| 125 | TEST(ValueMetricProducerTest, TestEventsWithNonSlicedCondition) { |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 126 | ValueMetric metric; |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame^] | 127 | metric.set_name(metricName); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 128 | metric.mutable_bucket()->set_bucket_size_millis(bucketSizeNs / 1000000); |
| 129 | metric.set_value_field(2); |
| 130 | metric.set_condition("SCREEN_ON"); |
| 131 | |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 132 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 133 | shared_ptr<MockStatsPullerManager> pullerManager = |
| 134 | make_shared<StrictMock<MockStatsPullerManager>>(); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 135 | EXPECT_CALL(*pullerManager, RegisterReceiver(tagId, _, _)).WillOnce(Return()); |
| 136 | EXPECT_CALL(*pullerManager, UnRegisterReceiver(tagId, _)).WillRepeatedly(Return()); |
| 137 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 138 | EXPECT_CALL(*pullerManager, Pull(tagId, _)) |
| 139 | .WillOnce(Invoke([](int tagId, vector<std::shared_ptr<LogEvent>>* data) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 140 | data->clear(); |
| 141 | shared_ptr<LogEvent> event = make_shared<LogEvent>(tagId, bucketStartTimeNs + 10); |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame^] | 142 | event->write(tagId); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 143 | event->write(100); |
| 144 | event->init(); |
| 145 | data->push_back(event); |
| 146 | return true; |
| 147 | })) |
| 148 | .WillOnce(Invoke([](int tagId, vector<std::shared_ptr<LogEvent>>* data) { |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 149 | data->clear(); |
| 150 | shared_ptr<LogEvent> event = make_shared<LogEvent>(tagId, bucket2StartTimeNs + 10); |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame^] | 151 | event->write(tagId); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 152 | event->write(120); |
| 153 | event->init(); |
| 154 | data->push_back(event); |
| 155 | return true; |
| 156 | })); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 157 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 158 | ValueMetricProducer valueProducer(kConfigKey, metric, 1, wizard, tagId, bucketStartTimeNs, |
| 159 | pullerManager); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 160 | |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame^] | 161 | valueProducer.onConditionChanged(true, bucketStartTimeNs + 8); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 162 | |
| 163 | // has one slice |
| 164 | EXPECT_EQ(1UL, valueProducer.mCurrentSlicedBucket.size()); |
| 165 | ValueMetricProducer::Interval curInterval = valueProducer.mCurrentSlicedBucket.begin()->second; |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame^] | 166 | // startUpdated:false tainted:0 sum:0 start:100 |
| 167 | EXPECT_EQ(100, curInterval.start); |
| 168 | EXPECT_EQ(true, curInterval.startUpdated); |
| 169 | EXPECT_EQ(0, curInterval.tainted); |
| 170 | EXPECT_EQ(0, curInterval.sum); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 171 | EXPECT_EQ(0UL, valueProducer.mPastBuckets.size()); |
| 172 | |
| 173 | vector<shared_ptr<LogEvent>> allData; |
| 174 | allData.clear(); |
| 175 | shared_ptr<LogEvent> event = make_shared<LogEvent>(tagId, bucket2StartTimeNs + 1); |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 176 | event->write(1); |
| 177 | event->write(110); |
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 | |
| 182 | // has one slice |
| 183 | EXPECT_EQ(1UL, valueProducer.mCurrentSlicedBucket.size()); |
| 184 | curInterval = valueProducer.mCurrentSlicedBucket.begin()->second; |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame^] | 185 | // startUpdated:false tainted:0 sum:0 start:110 |
| 186 | EXPECT_EQ(110, curInterval.start); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 187 | EXPECT_EQ(1UL, valueProducer.mPastBuckets.size()); |
| 188 | EXPECT_EQ(1UL, valueProducer.mPastBuckets.begin()->second.size()); |
| 189 | EXPECT_EQ(10, valueProducer.mPastBuckets.begin()->second.back().mValue); |
| 190 | |
| 191 | valueProducer.onConditionChanged(false, bucket2StartTimeNs + 1); |
| 192 | |
| 193 | // has one slice |
| 194 | EXPECT_EQ(1UL, valueProducer.mCurrentSlicedBucket.size()); |
| 195 | curInterval = valueProducer.mCurrentSlicedBucket.begin()->second; |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame^] | 196 | // startUpdated:false tainted:0 sum:0 start:110 |
| 197 | EXPECT_EQ(10, curInterval.sum); |
| 198 | EXPECT_EQ(false, curInterval.startUpdated); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | TEST(ValueMetricProducerTest, TestPushedEventsWithoutCondition) { |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 202 | ValueMetric metric; |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame^] | 203 | metric.set_name(metricName); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 204 | metric.mutable_bucket()->set_bucket_size_millis(bucketSizeNs / 1000000); |
| 205 | metric.set_value_field(2); |
| 206 | |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 207 | sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>(); |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 208 | shared_ptr<MockStatsPullerManager> pullerManager = |
| 209 | make_shared<StrictMock<MockStatsPullerManager>>(); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 210 | |
Yao Chen | b356151 | 2017-11-21 18:07:17 -0800 | [diff] [blame] | 211 | ValueMetricProducer valueProducer(kConfigKey, metric, -1, wizard, -1, bucketStartTimeNs, |
| 212 | pullerManager); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 213 | |
| 214 | shared_ptr<LogEvent> event1 = make_shared<LogEvent>(tagId, bucketStartTimeNs + 10); |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 215 | event1->write(1); |
| 216 | event1->write(10); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 217 | event1->init(); |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame^] | 218 | shared_ptr<LogEvent> event2 = make_shared<LogEvent>(tagId, bucketStartTimeNs + 20); |
Yao Chen | 8023540 | 2017-11-13 20:42:25 -0800 | [diff] [blame] | 219 | event2->write(1); |
| 220 | event2->write(20); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 221 | event2->init(); |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame^] | 222 | valueProducer.onMatchedLogEvent(1 /*log matcher index*/, *event1); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 223 | // has one slice |
| 224 | EXPECT_EQ(1UL, valueProducer.mCurrentSlicedBucket.size()); |
| 225 | ValueMetricProducer::Interval curInterval = valueProducer.mCurrentSlicedBucket.begin()->second; |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame^] | 226 | EXPECT_EQ(10, curInterval.sum); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 227 | |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame^] | 228 | valueProducer.onMatchedLogEvent(1 /*log matcher index*/, *event2); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 229 | |
| 230 | // has one slice |
| 231 | EXPECT_EQ(1UL, valueProducer.mCurrentSlicedBucket.size()); |
| 232 | curInterval = valueProducer.mCurrentSlicedBucket.begin()->second; |
Chenjie Yu | a7259ab | 2017-12-10 08:31:05 -0800 | [diff] [blame^] | 233 | EXPECT_EQ(30, curInterval.sum); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 234 | |
Yangster | f2bee6f | 2017-11-29 12:01:05 -0800 | [diff] [blame] | 235 | valueProducer.flushIfNeededLocked(bucket3StartTimeNs); |
Chenjie Yu | 6736c89 | 2017-11-09 10:50:09 -0800 | [diff] [blame] | 236 | EXPECT_EQ(1UL, valueProducer.mPastBuckets.size()); |
| 237 | EXPECT_EQ(1UL, valueProducer.mPastBuckets.begin()->second.size()); |
| 238 | EXPECT_EQ(30, valueProducer.mPastBuckets.begin()->second.back().mValue); |
| 239 | } |
| 240 | |
| 241 | } // namespace statsd |
| 242 | } // namespace os |
| 243 | } // namespace android |
| 244 | #else |
| 245 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 246 | #endif |