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
15 #include "src/metrics/EventMetricProducer.h"
16
17 #include <gmock/gmock.h>
18 #include <gtest/gtest.h>
19 #include <stdio.h>
20
21 #include <vector>
22
23 #include "metrics_test_helper.h"
24 #include "stats_event.h"
25 #include "tests/statsd_test_util.h"
26
27 using namespace testing;
28 using android::sp;
29 using std::set;
30 using std::unordered_map;
31 using std::vector;
32
33 #ifdef __ANDROID__
34
35 namespace android {
36 namespace os {
37 namespace statsd {
38
39 const ConfigKey kConfigKey(0, 12345);
40
41 namespace {
makeLogEvent(LogEvent * logEvent,int32_t atomId,int64_t timestampNs,string str)42 void makeLogEvent(LogEvent* logEvent, int32_t atomId, int64_t timestampNs, string str) {
43 AStatsEvent* statsEvent = AStatsEvent_obtain();
44 AStatsEvent_setAtomId(statsEvent, atomId);
45 AStatsEvent_overwriteTimestamp(statsEvent, timestampNs);
46 AStatsEvent_writeString(statsEvent, str.c_str());
47
48 parseStatsEventToLogEvent(statsEvent, logEvent);
49 }
50 } // anonymous namespace
51
TEST(EventMetricProducerTest,TestNoCondition)52 TEST(EventMetricProducerTest, TestNoCondition) {
53 int64_t bucketStartTimeNs = 10000000000;
54 int64_t eventStartTimeNs = bucketStartTimeNs + 1;
55 int64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
56
57 EventMetric metric;
58 metric.set_id(1);
59
60 LogEvent event1(/*uid=*/0, /*pid=*/0);
61 CreateNoValuesLogEvent(&event1, 1 /*tagId*/, bucketStartTimeNs + 1);
62
63 LogEvent event2(/*uid=*/0, /*pid=*/0);
64 CreateNoValuesLogEvent(&event2, 1 /*tagId*/, bucketStartTimeNs + 2);
65
66 sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
67
68 EventMetricProducer eventProducer(kConfigKey, metric, -1 /*-1 meaning no condition*/, {},
69 wizard, bucketStartTimeNs);
70
71 eventProducer.onMatchedLogEvent(1 /*matcher index*/, event1);
72 eventProducer.onMatchedLogEvent(1 /*matcher index*/, event2);
73
74 // Check dump report content.
75 ProtoOutputStream output;
76 std::set<string> strSet;
77 eventProducer.onDumpReport(bucketStartTimeNs + 20, true /*include current partial bucket*/,
78 true /*erase data*/, FAST, &strSet, &output);
79
80 StatsLogReport report = outputStreamToProto(&output);
81 EXPECT_TRUE(report.has_event_metrics());
82 ASSERT_EQ(2, report.event_metrics().data_size());
83 EXPECT_EQ(bucketStartTimeNs + 1, report.event_metrics().data(0).elapsed_timestamp_nanos());
84 EXPECT_EQ(bucketStartTimeNs + 2, report.event_metrics().data(1).elapsed_timestamp_nanos());
85 }
86
TEST(EventMetricProducerTest,TestEventsWithNonSlicedCondition)87 TEST(EventMetricProducerTest, TestEventsWithNonSlicedCondition) {
88 int64_t bucketStartTimeNs = 10000000000;
89 int64_t eventStartTimeNs = bucketStartTimeNs + 1;
90 int64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
91
92 EventMetric metric;
93 metric.set_id(1);
94 metric.set_condition(StringToId("SCREEN_ON"));
95
96 LogEvent event1(/*uid=*/0, /*pid=*/0);
97 CreateNoValuesLogEvent(&event1, 1 /*tagId*/, bucketStartTimeNs + 1);
98
99 LogEvent event2(/*uid=*/0, /*pid=*/0);
100 CreateNoValuesLogEvent(&event2, 1 /*tagId*/, bucketStartTimeNs + 10);
101
102 sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
103
104 EventMetricProducer eventProducer(kConfigKey, metric, 0 /*condition index*/,
105 {ConditionState::kUnknown}, wizard, bucketStartTimeNs);
106
107 eventProducer.onConditionChanged(true /*condition*/, bucketStartTimeNs);
108 eventProducer.onMatchedLogEvent(1 /*matcher index*/, event1);
109
110 eventProducer.onConditionChanged(false /*condition*/, bucketStartTimeNs + 2);
111
112 eventProducer.onMatchedLogEvent(1 /*matcher index*/, event2);
113
114 // Check dump report content.
115 ProtoOutputStream output;
116 std::set<string> strSet;
117 eventProducer.onDumpReport(bucketStartTimeNs + 20, true /*include current partial bucket*/,
118 true /*erase data*/, FAST, &strSet, &output);
119
120 StatsLogReport report = outputStreamToProto(&output);
121 EXPECT_TRUE(report.has_event_metrics());
122 ASSERT_EQ(1, report.event_metrics().data_size());
123 EXPECT_EQ(bucketStartTimeNs + 1, report.event_metrics().data(0).elapsed_timestamp_nanos());
124 }
125
TEST(EventMetricProducerTest,TestEventsWithSlicedCondition)126 TEST(EventMetricProducerTest, TestEventsWithSlicedCondition) {
127 int64_t bucketStartTimeNs = 10000000000;
128 int64_t bucketSizeNs = 30 * 1000 * 1000 * 1000LL;
129
130 int tagId = 1;
131 int conditionTagId = 2;
132
133 EventMetric metric;
134 metric.set_id(1);
135 metric.set_condition(StringToId("APP_IN_BACKGROUND_PER_UID_AND_SCREEN_ON"));
136 MetricConditionLink* link = metric.add_links();
137 link->set_condition(StringToId("APP_IN_BACKGROUND_PER_UID"));
138 buildSimpleAtomFieldMatcher(tagId, 1, link->mutable_fields_in_what());
139 buildSimpleAtomFieldMatcher(conditionTagId, 2, link->mutable_fields_in_condition());
140
141 LogEvent event1(/*uid=*/0, /*pid=*/0);
142 makeLogEvent(&event1, 1 /*tagId*/, bucketStartTimeNs + 1, "111");
143 ConditionKey key1;
144 key1[StringToId("APP_IN_BACKGROUND_PER_UID")] = {
145 getMockedDimensionKey(conditionTagId, 2, "111")};
146
147 LogEvent event2(/*uid=*/0, /*pid=*/0);
148 makeLogEvent(&event2, 1 /*tagId*/, bucketStartTimeNs + 10, "222");
149 ConditionKey key2;
150 key2[StringToId("APP_IN_BACKGROUND_PER_UID")] = {
151 getMockedDimensionKey(conditionTagId, 2, "222")};
152
153 sp<MockConditionWizard> wizard = new NaggyMock<MockConditionWizard>();
154 // Condition is false for first event.
155 EXPECT_CALL(*wizard, query(_, key1, _)).WillOnce(Return(ConditionState::kFalse));
156 // Condition is true for second event.
157 EXPECT_CALL(*wizard, query(_, key2, _)).WillOnce(Return(ConditionState::kTrue));
158
159 EventMetricProducer eventProducer(kConfigKey, metric, 0 /*condition index*/,
160 {ConditionState::kUnknown}, wizard, bucketStartTimeNs);
161
162 eventProducer.onMatchedLogEvent(1 /*matcher index*/, event1);
163 eventProducer.onMatchedLogEvent(1 /*matcher index*/, event2);
164
165 // Check dump report content.
166 ProtoOutputStream output;
167 std::set<string> strSet;
168 eventProducer.onDumpReport(bucketStartTimeNs + 20, true /*include current partial bucket*/,
169 true /*erase data*/, FAST, &strSet, &output);
170
171 StatsLogReport report = outputStreamToProto(&output);
172 EXPECT_TRUE(report.has_event_metrics());
173 ASSERT_EQ(1, report.event_metrics().data_size());
174 EXPECT_EQ(bucketStartTimeNs + 10, report.event_metrics().data(0).elapsed_timestamp_nanos());
175 }
176
177 } // namespace statsd
178 } // namespace os
179 } // namespace android
180 #else
181 GTEST_LOG_(INFO) << "This test does nothing.\n";
182 #endif
183