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 #include <benchmark/benchmark.h>
16 #include <string>
17 #include <vector>
18 
19 #include <cmath>
20 #include <cstdio>
21 #include <unistd.h>
22 #include <gtest/gtest.h>
23 #include <securec.h>
24 #include <hdf_base.h>
25 #include <hdf_log.h>
26 #include "osal_time.h"
27 #include "v1_0/iinput_interfaces.h"
28 #include "input_type.h"
29 #include "input_callback_impl.h"
30 
31 using namespace std;
32 using namespace OHOS::HDI::Input::V1_0;
33 using namespace testing::ext;
34 
35 namespace {
36 namespace {
37     sptr<IInputInterfaces>  g_inputInterfaces = nullptr;
38     sptr<IInputCallback> g_callback = nullptr;
39     sptr<IInputCallback> g_hotplugCb = nullptr;
40 
41     constexpr int32_t INIT_DEFAULT_VALUE = 255;
42     constexpr int32_t KEEP_ALIVE_TIME_MS = 3000;
43     constexpr int32_t TOUCH_INDEX = 1;
44     constexpr int32_t TEST_RESULT_LEN = 32;
45 }
46 
47 class PassthroughBenchmarkTest : 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 PassthroughBenchmarkTest::SetUp(const ::benchmark::State &state)
54 {
55     g_inputInterfaces = IInputInterfaces::Get(true);
56     if (g_inputInterfaces != nullptr) {
57         g_callback = new InputCallbackImpl(g_inputInterfaces, nullptr);
58         g_hotplugCb = new InputCallbackImpl(g_inputInterfaces, g_callback);
59     }
60 }
61 
TearDown(const ::benchmark::State &state)62 void PassthroughBenchmarkTest::TearDown(const ::benchmark::State &state)
63 {
64 }
65 
66 /**
67   * @tc.number: SUB_Driver_Input_PassthroughPerformance_0100
68   * @tc.name: ScanInputDevice001
69   * @tc.desc: scan input device test-benchmark
70   * @tc.type: FUNC
71   * @tc.require:
72   */
BENCHMARK_F(PassthroughBenchmarkTest, ScanInputDevice)73 BENCHMARK_F(PassthroughBenchmarkTest, ScanInputDevice)(benchmark::State &st)
74 {
75     if (g_inputInterfaces == nullptr) {
76         ASSERT_NE(nullptr, g_inputInterfaces);
77         return;
78     }
79     std::vector<DevDesc> sta;
80 
81     HDF_LOGI("%s: [Hdi-Input] ScanInputDevice001 enter", __func__);
82     int32_t ret;
83 
84     for (auto _ : st) {
85         ret = g_inputInterfaces->ScanInputDevice(sta);
86     }
87     if (ret == INPUT_SUCCESS) {
88         HDF_LOGE("%s: %d, %d, %d, %d", __func__, sta[0].devType, sta[0].devIndex, sta[1].devType, sta[1].devIndex);
89     }
90 }
91 BENCHMARK_REGISTER_F(PassthroughBenchmarkTest, ScanInputDevice)->Iterations(100)->
92     Repetitions(3)->ReportAggregatesOnly();
93 
94 /**
95   * @tc.number: SUB_Driver_Input_PassthroughPerformance_0200
96   * @tc.name: OpenInputDev001
97   * @tc.desc: open input device test-benchmark
98   * @tc.type: func
99   * @tc.require:
100   */
BENCHMARK_F(PassthroughBenchmarkTest, OpenInputDevice)101 BENCHMARK_F(PassthroughBenchmarkTest, OpenInputDevice)(benchmark::State &st)
102 {
103     if (g_inputInterfaces == nullptr) {
104         ASSERT_NE(nullptr, g_inputInterfaces);
105         return;
106     }
107     HDF_LOGI("%s: [Hdi-Input] OpenInputDevice001 enter", __func__);
108     int32_t ret;
109     for (auto _ : st) {
110         ret = g_inputInterfaces->OpenInputDevice(TOUCH_INDEX);
111     }
112     if (ret != INPUT_SUCCESS) {
113         HDF_LOGE("%s: open device failed, ret %d", __func__, ret);
114     }
115 }
116 BENCHMARK_REGISTER_F(PassthroughBenchmarkTest, OpenInputDevice)->Iterations(100)->
117     Repetitions(3)->ReportAggregatesOnly();
118 
119 /**
120   * @tc.number: SUB_Driver_Input_PassthroughPerformance_0300
121   * @tc.name: CloseInputDevice001
122   * @tc.desc: close input device test-benchmark
123   * @tc.type: func
124   * @tc.require:
125   */
BENCHMARK_F(PassthroughBenchmarkTest, CloseInputDevice)126 BENCHMARK_F(PassthroughBenchmarkTest, CloseInputDevice)(benchmark::State &st)
127 {
128     if (g_inputInterfaces == nullptr) {
129         ASSERT_NE(nullptr, g_inputInterfaces);
130         return;
131     }
132     HDF_LOGI("%s: [hdi-input] CloseInputDevice001 enter", __func__);
133     int32_t ret;
134     for (auto _ : st) {
135         ret = g_inputInterfaces->CloseInputDevice(TOUCH_INDEX);
136     }
137     if (ret != INPUT_SUCCESS) {
138         HDF_LOGE("%s: close device failed, ret %d", __func__, ret);
139     }
140 }
141 BENCHMARK_REGISTER_F(PassthroughBenchmarkTest, CloseInputDevice)->Iterations(100)->
142     Repetitions(3)->ReportAggregatesOnly();
143 
144 
145 /**
146   * @tc.number: SUB_Driver_Input_PassthroughPerformance_0400
147   * @tc.name: GetInputDeviceList001
148   * @tc.desc: get input device list info test-benchmark
149   * @tc.type: func
150   * @tc.require:
151   */
BENCHMARK_F(PassthroughBenchmarkTest, GetInputDeviceList)152 BENCHMARK_F(PassthroughBenchmarkTest, GetInputDeviceList)(benchmark::State &st)
153 {
154     if (g_inputInterfaces == nullptr) {
155         ASSERT_NE(nullptr, g_inputInterfaces);
156         return;
157     }
158     HDF_LOGI("%s: [hdi-input] GetInputDeviceList001 enter", __func__);
159     int32_t ret;
160     uint32_t num = 0;
161     std::vector<DeviceInfo> dev;
162     for (auto _ : st) {
163         ret = g_inputInterfaces->GetInputDeviceList(num, dev, MAX_INPUT_DEV_NUM);
164     }
165     if (ret != INPUT_SUCCESS) {
166         HDF_LOGE("%s: get device list failed, ret %d", __func__, ret);
167     }
168     ret = num <= MAX_INPUT_DEV_NUM ? HDF_SUCCESS : HDF_FAILURE;  /* num <= MAX_INPUT_DEV_NUM return true */
169     ASSERT_EQ(ret, HDF_SUCCESS);
170 
171 
172     for (uint32_t i = 0; i < num; i++) {
173         HDF_LOGI("%s: num = %u, device[%u]'s info is:", __func__, num, i);
174         HDF_LOGI("%s: index = %u, devType = %u", __func__, dev[i].devIndex, dev[i].devType);
175         HDF_LOGI("%s: chipInfo = %s, vendorName = %s, chipName = %s",
176             __func__, dev[i].chipInfo.c_str(), dev[i].vendorName.c_str(), dev[i].chipName.c_str());
177     }
178     EXPECT_EQ(ret, INPUT_SUCCESS);
179 }
180 BENCHMARK_REGISTER_F(PassthroughBenchmarkTest, GetInputDeviceList)->Iterations(100)->
181     Repetitions(3)->ReportAggregatesOnly();
182 
183 /**
184   * @tc.number: SUB_Driver_Input_PassthroughPerformance_0500
185   * @tc.name: GetDeviceType001
186   * @tc.desc: get input device type test-benchmark
187   * @tc.type: func
188   * @tc.require:
189   */
BENCHMARK_F(PassthroughBenchmarkTest, GetDeviceType)190 BENCHMARK_F(PassthroughBenchmarkTest, GetDeviceType)(benchmark::State &st)
191 {
192     if (g_inputInterfaces == nullptr) {
193         ASSERT_NE(nullptr, g_inputInterfaces);
194         return;
195     }
196     HDF_LOGI("%s: [hdi-input] GetDeviceType001 enter", __func__);
197     int32_t ret;
198     uint32_t devType = INIT_DEFAULT_VALUE;
199 
200     for (auto _ : st) {
201         ret = g_inputInterfaces->GetDeviceType(TOUCH_INDEX, devType);
202     }
203     if (ret != INPUT_SUCCESS) {
204         HDF_LOGE("%s: get device's type failed, ret %d", __func__, ret);
205     }
206 
207     HDF_LOGI("%s: device's type is %u", __func__, devType);
208 }
209 BENCHMARK_REGISTER_F(PassthroughBenchmarkTest, GetDeviceType)->Iterations(100)->
210     Repetitions(3)->ReportAggregatesOnly();
211 
212 /**
213   * @tc.number: SUB_Driver_Input_PassthroughPerformance_0600
214   * @tc.name: GetChipInfo001
215   * @tc.desc: get input device chip info test-benchmark
216   * @tc.type: func
217   * @tc.require:
218   */
BENCHMARK_F(PassthroughBenchmarkTest, GetChipInfo)219 BENCHMARK_F(PassthroughBenchmarkTest, GetChipInfo)(benchmark::State &st)
220 {
221     if (g_inputInterfaces == nullptr) {
222         ASSERT_NE(nullptr, g_inputInterfaces);
223         return;
224     }
225     HDF_LOGI("%s: [hdi-input] GetChipInfo001 enter", __func__);
226     int32_t ret;
227     std::string chipInfo;
228 
229     for (auto _ : st) {
230         ret = g_inputInterfaces->GetChipInfo(TOUCH_INDEX, chipInfo);
231     }
232     if (ret != INPUT_SUCCESS) {
233         HDF_LOGE("%s: get device's chip info failed, ret %d", __func__, ret);
234     }
235 
236     HDF_LOGI("%s: device's chip info is %s", __func__, chipInfo.c_str());
237 }
238 BENCHMARK_REGISTER_F(PassthroughBenchmarkTest, GetChipInfo)->Iterations(100)->
239     Repetitions(3)->ReportAggregatesOnly();
240 
241 /**
242   * @tc.number: SUB_Driver_Input_PassthroughPerformance_0700
243   * @tc.name: SetPowerStatus001
244   * @tc.desc: set device power status test-benchmark
245   * @tc.type: func
246   * @tc.require:
247   */
BENCHMARK_F(PassthroughBenchmarkTest, SetPowerStatus)248 BENCHMARK_F(PassthroughBenchmarkTest, SetPowerStatus)(benchmark::State &st)
249 {
250     if (g_inputInterfaces == nullptr) {
251         ASSERT_NE(nullptr, g_inputInterfaces);
252         return;
253     }
254     HDF_LOGI("%s: [hdi-input] SetPowerStatus001 enter", __func__);
255     int32_t ret;
256     uint32_t setStatus = INPUT_LOW_POWER;
257 
258     for (auto _ : st) {
259         ret = g_inputInterfaces->SetPowerStatus(TOUCH_INDEX, setStatus);
260     }
261     if (ret != INPUT_SUCCESS) {
262         HDF_LOGE("%s: set device's power status failed, ret %d", __func__, ret);
263     }
264 }
265 BENCHMARK_REGISTER_F(PassthroughBenchmarkTest, SetPowerStatus)->Iterations(100)->
266     Repetitions(3)->ReportAggregatesOnly();
267 
268 /**
269   * @tc.number: SUB_Driver_Input_PassthroughPerformance_0800
270   * @tc.name: GetPowerStatus001
271   * @tc.desc: get device power status test-benchmark
272   * @tc.type: func
273   * @tc.require:
274   */
BENCHMARK_F(PassthroughBenchmarkTest, GetPowerStatus)275 BENCHMARK_F(PassthroughBenchmarkTest, GetPowerStatus)(benchmark::State &st)
276 {
277     if (g_inputInterfaces == nullptr) {
278         ASSERT_NE(nullptr, g_inputInterfaces);
279         return;
280     }
281     HDF_LOGI("%s: [hdi-input] GetPowerStatus001 enter", __func__);
282     int32_t ret;
283     uint32_t getStatus = 0;
284 
285     for (auto _ : st) {
286         ret = g_inputInterfaces->GetPowerStatus(TOUCH_INDEX, getStatus);
287     }
288     if (ret != INPUT_SUCCESS) {
289         HDF_LOGE("%s: get device's power status failed, ret %d", __func__, ret);
290     }
291 
292     HDF_LOGI("%s: device's power status is %u:", __func__, getStatus);
293 }
294 BENCHMARK_REGISTER_F(PassthroughBenchmarkTest, GetPowerStatus)->Iterations(100)->
295     Repetitions(3)->ReportAggregatesOnly();
296 
297 /**
298   * @tc.number: SUB_Driver_Input_PassthroughPerformance_0900
299   * @tc.name: GetVendorName001
300   * @tc.desc: get device vendor name test-benchmark
301   * @tc.type: func
302   * @tc.require:
303   */
BENCHMARK_F(PassthroughBenchmarkTest, GetVendorName)304 BENCHMARK_F(PassthroughBenchmarkTest, GetVendorName)(benchmark::State &st)
305 {
306     if (g_inputInterfaces == nullptr) {
307         ASSERT_NE(nullptr, g_inputInterfaces);
308         return;
309     }
310     HDF_LOGI("%s: [hdi-input] GetVendorName001 enter", __func__);
311     int32_t ret;
312     std::string vendorName;
313 
314     for (auto _ : st) {
315         ret = g_inputInterfaces->GetVendorName(TOUCH_INDEX, vendorName);
316     }
317     if (ret != INPUT_SUCCESS) {
318         HDF_LOGE("%s: get device's vendor name failed, ret %d", __func__, ret);
319     }
320 
321     HDF_LOGI("%s: device's vendor name is %s:", __func__, vendorName.c_str());
322 }
323 BENCHMARK_REGISTER_F(PassthroughBenchmarkTest, GetVendorName)->Iterations(100)->
324     Repetitions(3)->ReportAggregatesOnly();
325 
326 /**
327   * @tc.number: SUB_Driver_Input_PassthroughPerformance_1000
328   * @tc.name: GetChipName001
329   * @tc.desc: get device chip name test-benchmark
330   * @tc.type: func
331   * @tc.require:
332   */
BENCHMARK_F(PassthroughBenchmarkTest, GetChipName)333 BENCHMARK_F(PassthroughBenchmarkTest, GetChipName)(benchmark::State &st)
334 {
335     if (g_inputInterfaces == nullptr) {
336         ASSERT_NE(nullptr, g_inputInterfaces);
337         return;
338     }
339     HDF_LOGI("%s: [hdi-input] GetChipName001 enter", __func__);
340     int32_t ret;
341     std::string chipName;
342 
343     for (auto _ : st) {
344         ret = g_inputInterfaces->GetChipName(TOUCH_INDEX, chipName);
345     }
346     if (ret != INPUT_SUCCESS) {
347         HDF_LOGE("%s: get device's chip name failed, ret %d", __func__, ret);
348     }
349 
350     HDF_LOGI("%s: device's chip name is %s", __func__, chipName.c_str());
351 }
352 BENCHMARK_REGISTER_F(PassthroughBenchmarkTest, GetChipName)->Iterations(100)->
353     Repetitions(3)->ReportAggregatesOnly();
354 
355 /**
356   * @tc.number: SUB_Driver_Input_PassthroughPerformance_1100
357   * @tc.name: SetGestureMode001
358   * @tc.desc: set device gesture mode test-benchmark
359   * @tc.type: func
360   * @tc.require:
361   */
BENCHMARK_F(PassthroughBenchmarkTest, SetGestureMode)362 BENCHMARK_F(PassthroughBenchmarkTest, SetGestureMode)(benchmark::State &st)
363 {
364     if (g_inputInterfaces == nullptr) {
365         ASSERT_NE(nullptr, g_inputInterfaces);
366         return;
367     }
368     HDF_LOGI("%s: [hdi-input] SetGestureMode001 enter", __func__);
369     int32_t ret;
370     uint32_t gestureMode = 1;
371 
372     for (auto _ : st) {
373         ret = g_inputInterfaces->SetGestureMode(TOUCH_INDEX, gestureMode);
374     }
375     if (ret != INPUT_SUCCESS) {
376         HDF_LOGE("%s: set device's gestureMode failed, ret %d", __func__, ret);
377     }
378 }
379 BENCHMARK_REGISTER_F(PassthroughBenchmarkTest, SetGestureMode)->Iterations(100)->
380     Repetitions(3)->ReportAggregatesOnly();
381 
382 /**
383   * @tc.number: SUB_Driver_Input_PassthroughPerformance_1200
384   * @tc.name: RunCapacitanceTest001
385   * @tc.desc: run capacitanceTest test-benchmark
386   * @tc.type: FUNC
387   * @tc.require:
388   */
BENCHMARK_F(PassthroughBenchmarkTest, RunCapacitanceTest)389 BENCHMARK_F(PassthroughBenchmarkTest, RunCapacitanceTest)(benchmark::State &st)
390 {
391     if (g_inputInterfaces == nullptr) {
392         ASSERT_NE(nullptr, g_inputInterfaces);
393         return;
394     }
395     HDF_LOGI("%s: [hdi-input] RunCapacitanceTest001 enter", __func__);
396     int32_t ret;
397     std::string result;
398     uint32_t testType = MMI_TEST;
399 
400     for (auto _ : st) {
401         ret = g_inputInterfaces->RunCapacitanceTest(TOUCH_INDEX, testType, result, TEST_RESULT_LEN);
402     }
403     if (ret != INPUT_SUCCESS) {
404         HDF_LOGE("%s: run capacitanceTest failed, ret %d", __func__, ret);
405     }
406 }
407 BENCHMARK_REGISTER_F(PassthroughBenchmarkTest, RunCapacitanceTest)->Iterations(100)->
408     Repetitions(3)->ReportAggregatesOnly();
409 
410 /**
411   * @tc.number: SUB_Driver_Input_PassthroughPerformance_1300
412   * @tc.name: RunExtraCommand001
413   * @tc.desc: run extra command test-benchmark
414   * @tc.type: FUNC
415   * @tc.require:
416   */
BENCHMARK_F(PassthroughBenchmarkTest, RunExtraCommand)417 BENCHMARK_F(PassthroughBenchmarkTest, RunExtraCommand)(benchmark::State &st)
418 {
419     if (g_inputInterfaces == nullptr) {
420         ASSERT_NE(nullptr, g_inputInterfaces);
421         return;
422     }
423     HDF_LOGI("%s: [hdi-input] RunExtraCommand001 enter", __func__);
424     int32_t ret;
425     struct ExtraCmd extraCmd;
426     extraCmd.cmdCode = "WakeUpMode";
427     extraCmd.cmdValue = "Enable";
428 
429     for (auto _ : st) {
430         ret = g_inputInterfaces->RunExtraCommand(TOUCH_INDEX, extraCmd);
431     }
432     if (ret != INPUT_SUCCESS) {
433         HDF_LOGE("%s: run extraCommand failed, ret %d", __func__, ret);
434     }
435 }
436 BENCHMARK_REGISTER_F(PassthroughBenchmarkTest, RunExtraCommand)->Iterations(100)->
437     Repetitions(3)->ReportAggregatesOnly();
438 
439 /**
440   * @tc.number: SUB_Driver_Input_PassthroughPerformance_1400
441   * @tc.name: RegisterCallbackAndReportData001
442   * @tc.desc: register callback and report data test-benchmark
443   * @tc.type: func
444   * @tc.require:
445   */
BENCHMARK_F(PassthroughBenchmarkTest, RegisterReportCallback)446 BENCHMARK_F(PassthroughBenchmarkTest, RegisterReportCallback)(benchmark::State &st)
447 {
448     if (g_inputInterfaces == nullptr) {
449         ASSERT_NE(nullptr, g_inputInterfaces);
450         return;
451     }
452     HDF_LOGI("%s: [hdi-input] RegisterCallbackAndReportData001 enter", __func__);
453     int32_t ret;
454 
455     for (auto _ : st) {
456         ret = g_inputInterfaces->RegisterReportCallback(TOUCH_INDEX, g_callback);
457     }
458     if (ret != INPUT_SUCCESS) {
459         HDF_LOGE("%s: register callback failed for device 1, ret %d", __func__, ret);
460     }
461     OsalMSleep(KEEP_ALIVE_TIME_MS);
462 }
463 BENCHMARK_REGISTER_F(PassthroughBenchmarkTest, RegisterReportCallback)->Iterations(100)->
464     Repetitions(3)->ReportAggregatesOnly();
465 
466 /**
467   * @tc.number: SUB_Driver_Input_PassthroughPerformance_1500
468   * @tc.name: UnregisterReportCallback001
469   * @tc.desc: unregister reportCallback test-benchmark
470   * @tc.type: func
471   * @tc.require:
472   */
BENCHMARK_F(PassthroughBenchmarkTest, UnregisterReportCallback)473 BENCHMARK_F(PassthroughBenchmarkTest, UnregisterReportCallback)(benchmark::State &st)
474 {
475     if (g_inputInterfaces == nullptr) {
476         ASSERT_NE(nullptr, g_inputInterfaces);
477         return;
478     }
479     HDF_LOGI("%s: [hdi-input] UnregisterReportCallback001 enter", __func__);
480     int32_t ret;
481 
482     for (auto _ : st) {
483         ret  = g_inputInterfaces->UnregisterReportCallback(TOUCH_INDEX);
484     }
485     if (ret != INPUT_SUCCESS) {
486         HDF_LOGE("%s: unregister callback failed for device, ret %d", __func__, ret);
487     }
488 
489     ret = g_inputInterfaces->CloseInputDevice(TOUCH_INDEX);
490     if (ret != INPUT_SUCCESS) {
491         HDF_LOGE("%s: close device failed, ret %d", __func__, ret);
492     }
493 }
494 BENCHMARK_REGISTER_F(PassthroughBenchmarkTest, UnregisterReportCallback)->Iterations(100)->
495     Repetitions(3)->ReportAggregatesOnly();
496 
497 }
498 BENCHMARK_MAIN();