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