blob: e8e4d8be1cd64e3cb51083e0fdcae5ab938b7561 [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"
20#include "src/metrics/MetricProducer.h"
Chenjie Yub3dda412017-10-24 13:41:59 -070021#include "src/metrics/ValueMetricProducer.h"
Joe Onorato9fc9edf2017-10-15 20:08:52 -070022#include "src/metrics/metrics_manager_util.h"
Yao Chencaf339d2017-10-06 16:01:10 -070023
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
31using namespace android::os::statsd;
32using android::sp;
33using std::set;
34using std::unordered_map;
35using std::vector;
36
37#ifdef __ANDROID__
38
39// TODO: ADD MORE TEST CASES.
40
41StatsdConfig 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 Chen729093d2017-10-16 10:33:26 -070049 simpleLogEntryMatcher->set_tag(2 /*SCREEN_STATE_CHANGE*/);
Yao Chencaf339d2017-10-06 16:01:10 -070050 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 Chen729093d2017-10-16 10:33:26 -070059 simpleLogEntryMatcher->set_tag(2 /*SCREEN_STATE_CHANGE*/);
Yao Chencaf339d2017-10-06 16:01:10 -070060 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
76StatsdConfig 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 Chen729093d2017-10-16 10:33:26 -070084 simpleLogEntryMatcher->set_tag(2 /*SCREEN_STATE_CHANGE*/);
Yao Chencaf339d2017-10-06 16:01:10 -070085 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
102StatsdConfig 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 Chen729093d2017-10-16 10:33:26 -0700110 simpleLogEntryMatcher->set_tag(2 /*SCREEN_STATE_CHANGE*/);
Yao Chencaf339d2017-10-06 16:01:10 -0700111 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 Chen5154a3792017-10-30 22:57:06 -0700128StatsdConfig 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 Chencaf339d2017-10-06 16:01:10 -0700161StatsdConfig 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 Chen729093d2017-10-16 10:33:26 -0700169 simpleLogEntryMatcher->set_tag(2 /*SCREEN_STATE_CHANGE*/);
Yao Chencaf339d2017-10-06 16:01:10 -0700170 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 Chen729093d2017-10-16 10:33:26 -0700179 simpleLogEntryMatcher->set_tag(2 /*SCREEN_STATE_CHANGE*/);
Yao Chencaf339d2017-10-06 16:01:10 -0700180 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
202TEST(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 Chen5154a3792017-10-30 22:57:06 -0700217TEST(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 Chencaf339d2017-10-06 16:01:10 -0700232TEST(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
247TEST(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
262TEST(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
278GTEST_LOG_(INFO) << "This test does nothing.\n";
279#endif