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
16 #include <benchmark/benchmark.h>
17 #include <gtest/gtest.h>
18 #include "hdf_base.h"
19 #include <linux/uinput.h>
20 #include <vector>
21 #include "v1_0/ihid_ddk.h"
22
23 using namespace OHOS::HDI::Input::Ddk::V1_0;
24
25 namespace {
26 constexpr int32_t ITERATION_FREQUENCY = 100;
27 constexpr int32_t REPETITION_FREQUENCY = 3;
28
29 sptr<IHidDdk> g_hidDdk = nullptr;
30 }
31
32 class HidDdkBenchmarkTest : public benchmark::Fixture {
33 public:
34 void SetUp(const ::benchmark::State &state);
35 void TearDown(const ::benchmark::State &state);
36 };
37
SetUp(const::benchmark::State & state)38 void HidDdkBenchmarkTest::SetUp(const ::benchmark::State &state)
39 {
40 g_hidDdk = IHidDdk::Get();
41 }
42
TearDown(const::benchmark::State & state)43 void HidDdkBenchmarkTest::TearDown(const ::benchmark::State &state)
44 {
45 g_hidDdk = nullptr;
46 }
47
48 /**
49 * @tc.name: CreateDevice_benchmark
50 * @tc.desc: Benchmarktest for interface CreateDevice and DestroyDevice.
51 * @tc.type: FUNC
52 */
BENCHMARK_F(HidDdkBenchmarkTest,CreateDevice_benchmark)53 BENCHMARK_F(HidDdkBenchmarkTest, CreateDevice_benchmark)(benchmark::State &state)
54 {
55 ASSERT_TRUE(g_hidDdk != nullptr);
56
57 auto ret = 0;
58 uint32_t deviceId = 0;
59 struct Hid_Device hidDevice = {
60 .deviceName = "VSoC keyboard",
61 .vendorId = 0x6006,
62 .productId = 0x6008,
63 .version = 1,
64 .bustype = BUS_USB
65 };
66 struct Hid_EventProperties hidEventProp = {
67 .hidEventTypes = {HID_EV_KEY},
68 .hidKeys = {HID_KEY_1, HID_KEY_SPACE, HID_KEY_BACKSPACE, HID_KEY_ENTER}
69 };
70
71 for (auto _ : state) {
72 ret = g_hidDdk->CreateDevice(hidDevice, hidEventProp, deviceId);
73 ret = g_hidDdk->DestroyDevice(deviceId);
74 }
75 ASSERT_EQ(0, ret);
76 }
77
78 BENCHMARK_REGISTER_F(HidDdkBenchmarkTest, CreateDevice_benchmark)->
79 Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
80
81 /**
82 * @tc.name: EmitEvent_benchmark
83 * @tc.desc: Benchmarktest for interface EmitEvent.
84 * @tc.type: FUNC
85 */
BENCHMARK_F(HidDdkBenchmarkTest,EmitEvent_benchmark)86 BENCHMARK_F(HidDdkBenchmarkTest, EmitEvent_benchmark)(benchmark::State &state)
87 {
88 ASSERT_TRUE(g_hidDdk != nullptr);
89
90 auto ret = 0;
91 uint32_t deviceId = 0;
92 struct Hid_Device hidDevice = {
93 .deviceName = "VSoC keyboard",
94 .vendorId = 0x6006,
95 .productId = 0x6008,
96 .version = 1,
97 .bustype = BUS_USB
98 };
99 struct Hid_EventProperties hidEventProp = {
100 .hidEventTypes = {HID_EV_KEY},
101 .hidKeys = {HID_KEY_1, HID_KEY_SPACE, HID_KEY_BACKSPACE, HID_KEY_ENTER}
102 };
103 ret = g_hidDdk->CreateDevice(hidDevice, hidEventProp, deviceId);
104 ASSERT_EQ(0, ret);
105
106 std::vector<struct Hid_EmitItem> items = {
107 {1, 0x14a, 108},
108 {3, 0, 50 },
109 {3, 1, 50 }
110 };
111
112 for (auto _ : state) {
113 ret = g_hidDdk->EmitEvent(deviceId, items);
114 }
115 ASSERT_EQ(0, ret);
116 }
117
118 BENCHMARK_REGISTER_F(HidDdkBenchmarkTest, EmitEvent_benchmark)->
119 Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
120
121 BENCHMARK_MAIN();