Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 1 | // Copyright (C) 2017 The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 15 | #include <gtest/gtest.h> |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 16 | |
| 17 | #include "src/condition/ConditionTracker.h" |
| 18 | #include "src/matchers/LogMatchingTracker.h" |
| 19 | #include "src/metrics/CountMetricProducer.h" |
| 20 | #include "src/metrics/MetricProducer.h" |
Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 21 | #include "src/metrics/ValueMetricProducer.h" |
Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 22 | #include "src/metrics/metrics_manager_util.h" |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 23 | |
| 24 | #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h" |
| 25 | |
| 26 | #include <stdio.h> |
| 27 | #include <set> |
| 28 | #include <unordered_map> |
| 29 | #include <vector> |
| 30 | |
| 31 | using namespace android::os::statsd; |
| 32 | using android::sp; |
| 33 | using std::set; |
| 34 | using std::unordered_map; |
| 35 | using std::vector; |
| 36 | |
| 37 | #ifdef __ANDROID__ |
| 38 | |
| 39 | // TODO: ADD MORE TEST CASES. |
| 40 | |
| 41 | StatsdConfig buildGoodConfig() { |
| 42 | StatsdConfig config; |
| 43 | config.set_config_id(12345L); |
| 44 | |
| 45 | LogEntryMatcher* eventMatcher = config.add_log_entry_matcher(); |
| 46 | eventMatcher->set_name("SCREEN_IS_ON"); |
| 47 | |
| 48 | SimpleLogEntryMatcher* simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher(); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 49 | simpleLogEntryMatcher->set_tag(2 /*SCREEN_STATE_CHANGE*/); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 50 | simpleLogEntryMatcher->add_key_value_matcher()->mutable_key_matcher()->set_key( |
| 51 | 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE*/); |
| 52 | simpleLogEntryMatcher->mutable_key_value_matcher(0)->set_eq_int( |
| 53 | 2 /*SCREEN_STATE_CHANGE__DISPLAY_STATE__STATE_ON*/); |
| 54 | |
| 55 | eventMatcher = config.add_log_entry_matcher(); |
| 56 | eventMatcher->set_name("SCREEN_IS_OFF"); |
| 57 | |
| 58 | simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher(); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 59 | simpleLogEntryMatcher->set_tag(2 /*SCREEN_STATE_CHANGE*/); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 60 | simpleLogEntryMatcher->add_key_value_matcher()->mutable_key_matcher()->set_key( |
| 61 | 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE*/); |
| 62 | simpleLogEntryMatcher->mutable_key_value_matcher(0)->set_eq_int( |
| 63 | 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE__STATE_OFF*/); |
| 64 | |
| 65 | eventMatcher = config.add_log_entry_matcher(); |
| 66 | eventMatcher->set_name("SCREEN_ON_OR_OFF"); |
| 67 | |
| 68 | LogEntryMatcher_Combination* combination = eventMatcher->mutable_combination(); |
| 69 | combination->set_operation(LogicalOperation::OR); |
| 70 | combination->add_matcher("SCREEN_IS_ON"); |
| 71 | combination->add_matcher("SCREEN_IS_OFF"); |
| 72 | |
| 73 | return config; |
| 74 | } |
| 75 | |
| 76 | StatsdConfig buildCircleMatchers() { |
| 77 | StatsdConfig config; |
| 78 | config.set_config_id(12345L); |
| 79 | |
| 80 | LogEntryMatcher* eventMatcher = config.add_log_entry_matcher(); |
| 81 | eventMatcher->set_name("SCREEN_IS_ON"); |
| 82 | |
| 83 | SimpleLogEntryMatcher* simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher(); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 84 | simpleLogEntryMatcher->set_tag(2 /*SCREEN_STATE_CHANGE*/); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 85 | simpleLogEntryMatcher->add_key_value_matcher()->mutable_key_matcher()->set_key( |
| 86 | 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE*/); |
| 87 | simpleLogEntryMatcher->mutable_key_value_matcher(0)->set_eq_int( |
| 88 | 2 /*SCREEN_STATE_CHANGE__DISPLAY_STATE__STATE_ON*/); |
| 89 | |
| 90 | eventMatcher = config.add_log_entry_matcher(); |
| 91 | eventMatcher->set_name("SCREEN_ON_OR_OFF"); |
| 92 | |
| 93 | LogEntryMatcher_Combination* combination = eventMatcher->mutable_combination(); |
| 94 | combination->set_operation(LogicalOperation::OR); |
| 95 | combination->add_matcher("SCREEN_IS_ON"); |
| 96 | // Circle dependency |
| 97 | combination->add_matcher("SCREEN_ON_OR_OFF"); |
| 98 | |
| 99 | return config; |
| 100 | } |
| 101 | |
| 102 | StatsdConfig buildMissingMatchers() { |
| 103 | StatsdConfig config; |
| 104 | config.set_config_id(12345L); |
| 105 | |
| 106 | LogEntryMatcher* eventMatcher = config.add_log_entry_matcher(); |
| 107 | eventMatcher->set_name("SCREEN_IS_ON"); |
| 108 | |
| 109 | SimpleLogEntryMatcher* simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher(); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 110 | simpleLogEntryMatcher->set_tag(2 /*SCREEN_STATE_CHANGE*/); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 111 | simpleLogEntryMatcher->add_key_value_matcher()->mutable_key_matcher()->set_key( |
| 112 | 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE*/); |
| 113 | simpleLogEntryMatcher->mutable_key_value_matcher(0)->set_eq_int( |
| 114 | 2 /*SCREEN_STATE_CHANGE__DISPLAY_STATE__STATE_ON*/); |
| 115 | |
| 116 | eventMatcher = config.add_log_entry_matcher(); |
| 117 | eventMatcher->set_name("SCREEN_ON_OR_OFF"); |
| 118 | |
| 119 | LogEntryMatcher_Combination* combination = eventMatcher->mutable_combination(); |
| 120 | combination->set_operation(LogicalOperation::OR); |
| 121 | combination->add_matcher("SCREEN_IS_ON"); |
| 122 | // undefined matcher |
| 123 | combination->add_matcher("ABC"); |
| 124 | |
| 125 | return config; |
| 126 | } |
| 127 | |
Yao Chen | 5154a379 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 128 | StatsdConfig buildDimensionMetricsWithMultiTags() { |
| 129 | StatsdConfig config; |
| 130 | config.set_config_id(12345L); |
| 131 | |
| 132 | LogEntryMatcher* eventMatcher = config.add_log_entry_matcher(); |
| 133 | eventMatcher->set_name("BATTERY_VERY_LOW"); |
| 134 | SimpleLogEntryMatcher* simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher(); |
| 135 | simpleLogEntryMatcher->set_tag(2); |
| 136 | |
| 137 | eventMatcher = config.add_log_entry_matcher(); |
| 138 | eventMatcher->set_name("BATTERY_VERY_VERY_LOW"); |
| 139 | simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher(); |
| 140 | simpleLogEntryMatcher->set_tag(3); |
| 141 | |
| 142 | eventMatcher = config.add_log_entry_matcher(); |
| 143 | eventMatcher->set_name("BATTERY_LOW"); |
| 144 | |
| 145 | LogEntryMatcher_Combination* combination = eventMatcher->mutable_combination(); |
| 146 | combination->set_operation(LogicalOperation::OR); |
| 147 | combination->add_matcher("BATTERY_VERY_LOW"); |
| 148 | combination->add_matcher("BATTERY_VERY_VERY_LOW"); |
| 149 | |
| 150 | // Count process state changes, slice by uid, while SCREEN_IS_OFF |
| 151 | CountMetric* metric = config.add_count_metric(); |
| 152 | metric->set_metric_id(3); |
| 153 | metric->set_what("BATTERY_LOW"); |
| 154 | metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L); |
| 155 | KeyMatcher* keyMatcher = metric->add_dimension(); |
| 156 | keyMatcher->set_key(1); |
| 157 | |
| 158 | return config; |
| 159 | } |
| 160 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 161 | StatsdConfig buildCircleConditions() { |
| 162 | StatsdConfig config; |
| 163 | config.set_config_id(12345L); |
| 164 | |
| 165 | LogEntryMatcher* eventMatcher = config.add_log_entry_matcher(); |
| 166 | eventMatcher->set_name("SCREEN_IS_ON"); |
| 167 | |
| 168 | SimpleLogEntryMatcher* simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher(); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 169 | simpleLogEntryMatcher->set_tag(2 /*SCREEN_STATE_CHANGE*/); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 170 | simpleLogEntryMatcher->add_key_value_matcher()->mutable_key_matcher()->set_key( |
| 171 | 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE*/); |
| 172 | simpleLogEntryMatcher->mutable_key_value_matcher(0)->set_eq_int( |
| 173 | 2 /*SCREEN_STATE_CHANGE__DISPLAY_STATE__STATE_ON*/); |
| 174 | |
| 175 | eventMatcher = config.add_log_entry_matcher(); |
| 176 | eventMatcher->set_name("SCREEN_IS_OFF"); |
| 177 | |
| 178 | simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher(); |
Yao Chen | 729093d | 2017-10-16 10:33:26 -0700 | [diff] [blame] | 179 | simpleLogEntryMatcher->set_tag(2 /*SCREEN_STATE_CHANGE*/); |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 180 | simpleLogEntryMatcher->add_key_value_matcher()->mutable_key_matcher()->set_key( |
| 181 | 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE*/); |
| 182 | simpleLogEntryMatcher->mutable_key_value_matcher(0)->set_eq_int( |
| 183 | 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE__STATE_OFF*/); |
| 184 | |
| 185 | Condition* condition = config.add_condition(); |
| 186 | condition->set_name("SCREEN_IS_ON"); |
| 187 | SimpleCondition* simpleCondition = condition->mutable_simple_condition(); |
| 188 | simpleCondition->set_start("SCREEN_IS_ON"); |
| 189 | simpleCondition->set_stop("SCREEN_IS_OFF"); |
| 190 | |
| 191 | condition = config.add_condition(); |
| 192 | condition->set_name("SCREEN_IS_EITHER_ON_OFF"); |
| 193 | |
| 194 | Condition_Combination* combination = condition->mutable_combination(); |
| 195 | combination->set_operation(LogicalOperation::OR); |
| 196 | combination->add_condition("SCREEN_IS_ON"); |
| 197 | combination->add_condition("SCREEN_IS_EITHER_ON_OFF"); |
| 198 | |
| 199 | return config; |
| 200 | } |
| 201 | |
| 202 | TEST(MetricsManagerTest, TestGoodConfig) { |
| 203 | StatsdConfig config = buildGoodConfig(); |
| 204 | set<int> allTagIds; |
| 205 | vector<sp<LogMatchingTracker>> allLogEntryMatchers; |
| 206 | vector<sp<ConditionTracker>> allConditionTrackers; |
| 207 | vector<sp<MetricProducer>> allMetricProducers; |
| 208 | unordered_map<int, std::vector<int>> conditionToMetricMap; |
| 209 | unordered_map<int, std::vector<int>> trackerToMetricMap; |
| 210 | unordered_map<int, std::vector<int>> trackerToConditionMap; |
| 211 | |
| 212 | EXPECT_TRUE(initStatsdConfig(config, allTagIds, allLogEntryMatchers, allConditionTrackers, |
| 213 | allMetricProducers, conditionToMetricMap, trackerToMetricMap, |
| 214 | trackerToConditionMap)); |
| 215 | } |
| 216 | |
Yao Chen | 5154a379 | 2017-10-30 22:57:06 -0700 | [diff] [blame] | 217 | TEST(MetricsManagerTest, TestDimensionMetricsWithMultiTags) { |
| 218 | StatsdConfig config = buildDimensionMetricsWithMultiTags(); |
| 219 | set<int> allTagIds; |
| 220 | vector<sp<LogMatchingTracker>> allLogEntryMatchers; |
| 221 | vector<sp<ConditionTracker>> allConditionTrackers; |
| 222 | vector<sp<MetricProducer>> allMetricProducers; |
| 223 | unordered_map<int, std::vector<int>> conditionToMetricMap; |
| 224 | unordered_map<int, std::vector<int>> trackerToMetricMap; |
| 225 | unordered_map<int, std::vector<int>> trackerToConditionMap; |
| 226 | |
| 227 | EXPECT_FALSE(initStatsdConfig(config, allTagIds, allLogEntryMatchers, allConditionTrackers, |
| 228 | allMetricProducers, conditionToMetricMap, trackerToMetricMap, |
| 229 | trackerToConditionMap)); |
| 230 | } |
| 231 | |
Yao Chen | caf339d | 2017-10-06 16:01:10 -0700 | [diff] [blame] | 232 | TEST(MetricsManagerTest, TestCircleLogMatcherDependency) { |
| 233 | StatsdConfig config = buildCircleMatchers(); |
| 234 | set<int> allTagIds; |
| 235 | vector<sp<LogMatchingTracker>> allLogEntryMatchers; |
| 236 | vector<sp<ConditionTracker>> allConditionTrackers; |
| 237 | vector<sp<MetricProducer>> allMetricProducers; |
| 238 | unordered_map<int, std::vector<int>> conditionToMetricMap; |
| 239 | unordered_map<int, std::vector<int>> trackerToMetricMap; |
| 240 | unordered_map<int, std::vector<int>> trackerToConditionMap; |
| 241 | |
| 242 | EXPECT_FALSE(initStatsdConfig(config, allTagIds, allLogEntryMatchers, allConditionTrackers, |
| 243 | allMetricProducers, conditionToMetricMap, trackerToMetricMap, |
| 244 | trackerToConditionMap)); |
| 245 | } |
| 246 | |
| 247 | TEST(MetricsManagerTest, TestMissingMatchers) { |
| 248 | StatsdConfig config = buildMissingMatchers(); |
| 249 | set<int> allTagIds; |
| 250 | vector<sp<LogMatchingTracker>> allLogEntryMatchers; |
| 251 | vector<sp<ConditionTracker>> allConditionTrackers; |
| 252 | vector<sp<MetricProducer>> allMetricProducers; |
| 253 | unordered_map<int, std::vector<int>> conditionToMetricMap; |
| 254 | unordered_map<int, std::vector<int>> trackerToMetricMap; |
| 255 | unordered_map<int, std::vector<int>> trackerToConditionMap; |
| 256 | |
| 257 | EXPECT_FALSE(initStatsdConfig(config, allTagIds, allLogEntryMatchers, allConditionTrackers, |
| 258 | allMetricProducers, conditionToMetricMap, trackerToMetricMap, |
| 259 | trackerToConditionMap)); |
| 260 | } |
| 261 | |
| 262 | TEST(MetricsManagerTest, TestCircleConditionDependency) { |
| 263 | StatsdConfig config = buildCircleConditions(); |
| 264 | set<int> allTagIds; |
| 265 | vector<sp<LogMatchingTracker>> allLogEntryMatchers; |
| 266 | vector<sp<ConditionTracker>> allConditionTrackers; |
| 267 | vector<sp<MetricProducer>> allMetricProducers; |
| 268 | unordered_map<int, std::vector<int>> conditionToMetricMap; |
| 269 | unordered_map<int, std::vector<int>> trackerToMetricMap; |
| 270 | unordered_map<int, std::vector<int>> trackerToConditionMap; |
| 271 | |
| 272 | EXPECT_FALSE(initStatsdConfig(config, allTagIds, allLogEntryMatchers, allConditionTrackers, |
| 273 | allMetricProducers, conditionToMetricMap, trackerToMetricMap, |
| 274 | trackerToConditionMap)); |
| 275 | } |
| 276 | |
| 277 | #else |
| 278 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 279 | #endif |