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