1 /*
2 * Copyright (c) 2024 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 "sensor_uhdf_log.h"
29 #include "v3_0/isensor_interface.h"
30
31 using namespace OHOS::HDI::Sensor::V3_0;
32 using namespace OHOS::HDI::Sensor;
33 using namespace testing::ext;
34 using namespace std;
35
36 namespace {
37 sptr<ISensorInterface> g_sensorInterface = nullptr;
38 sptr<V3_0::ISensorCallback> g_traditionalCallback = new SensorCallbackImpl();
39 std::vector<HdfSensorInformation> g_info;
40
41 constexpr int32_t ITERATION_FREQUENCY = 100;
42 constexpr int32_t REPETITION_FREQUENCY = 3;
43 constexpr int32_t SENSOR_INTERVAL1 = 20;
44 constexpr int32_t SENSOR_POLL_TIME = 3;
45 constexpr uint32_t OPTION = 0;
46 constexpr uint32_t SENSOR_ID = 1;
47
48 class SensorBenchmarkTest : public benchmark::Fixture {
49 public:
50 void SetUp(const ::benchmark::State &state);
51 void TearDown(const ::benchmark::State &state);
52 };
53
SetUp(const::benchmark::State & state)54 void SensorBenchmarkTest::SetUp(const ::benchmark::State &state)
55 {
56 g_sensorInterface = ISensorInterface::Get();
57 }
58
TearDown(const::benchmark::State & state)59 void SensorBenchmarkTest::TearDown(const ::benchmark::State &state)
60 {
61 }
62
63 /**
64 * @tc.name: DriverSystem_SensorBenchmark_GetAllSensorInfo
65 * @tc.desc: Benchmarktest for interface GetAllSensorInfo
66 * Obtains information about all sensors in the system
67 * @tc.type: FUNC
68 */
BENCHMARK_F(SensorBenchmarkTest,GetAllSensorInfo)69 BENCHMARK_F(SensorBenchmarkTest, GetAllSensorInfo)(benchmark::State &state)
70 {
71 for (auto _ : state) {
72 int32_t ret = g_sensorInterface->GetAllSensorInfo(g_info);
73 EXPECT_EQ(SENSOR_SUCCESS, ret);
74 }
75 }
76
77 BENCHMARK_REGISTER_F(SensorBenchmarkTest, GetAllSensorInfo)->
78 Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
79
80 /**
81 * @tc.name: DriverSystem_SensorBenchmark_register
82 * @tc.desc: Benchmarktest for interface register
83 * Returns 0 if the callback is successfully registered; returns a negative value otherwise
84 * @tc.type: FUNC
85 */
BENCHMARK_F(SensorBenchmarkTest,Register)86 BENCHMARK_F(SensorBenchmarkTest, Register)(benchmark::State &state)
87 {
88 for (auto _ : state) {
89 int32_t ret = g_sensorInterface->Register(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback);
90 EXPECT_EQ(SENSOR_SUCCESS, ret);
91 }
92 }
93
94 BENCHMARK_REGISTER_F(SensorBenchmarkTest, Register)->
95 Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
96
97 /**
98 * @tc.name: DriverSystem_SensorBenchmark_SetBatch
99 * @tc.desc: Benchmarktest for interface SetBatch
100 * Sets the sampling time and data report interval for sensors in batches
101 * @tc.type: FUNC
102 */
BENCHMARK_F(SensorBenchmarkTest,SetBatch)103 BENCHMARK_F(SensorBenchmarkTest, SetBatch)(benchmark::State &state)
104 {
105 for (auto _ : state) {
106 int32_t ret = g_sensorInterface->SetBatch(SENSOR_ID, SENSOR_INTERVAL1, SENSOR_POLL_TIME);
107 EXPECT_EQ(SENSOR_SUCCESS, ret);
108 }
109 }
110
111 BENCHMARK_REGISTER_F(SensorBenchmarkTest, SetBatch)->
112 Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
113
114 /**
115 * @tc.name: DriverSystem_SensorBenchmark_SetMode
116 * @tc.desc: Benchmarktest for interface SetMode
117 * Sets the data reporting mode for the specified sensor
118 * @tc.type: FUNC
119 */
BENCHMARK_F(SensorBenchmarkTest,SetMode)120 BENCHMARK_F(SensorBenchmarkTest, SetMode)(benchmark::State &state)
121 {
122 for (auto _ : state) {
123 int32_t ret = g_sensorInterface->SetMode(SENSOR_ID, SENSOR_MODE_ON_CHANGE);
124 EXPECT_EQ(SENSOR_SUCCESS, ret);
125 }
126 }
127
128 BENCHMARK_REGISTER_F(SensorBenchmarkTest, SetMode)->
129 Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
130
131 /**
132 * @tc.name: DriverSystem_SensorBenchmark_SetOption
133 * @tc.desc: Benchmarktest for interface SetOption
134 * Sets options for the specified sensor, including its measurement range and accuracy
135 * @tc.type: FUNC
136 */
BENCHMARK_F(SensorBenchmarkTest,SetOption)137 BENCHMARK_F(SensorBenchmarkTest, SetOption)(benchmark::State &state)
138 {
139 for (auto _ : state) {
140 int32_t ret = g_sensorInterface->SetOption(SENSOR_ID, OPTION);
141 EXPECT_EQ(SENSOR_SUCCESS, ret);
142 }
143 }
144
145 BENCHMARK_REGISTER_F(SensorBenchmarkTest, SetOption)->
146 Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
147
148 /**
149 * @tc.name: DriverSystem_SensorBenchmark_Unregister
150 * @tc.desc: Benchmarktest for interface Unregister
151 * Returns 0 if the callback is successfully registered; returns a negative value otherwise
152 * @tc.type: FUNC
153 */
BENCHMARK_F(SensorBenchmarkTest,Unregister)154 BENCHMARK_F(SensorBenchmarkTest, Unregister)(benchmark::State &state)
155 {
156 for (auto _ : state) {
157 int32_t ret = g_sensorInterface->Unregister(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback);
158 EXPECT_EQ(SENSOR_SUCCESS, ret);
159 }
160 }
161
162 BENCHMARK_REGISTER_F(SensorBenchmarkTest, Unregister)->
163 Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
164
165 /**
166 * @tc.name: DriverSystem_SensorBenchmark_Enable
167 * @tc.desc: Benchmarktest for interface Enable
168 * Enables the sensor unavailable in the sensor list based on the specified sensor ID
169 * @tc.type: FUNC
170 */
BENCHMARK_F(SensorBenchmarkTest,EnableAndDisable)171 BENCHMARK_F(SensorBenchmarkTest, EnableAndDisable)(benchmark::State &state)
172 {
173 for (auto _ : state) {
174 int32_t ret = g_sensorInterface->Enable(SENSOR_ID);
175 EXPECT_EQ(SENSOR_SUCCESS, ret);
176 ret = g_sensorInterface->Disable(SENSOR_ID);
177 EXPECT_EQ(SENSOR_SUCCESS, ret);
178 }
179 }
180
181 BENCHMARK_REGISTER_F(SensorBenchmarkTest, EnableAndDisable)->
182 Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
183 }
184
185 BENCHMARK_MAIN();
186