blob: 2adec945b85a5e02e36491cf86c04834e4c7ef40 [file] [log] [blame]
Yao Chencaf339d2017-10-06 16:01:10 -07001// 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 Chencaf339d2017-10-06 16:01:10 -070015#include <gtest/gtest.h>
Joe Onorato9fc9edf2017-10-15 20:08:52 -070016
17#include "src/condition/ConditionTracker.h"
18#include "src/matchers/LogMatchingTracker.h"
19#include "src/metrics/CountMetricProducer.h"
Yangster1d4d6862017-10-31 12:58:51 -070020#include "src/metrics/GaugeMetricProducer.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070021#include "src/metrics/MetricProducer.h"
Chenjie Yub3dda412017-10-24 13:41:59 -070022#include "src/metrics/ValueMetricProducer.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070023#include "src/metrics/metrics_manager_util.h"
Yao Chencaf339d2017-10-06 16:01:10 -070024
25#include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
26
27#include <stdio.h>
28#include <set>
29#include <unordered_map>
30#include <vector>
31
32using namespace android::os::statsd;
33using android::sp;
34using std::set;
35using std::unordered_map;
36using std::vector;
Yangster-mace2cd6d52017-11-09 20:38:30 -080037using android::os::statsd::Condition;
Yao Chencaf339d2017-10-06 16:01:10 -070038
39#ifdef __ANDROID__
40
41// TODO: ADD MORE TEST CASES.
42
Yao Chenb3561512017-11-21 18:07:17 -080043const ConfigKey kConfigKey(0, "test");
44
Yao Chencaf339d2017-10-06 16:01:10 -070045StatsdConfig buildGoodConfig() {
46 StatsdConfig config;
Yangster-macd1815dc2017-11-13 21:43:15 -080047 config.set_name("12345");
Yao Chencaf339d2017-10-06 16:01:10 -070048
49 LogEntryMatcher* eventMatcher = config.add_log_entry_matcher();
50 eventMatcher->set_name("SCREEN_IS_ON");
51
52 SimpleLogEntryMatcher* simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
Yao Chen729093d2017-10-16 10:33:26 -070053 simpleLogEntryMatcher->set_tag(2 /*SCREEN_STATE_CHANGE*/);
Yao Chencaf339d2017-10-06 16:01:10 -070054 simpleLogEntryMatcher->add_key_value_matcher()->mutable_key_matcher()->set_key(
55 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE*/);
56 simpleLogEntryMatcher->mutable_key_value_matcher(0)->set_eq_int(
57 2 /*SCREEN_STATE_CHANGE__DISPLAY_STATE__STATE_ON*/);
58
59 eventMatcher = config.add_log_entry_matcher();
60 eventMatcher->set_name("SCREEN_IS_OFF");
61
62 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
Yao Chen729093d2017-10-16 10:33:26 -070063 simpleLogEntryMatcher->set_tag(2 /*SCREEN_STATE_CHANGE*/);
Yao Chencaf339d2017-10-06 16:01:10 -070064 simpleLogEntryMatcher->add_key_value_matcher()->mutable_key_matcher()->set_key(
65 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE*/);
66 simpleLogEntryMatcher->mutable_key_value_matcher(0)->set_eq_int(
67 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE__STATE_OFF*/);
68
69 eventMatcher = config.add_log_entry_matcher();
70 eventMatcher->set_name("SCREEN_ON_OR_OFF");
71
72 LogEntryMatcher_Combination* combination = eventMatcher->mutable_combination();
73 combination->set_operation(LogicalOperation::OR);
74 combination->add_matcher("SCREEN_IS_ON");
75 combination->add_matcher("SCREEN_IS_OFF");
76
Yangster-mace2cd6d52017-11-09 20:38:30 -080077 CountMetric* metric = config.add_count_metric();
78 metric->set_name("3");
79 metric->set_what("SCREEN_IS_ON");
80 metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
81 KeyMatcher* keyMatcher = metric->add_dimension();
82 keyMatcher->set_key(1);
83
84 auto alert = config.add_alert();
85 alert->set_name("3");
86 alert->set_metric_name("3");
87 alert->set_number_of_buckets(10);
88 alert->set_refractory_period_secs(100);
89 alert->set_trigger_if_sum_gt(100);
Yao Chencaf339d2017-10-06 16:01:10 -070090 return config;
91}
92
93StatsdConfig buildCircleMatchers() {
94 StatsdConfig config;
Yangster-macd1815dc2017-11-13 21:43:15 -080095 config.set_name("12345");
Yao Chencaf339d2017-10-06 16:01:10 -070096
97 LogEntryMatcher* eventMatcher = config.add_log_entry_matcher();
98 eventMatcher->set_name("SCREEN_IS_ON");
99
100 SimpleLogEntryMatcher* simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
Yao Chen729093d2017-10-16 10:33:26 -0700101 simpleLogEntryMatcher->set_tag(2 /*SCREEN_STATE_CHANGE*/);
Yao Chencaf339d2017-10-06 16:01:10 -0700102 simpleLogEntryMatcher->add_key_value_matcher()->mutable_key_matcher()->set_key(
103 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE*/);
104 simpleLogEntryMatcher->mutable_key_value_matcher(0)->set_eq_int(
105 2 /*SCREEN_STATE_CHANGE__DISPLAY_STATE__STATE_ON*/);
106
107 eventMatcher = config.add_log_entry_matcher();
108 eventMatcher->set_name("SCREEN_ON_OR_OFF");
109
110 LogEntryMatcher_Combination* combination = eventMatcher->mutable_combination();
111 combination->set_operation(LogicalOperation::OR);
112 combination->add_matcher("SCREEN_IS_ON");
113 // Circle dependency
114 combination->add_matcher("SCREEN_ON_OR_OFF");
115
116 return config;
117}
118
Yangster-mace2cd6d52017-11-09 20:38:30 -0800119StatsdConfig buildAlertWithUnknownMetric() {
120 StatsdConfig config;
121 config.set_name("12345");
122
123 LogEntryMatcher* eventMatcher = config.add_log_entry_matcher();
124 eventMatcher->set_name("SCREEN_IS_ON");
125
126 CountMetric* metric = config.add_count_metric();
127 metric->set_name("3");
128 metric->set_what("SCREEN_IS_ON");
129 metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
130 KeyMatcher* keyMatcher = metric->add_dimension();
131 keyMatcher->set_key(1);
132
133 auto alert = config.add_alert();
134 alert->set_name("3");
135 alert->set_metric_name("2");
136 alert->set_number_of_buckets(10);
137 alert->set_refractory_period_secs(100);
138 alert->set_trigger_if_sum_gt(100);
139 return config;
140}
141
Yao Chencaf339d2017-10-06 16:01:10 -0700142StatsdConfig buildMissingMatchers() {
143 StatsdConfig config;
Yangster-macd1815dc2017-11-13 21:43:15 -0800144 config.set_name("12345");
Yao Chencaf339d2017-10-06 16:01:10 -0700145
146 LogEntryMatcher* eventMatcher = config.add_log_entry_matcher();
147 eventMatcher->set_name("SCREEN_IS_ON");
148
149 SimpleLogEntryMatcher* simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
Yao Chen729093d2017-10-16 10:33:26 -0700150 simpleLogEntryMatcher->set_tag(2 /*SCREEN_STATE_CHANGE*/);
Yao Chencaf339d2017-10-06 16:01:10 -0700151 simpleLogEntryMatcher->add_key_value_matcher()->mutable_key_matcher()->set_key(
152 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE*/);
153 simpleLogEntryMatcher->mutable_key_value_matcher(0)->set_eq_int(
154 2 /*SCREEN_STATE_CHANGE__DISPLAY_STATE__STATE_ON*/);
155
156 eventMatcher = config.add_log_entry_matcher();
157 eventMatcher->set_name("SCREEN_ON_OR_OFF");
158
159 LogEntryMatcher_Combination* combination = eventMatcher->mutable_combination();
160 combination->set_operation(LogicalOperation::OR);
161 combination->add_matcher("SCREEN_IS_ON");
162 // undefined matcher
163 combination->add_matcher("ABC");
164
165 return config;
166}
167
Yao Chen5154a3792017-10-30 22:57:06 -0700168StatsdConfig buildDimensionMetricsWithMultiTags() {
169 StatsdConfig config;
Yangster-macd1815dc2017-11-13 21:43:15 -0800170 config.set_name("12345");
Yao Chen5154a3792017-10-30 22:57:06 -0700171
172 LogEntryMatcher* eventMatcher = config.add_log_entry_matcher();
173 eventMatcher->set_name("BATTERY_VERY_LOW");
174 SimpleLogEntryMatcher* simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
175 simpleLogEntryMatcher->set_tag(2);
176
177 eventMatcher = config.add_log_entry_matcher();
178 eventMatcher->set_name("BATTERY_VERY_VERY_LOW");
179 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
180 simpleLogEntryMatcher->set_tag(3);
181
182 eventMatcher = config.add_log_entry_matcher();
183 eventMatcher->set_name("BATTERY_LOW");
184
185 LogEntryMatcher_Combination* combination = eventMatcher->mutable_combination();
186 combination->set_operation(LogicalOperation::OR);
187 combination->add_matcher("BATTERY_VERY_LOW");
188 combination->add_matcher("BATTERY_VERY_VERY_LOW");
189
190 // Count process state changes, slice by uid, while SCREEN_IS_OFF
191 CountMetric* metric = config.add_count_metric();
Yangster-macd1815dc2017-11-13 21:43:15 -0800192 metric->set_name("3");
Yao Chen5154a3792017-10-30 22:57:06 -0700193 metric->set_what("BATTERY_LOW");
194 metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
195 KeyMatcher* keyMatcher = metric->add_dimension();
196 keyMatcher->set_key(1);
197
Yangster-mace2cd6d52017-11-09 20:38:30 -0800198 auto alert = config.add_alert();
199 alert->set_name("3");
200 alert->set_metric_name("3");
201 alert->set_number_of_buckets(10);
202 alert->set_refractory_period_secs(100);
203 alert->set_trigger_if_sum_gt(100);
Yao Chen5154a3792017-10-30 22:57:06 -0700204 return config;
205}
206
Yao Chencaf339d2017-10-06 16:01:10 -0700207StatsdConfig buildCircleConditions() {
208 StatsdConfig config;
Yangster-macd1815dc2017-11-13 21:43:15 -0800209 config.set_name("12345");
Yao Chencaf339d2017-10-06 16:01:10 -0700210
211 LogEntryMatcher* eventMatcher = config.add_log_entry_matcher();
212 eventMatcher->set_name("SCREEN_IS_ON");
213
214 SimpleLogEntryMatcher* simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
Yao Chen729093d2017-10-16 10:33:26 -0700215 simpleLogEntryMatcher->set_tag(2 /*SCREEN_STATE_CHANGE*/);
Yao Chencaf339d2017-10-06 16:01:10 -0700216 simpleLogEntryMatcher->add_key_value_matcher()->mutable_key_matcher()->set_key(
217 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE*/);
218 simpleLogEntryMatcher->mutable_key_value_matcher(0)->set_eq_int(
219 2 /*SCREEN_STATE_CHANGE__DISPLAY_STATE__STATE_ON*/);
220
221 eventMatcher = config.add_log_entry_matcher();
222 eventMatcher->set_name("SCREEN_IS_OFF");
223
224 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
Yao Chen729093d2017-10-16 10:33:26 -0700225 simpleLogEntryMatcher->set_tag(2 /*SCREEN_STATE_CHANGE*/);
Yao Chencaf339d2017-10-06 16:01:10 -0700226 simpleLogEntryMatcher->add_key_value_matcher()->mutable_key_matcher()->set_key(
227 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE*/);
228 simpleLogEntryMatcher->mutable_key_value_matcher(0)->set_eq_int(
229 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE__STATE_OFF*/);
230
Yangster-mace2cd6d52017-11-09 20:38:30 -0800231 auto condition = config.add_condition();
Yao Chencaf339d2017-10-06 16:01:10 -0700232 condition->set_name("SCREEN_IS_ON");
233 SimpleCondition* simpleCondition = condition->mutable_simple_condition();
234 simpleCondition->set_start("SCREEN_IS_ON");
235 simpleCondition->set_stop("SCREEN_IS_OFF");
236
237 condition = config.add_condition();
238 condition->set_name("SCREEN_IS_EITHER_ON_OFF");
239
240 Condition_Combination* combination = condition->mutable_combination();
241 combination->set_operation(LogicalOperation::OR);
242 combination->add_condition("SCREEN_IS_ON");
243 combination->add_condition("SCREEN_IS_EITHER_ON_OFF");
244
245 return config;
246}
247
248TEST(MetricsManagerTest, TestGoodConfig) {
249 StatsdConfig config = buildGoodConfig();
250 set<int> allTagIds;
251 vector<sp<LogMatchingTracker>> allLogEntryMatchers;
252 vector<sp<ConditionTracker>> allConditionTrackers;
253 vector<sp<MetricProducer>> allMetricProducers;
Yangster-mace2cd6d52017-11-09 20:38:30 -0800254 std::vector<sp<AnomalyTracker>> allAnomalyTrackers;
Yao Chencaf339d2017-10-06 16:01:10 -0700255 unordered_map<int, std::vector<int>> conditionToMetricMap;
256 unordered_map<int, std::vector<int>> trackerToMetricMap;
257 unordered_map<int, std::vector<int>> trackerToConditionMap;
258
Yao Chenb3561512017-11-21 18:07:17 -0800259 EXPECT_TRUE(initStatsdConfig(kConfigKey, config, allTagIds, allLogEntryMatchers,
260 allConditionTrackers, allMetricProducers, allAnomalyTrackers,
261 conditionToMetricMap, trackerToMetricMap, trackerToConditionMap));
Yangster-mace2cd6d52017-11-09 20:38:30 -0800262 EXPECT_EQ(1u, allMetricProducers.size());
263 EXPECT_EQ(1u, allAnomalyTrackers.size());
Yao Chencaf339d2017-10-06 16:01:10 -0700264}
265
Yao Chen5154a3792017-10-30 22:57:06 -0700266TEST(MetricsManagerTest, TestDimensionMetricsWithMultiTags) {
267 StatsdConfig config = buildDimensionMetricsWithMultiTags();
268 set<int> allTagIds;
269 vector<sp<LogMatchingTracker>> allLogEntryMatchers;
270 vector<sp<ConditionTracker>> allConditionTrackers;
271 vector<sp<MetricProducer>> allMetricProducers;
Yangster-mace2cd6d52017-11-09 20:38:30 -0800272 std::vector<sp<AnomalyTracker>> allAnomalyTrackers;
Yao Chen5154a3792017-10-30 22:57:06 -0700273 unordered_map<int, std::vector<int>> conditionToMetricMap;
274 unordered_map<int, std::vector<int>> trackerToMetricMap;
275 unordered_map<int, std::vector<int>> trackerToConditionMap;
276
Yao Chenb3561512017-11-21 18:07:17 -0800277 EXPECT_FALSE(initStatsdConfig(kConfigKey, config, allTagIds, allLogEntryMatchers,
278 allConditionTrackers, allMetricProducers, allAnomalyTrackers,
279 conditionToMetricMap, trackerToMetricMap, trackerToConditionMap));
Yao Chen5154a3792017-10-30 22:57:06 -0700280}
281
Yao Chencaf339d2017-10-06 16:01:10 -0700282TEST(MetricsManagerTest, TestCircleLogMatcherDependency) {
283 StatsdConfig config = buildCircleMatchers();
284 set<int> allTagIds;
285 vector<sp<LogMatchingTracker>> allLogEntryMatchers;
286 vector<sp<ConditionTracker>> allConditionTrackers;
287 vector<sp<MetricProducer>> allMetricProducers;
Yangster-mace2cd6d52017-11-09 20:38:30 -0800288 std::vector<sp<AnomalyTracker>> allAnomalyTrackers;
Yao Chencaf339d2017-10-06 16:01:10 -0700289 unordered_map<int, std::vector<int>> conditionToMetricMap;
290 unordered_map<int, std::vector<int>> trackerToMetricMap;
291 unordered_map<int, std::vector<int>> trackerToConditionMap;
292
Yao Chenb3561512017-11-21 18:07:17 -0800293 EXPECT_FALSE(initStatsdConfig(kConfigKey, config, allTagIds, allLogEntryMatchers,
294 allConditionTrackers, allMetricProducers, allAnomalyTrackers,
295 conditionToMetricMap, trackerToMetricMap, trackerToConditionMap));
Yao Chencaf339d2017-10-06 16:01:10 -0700296}
297
298TEST(MetricsManagerTest, TestMissingMatchers) {
299 StatsdConfig config = buildMissingMatchers();
300 set<int> allTagIds;
301 vector<sp<LogMatchingTracker>> allLogEntryMatchers;
302 vector<sp<ConditionTracker>> allConditionTrackers;
303 vector<sp<MetricProducer>> allMetricProducers;
Yangster-mace2cd6d52017-11-09 20:38:30 -0800304 std::vector<sp<AnomalyTracker>> allAnomalyTrackers;
Yao Chencaf339d2017-10-06 16:01:10 -0700305 unordered_map<int, std::vector<int>> conditionToMetricMap;
306 unordered_map<int, std::vector<int>> trackerToMetricMap;
307 unordered_map<int, std::vector<int>> trackerToConditionMap;
Yao Chenb3561512017-11-21 18:07:17 -0800308 EXPECT_FALSE(initStatsdConfig(kConfigKey, config, allTagIds, allLogEntryMatchers,
309 allConditionTrackers, allMetricProducers, allAnomalyTrackers,
310 conditionToMetricMap, trackerToMetricMap, trackerToConditionMap));
Yao Chencaf339d2017-10-06 16:01:10 -0700311}
312
313TEST(MetricsManagerTest, TestCircleConditionDependency) {
314 StatsdConfig config = buildCircleConditions();
315 set<int> allTagIds;
316 vector<sp<LogMatchingTracker>> allLogEntryMatchers;
317 vector<sp<ConditionTracker>> allConditionTrackers;
318 vector<sp<MetricProducer>> allMetricProducers;
Yangster-mace2cd6d52017-11-09 20:38:30 -0800319 std::vector<sp<AnomalyTracker>> allAnomalyTrackers;
Yao Chencaf339d2017-10-06 16:01:10 -0700320 unordered_map<int, std::vector<int>> conditionToMetricMap;
321 unordered_map<int, std::vector<int>> trackerToMetricMap;
322 unordered_map<int, std::vector<int>> trackerToConditionMap;
323
Yao Chenb3561512017-11-21 18:07:17 -0800324 EXPECT_FALSE(initStatsdConfig(kConfigKey, config, allTagIds, allLogEntryMatchers,
325 allConditionTrackers, allMetricProducers, allAnomalyTrackers,
326 conditionToMetricMap, trackerToMetricMap, trackerToConditionMap));
Yangster-mace2cd6d52017-11-09 20:38:30 -0800327}
328
329TEST(MetricsManagerTest, testAlertWithUnknownMetric) {
330 StatsdConfig config = buildAlertWithUnknownMetric();
331 set<int> allTagIds;
332 vector<sp<LogMatchingTracker>> allLogEntryMatchers;
333 vector<sp<ConditionTracker>> allConditionTrackers;
334 vector<sp<MetricProducer>> allMetricProducers;
335 std::vector<sp<AnomalyTracker>> allAnomalyTrackers;
336 unordered_map<int, std::vector<int>> conditionToMetricMap;
337 unordered_map<int, std::vector<int>> trackerToMetricMap;
338 unordered_map<int, std::vector<int>> trackerToConditionMap;
339
Yao Chenb3561512017-11-21 18:07:17 -0800340 EXPECT_FALSE(initStatsdConfig(kConfigKey, config, allTagIds, allLogEntryMatchers,
341 allConditionTrackers, allMetricProducers, allAnomalyTrackers,
342 conditionToMetricMap, trackerToMetricMap, trackerToConditionMap));
Yao Chencaf339d2017-10-06 16:01:10 -0700343}
344
345#else
346GTEST_LOG_(INFO) << "This test does nothing.\n";
347#endif