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 "v2_0/isensor_interface.h"
30 
31 using namespace OHOS::HDI::Sensor::V2_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     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_POLL_TIME = 3;
44     constexpr uint32_t OPTION = 0;
45     constexpr uint32_t SENSOR_ID = 1;
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     for (auto _ : state) {
71         int32_t ret = g_sensorInterface->GetAllSensorInfo(g_info);
72         EXPECT_EQ(SENSOR_SUCCESS, ret);
73     }
74 }
75 
76 BENCHMARK_REGISTER_F(SensorBenchmarkTest, GetAllSensorInfo)->
77     Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
78 
79 /**
80   * @tc.name: DriverSystem_SensorBenchmark_register
81   * @tc.desc: Benchmarktest for interface register
82   * Returns 0 if the callback is successfully registered; returns a negative value otherwise
83   * @tc.type: FUNC
84   */
BENCHMARK_F(SensorBenchmarkTest,Register)85 BENCHMARK_F(SensorBenchmarkTest, Register)(benchmark::State &state)
86 {
87     for (auto _ : state) {
88         int32_t ret = g_sensorInterface->Register(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback);
89         EXPECT_EQ(SENSOR_SUCCESS, ret);
90     }
91 }
92 
93 BENCHMARK_REGISTER_F(SensorBenchmarkTest, Register)->
94     Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
95 
96 /**
97   * @tc.name: DriverSystem_SensorBenchmark_SetBatch
98   * @tc.desc: Benchmarktest for interface SetBatch
99   * Sets the sampling time and data report interval for sensors in batches
100   * @tc.type: FUNC
101   */
BENCHMARK_F(SensorBenchmarkTest,SetBatch)102 BENCHMARK_F(SensorBenchmarkTest, SetBatch)(benchmark::State &state)
103 {
104     for (auto _ : state) {
105         int32_t ret = g_sensorInterface->SetBatch(SENSOR_ID, SENSOR_INTERVAL1, SENSOR_POLL_TIME);
106         EXPECT_EQ(SENSOR_SUCCESS, ret);
107     }
108 }
109 
110 BENCHMARK_REGISTER_F(SensorBenchmarkTest, SetBatch)->
111     Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
112 
113 /**
114   * @tc.name: DriverSystem_SensorBenchmark_SetMode
115   * @tc.desc: Benchmarktest for interface SetMode
116   * Sets the data reporting mode for the specified sensor
117   * @tc.type: FUNC
118   */
BENCHMARK_F(SensorBenchmarkTest,SetMode)119 BENCHMARK_F(SensorBenchmarkTest, SetMode)(benchmark::State &state)
120 {
121     for (auto _ : state) {
122         int32_t ret = g_sensorInterface->SetMode(SENSOR_ID, SENSOR_MODE_ON_CHANGE);
123         EXPECT_EQ(SENSOR_SUCCESS, ret);
124     }
125 }
126 
127 BENCHMARK_REGISTER_F(SensorBenchmarkTest, SetMode)->
128     Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
129 
130 /**
131   * @tc.name: DriverSystem_SensorBenchmark_SetOption
132   * @tc.desc: Benchmarktest for interface SetOption
133   * Sets options for the specified sensor, including its measurement range and accuracy
134   * @tc.type: FUNC
135   */
BENCHMARK_F(SensorBenchmarkTest,SetOption)136 BENCHMARK_F(SensorBenchmarkTest, SetOption)(benchmark::State &state)
137 {
138     for (auto _ : state) {
139         int32_t ret = g_sensorInterface->SetOption(SENSOR_ID, OPTION);
140         EXPECT_EQ(SENSOR_SUCCESS, ret);
141     }
142 }
143 
144 BENCHMARK_REGISTER_F(SensorBenchmarkTest, SetOption)->
145     Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
146 
147 /**
148   * @tc.name: DriverSystem_SensorBenchmark_Unregister
149   * @tc.desc: Benchmarktest for interface Unregister
150   * Returns 0 if the callback is successfully registered; returns a negative value otherwise
151   * @tc.type: FUNC
152   */
BENCHMARK_F(SensorBenchmarkTest,Unregister)153 BENCHMARK_F(SensorBenchmarkTest, Unregister)(benchmark::State &state)
154 {
155     for (auto _ : state) {
156         int32_t ret = g_sensorInterface->Unregister(TRADITIONAL_SENSOR_TYPE, g_traditionalCallback);
157         EXPECT_EQ(SENSOR_SUCCESS, ret);
158     }
159 }
160 
161 BENCHMARK_REGISTER_F(SensorBenchmarkTest, Unregister)->
162     Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
163 
164 /**
165   * @tc.name: DriverSystem_SensorBenchmark_Enable
166   * @tc.desc: Benchmarktest for interface Enable
167   * Enables the sensor unavailable in the sensor list based on the specified sensor ID
168   * @tc.type: FUNC
169   */
BENCHMARK_F(SensorBenchmarkTest,EnableAndDisable)170 BENCHMARK_F(SensorBenchmarkTest, EnableAndDisable)(benchmark::State &state)
171 {
172     for (auto _ : state) {
173         int32_t ret = g_sensorInterface->Enable(SENSOR_ID);
174         EXPECT_EQ(SENSOR_SUCCESS, ret);
175         ret = g_sensorInterface->Disable(SENSOR_ID);
176         EXPECT_EQ(SENSOR_SUCCESS, ret);
177     }
178 }
179 
180 BENCHMARK_REGISTER_F(SensorBenchmarkTest, EnableAndDisable)->
181     Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
182 }
183 
184 BENCHMARK_MAIN();