1 /*
2 * Copyright (c) 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 #include <benchmark/benchmark.h>
16 #include <cmath>
17 #include <cstdio>
18 #include <gtest/gtest.h>
19 #include <securec.h>
20 #include <string>
21 #include <unistd.h>
22 #include <vector>
23 #include "hdf_base.h"
24 #include "input_callback_impl.h"
25 #include "input_type.h"
26 #include "osal_time.h"
27 #include "v1_0/iinput_interfaces.h"
28
29 using namespace OHOS::HDI::Input::V1_0;
30 using namespace std;
31 using namespace testing::ext;
32
33 namespace {
34 sptr<IInputInterfaces> g_inputInterfaces = nullptr;
35 sptr<IInputCallback> g_callback = nullptr;
36
37 constexpr int32_t INIT_DEFAULT_VALUE = 255;
38 constexpr int32_t TOUCH_INDEX = 1;
39 constexpr int32_t TEST_RESULT_LEN = 32;
40
41 class InputBenchmarkTest : public benchmark::Fixture {
42 public:
43 void SetUp(const ::benchmark::State &state);
44 void TearDown(const ::benchmark::State &state);
45 };
46
SetUp(const::benchmark::State & state)47 void InputBenchmarkTest::SetUp(const ::benchmark::State &state)
48 {
49 g_inputInterfaces = IInputInterfaces::Get(true);
50 if (g_inputInterfaces != nullptr) {
51 g_callback = new InputCallbackImpl(g_inputInterfaces, nullptr);
52 }
53 }
54
TearDown(const::benchmark::State & state)55 void InputBenchmarkTest::TearDown(const ::benchmark::State &state)
56 {
57 g_inputInterfaces = nullptr;
58 }
59
60 /**
61 * @tc.name: HdfInput_ScanInputDevice_test
62 * @tc.desc: Benchmarktest for interface ScanInputDevice.
63 * @tc.type: FUNC
64 */
BENCHMARK_F(InputBenchmarkTest,HdfInput_ScanInputDevice_test)65 BENCHMARK_F(InputBenchmarkTest, HdfInput_ScanInputDevice_test)(benchmark::State &state)
66 {
67 ASSERT_NE(nullptr, g_inputInterfaces);
68
69 std::vector<DevDesc> sta;
70 int32_t ret;
71 for (auto _ : state) {
72 ret = g_inputInterfaces->ScanInputDevice(sta);
73 }
74 EXPECT_EQ(INPUT_SUCCESS, ret);
75 }
76
77 BENCHMARK_REGISTER_F(InputBenchmarkTest, HdfInput_ScanInputDevice_test)->
78 Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
79
80 /**
81 * @tc.name: HdfInput_OpenInputDevice_test
82 * @tc.desc: Benchmarktest for interface OpenInputDevice and CloseInputDevice.
83 * @tc.type: FUNC
84 */
85
BENCHMARK_F(InputBenchmarkTest,HdfInput_OpenInputDevice_test)86 BENCHMARK_F(InputBenchmarkTest, HdfInput_OpenInputDevice_test)(benchmark::State &state)
87 {
88 ASSERT_NE(nullptr, g_inputInterfaces);
89
90 int32_t ret;
91 for (auto _ : state) {
92 ret = g_inputInterfaces->OpenInputDevice(TOUCH_INDEX);
93 ret = g_inputInterfaces->CloseInputDevice(TOUCH_INDEX);
94 }
95 EXPECT_EQ(INPUT_SUCCESS, ret);
96 }
97
98 BENCHMARK_REGISTER_F(InputBenchmarkTest, HdfInput_OpenInputDevice_test)->
99 Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
100
101 /**
102 * @tc.name: HdfInput_GetInputDevice_test
103 * @tc.desc: Benchmarktest for interface GetInputDevice.
104 * @tc.type: FUNC
105 */
BENCHMARK_F(InputBenchmarkTest,HdfInput_GetInputDevice_test)106 BENCHMARK_F(InputBenchmarkTest, HdfInput_GetInputDevice_test)(benchmark::State &state)
107 {
108 ASSERT_NE(nullptr, g_inputInterfaces);
109
110 int32_t ret;
111 struct DeviceInfo dev;
112 ret = g_inputInterfaces->OpenInputDevice(TOUCH_INDEX);
113 EXPECT_EQ(INPUT_SUCCESS, ret);
114 for (auto _ : state) {
115 ret = g_inputInterfaces->GetInputDevice(TOUCH_INDEX, dev);
116 }
117 EXPECT_EQ(INPUT_SUCCESS, ret);
118 EXPECT_EQ((uint32_t)TOUCH_INDEX, dev.devIndex);
119 }
120
121 BENCHMARK_REGISTER_F(InputBenchmarkTest, HdfInput_GetInputDevice_test)->
122 Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
123
124 /**
125 * @tc.name: HdfInput_GetInputDeviceList_test
126 * @tc.desc: Benchmarktest for interface GetInputDeviceList.
127 * @tc.type: FUNC
128 */
BENCHMARK_F(InputBenchmarkTest,HdfInput_GetInputDeviceList_test)129 BENCHMARK_F(InputBenchmarkTest, HdfInput_GetInputDeviceList_test)(benchmark::State &state)
130 {
131 ASSERT_NE(nullptr, g_inputInterfaces);
132
133 int32_t ret;
134 uint32_t num = 0;
135 std::vector<DeviceInfo> dev;
136 ret = g_inputInterfaces->OpenInputDevice(TOUCH_INDEX);
137 EXPECT_EQ(INPUT_SUCCESS, ret);
138 for (auto _ : state) {
139 ret = g_inputInterfaces->GetInputDeviceList(num, dev, MAX_INPUT_DEV_NUM);
140 }
141 EXPECT_EQ(ret, INPUT_SUCCESS);
142 ASSERT_LE(num, (uint32_t)MAX_INPUT_DEV_NUM);
143 }
144
145 BENCHMARK_REGISTER_F(InputBenchmarkTest, HdfInput_GetInputDeviceList_test)->
146 Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
147
148 /**
149 * @tc.name: HdfInput_GetDeviceType_test
150 * @tc.desc: Benchmarktest for interface GetDeviceType.
151 * @tc.type: FUNC
152 */
BENCHMARK_F(InputBenchmarkTest,HdfInput_GetDeviceType_test)153 BENCHMARK_F(InputBenchmarkTest, HdfInput_GetDeviceType_test)(benchmark::State &state)
154 {
155 ASSERT_NE(nullptr, g_inputInterfaces);
156
157 int32_t ret;
158 uint32_t devType = INIT_DEFAULT_VALUE;
159 ret = g_inputInterfaces->OpenInputDevice(TOUCH_INDEX);
160 EXPECT_EQ(INPUT_SUCCESS, ret);
161 for (auto _ : state) {
162 ret = g_inputInterfaces->GetDeviceType(TOUCH_INDEX, devType);
163 }
164 EXPECT_EQ(ret, INPUT_SUCCESS);
165 }
166
167 BENCHMARK_REGISTER_F(InputBenchmarkTest, HdfInput_GetDeviceType_test)->
168 Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
169
170 /**
171 * @tc.name: HdfInput_GetChipInfo_test
172 * @tc.desc: Benchmarktest for interface GetChipInfo.
173 * @tc.type: FUNC
174 */
BENCHMARK_F(InputBenchmarkTest,HdfInput_GetChipInfo_test)175 BENCHMARK_F(InputBenchmarkTest, HdfInput_GetChipInfo_test)(benchmark::State &state)
176 {
177 ASSERT_NE(nullptr, g_inputInterfaces);
178
179 int32_t ret;
180 std::string chipInfo;
181 ret = g_inputInterfaces->OpenInputDevice(TOUCH_INDEX);
182 EXPECT_EQ(INPUT_SUCCESS, ret);
183 for (auto _ : state) {
184 ret = g_inputInterfaces->GetChipInfo(TOUCH_INDEX, chipInfo);
185 }
186 EXPECT_EQ(ret, INPUT_SUCCESS);
187 }
188
189 BENCHMARK_REGISTER_F(InputBenchmarkTest, HdfInput_GetChipInfo_test)->
190 Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
191
192 /**
193 * @tc.name: HdfInput_SetPowerStatus_test
194 * @tc.desc: Benchmarktest for interface SetPowerStatus.
195 * @tc.type: FUNC
196 */
BENCHMARK_F(InputBenchmarkTest,HdfInput_SetPowerStatus_test)197 BENCHMARK_F(InputBenchmarkTest, HdfInput_SetPowerStatus_test)(benchmark::State &state)
198 {
199 ASSERT_NE(nullptr, g_inputInterfaces);
200
201 int32_t ret;
202 uint32_t setStatus = INPUT_LOW_POWER;
203 ret = g_inputInterfaces->OpenInputDevice(TOUCH_INDEX);
204 EXPECT_EQ(INPUT_SUCCESS, ret);
205 for (auto _ : state) {
206 ret = g_inputInterfaces->SetPowerStatus(TOUCH_INDEX, setStatus);
207 }
208 EXPECT_EQ(ret, INPUT_SUCCESS);
209 }
210
211 BENCHMARK_REGISTER_F(InputBenchmarkTest, HdfInput_SetPowerStatus_test)->
212 Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
213
214 /**
215 * @tc.name: HdfInput_GetPowerStatus_test
216 * @tc.desc: Benchmarktest for interface GetPowerStatus.
217 * @tc.type: FUNC
218 */
BENCHMARK_F(InputBenchmarkTest,HdfInput_GetPowerStatus_test)219 BENCHMARK_F(InputBenchmarkTest, HdfInput_GetPowerStatus_test)(benchmark::State &state)
220 {
221 ASSERT_NE(nullptr, g_inputInterfaces);
222
223 int32_t ret;
224 uint32_t getStatus = 0;
225 ret = g_inputInterfaces->OpenInputDevice(TOUCH_INDEX);
226 EXPECT_EQ(INPUT_SUCCESS, ret);
227 for (auto _ : state) {
228 ret = g_inputInterfaces->GetPowerStatus(TOUCH_INDEX, getStatus);
229 }
230 EXPECT_EQ(ret, INPUT_SUCCESS);
231 }
232
233 BENCHMARK_REGISTER_F(InputBenchmarkTest, HdfInput_GetPowerStatus_test)->
234 Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
235
236 /**
237 * @tc.name: HdfInput_GetVendorName_test
238 * @tc.desc: Benchmarktest for interface GetVendorName.
239 * @tc.type: FUNC
240 */
BENCHMARK_F(InputBenchmarkTest,HdfInput_GetVendorName_test)241 BENCHMARK_F(InputBenchmarkTest, HdfInput_GetVendorName_test)(benchmark::State &state)
242 {
243 ASSERT_NE(nullptr, g_inputInterfaces);
244
245 int32_t ret;
246 std::string vendorName;
247 ret = g_inputInterfaces->OpenInputDevice(TOUCH_INDEX);
248 EXPECT_EQ(INPUT_SUCCESS, ret);
249 for (auto _ : state) {
250 ret = g_inputInterfaces->GetVendorName(TOUCH_INDEX, vendorName);
251 }
252 EXPECT_EQ(ret, INPUT_SUCCESS);
253 }
254
255 BENCHMARK_REGISTER_F(InputBenchmarkTest, HdfInput_GetVendorName_test)->
256 Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
257
258 /**
259 * @tc.name: HdfInput_GetChipName_test
260 * @tc.desc: Benchmarktest for interface GetChipName.
261 * @tc.type: FUNC
262 */
BENCHMARK_F(InputBenchmarkTest,HdfInput_GetChipName_test)263 BENCHMARK_F(InputBenchmarkTest, HdfInput_GetChipName_test)(benchmark::State &state)
264 {
265 ASSERT_NE(nullptr, g_inputInterfaces);
266
267 int32_t ret;
268 std::string chipName;
269 ret = g_inputInterfaces->OpenInputDevice(TOUCH_INDEX);
270 EXPECT_EQ(INPUT_SUCCESS, ret);
271 for (auto _ : state) {
272 ret = g_inputInterfaces->GetChipName(TOUCH_INDEX, chipName);
273 }
274 EXPECT_EQ(ret, INPUT_SUCCESS);
275 }
276
277 BENCHMARK_REGISTER_F(InputBenchmarkTest, HdfInput_GetChipName_test)->
278 Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
279
280 /**
281 * @tc.name: HdfInput_SetGestureMode_test
282 * @tc.desc: Benchmarktest for interface SetGestureMode.
283 * @tc.type: FUNC
284 */
BENCHMARK_F(InputBenchmarkTest,HdfInput_SetGestureMode_test)285 BENCHMARK_F(InputBenchmarkTest, HdfInput_SetGestureMode_test)(benchmark::State &state)
286 {
287 ASSERT_NE(nullptr, g_inputInterfaces);
288
289 int32_t ret;
290 uint32_t gestureMode = 1;
291 ret = g_inputInterfaces->OpenInputDevice(TOUCH_INDEX);
292 EXPECT_EQ(INPUT_SUCCESS, ret);
293 for (auto _ : state) {
294 ret = g_inputInterfaces->SetGestureMode(TOUCH_INDEX, gestureMode);
295 }
296 EXPECT_EQ(ret, INPUT_SUCCESS);
297 }
298
299 BENCHMARK_REGISTER_F(InputBenchmarkTest, HdfInput_SetGestureMode_test)->
300 Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
301
302 /**
303 * @tc.name: HdfInput_RunCapacitanceTest_test
304 * @tc.desc: Benchmarktest for interface RunCapacitanceTest.
305 * @tc.type: FUNC
306 */
BENCHMARK_F(InputBenchmarkTest,HdfInput_RunCapacitanceTest_test)307 BENCHMARK_F(InputBenchmarkTest, HdfInput_RunCapacitanceTest_test)(benchmark::State &state)
308 {
309 ASSERT_NE(nullptr, g_inputInterfaces);
310
311 int32_t ret;
312 uint32_t testType = MMI_TEST;
313 std::string result;
314 ret = g_inputInterfaces->OpenInputDevice(TOUCH_INDEX);
315 EXPECT_EQ(INPUT_SUCCESS, ret);
316 for (auto _ : state) {
317 ret = g_inputInterfaces->RunCapacitanceTest(TOUCH_INDEX, testType, result, TEST_RESULT_LEN);
318 }
319 EXPECT_EQ(ret, INPUT_SUCCESS);
320 }
321
322 BENCHMARK_REGISTER_F(InputBenchmarkTest, HdfInput_RunCapacitanceTest_test)->
323 Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
324
325 /**
326 * @tc.name: HdfInput_RunExtraCommand_test
327 * @tc.desc: Benchmarktest for interface RunExtraCommand.
328 * @tc.type: FUNC
329 */
BENCHMARK_F(InputBenchmarkTest,HdfInput_RunExtraCommand_test)330 BENCHMARK_F(InputBenchmarkTest, HdfInput_RunExtraCommand_test)(benchmark::State &state)
331 {
332 ASSERT_NE(nullptr, g_inputInterfaces);
333
334 int32_t ret;
335 struct ExtraCmd extraCmd;
336 extraCmd.cmdCode = "WakeUpMode";
337 extraCmd.cmdValue = "Enable";
338 ret = g_inputInterfaces->OpenInputDevice(TOUCH_INDEX);
339 EXPECT_EQ(INPUT_SUCCESS, ret);
340 for (auto _ : state) {
341 ret = g_inputInterfaces->RunExtraCommand(TOUCH_INDEX, extraCmd);
342 }
343 EXPECT_EQ(ret, INPUT_SUCCESS);
344 }
345
346 BENCHMARK_REGISTER_F(InputBenchmarkTest, HdfInput_RunExtraCommand_test)->
347 Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
348
349
350 /**
351 * @tc.name: HdfInput_RegisterReportCallback_test
352 * @tc.desc: Benchmarktest for interface RegisterReportCallback and UnregisterReportCallback.
353 * @tc.type: FUNC
354 */
BENCHMARK_F(InputBenchmarkTest,HdfInput_RegisterReportCallback_test)355 BENCHMARK_F(InputBenchmarkTest, HdfInput_RegisterReportCallback_test)(benchmark::State &state)
356 {
357 ASSERT_NE(nullptr, g_inputInterfaces);
358
359 int32_t ret;
360 ret = g_inputInterfaces->OpenInputDevice(TOUCH_INDEX);
361 EXPECT_EQ(INPUT_SUCCESS, ret);
362 for (auto _ : state) {
363 ret = g_inputInterfaces->RegisterReportCallback(TOUCH_INDEX, g_callback);
364 ret = g_inputInterfaces->UnregisterReportCallback(TOUCH_INDEX);
365 }
366 EXPECT_EQ(ret, INPUT_SUCCESS);
367 ret = g_inputInterfaces->CloseInputDevice(TOUCH_INDEX);
368 EXPECT_EQ(INPUT_SUCCESS, ret);
369 }
370
371 BENCHMARK_REGISTER_F(InputBenchmarkTest, HdfInput_RegisterReportCallback_test)->
372 Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
373 } // namespace
374
375 BENCHMARK_MAIN();
376