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