1 /*
2 * Copyright (c) 2021-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 <string>
18 #include <vector>
19
20 #include <cmath>
21 #include <cstdio>
22 #include <unistd.h>
23 #include <gtest/gtest.h>
24 #include <securec.h>
25 #include "hdf_base.h"
26 #include "osal_time.h"
27 #include "v1_0/isensor_interface.h"
28 #include "sensor_type.h"
29 #include "../hdiService/sensor_callback_impl.h"
30
31 using namespace OHOS::HDI::Sensor::V1_0;
32 using namespace testing::ext;
33 using namespace std;
34
35 namespace {
36 sptr<ISensorInterface> g_sensorInterface = nullptr;
37 sptr<ISensorCallback> g_traditionalCallback = new SensorCallbackImpl();
38 sptr<ISensorCallback> g_medicalCallback = new SensorCallbackImpl();
39 std::vector<HdfSensorInformation> g_info;
40 struct SensorValueRange {
41 float highThreshold;
42 float lowThreshold;
43 };
44
45 struct SensorDevelopmentList {
46 int32_t sensorTypeId;
47 char sensorName[SENSOR_NAME_MAX_LEN];
48 int32_t dataForm; // 0: fixed, 1: range
49 int32_t dataDimension;
50 struct SensorValueRange *valueRange;
51 };
52
53 struct SensorValueRange g_testRange[] = {{1e5, 0}};
54 struct SensorValueRange g_accelRange[] = {{78, -78}, {78, -78}, {78, -78}};
55 struct SensorValueRange g_alsRange[] = {{10000, 0}};
56 struct SensorValueRange g_pedometerRange[] = {{10000, 0}};
57 struct SensorValueRange g_proximityRange[] = {{5, 0}};
58 struct SensorValueRange g_hallRange[] = {{1, 0}};
59 struct SensorValueRange g_barometerRange[] = {{1100, -1100}, {1100, -1100}};
60 struct SensorValueRange g_magneticRange[] = {{2000, -2000}, {2000, -2000}, {2000, -2000}};
61 struct SensorValueRange g_gyroscopeRange[] = {{35, -35}, {35, -35}, {35, -35}};
62 struct SensorValueRange g_gravityRange[] = {{78, -78}, {78, -78}, {78, -78}};
63
64 struct SensorDevelopmentList g_sensorList[] = {
65 {SENSOR_TYPE_NONE, "sensor_test", 1, 1, g_testRange},
66 {SENSOR_TYPE_ACCELEROMETER, "accelerometer", 1, 3, g_accelRange},
67 {SENSOR_TYPE_PEDOMETER, "pedometer", 1, 1, g_pedometerRange},
68 {SENSOR_TYPE_PROXIMITY, "proximity", 0, 1, g_proximityRange},
69 {SENSOR_TYPE_HALL, "hallrometer", 0, 1, g_hallRange},
70 {SENSOR_TYPE_BAROMETER, "barometer", 1, 2, g_barometerRange},
71 {SENSOR_TYPE_AMBIENT_LIGHT, "als", 1, 1, g_alsRange},
72 {SENSOR_TYPE_MAGNETIC_FIELD, "magnetometer", 1, 3, g_magneticRange},
73 {SENSOR_TYPE_GYROSCOPE, "gyroscope", 1, 3, g_gyroscopeRange},
74 {SENSOR_TYPE_GRAVITY, "gravity", 1, 3, g_gravityRange}
75 };
76
77 constexpr int g_listNum = sizeof(g_sensorList) / sizeof(g_sensorList[0]);
78 constexpr int32_t SENSOR_INTERVAL1 = 20;
79 constexpr int32_t SENSOR_INTERVAL2 = 2;
80 constexpr int32_t SENSOR_POLL_TIME = 1;
81 constexpr int32_t SENSOR_WAIT_TIME = 10;
82
83 class sensorBenchmarkTest : public benchmark::Fixture {
84 public:
85 void SetUp(const ::benchmark::State &state);
86 void TearDown(const ::benchmark::State &state);
87 };
88
SetUp(const::benchmark::State & state)89 void sensorBenchmarkTest::SetUp(const ::benchmark::State &state)
90 {
91 g_sensorInterface = ISensorInterface::Get();
92 }
93
TearDown(const::benchmark::State & state)94 void sensorBenchmarkTest::TearDown(const ::benchmark::State &state)
95 {
96 }
97
98 /**
99 * @tc.name: SUB_DriverSystem_SensorBenchmark_0010
100 * @tc.desc: Benchmarktest for interface GetAllSensorInfo.
101 * Obtains information about all sensors in the system.
102 * @tc.type: FUNC
103 */
BENCHMARK_F(sensorBenchmarkTest,SUB_DriverSystem_SensorBenchmark_0010)104 BENCHMARK_F(sensorBenchmarkTest, SUB_DriverSystem_SensorBenchmark_0010)(benchmark::State &st)
105 {
106 if (g_sensorInterface == nullptr) {
107 ASSERT_NE(nullptr, g_sensorInterface);
108 return;
109 }
110 int32_t ret;
111 for (auto _ : st) {
112 ret = g_sensorInterface->GetAllSensorInfo(g_info);
113 }
114 EXPECT_EQ(SENSOR_SUCCESS, ret);
115
116 for (auto iter : g_info) {
117 for (int j =0; j < g_listNum; ++j) {
118 if (iter.sensorId == g_sensorList[j].sensorTypeId) {
119 EXPECT_GT(iter.sensorName.size(), 0);
120 break;
121 }
122 }
123 }
124 }
125
126 BENCHMARK_REGISTER_F(sensorBenchmarkTest, SUB_DriverSystem_SensorBenchmark_0010)->
127 Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
128
129 /**
130 * @tc.name: SUB_DriverSystem_SensorBenchmark_0020
131 * @tc.desc: Benchmarktest for interface register.
132 * Returns 0 if the callback is successfully registered; returns a negative value otherwise.
133 * @tc.type: FUNC
134 */
BENCHMARK_F(sensorBenchmarkTest,SUB_DriverSystem_SensorBenchmark_0020)135 BENCHMARK_F(sensorBenchmarkTest, SUB_DriverSystem_SensorBenchmark_0020)(benchmark::State &st)
136 {
137 if (g_sensorInterface == nullptr) {
138 ASSERT_NE(nullptr, g_sensorInterface);
139 return;
140 }
141 int32_t ret;
142 for (auto _ : st) {
143 ret = g_sensorInterface->Register(TRADITIONAL_SENSOR_TYPE, g_medicalCallback);
144 EXPECT_EQ(SENSOR_SUCCESS, ret);
145 ret = g_sensorInterface->Unregister(TRADITIONAL_SENSOR_TYPE, g_medicalCallback);
146 EXPECT_EQ(SENSOR_SUCCESS, ret);
147 }
148 }
149
150 BENCHMARK_REGISTER_F(sensorBenchmarkTest, SUB_DriverSystem_SensorBenchmark_0020)->
151 Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
152
153 /**
154 * @tc.name: SUB_DriverSystem_SensorBenchmark_0040
155 * @tc.desc: Benchmarktest for interface Enable.
156 * Enables the sensor unavailable in the sensor list based on the specified sensor ID.
157 * @tc.type: FUNC
158 */
159
BENCHMARK_F(sensorBenchmarkTest,SUB_DriverSystem_SensorBenchmark_0040)160 BENCHMARK_F(sensorBenchmarkTest, SUB_DriverSystem_SensorBenchmark_0040)(benchmark::State &st)
161 {
162 if (g_sensorInterface == nullptr) {
163 ASSERT_NE(nullptr, g_sensorInterface);
164 return;
165 }
166 int32_t ret = g_sensorInterface->Register(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback);
167 EXPECT_EQ(SENSOR_SUCCESS, ret);
168
169 EXPECT_GT(g_info.size(), 0);
170
171 ret = g_sensorInterface->SetBatch(0, SENSOR_INTERVAL1, SENSOR_POLL_TIME);
172 EXPECT_EQ(SENSOR_SUCCESS, ret);
173 for (auto _ : st) {
174 ret = g_sensorInterface->Enable(0);
175 }
176 OsalMSleep(SENSOR_POLL_TIME);
177 ret = g_sensorInterface->Disable(0);
178 EXPECT_EQ(SENSOR_SUCCESS, ret);
179 ret = g_sensorInterface->Unregister(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback);
180 EXPECT_EQ(SensorCallbackImpl::sensorDataFlag, 1);
181 SensorCallbackImpl::sensorDataFlag = 1;
182 }
183
184 BENCHMARK_REGISTER_F(sensorBenchmarkTest, SUB_DriverSystem_SensorBenchmark_0040)->
185 Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
186
187 /**
188 * @tc.name: SUB_DriverSystem_SensorBenchmark_0050
189 * @tc.desc: Benchmarktest for interface Disable.
190 * Enables the sensor unavailable in the sensor list based on the specified sensor ID.
191 * @tc.type: FUNC
192 */
193
BENCHMARK_F(sensorBenchmarkTest,SUB_DriverSystem_SensorBenchmark_0050)194 BENCHMARK_F(sensorBenchmarkTest, SUB_DriverSystem_SensorBenchmark_0050)(benchmark::State &st)
195 {
196 if (g_sensorInterface == nullptr) {
197 ASSERT_NE(nullptr, g_sensorInterface);
198 return;
199 }
200 int32_t ret = g_sensorInterface->Register(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback);
201 EXPECT_EQ(SENSOR_SUCCESS, ret);
202
203 EXPECT_GT(g_info.size(), 0);
204
205 ret = g_sensorInterface->SetBatch(0, SENSOR_INTERVAL1, SENSOR_POLL_TIME);
206 EXPECT_EQ(SENSOR_SUCCESS, ret);
207 ret = g_sensorInterface->Enable(0);
208 EXPECT_EQ(SENSOR_SUCCESS, ret);
209 OsalMSleep(SENSOR_POLL_TIME);
210 for (auto _ : st) {
211 ret = g_sensorInterface->Disable(0);
212 }
213 ret = g_sensorInterface->Unregister(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback);
214 EXPECT_EQ(SensorCallbackImpl::sensorDataFlag, 1);
215 SensorCallbackImpl::sensorDataFlag = 1;
216 }
217
218 BENCHMARK_REGISTER_F(sensorBenchmarkTest, SUB_DriverSystem_SensorBenchmark_0050)->
219 Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
220
221 /**
222 * @tc.name: SUB_DriverSystem_SensorBenchmark_0060
223 * @tc.desc: Benchmarktest for interface SetBatch.
224 * Sets the sampling time and data report interval for sensors in batches.
225 * @tc.type: FUNC
226 */
BENCHMARK_F(sensorBenchmarkTest,SUB_DriverSystem_SensorBenchmark_0060)227 BENCHMARK_F(sensorBenchmarkTest, SUB_DriverSystem_SensorBenchmark_0060)(benchmark::State &st)
228 {
229 if (g_sensorInterface == nullptr) {
230 ASSERT_NE(nullptr, g_sensorInterface);
231 return;
232 }
233 int32_t ret = g_sensorInterface->Register(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback);
234 EXPECT_EQ(SENSOR_SUCCESS, ret);
235
236 for (auto _ : st) {
237 ret = g_sensorInterface->SetBatch(0, SENSOR_INTERVAL2, SENSOR_POLL_TIME);
238 }
239 EXPECT_EQ(SENSOR_SUCCESS, ret);
240 ret = g_sensorInterface->Enable(0);
241 EXPECT_EQ(SENSOR_SUCCESS, ret);
242 OsalMSleep(SENSOR_WAIT_TIME);
243 ret = g_sensorInterface->Disable(0);
244 EXPECT_EQ(SENSOR_SUCCESS, ret);
245
246 ret = g_sensorInterface->Unregister(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback);
247 EXPECT_EQ(SENSOR_SUCCESS, ret);
248 EXPECT_EQ(SensorCallbackImpl::sensorDataFlag, 1);
249 SensorCallbackImpl::sensorDataFlag = 1;
250 }
251
252 BENCHMARK_REGISTER_F(sensorBenchmarkTest, SUB_DriverSystem_SensorBenchmark_0060)->
253 Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
254
255 /**
256 * @tc.name: SUB_DriverSystem_SensorBenchmark_0070
257 * @tc.desc: Benchmarktest for interface SetMode.
258 * Sets the data reporting mode for the specified sensor.
259 * @tc.type: FUNC
260 */
BENCHMARK_F(sensorBenchmarkTest,SUB_DriverSystem_SensorBenchmark_0070)261 BENCHMARK_F(sensorBenchmarkTest, SUB_DriverSystem_SensorBenchmark_0070)(benchmark::State &st)
262 {
263 if (g_sensorInterface == nullptr) {
264 ASSERT_NE(nullptr, g_sensorInterface);
265 return;
266 }
267 EXPECT_GT(g_info.size(), 0);
268 int32_t ret ;
269 ret = g_sensorInterface->SetBatch(0, SENSOR_INTERVAL1, SENSOR_POLL_TIME);
270 EXPECT_EQ(SENSOR_SUCCESS, ret);
271 for (auto _ : st) {
272 if (0 == SENSOR_TYPE_HALL) {
273 ret = g_sensorInterface->SetMode(0, SENSOR_MODE_ON_CHANGE);
274 } else {
275 ret = g_sensorInterface->SetMode(0, SENSOR_MODE_REALTIME);
276 }
277 }
278 ret = g_sensorInterface->Enable(0);
279 EXPECT_EQ(SENSOR_SUCCESS, ret);
280 OsalMSleep(SENSOR_WAIT_TIME);
281 ret = g_sensorInterface->Disable(0);
282 EXPECT_EQ(SENSOR_SUCCESS, ret);
283 }
284
285 BENCHMARK_REGISTER_F(sensorBenchmarkTest, SUB_DriverSystem_SensorBenchmark_0070)->
286 Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
287
288 /**
289 * @tc.name: SUB_DriverSystem_SensorBenchmark_0080
290 * @tc.desc: Benchmarktest for interface SetOption.
291 * Sets options for the specified sensor, including its measurement range and accuracy.
292 * @tc.type: FUNC
293 */
BENCHMARK_F(sensorBenchmarkTest,SUB_DriverSystem_SensorBenchmark_0080)294 BENCHMARK_F(sensorBenchmarkTest, SUB_DriverSystem_SensorBenchmark_0080)(benchmark::State &st)
295 {
296 if (g_sensorInterface == nullptr) {
297 ASSERT_NE(nullptr, g_sensorInterface);
298 return;
299 }
300 EXPECT_GT(g_info.size(), 0);
301 int32_t ret;
302
303 for (auto _ : st) {
304 ret = g_sensorInterface->SetOption(0, 0);
305 }
306 EXPECT_EQ(SENSOR_SUCCESS, ret);
307 }
308
309 BENCHMARK_REGISTER_F(sensorBenchmarkTest, SUB_DriverSystem_SensorBenchmark_0080)->
310 Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
311 }
312
313 BENCHMARK_MAIN();