blob: 3dd4e70ff49c76459fef3a0e05b32c3015f8f2e4 [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
43StatsdConfig buildGoodConfig() {
44 StatsdConfig config;
Yangster-macd1815dc2017-11-13 21:43:15 -080045 config.set_name("12345");
Yao Chencaf339d2017-10-06 16:01:10 -070046
47 LogEntryMatcher* eventMatcher = config.add_log_entry_matcher();
48 eventMatcher->set_name("SCREEN_IS_ON");
49
50 SimpleLogEntryMatcher* simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
Yao Chen729093d2017-10-16 10:33:26 -070051 simpleLogEntryMatcher->set_tag(2 /*SCREEN_STATE_CHANGE*/);
Yao Chencaf339d2017-10-06 16:01:10 -070052 simpleLogEntryMatcher->add_key_value_matcher()->mutable_key_matcher()->set_key(
53 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE*/);
54 simpleLogEntryMatcher->mutable_key_value_matcher(0)->set_eq_int(
55 2 /*SCREEN_STATE_CHANGE__DISPLAY_STATE__STATE_ON*/);
56
57 eventMatcher = config.add_log_entry_matcher();
58 eventMatcher->set_name("SCREEN_IS_OFF");
59
60 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
Yao Chen729093d2017-10-16 10:33:26 -070061 simpleLogEntryMatcher->set_tag(2 /*SCREEN_STATE_CHANGE*/);
Yao Chencaf339d2017-10-06 16:01:10 -070062 simpleLogEntryMatcher->add_key_value_matcher()->mutable_key_matcher()->set_key(
63 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE*/);
64 simpleLogEntryMatcher->mutable_key_value_matcher(0)->set_eq_int(
65 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE__STATE_OFF*/);
66
67 eventMatcher = config.add_log_entry_matcher();
68 eventMatcher->set_name("SCREEN_ON_OR_OFF");
69
70 LogEntryMatcher_Combination* combination = eventMatcher->mutable_combination();
71 combination->set_operation(LogicalOperation::OR);
72 combination->add_matcher("SCREEN_IS_ON");
73 combination->add_matcher("SCREEN_IS_OFF");
74
Yangster-mace2cd6d52017-11-09 20:38:30 -080075 CountMetric* metric = config.add_count_metric();
76 metric->set_name("3");
77 metric->set_what("SCREEN_IS_ON");
78 metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
79 KeyMatcher* keyMatcher = metric->add_dimension();
80 keyMatcher->set_key(1);
81
82 auto alert = config.add_alert();
83 alert->set_name("3");
84 alert->set_metric_name("3");
85 alert->set_number_of_buckets(10);
86 alert->set_refractory_period_secs(100);
87 alert->set_trigger_if_sum_gt(100);
Yao Chencaf339d2017-10-06 16:01:10 -070088 return config;
89}
90
91StatsdConfig buildCircleMatchers() {
92 StatsdConfig config;
Yangster-macd1815dc2017-11-13 21:43:15 -080093 config.set_name("12345");
Yao Chencaf339d2017-10-06 16:01:10 -070094
95 LogEntryMatcher* eventMatcher = config.add_log_entry_matcher();
96 eventMatcher->set_name("SCREEN_IS_ON");
97
98 SimpleLogEntryMatcher* simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
Yao Chen729093d2017-10-16 10:33:26 -070099 simpleLogEntryMatcher->set_tag(2 /*SCREEN_STATE_CHANGE*/);
Yao Chencaf339d2017-10-06 16:01:10 -0700100 simpleLogEntryMatcher->add_key_value_matcher()->mutable_key_matcher()->set_key(
101 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE*/);
102 simpleLogEntryMatcher->mutable_key_value_matcher(0)->set_eq_int(
103 2 /*SCREEN_STATE_CHANGE__DISPLAY_STATE__STATE_ON*/);
104
105 eventMatcher = config.add_log_entry_matcher();
106 eventMatcher->set_name("SCREEN_ON_OR_OFF");
107
108 LogEntryMatcher_Combination* combination = eventMatcher->mutable_combination();
109 combination->set_operation(LogicalOperation::OR);
110 combination->add_matcher("SCREEN_IS_ON");
111 // Circle dependency
112 combination->add_matcher("SCREEN_ON_OR_OFF");
113
114 return config;
115}
116
Yangster-mace2cd6d52017-11-09 20:38:30 -0800117StatsdConfig buildAlertWithUnknownMetric() {
118 StatsdConfig config;
119 config.set_name("12345");
120
121 LogEntryMatcher* eventMatcher = config.add_log_entry_matcher();
122 eventMatcher->set_name("SCREEN_IS_ON");
123
124 CountMetric* metric = config.add_count_metric();
125 metric->set_name("3");
126 metric->set_what("SCREEN_IS_ON");
127 metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
128 KeyMatcher* keyMatcher = metric->add_dimension();
129 keyMatcher->set_key(1);
130
131 auto alert = config.add_alert();
132 alert->set_name("3");
133 alert->set_metric_name("2");
134 alert->set_number_of_buckets(10);
135 alert->set_refractory_period_secs(100);
136 alert->set_trigger_if_sum_gt(100);
137 return config;
138}
139
Yao Chencaf339d2017-10-06 16:01:10 -0700140StatsdConfig buildMissingMatchers() {
141 StatsdConfig config;
Yangster-macd1815dc2017-11-13 21:43:15 -0800142 config.set_name("12345");
Yao Chencaf339d2017-10-06 16:01:10 -0700143
144 LogEntryMatcher* eventMatcher = config.add_log_entry_matcher();
145 eventMatcher->set_name("SCREEN_IS_ON");
146
147 SimpleLogEntryMatcher* simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
Yao Chen729093d2017-10-16 10:33:26 -0700148 simpleLogEntryMatcher->set_tag(2 /*SCREEN_STATE_CHANGE*/);
Yao Chencaf339d2017-10-06 16:01:10 -0700149 simpleLogEntryMatcher->add_key_value_matcher()->mutable_key_matcher()->set_key(
150 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE*/);
151 simpleLogEntryMatcher->mutable_key_value_matcher(0)->set_eq_int(
152 2 /*SCREEN_STATE_CHANGE__DISPLAY_STATE__STATE_ON*/);
153
154 eventMatcher = config.add_log_entry_matcher();
155 eventMatcher->set_name("SCREEN_ON_OR_OFF");
156
157 LogEntryMatcher_Combination* combination = eventMatcher->mutable_combination();
158 combination->set_operation(LogicalOperation::OR);
159 combination->add_matcher("SCREEN_IS_ON");
160 // undefined matcher
161 combination->add_matcher("ABC");
162
163 return config;
164}
165
Yao Chen5154a3792017-10-30 22:57:06 -0700166StatsdConfig buildDimensionMetricsWithMultiTags() {
167 StatsdConfig config;
Yangster-macd1815dc2017-11-13 21:43:15 -0800168 config.set_name("12345");
Yao Chen5154a3792017-10-30 22:57:06 -0700169
170 LogEntryMatcher* eventMatcher = config.add_log_entry_matcher();
171 eventMatcher->set_name("BATTERY_VERY_LOW");
172 SimpleLogEntryMatcher* simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
173 simpleLogEntryMatcher->set_tag(2);
174
175 eventMatcher = config.add_log_entry_matcher();
176 eventMatcher->set_name("BATTERY_VERY_VERY_LOW");
177 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
178 simpleLogEntryMatcher->set_tag(3);
179
180 eventMatcher = config.add_log_entry_matcher();
181 eventMatcher->set_name("BATTERY_LOW");
182
183 LogEntryMatcher_Combination* combination = eventMatcher->mutable_combination();
184 combination->set_operation(LogicalOperation::OR);
185 combination->add_matcher("BATTERY_VERY_LOW");
186 combination->add_matcher("BATTERY_VERY_VERY_LOW");
187
188 // Count process state changes, slice by uid, while SCREEN_IS_OFF
189 CountMetric* metric = config.add_count_metric();
Yangster-macd1815dc2017-11-13 21:43:15 -0800190 metric->set_name("3");
Yao Chen5154a3792017-10-30 22:57:06 -0700191 metric->set_what("BATTERY_LOW");
192 metric->mutable_bucket()->set_bucket_size_millis(30 * 1000L);
193 KeyMatcher* keyMatcher = metric->add_dimension();
194 keyMatcher->set_key(1);
195
Yangster-mace2cd6d52017-11-09 20:38:30 -0800196 auto alert = config.add_alert();
197 alert->set_name("3");
198 alert->set_metric_name("3");
199 alert->set_number_of_buckets(10);
200 alert->set_refractory_period_secs(100);
201 alert->set_trigger_if_sum_gt(100);
Yao Chen5154a3792017-10-30 22:57:06 -0700202 return config;
203}
204
Yao Chencaf339d2017-10-06 16:01:10 -0700205StatsdConfig buildCircleConditions() {
206 StatsdConfig config;
Yangster-macd1815dc2017-11-13 21:43:15 -0800207 config.set_name("12345");
Yao Chencaf339d2017-10-06 16:01:10 -0700208
209 LogEntryMatcher* eventMatcher = config.add_log_entry_matcher();
210 eventMatcher->set_name("SCREEN_IS_ON");
211
212 SimpleLogEntryMatcher* simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
Yao Chen729093d2017-10-16 10:33:26 -0700213 simpleLogEntryMatcher->set_tag(2 /*SCREEN_STATE_CHANGE*/);
Yao Chencaf339d2017-10-06 16:01:10 -0700214 simpleLogEntryMatcher->add_key_value_matcher()->mutable_key_matcher()->set_key(
215 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE*/);
216 simpleLogEntryMatcher->mutable_key_value_matcher(0)->set_eq_int(
217 2 /*SCREEN_STATE_CHANGE__DISPLAY_STATE__STATE_ON*/);
218
219 eventMatcher = config.add_log_entry_matcher();
220 eventMatcher->set_name("SCREEN_IS_OFF");
221
222 simpleLogEntryMatcher = eventMatcher->mutable_simple_log_entry_matcher();
Yao Chen729093d2017-10-16 10:33:26 -0700223 simpleLogEntryMatcher->set_tag(2 /*SCREEN_STATE_CHANGE*/);
Yao Chencaf339d2017-10-06 16:01:10 -0700224 simpleLogEntryMatcher->add_key_value_matcher()->mutable_key_matcher()->set_key(
225 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE*/);
226 simpleLogEntryMatcher->mutable_key_value_matcher(0)->set_eq_int(
227 1 /*SCREEN_STATE_CHANGE__DISPLAY_STATE__STATE_OFF*/);
228
Yangster-mace2cd6d52017-11-09 20:38:30 -0800229 auto condition = config.add_condition();
Yao Chencaf339d2017-10-06 16:01:10 -0700230 condition->set_name("SCREEN_IS_ON");
231 SimpleCondition* simpleCondition = condition->mutable_simple_condition();
232 simpleCondition->set_start("SCREEN_IS_ON");
233 simpleCondition->set_stop("SCREEN_IS_OFF");
234
235 condition = config.add_condition();
236 condition->set_name("SCREEN_IS_EITHER_ON_OFF");
237
238 Condition_Combination* combination = condition->mutable_combination();
239 combination->set_operation(LogicalOperation::OR);
240 combination->add_condition("SCREEN_IS_ON");
241 combination->add_condition("SCREEN_IS_EITHER_ON_OFF");
242
243 return config;
244}
245
246TEST(MetricsManagerTest, TestGoodConfig) {
247 StatsdConfig config = buildGoodConfig();
248 set<int> allTagIds;
249 vector<sp<LogMatchingTracker>> allLogEntryMatchers;
250 vector<sp<ConditionTracker>> allConditionTrackers;
251 vector<sp<MetricProducer>> allMetricProducers;
Yangster-mace2cd6d52017-11-09 20:38:30 -0800252 std::vector<sp<AnomalyTracker>> allAnomalyTrackers;
Yao Chencaf339d2017-10-06 16:01:10 -0700253 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_TRUE(initStatsdConfig(config, allTagIds, allLogEntryMatchers, allConditionTrackers,
Yangster-mace2cd6d52017-11-09 20:38:30 -0800258 allMetricProducers, allAnomalyTrackers, conditionToMetricMap,
259 trackerToMetricMap, trackerToConditionMap));
260 EXPECT_EQ(1u, allMetricProducers.size());
261 EXPECT_EQ(1u, allAnomalyTrackers.size());
Yao Chencaf339d2017-10-06 16:01:10 -0700262}
263
Yao Chen5154a3792017-10-30 22:57:06 -0700264TEST(MetricsManagerTest, TestDimensionMetricsWithMultiTags) {
265 StatsdConfig config = buildDimensionMetricsWithMultiTags();
266 set<int> allTagIds;
267 vector<sp<LogMatchingTracker>> allLogEntryMatchers;
268 vector<sp<ConditionTracker>> allConditionTrackers;
269 vector<sp<MetricProducer>> allMetricProducers;
Yangster-mace2cd6d52017-11-09 20:38:30 -0800270 std::vector<sp<AnomalyTracker>> allAnomalyTrackers;
Yao Chen5154a3792017-10-30 22:57:06 -0700271 unordered_map<int, std::vector<int>> conditionToMetricMap;
272 unordered_map<int, std::vector<int>> trackerToMetricMap;
273 unordered_map<int, std::vector<int>> trackerToConditionMap;
274
275 EXPECT_FALSE(initStatsdConfig(config, allTagIds, allLogEntryMatchers, allConditionTrackers,
Yangster-mace2cd6d52017-11-09 20:38:30 -0800276 allMetricProducers, allAnomalyTrackers, conditionToMetricMap,
277 trackerToMetricMap, trackerToConditionMap));
Yao Chen5154a3792017-10-30 22:57:06 -0700278}
279
Yao Chencaf339d2017-10-06 16:01:10 -0700280TEST(MetricsManagerTest, TestCircleLogMatcherDependency) {
281 StatsdConfig config = buildCircleMatchers();
282 set<int> allTagIds;
283 vector<sp<LogMatchingTracker>> allLogEntryMatchers;
284 vector<sp<ConditionTracker>> allConditionTrackers;
285 vector<sp<MetricProducer>> allMetricProducers;
Yangster-mace2cd6d52017-11-09 20:38:30 -0800286 std::vector<sp<AnomalyTracker>> allAnomalyTrackers;
Yao Chencaf339d2017-10-06 16:01:10 -0700287 unordered_map<int, std::vector<int>> conditionToMetricMap;
288 unordered_map<int, std::vector<int>> trackerToMetricMap;
289 unordered_map<int, std::vector<int>> trackerToConditionMap;
290
291 EXPECT_FALSE(initStatsdConfig(config, allTagIds, allLogEntryMatchers, allConditionTrackers,
Yangster-mace2cd6d52017-11-09 20:38:30 -0800292 allMetricProducers, allAnomalyTrackers, conditionToMetricMap,
293 trackerToMetricMap, trackerToConditionMap));
Yao Chencaf339d2017-10-06 16:01:10 -0700294}
295
296TEST(MetricsManagerTest, TestMissingMatchers) {
297 StatsdConfig config = buildMissingMatchers();
298 set<int> allTagIds;
299 vector<sp<LogMatchingTracker>> allLogEntryMatchers;
300 vector<sp<ConditionTracker>> allConditionTrackers;
301 vector<sp<MetricProducer>> allMetricProducers;
Yangster-mace2cd6d52017-11-09 20:38:30 -0800302 std::vector<sp<AnomalyTracker>> allAnomalyTrackers;
Yao Chencaf339d2017-10-06 16:01:10 -0700303 unordered_map<int, std::vector<int>> conditionToMetricMap;
304 unordered_map<int, std::vector<int>> trackerToMetricMap;
305 unordered_map<int, std::vector<int>> trackerToConditionMap;
Yao Chencaf339d2017-10-06 16:01:10 -0700306 EXPECT_FALSE(initStatsdConfig(config, allTagIds, allLogEntryMatchers, allConditionTrackers,
Yangster-mace2cd6d52017-11-09 20:38:30 -0800307 allMetricProducers, allAnomalyTrackers, conditionToMetricMap,
308 trackerToMetricMap, trackerToConditionMap));
Yao Chencaf339d2017-10-06 16:01:10 -0700309}
310
311TEST(MetricsManagerTest, TestCircleConditionDependency) {
312 StatsdConfig config = buildCircleConditions();
313 set<int> allTagIds;
314 vector<sp<LogMatchingTracker>> allLogEntryMatchers;
315 vector<sp<ConditionTracker>> allConditionTrackers;
316 vector<sp<MetricProducer>> allMetricProducers;
Yangster-mace2cd6d52017-11-09 20:38:30 -0800317 std::vector<sp<AnomalyTracker>> allAnomalyTrackers;
Yao Chencaf339d2017-10-06 16:01:10 -0700318 unordered_map<int, std::vector<int>> conditionToMetricMap;
319 unordered_map<int, std::vector<int>> trackerToMetricMap;
320 unordered_map<int, std::vector<int>> trackerToConditionMap;
321
322 EXPECT_FALSE(initStatsdConfig(config, allTagIds, allLogEntryMatchers, allConditionTrackers,
Yangster-mace2cd6d52017-11-09 20:38:30 -0800323 allMetricProducers, allAnomalyTrackers, conditionToMetricMap,
324 trackerToMetricMap, trackerToConditionMap));
325}
326
327TEST(MetricsManagerTest, testAlertWithUnknownMetric) {
328 StatsdConfig config = buildAlertWithUnknownMetric();
329 set<int> allTagIds;
330 vector<sp<LogMatchingTracker>> allLogEntryMatchers;
331 vector<sp<ConditionTracker>> allConditionTrackers;
332 vector<sp<MetricProducer>> allMetricProducers;
333 std::vector<sp<AnomalyTracker>> allAnomalyTrackers;
334 unordered_map<int, std::vector<int>> conditionToMetricMap;
335 unordered_map<int, std::vector<int>> trackerToMetricMap;
336 unordered_map<int, std::vector<int>> trackerToConditionMap;
337
338 EXPECT_FALSE(initStatsdConfig(config, allTagIds, allLogEntryMatchers, allConditionTrackers,
339 allMetricProducers, allAnomalyTrackers, conditionToMetricMap,
340 trackerToMetricMap, trackerToConditionMap));
Yao Chencaf339d2017-10-06 16:01:10 -0700341}
342
343#else
344GTEST_LOG_(INFO) << "This test does nothing.\n";
345#endif