1 /*
2 * Copyright 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <gtest/gtest.h>
18
19 #include <media/EventMetric.h>
20
21 namespace android {
22
23 /**
24 * Unit tests for the EventMetric class.
25 */
26
TEST(EventMetricTest,IntDataTypeEmpty)27 TEST(EventMetricTest, IntDataTypeEmpty) {
28 EventMetric<int> metric("MyMetricName", "MetricAttributeName");
29
30 std::map<int, EventStatistics> values;
31
32 metric.ExportValues(
33 [&] (int attribute_value, const EventStatistics& value) {
34 values[attribute_value] = value;
35 });
36
37 EXPECT_TRUE(values.empty());
38 }
39
TEST(EventMetricTest,IntDataType)40 TEST(EventMetricTest, IntDataType) {
41 EventMetric<int> metric("MyMetricName", "MetricAttributeName");
42
43 std::map<int, EventStatistics> values;
44
45 metric.Record(4, 7);
46 metric.Record(5, 8);
47 metric.Record(5, 8);
48 metric.Record(5, 8);
49 metric.Record(6, 8);
50 metric.Record(6, 8);
51 metric.Record(6, 8);
52
53 metric.ExportValues(
54 [&] (int attribute_value, const EventStatistics& value) {
55 values[attribute_value] = value;
56 });
57
58 ASSERT_EQ(2u, values.size());
59 EXPECT_EQ(4, values[7].min);
60 EXPECT_EQ(4, values[7].max);
61 EXPECT_EQ(4, values[7].mean);
62 EXPECT_EQ(1, values[7].count);
63
64 EXPECT_EQ(5, values[8].min);
65 EXPECT_EQ(6, values[8].max);
66 // This is an approximate value because of the technique we're using.
67 EXPECT_NEAR(5.5, values[8].mean, 0.2);
68 EXPECT_EQ(6, values[8].count);
69 }
70
TEST(EventMetricTest,StringDataType)71 TEST(EventMetricTest, StringDataType) {
72 EventMetric<std::string> metric("MyMetricName", "MetricAttributeName");
73
74 std::map<std::string, EventStatistics> values;
75
76 metric.Record(1, "a");
77 metric.Record(2, "b");
78 metric.Record(2, "b");
79 metric.Record(3, "b");
80 metric.Record(3, "b");
81
82 metric.ExportValues(
83 [&] (std::string attribute_value, const EventStatistics& value) {
84 values[attribute_value] = value;
85 });
86
87 ASSERT_EQ(2u, values.size());
88 EXPECT_EQ(1, values["a"].min);
89 EXPECT_EQ(1, values["a"].max);
90 EXPECT_EQ(1, values["a"].mean);
91 EXPECT_EQ(1, values["a"].count);
92
93 EXPECT_EQ(2, values["b"].min);
94 EXPECT_EQ(3, values["b"].max);
95 EXPECT_NEAR(2.5, values["b"].mean, 0.2);
96 EXPECT_EQ(4, values["b"].count);
97 }
98
99 // Helper class that allows us to mock the clock.
100 template<typename AttributeType>
101 class MockEventTimer : public EventTimer<AttributeType> {
102 public:
MockEventTimer(nsecs_t time_delta_ns,EventMetric<AttributeType> * metric)103 explicit MockEventTimer(nsecs_t time_delta_ns,
104 EventMetric<AttributeType>* metric)
105 : EventTimer<AttributeType>(metric) {
106 // Pretend the event started earlier.
107 this->start_time_ = systemTime() - time_delta_ns;
108 }
109 };
110
TEST(EventTimerTest,IntDataType)111 TEST(EventTimerTest, IntDataType) {
112 EventMetric<int> metric("MyMetricName", "MetricAttributeName");
113
114 for (int i = 0; i < 5; i++) {
115 {
116 // Add a mock time delta.
117 MockEventTimer<int> metric_timer(i * 1000000, &metric);
118 metric_timer.SetAttribute(i % 2);
119 }
120 }
121
122 std::map<int, EventStatistics> values;
123 metric.ExportValues(
124 [&] (int attribute_value, const EventStatistics& value) {
125 values[attribute_value] = value;
126 });
127
128 ASSERT_EQ(2u, values.size());
129 EXPECT_LT(values[0].min, values[0].max);
130 EXPECT_GE(4000, values[0].max);
131 EXPECT_GT(values[0].mean, values[0].min);
132 EXPECT_LE(values[0].mean, values[0].max);
133 EXPECT_EQ(3, values[0].count);
134
135 EXPECT_LT(values[1].min, values[1].max);
136 EXPECT_GE(3000, values[1].max);
137 EXPECT_GT(values[1].mean, values[1].min);
138 EXPECT_LE(values[1].mean, values[1].max);
139 EXPECT_EQ(2, values[1].count);
140 }
141
142 } // namespace android
143