1 /*
2 * Copyright (c) 2022-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 <cmath>
18 #include <cstdio>
19 #include <gtest/gtest.h>
20 #include <securec.h>
21 #include <string>
22 #include <unistd.h>
23 #include <vector>
24 #include "hdf_base.h"
25 #include "osal_time.h"
26 #include "sensor_callback_impl.h"
27 #include "sensor_type.h"
28 #include "v1_1/isensor_interface.h"
29
30 using namespace OHOS::HDI::Sensor::V1_1;
31 using namespace testing::ext;
32 using namespace std;
33
34 namespace {
35 sptr<ISensorInterface> g_sensorInterface = nullptr;
36 sptr<ISensorCallback> g_traditionalCallback = new SensorCallbackImpl();
37 sptr<ISensorCallback> g_medicalCallback = new SensorCallbackImpl();
38 std::vector<HdfSensorInformation> g_info;
39
40 constexpr int32_t ITERATION_FREQUENCY = 100;
41 constexpr int32_t REPETITION_FREQUENCY = 3;
42 constexpr int32_t SENSOR_INTERVAL1 = 20;
43 constexpr int32_t SENSOR_INTERVAL2 = 2;
44 constexpr int32_t SENSOR_POLL_TIME = 3;
45 constexpr int32_t SENSOR_WAIT_TIME = 10;
46
47 class SensorBenchmarkTest : public benchmark::Fixture {
48 public:
49 void SetUp(const ::benchmark::State &state);
50 void TearDown(const ::benchmark::State &state);
51 };
52
SetUp(const::benchmark::State & state)53 void SensorBenchmarkTest::SetUp(const ::benchmark::State &state)
54 {
55 g_sensorInterface = ISensorInterface::Get();
56 }
57
TearDown(const::benchmark::State & state)58 void SensorBenchmarkTest::TearDown(const ::benchmark::State &state)
59 {
60 }
61
62 /**
63 * @tc.name: DriverSystem_SensorBenchmark_GetAllSensorInfo
64 * @tc.desc: Benchmarktest for interface GetAllSensorInfo
65 * Obtains information about all sensors in the system
66 * @tc.type: FUNC
67 */
BENCHMARK_F(SensorBenchmarkTest,GetAllSensorInfo)68 BENCHMARK_F(SensorBenchmarkTest, GetAllSensorInfo)(benchmark::State &state)
69 {
70 ASSERT_NE(nullptr, g_sensorInterface);
71
72 int32_t ret;
73
74 for (auto _ : state) {
75 ret = g_sensorInterface->GetAllSensorInfo(g_info);
76 EXPECT_EQ(SENSOR_SUCCESS, ret);
77 }
78 }
79
80 BENCHMARK_REGISTER_F(SensorBenchmarkTest, GetAllSensorInfo)->
81 Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
82
83 /**
84 * @tc.name: DriverSystem_SensorBenchmark_register
85 * @tc.desc: Benchmarktest for interface register
86 * Returns 0 if the callback is successfully registered; returns a negative value otherwise
87 * @tc.type: FUNC
88 */
BENCHMARK_F(SensorBenchmarkTest,register)89 BENCHMARK_F(SensorBenchmarkTest, register)(benchmark::State &state)
90 {
91 ASSERT_NE(nullptr, g_sensorInterface);
92
93 int32_t ret;
94
95 for (auto _ : state) {
96 ret = g_sensorInterface->Register(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback);
97 EXPECT_EQ(SENSOR_SUCCESS, ret);
98 }
99 ret = g_sensorInterface->Unregister(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback);
100 EXPECT_EQ(SENSOR_SUCCESS, ret);
101 }
102
103 BENCHMARK_REGISTER_F(SensorBenchmarkTest, register)->
104 Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
105
106 /**
107 * @tc.name: DriverSystem_SensorBenchmark_Unregister
108 * @tc.desc: Benchmarktest for interface Unregister
109 * Returns 0 if the callback is successfully registered; returns a negative value otherwise
110 * @tc.type: FUNC
111 */
BENCHMARK_F(SensorBenchmarkTest,Unregister)112 BENCHMARK_F(SensorBenchmarkTest, Unregister)(benchmark::State &state)
113 {
114 ASSERT_NE(nullptr, g_sensorInterface);
115
116 int32_t ret;
117
118 for (auto _ : state) {
119 ret = g_sensorInterface->Register(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback);
120 OsalMSleep(SENSOR_POLL_TIME);
121 EXPECT_EQ(SENSOR_SUCCESS, ret);
122 ret = g_sensorInterface->Unregister(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback);
123 OsalMSleep(SENSOR_POLL_TIME);
124 EXPECT_EQ(SENSOR_SUCCESS, ret);
125 }
126 }
127
128 BENCHMARK_REGISTER_F(SensorBenchmarkTest, Unregister)->
129 Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
130
131 /**
132 * @tc.name: DriverSystem_SensorBenchmark_Enable
133 * @tc.desc: Benchmarktest for interface Enable
134 * Enables the sensor unavailable in the sensor list based on the specified sensor ID
135 * @tc.type: FUNC
136 */
BENCHMARK_F(SensorBenchmarkTest,Enable)137 BENCHMARK_F(SensorBenchmarkTest, Enable)(benchmark::State &state)
138 {
139 ASSERT_NE(nullptr, g_sensorInterface);
140
141 int32_t ret;
142
143 ret = g_sensorInterface->Register(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback);
144 EXPECT_EQ(SENSOR_SUCCESS, ret);
145
146 EXPECT_GT(g_info.size(), 0);
147
148 ret = g_sensorInterface->SetBatch(0, SENSOR_INTERVAL1, SENSOR_POLL_TIME);
149 EXPECT_EQ(SENSOR_SUCCESS, ret);
150 for (auto _ : state) {
151 ret = g_sensorInterface->Enable(0);
152 EXPECT_EQ(SENSOR_SUCCESS, ret);
153 }
154 OsalMSleep(SENSOR_POLL_TIME);
155 ret = g_sensorInterface->Disable(0);
156 EXPECT_EQ(SENSOR_SUCCESS, ret);
157 ret = g_sensorInterface->Unregister(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback);
158 EXPECT_EQ(SensorCallbackImpl::sensorDataFlag, 1);
159 SensorCallbackImpl::sensorDataFlag = 1;
160 }
161
162 BENCHMARK_REGISTER_F(SensorBenchmarkTest, Enable)->
163 Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
164
165 /**
166 * @tc.name: DriverSystem_SensorBenchmark_Disable
167 * @tc.desc: Benchmarktest for interface Disable
168 * Enables the sensor unavailable in the sensor list based on the specified sensor ID
169 * @tc.type: FUNC
170 */
BENCHMARK_F(SensorBenchmarkTest,Disable)171 BENCHMARK_F(SensorBenchmarkTest, Disable)(benchmark::State &state)
172 {
173 ASSERT_NE(nullptr, g_sensorInterface);
174
175 int32_t ret;
176
177 ret = g_sensorInterface->Register(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback);
178 EXPECT_EQ(SENSOR_SUCCESS, ret);
179 EXPECT_GT(g_info.size(), 0);
180
181 ret = g_sensorInterface->SetBatch(0, SENSOR_INTERVAL1, SENSOR_POLL_TIME);
182 EXPECT_EQ(SENSOR_SUCCESS, ret);
183 ret = g_sensorInterface->Enable(0);
184 EXPECT_EQ(SENSOR_SUCCESS, ret);
185 OsalMSleep(SENSOR_POLL_TIME);
186 for (auto _ : state) {
187 ret = g_sensorInterface->Disable(0);
188 EXPECT_EQ(SENSOR_SUCCESS, ret);
189 }
190 ret = g_sensorInterface->Unregister(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback);
191 EXPECT_EQ(SensorCallbackImpl::sensorDataFlag, 1);
192 SensorCallbackImpl::sensorDataFlag = 1;
193 }
194
195 BENCHMARK_REGISTER_F(SensorBenchmarkTest, Disable)->
196 Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
197
198 /**
199 * @tc.name: DriverSystem_SensorBenchmark_SetBatch
200 * @tc.desc: Benchmarktest for interface SetBatch
201 * Sets the sampling time and data report interval for sensors in batches
202 * @tc.type: FUNC
203 */
BENCHMARK_F(SensorBenchmarkTest,SetBatch)204 BENCHMARK_F(SensorBenchmarkTest, SetBatch)(benchmark::State &state)
205 {
206 ASSERT_NE(nullptr, g_sensorInterface);
207
208 int32_t ret;
209
210 ret = g_sensorInterface->Register(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback);
211 EXPECT_EQ(SENSOR_SUCCESS, ret);
212
213 for (auto _ : state) {
214 ret = g_sensorInterface->SetBatch(0, SENSOR_INTERVAL2, SENSOR_POLL_TIME);
215 EXPECT_EQ(SENSOR_SUCCESS, ret);
216 }
217 ret = g_sensorInterface->Enable(0);
218 EXPECT_EQ(SENSOR_SUCCESS, ret);
219 OsalMSleep(SENSOR_WAIT_TIME);
220 ret = g_sensorInterface->Disable(0);
221 EXPECT_EQ(SENSOR_SUCCESS, ret);
222 ret = g_sensorInterface->Unregister(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback);
223 EXPECT_EQ(SENSOR_SUCCESS, ret);
224 EXPECT_EQ(SensorCallbackImpl::sensorDataFlag, 1);
225 SensorCallbackImpl::sensorDataFlag = 1;
226 }
227
228 BENCHMARK_REGISTER_F(SensorBenchmarkTest, SetBatch)->
229 Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
230
231 /**
232 * @tc.name: DriverSystem_SensorBenchmark_SetMode
233 * @tc.desc: Benchmarktest for interface SetMode
234 * Sets the data reporting mode for the specified sensor
235 * @tc.type: FUNC
236 */
BENCHMARK_F(SensorBenchmarkTest,SetMode)237 BENCHMARK_F(SensorBenchmarkTest, SetMode)(benchmark::State &state)
238 {
239 ASSERT_NE(nullptr, g_sensorInterface);
240 EXPECT_GT(g_info.size(), 0);
241
242 int32_t ret;
243
244 ret = g_sensorInterface->SetBatch(0, SENSOR_INTERVAL1, SENSOR_POLL_TIME);
245 EXPECT_EQ(SENSOR_SUCCESS, ret);
246 for (auto _ : state) {
247 if (SENSOR_TYPE_HALL == 0) {
248 ret = g_sensorInterface->SetMode(0, SENSOR_MODE_ON_CHANGE);
249 } else {
250 ret = g_sensorInterface->SetMode(0, SENSOR_MODE_REALTIME);
251 }
252 EXPECT_EQ(SENSOR_SUCCESS, ret);
253 }
254 ret = g_sensorInterface->Enable(0);
255 EXPECT_EQ(SENSOR_SUCCESS, ret);
256 OsalMSleep(SENSOR_WAIT_TIME);
257 ret = g_sensorInterface->Disable(0);
258 EXPECT_EQ(SENSOR_SUCCESS, ret);
259 }
260
261 BENCHMARK_REGISTER_F(SensorBenchmarkTest, SetMode)->
262 Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
263
264 /**
265 * @tc.name: DriverSystem_SensorBenchmark_SetOption
266 * @tc.desc: Benchmarktest for interface SetOption
267 * Sets options for the specified sensor, including its measurement range and accuracy
268 * @tc.type: FUNC
269 */
BENCHMARK_F(SensorBenchmarkTest,SetOption)270 BENCHMARK_F(SensorBenchmarkTest, SetOption)(benchmark::State &state)
271 {
272 ASSERT_NE(nullptr, g_sensorInterface);
273 EXPECT_GT(g_info.size(), 0);
274
275 int32_t ret;
276
277 for (auto _ : state) {
278 ret = g_sensorInterface->SetOption(0, 0);
279 EXPECT_EQ(SENSOR_SUCCESS, ret);
280 }
281 }
282
283 BENCHMARK_REGISTER_F(SensorBenchmarkTest, SetOption)->
284 Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
285 }
286
287 BENCHMARK_MAIN();
288