1 /*
2 * Copyright (C) 2024 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 <psh_utils/PowerStatsCollector.h>
18 #include <gtest/gtest.h>
19 #include <utils/Log.h>
20
21 using namespace android::media::psh_utils;
22
23 template <typename T>
inRange(const T & a,const T & b,const T & c)24 void inRange(const T& a, const T& b, const T& c) {
25 ASSERT_GE(a, std::min(b, c));
26 ASSERT_LE(a, std::max(b, c));
27 }
28
TEST(powerstat_collector_tests,basic)29 TEST(powerstat_collector_tests, basic) {
30 auto& psc = PowerStatsCollector::getCollector();
31
32 // This test is used for debugging the string through logcat, we validate a non-empty string.
33 auto powerStats = psc.getStats();
34 ALOGD("%s: %s", __func__, powerStats->toString().c_str());
35 EXPECT_FALSE(powerStats->toString().empty());
36 }
37
TEST(powerstat_collector_tests,metadata)38 TEST(powerstat_collector_tests, metadata) {
39 PowerStats ps1, ps2;
40
41 constexpr uint64_t kDurationMs1 = 5;
42 constexpr uint64_t kDurationMs2 = 10;
43 ps1.metadata.duration_ms = kDurationMs1;
44 ps2.metadata.duration_ms = kDurationMs2;
45
46 constexpr uint64_t kDurationMonotonicMs1 = 3;
47 constexpr uint64_t kDurationMonotonicMs2 = 9;
48 ps1.metadata.duration_monotonic_ms = kDurationMonotonicMs1;
49 ps2.metadata.duration_monotonic_ms = kDurationMonotonicMs2;
50
51 constexpr uint64_t kStartTimeSinceBootMs1 = 1616;
52 constexpr uint64_t kStartTimeEpochMs1 = 1121;
53 constexpr uint64_t kStartTimeMonotonicMs1 = 1525;
54 constexpr uint64_t kStartTimeSinceBootMs2 = 2616;
55 constexpr uint64_t kStartTimeEpochMs2 = 2121;
56 constexpr uint64_t kStartTimeMonotonicMs2 = 2525;
57
58 ps1.metadata.start_time_since_boot_ms = kStartTimeSinceBootMs1;
59 ps1.metadata.start_time_epoch_ms = kStartTimeEpochMs1;
60 ps1.metadata.start_time_monotonic_ms = kStartTimeMonotonicMs1;
61 ps2.metadata.start_time_since_boot_ms = kStartTimeSinceBootMs2;
62 ps2.metadata.start_time_epoch_ms = kStartTimeEpochMs2;
63 ps2.metadata.start_time_monotonic_ms = kStartTimeMonotonicMs2;
64
65 PowerStats ps3 = ps1 + ps2;
66 PowerStats ps4 = ps2 + ps1;
67 EXPECT_EQ(ps3, ps4);
68 EXPECT_EQ(kDurationMs1 + kDurationMs2,
69 ps3.metadata.duration_ms);
70 EXPECT_EQ(kDurationMonotonicMs1 + kDurationMonotonicMs2,
71 ps3.metadata.duration_monotonic_ms);
72
73 EXPECT_NO_FATAL_FAILURE(inRange(ps3.metadata.start_time_since_boot_ms,
74 kStartTimeSinceBootMs1, kStartTimeSinceBootMs2));
75 EXPECT_NO_FATAL_FAILURE(inRange(ps3.metadata.start_time_epoch_ms,
76 kStartTimeEpochMs1, kStartTimeEpochMs2));
77 EXPECT_NO_FATAL_FAILURE(inRange(ps3.metadata.start_time_monotonic_ms,
78 kStartTimeMonotonicMs1, kStartTimeMonotonicMs2));
79
80 PowerStats ps5 = ps2 - ps1;
81 EXPECT_EQ(kDurationMs2 - kDurationMs1,
82 ps5.metadata.duration_ms);
83 EXPECT_EQ(kDurationMonotonicMs2 - kDurationMonotonicMs1,
84 ps5.metadata.duration_monotonic_ms);
85
86 EXPECT_NO_FATAL_FAILURE(inRange(ps5.metadata.start_time_since_boot_ms,
87 kStartTimeSinceBootMs1, kStartTimeSinceBootMs2));
88 EXPECT_NO_FATAL_FAILURE(inRange(ps5.metadata.start_time_epoch_ms,
89 kStartTimeEpochMs1, kStartTimeEpochMs2));
90 EXPECT_NO_FATAL_FAILURE(inRange(ps5.metadata.start_time_monotonic_ms,
91 kStartTimeMonotonicMs1, kStartTimeMonotonicMs2));
92 }
93
TEST(powerstat_collector_tests,state_residency)94 TEST(powerstat_collector_tests, state_residency) {
95 PowerStats ps1, ps2;
96
97 constexpr uint64_t kTimeMs1 = 5;
98 constexpr uint64_t kTimeMs2 = 10;
99 constexpr uint64_t kEntryCount1 = 15;
100 constexpr uint64_t kEntryCount2 = 18;
101
102 ps1.power_entity_state_residency.emplace_back(
103 PowerStats::StateResidency{"", "", kTimeMs1, kEntryCount1});
104 ps2.power_entity_state_residency.emplace_back(
105 PowerStats::StateResidency{"", "", kTimeMs2, kEntryCount2});
106
107 PowerStats ps3 = ps1 + ps2;
108 PowerStats ps4 = ps2 + ps1;
109 EXPECT_EQ(ps3, ps4);
110 EXPECT_EQ(kTimeMs1 + kTimeMs2,
111 ps3.power_entity_state_residency[0].time_ms);
112 EXPECT_EQ(kEntryCount1 + kEntryCount2,
113 ps3.power_entity_state_residency[0].entry_count);
114
115 PowerStats ps5 = ps2 - ps1;
116 EXPECT_EQ(kTimeMs2 - kTimeMs1,
117 ps5.power_entity_state_residency[0].time_ms);
118 EXPECT_EQ(kEntryCount2 - kEntryCount1,
119 ps5.power_entity_state_residency[0].entry_count);
120 }
121
TEST(powerstat_collector_tests,rail_energy)122 TEST(powerstat_collector_tests, rail_energy) {
123 PowerStats ps1, ps2;
124
125 constexpr uint64_t kEnergyUws1 = 5;
126 constexpr uint64_t kEnergyUws2 = 10;
127
128 ps1.rail_energy.emplace_back(
129 PowerStats::RailEnergy{"", "", kEnergyUws1});
130 ps2.rail_energy.emplace_back(
131 PowerStats::RailEnergy{"", "", kEnergyUws2});
132
133 PowerStats ps3 = ps1 + ps2;
134 PowerStats ps4 = ps2 + ps1;
135 EXPECT_EQ(ps3, ps4);
136 EXPECT_EQ(kEnergyUws1 + kEnergyUws2,
137 ps3.rail_energy[0].energy_uws);
138
139 PowerStats ps5 = ps2 - ps1;
140 EXPECT_EQ(kEnergyUws2 - kEnergyUws1,
141 ps5.rail_energy[0].energy_uws);
142 }
143
TEST(powerstat_collector_tests,health_stats)144 TEST(powerstat_collector_tests, health_stats) {
145 PowerStats ps1, ps2;
146
147 constexpr double kBatteryChargeCounterUah1 = 21;
148 constexpr double kBatteryChargeCounterUah2 = 25;
149 ps1.health_stats.batteryChargeCounterUah = kBatteryChargeCounterUah1;
150 ps2.health_stats.batteryChargeCounterUah = kBatteryChargeCounterUah2;
151
152 constexpr double kBatteryFullChargeUah1 = 32;
153 constexpr double kBatteryFullChargeUah2 = 33;
154 ps1.health_stats.batteryFullChargeUah = kBatteryFullChargeUah1;
155 ps2.health_stats.batteryFullChargeUah = kBatteryFullChargeUah2;
156
157 constexpr double kBatteryVoltageMillivolts1 = 42;
158 constexpr double kBatteryVoltageMillivolts2 = 43;
159 ps1.health_stats.batteryVoltageMillivolts = kBatteryVoltageMillivolts1;
160 ps2.health_stats.batteryVoltageMillivolts = kBatteryVoltageMillivolts2;
161
162 PowerStats ps3 = ps1 + ps2;
163 PowerStats ps4 = ps2 + ps1;
164 EXPECT_EQ(ps3, ps4);
165 EXPECT_EQ(kBatteryChargeCounterUah1 + kBatteryChargeCounterUah2,
166 ps3.health_stats.batteryChargeCounterUah);
167
168 EXPECT_NO_FATAL_FAILURE(inRange(ps3.health_stats.batteryFullChargeUah,
169 kBatteryFullChargeUah1, kBatteryFullChargeUah2));
170 EXPECT_NO_FATAL_FAILURE(inRange(ps3.health_stats.batteryVoltageMillivolts,
171 kBatteryVoltageMillivolts1, kBatteryVoltageMillivolts2));
172
173 PowerStats ps5 = ps2 - ps1;
174 EXPECT_EQ(kBatteryChargeCounterUah2 - kBatteryChargeCounterUah1,
175 ps5.health_stats.batteryChargeCounterUah);
176
177 EXPECT_NO_FATAL_FAILURE(inRange(ps5.health_stats.batteryFullChargeUah,
178 kBatteryFullChargeUah1, kBatteryFullChargeUah2));
179 EXPECT_NO_FATAL_FAILURE(inRange(ps5.health_stats.batteryVoltageMillivolts,
180 kBatteryVoltageMillivolts1, kBatteryVoltageMillivolts2));
181 }
182