1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
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
16 #include <benchmark/benchmark.h>
17 #include <gtest/gtest.h>
18 #include <string>
19 #include <vector>
20
21 #include "battery_info.h"
22 #include "battery_srv_client.h"
23
24 using namespace std;
25
26 namespace OHOS {
27 namespace PowerMgr {
28 class BatteryBenchmarkTest : public benchmark::Fixture {
29 public:
SetUp(const::benchmark::State & state)30 void SetUp(const ::benchmark::State& state) {}
TearDown(const::benchmark::State & state)31 void TearDown(const ::benchmark::State& state) {}
32 };
33
34 namespace {
35 auto& g_batterySrvClient = BatterySrvClient::GetInstance();
36 const int32_t ITERATION_FREQUENCY = 100;
37 const int32_t REPETITION_FREQUENCY = 3;
38
39 /**
40 * @tc.name: GetCapacity
41 * @tc.desc: Testcase for testing "GetCapacity" function.
42 * @tc.type: FUNC
43 */
BENCHMARK_F(BatteryBenchmarkTest,GetCapacity)44 BENCHMARK_F(BatteryBenchmarkTest, GetCapacity)(benchmark::State& st)
45 {
46 for (auto _ : st) {
47 int32_t capactiy = g_batterySrvClient.GetCapacity();
48 // capacity is range of 0 - 100
49 ASSERT_TRUE(capactiy >= 0 && capactiy <= 100);
50 }
51 }
52 BENCHMARK_REGISTER_F(BatteryBenchmarkTest, GetCapacity)
53 ->Iterations(ITERATION_FREQUENCY)
54 ->Repetitions(REPETITION_FREQUENCY)
55 ->ReportAggregatesOnly();
56
57 /**
58 * @tc.name: GetChargingStatus
59 * @tc.desc: Testcase for testing "GetChargingStatus" function.
60 * @tc.type: FUNC
61 */
BENCHMARK_F(BatteryBenchmarkTest,GetChargingStatus)62 BENCHMARK_F(BatteryBenchmarkTest, GetChargingStatus)(benchmark::State& st)
63 {
64 for (auto _ : st) {
65 auto chargeState = g_batterySrvClient.GetChargingStatus();
66 ASSERT_TRUE(chargeState >= BatteryChargeState::CHARGE_STATE_NONE &&
67 chargeState < BatteryChargeState::CHARGE_STATE_BUTT);
68 }
69 }
70 BENCHMARK_REGISTER_F(BatteryBenchmarkTest, GetChargingStatus)
71 ->Iterations(ITERATION_FREQUENCY)
72 ->Repetitions(REPETITION_FREQUENCY)
73 ->ReportAggregatesOnly();
74
75 /**
76 * @tc.name: GetHealthStatus
77 * @tc.desc: Testcase for testing "GetHealthStatus" function.
78 * @tc.type: FUNC
79 */
BENCHMARK_F(BatteryBenchmarkTest,GetHealthStatus)80 BENCHMARK_F(BatteryBenchmarkTest, GetHealthStatus)(benchmark::State& st)
81 {
82 for (auto _ : st) {
83 auto healthStatus = g_batterySrvClient.GetHealthStatus();
84 ASSERT_TRUE(healthStatus >= BatteryHealthState::HEALTH_STATE_UNKNOWN &&
85 healthStatus < BatteryHealthState::HEALTH_STATE_BUTT);
86 }
87 }
88 BENCHMARK_REGISTER_F(BatteryBenchmarkTest, GetHealthStatus)
89 ->Iterations(ITERATION_FREQUENCY)
90 ->Repetitions(REPETITION_FREQUENCY)
91 ->ReportAggregatesOnly();
92
93 /**
94 * @tc.name: GetPluggedType
95 * @tc.desc: Testcase for testing "GetPluggedType" function.
96 * @tc.type: FUNC
97 */
BENCHMARK_F(BatteryBenchmarkTest,GetPluggedType)98 BENCHMARK_F(BatteryBenchmarkTest, GetPluggedType)(benchmark::State& st)
99 {
100 for (auto _ : st) {
101 auto pluggedType = g_batterySrvClient.GetPluggedType();
102 ASSERT_TRUE(pluggedType >= BatteryPluggedType::PLUGGED_TYPE_NONE &&
103 pluggedType < BatteryPluggedType::PLUGGED_TYPE_BUTT);
104 }
105 }
106 BENCHMARK_REGISTER_F(BatteryBenchmarkTest, GetPluggedType)
107 ->Iterations(ITERATION_FREQUENCY)
108 ->Repetitions(REPETITION_FREQUENCY)
109 ->ReportAggregatesOnly();
110
111 /**
112 * @tc.name: GetVoltage
113 * @tc.desc: Testcase for testing "GetVoltage" function.
114 * @tc.type: FUNC
115 */
BENCHMARK_F(BatteryBenchmarkTest,GetVoltage)116 BENCHMARK_F(BatteryBenchmarkTest, GetVoltage)(benchmark::State& st)
117 {
118 for (auto _ : st) {
119 // voltage above 0μV
120 ASSERT_TRUE(g_batterySrvClient.GetVoltage() >= 0);
121 }
122 }
123 BENCHMARK_REGISTER_F(BatteryBenchmarkTest, GetVoltage)
124 ->Iterations(ITERATION_FREQUENCY)
125 ->Repetitions(REPETITION_FREQUENCY)
126 ->ReportAggregatesOnly();
127
128 /**
129 * @tc.name: GetPresent
130 * @tc.desc: Testcase for testing "GetPresent" function.
131 * @tc.type: FUNC
132 */
BENCHMARK_F(BatteryBenchmarkTest,GetPresent)133 BENCHMARK_F(BatteryBenchmarkTest, GetPresent)(benchmark::State& st)
134 {
135 for (auto _ : st) {
136 bool present = g_batterySrvClient.GetPresent();
137 ASSERT_TRUE(present || !present);
138 }
139 }
140 BENCHMARK_REGISTER_F(BatteryBenchmarkTest, GetPresent)
141 ->Iterations(ITERATION_FREQUENCY)
142 ->Repetitions(REPETITION_FREQUENCY)
143 ->ReportAggregatesOnly();
144
145 /**
146 * @tc.name: GetTechnology
147 * @tc.desc: Testcase for testing "GetTechnology" function.
148 * @tc.type: FUNC
149 */
BENCHMARK_F(BatteryBenchmarkTest,GetTechnology)150 BENCHMARK_F(BatteryBenchmarkTest, GetTechnology)(benchmark::State& st)
151 {
152 for (auto _ : st) {
153 auto technology = g_batterySrvClient.GetTechnology();
154 ASSERT_TRUE(!technology.empty());
155 }
156 }
157 BENCHMARK_REGISTER_F(BatteryBenchmarkTest, GetTechnology)
158 ->Iterations(ITERATION_FREQUENCY)
159 ->Repetitions(REPETITION_FREQUENCY)
160 ->ReportAggregatesOnly();
161
162 /**
163 * @tc.name: GetBatteryTemperature
164 * @tc.desc: Testcase for testing "GetBatteryTemperature" function.
165 * @tc.type: FUNC
166 */
BENCHMARK_F(BatteryBenchmarkTest,GetBatteryTemperature)167 BENCHMARK_F(BatteryBenchmarkTest, GetBatteryTemperature)(benchmark::State& st)
168 {
169 for (auto _ : st) {
170 auto temperature = g_batterySrvClient.GetBatteryTemperature();
171 // Temperature range -20.0°C to 60.0°C
172 ASSERT_TRUE(temperature >= -200 && temperature <= 600);
173 }
174 }
175 BENCHMARK_REGISTER_F(BatteryBenchmarkTest, GetBatteryTemperature)
176 ->Iterations(ITERATION_FREQUENCY)
177 ->Repetitions(REPETITION_FREQUENCY)
178 ->ReportAggregatesOnly();
179
180 /**
181 * @tc.name: GetNowCurrent
182 * @tc.desc: Testcase for testing "GetNowCurrent" function.
183 * @tc.type: FUNC
184 */
BENCHMARK_F(BatteryBenchmarkTest,GetNowCurrent)185 BENCHMARK_F(BatteryBenchmarkTest, GetNowCurrent)(benchmark::State& st)
186 {
187 for (auto _ : st) {
188 auto currnow = g_batterySrvClient.GetNowCurrent();
189 // Now current range -20000mA - 20000mA
190 ASSERT_TRUE(currnow >= -20000 && currnow <= 20000);
191 }
192 }
193 BENCHMARK_REGISTER_F(BatteryBenchmarkTest, GetNowCurrent)
194 ->Iterations(ITERATION_FREQUENCY)
195 ->Repetitions(REPETITION_FREQUENCY)
196 ->ReportAggregatesOnly();
197
198 /**
199 * @tc.name: GetRemainEnergy
200 * @tc.desc: Testcase for testing "GetRemainEnergy" function.
201 * @tc.type: FUNC
202 */
BENCHMARK_F(BatteryBenchmarkTest,GetRemainEnergy)203 BENCHMARK_F(BatteryBenchmarkTest, GetRemainEnergy)(benchmark::State& st)
204 {
205 for (auto _ : st) {
206 auto chargenow = g_batterySrvClient.GetRemainEnergy();
207 // RemainEnergy above 0mA
208 ASSERT_TRUE(chargenow >= 0);
209 }
210 }
211 BENCHMARK_REGISTER_F(BatteryBenchmarkTest, GetRemainEnergy)
212 ->Iterations(ITERATION_FREQUENCY)
213 ->Repetitions(REPETITION_FREQUENCY)
214 ->ReportAggregatesOnly();
215
216 /**
217 * @tc.name: GetTotalEnergy
218 * @tc.desc: Testcase for testing "GetTotalEnergy" function.
219 * @tc.type: FUNC
220 */
BENCHMARK_F(BatteryBenchmarkTest,GetTotalEnergy)221 BENCHMARK_F(BatteryBenchmarkTest, GetTotalEnergy)(benchmark::State& st)
222 {
223 for (auto _ : st) {
224 auto totalenergy = g_batterySrvClient.GetTotalEnergy();
225 // Totalenergy above 0mA
226 ASSERT_TRUE(totalenergy >= 0);
227 }
228 }
229 BENCHMARK_REGISTER_F(BatteryBenchmarkTest, GetTotalEnergy)
230 ->Iterations(ITERATION_FREQUENCY)
231 ->Repetitions(REPETITION_FREQUENCY)
232 ->ReportAggregatesOnly();
233
234 /**
235 * @tc.name: GetCapacityLevel
236 * @tc.desc: Testcase for testing "GetCapacityLevel" function.
237 * @tc.type: FUNC
238 */
BENCHMARK_F(BatteryBenchmarkTest,GetCapacityLevel)239 BENCHMARK_F(BatteryBenchmarkTest, GetCapacityLevel)(benchmark::State& st)
240 {
241 for (auto _ : st) {
242 auto batterylevel = g_batterySrvClient.GetCapacityLevel();
243 ASSERT_TRUE(batterylevel >= BatteryCapacityLevel::LEVEL_NONE &&
244 batterylevel < BatteryCapacityLevel::LEVEL_RESERVED);
245 }
246 }
247 BENCHMARK_REGISTER_F(BatteryBenchmarkTest, GetCapacityLevel)
248 ->Iterations(ITERATION_FREQUENCY)
249 ->Repetitions(REPETITION_FREQUENCY)
250 ->ReportAggregatesOnly();
251
252 /**
253 * @tc.name: GetRemainingChargeTime
254 * @tc.desc: Testcase for testing "GetRemainingChargeTime" function.
255 * @tc.type: FUNC
256 */
BENCHMARK_F(BatteryBenchmarkTest,GetRemainingChargeTime)257 BENCHMARK_F(BatteryBenchmarkTest, GetRemainingChargeTime)(benchmark::State& st)
258 {
259 for (auto _ : st) {
260 // RemainingChargeTime above 0ms
261 ASSERT_TRUE(g_batterySrvClient.GetRemainingChargeTime() >= 0);
262 }
263 }
264 BENCHMARK_REGISTER_F(BatteryBenchmarkTest, GetRemainingChargeTime)
265 ->Iterations(ITERATION_FREQUENCY)
266 ->Repetitions(REPETITION_FREQUENCY)
267 ->ReportAggregatesOnly();
268 } // namespace
269 } // namespace PowerMgr
270 } // namespace OHOS
271
272 // Run the benchmark
273 BENCHMARK_MAIN();
274