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 #include "accesstoken_kit.h"
23 #include "nativetoken_kit.h"
24 #include "token_setproc.h"
25
26 using namespace OHOS::HDI::Input::Ddk::V1_0;
27
28 namespace {
29 constexpr int32_t ITERATION_FREQUENCY = 100;
30 constexpr int32_t REPETITION_FREQUENCY = 3;
31
32 sptr<IHidDdk> g_hidDdk = nullptr;
33 }
34
35 class HidDdkBenchmarkTest : public benchmark::Fixture {
36 public:
37 void SetUp(const ::benchmark::State &state);
38 void TearDown(const ::benchmark::State &state);
39 static void MockPermission();
40 };
41
SetUp(const::benchmark::State & state)42 void HidDdkBenchmarkTest::SetUp(const ::benchmark::State &state)
43 {
44 HidDdkBenchmarkTest::MockPermission();
45 g_hidDdk = IHidDdk::Get();
46 }
47
TearDown(const::benchmark::State & state)48 void HidDdkBenchmarkTest::TearDown(const ::benchmark::State &state)
49 {
50 g_hidDdk = nullptr;
51 }
52
MockPermission()53 void HidDdkBenchmarkTest::MockPermission()
54 {
55 const char *permissions[] = {
56 "ohos.permission.ACCESS_DDK_HID"
57 };
58 NativeTokenInfoParams infoInstance = {
59 .dcapsNum = 0,
60 .permsNum = 1,
61 .aclsNum = 0,
62 .dcaps = nullptr,
63 .perms = permissions,
64 .acls = nullptr,
65 .processName = "hidDdkTestCase",
66 .aplStr = "system_core",
67 };
68 uint64_t tokenId = GetAccessTokenId(&infoInstance);
69 EXPECT_EQ(0, SetSelfTokenID(tokenId));
70 OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
71 }
72
73 /**
74 * @tc.name: CreateDevice_benchmark
75 * @tc.desc: Benchmarktest for interface CreateDevice and DestroyDevice.
76 * @tc.type: FUNC
77 */
BENCHMARK_F(HidDdkBenchmarkTest,CreateDevice_benchmark)78 BENCHMARK_F(HidDdkBenchmarkTest, CreateDevice_benchmark)(benchmark::State &state)
79 {
80 ASSERT_TRUE(g_hidDdk != nullptr);
81
82 auto ret = 0;
83 uint32_t deviceId = 0;
84 struct Hid_Device hidDevice = {
85 .deviceName = "VSoC keyboard",
86 .vendorId = 0x6006,
87 .productId = 0x6008,
88 .version = 1,
89 .bustype = BUS_USB
90 };
91 struct Hid_EventProperties hidEventProp = {
92 .hidEventTypes = {HID_EV_KEY},
93 .hidKeys = {HID_KEY_1, HID_KEY_SPACE, HID_KEY_BACKSPACE, HID_KEY_ENTER}
94 };
95
96 for (auto _ : state) {
97 ret = g_hidDdk->CreateDevice(hidDevice, hidEventProp, deviceId);
98 ret = g_hidDdk->DestroyDevice(deviceId);
99 }
100 ASSERT_EQ(0, ret);
101 }
102
103 BENCHMARK_REGISTER_F(HidDdkBenchmarkTest, CreateDevice_benchmark)->
104 Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
105
106 /**
107 * @tc.name: EmitEvent_benchmark
108 * @tc.desc: Benchmarktest for interface EmitEvent.
109 * @tc.type: FUNC
110 */
BENCHMARK_F(HidDdkBenchmarkTest,EmitEvent_benchmark)111 BENCHMARK_F(HidDdkBenchmarkTest, EmitEvent_benchmark)(benchmark::State &state)
112 {
113 ASSERT_TRUE(g_hidDdk != nullptr);
114
115 auto ret = 0;
116 uint32_t deviceId = 0;
117 struct Hid_Device hidDevice = {
118 .deviceName = "VSoC keyboard",
119 .vendorId = 0x6006,
120 .productId = 0x6008,
121 .version = 1,
122 .bustype = BUS_USB
123 };
124 struct Hid_EventProperties hidEventProp = {
125 .hidEventTypes = {HID_EV_KEY},
126 .hidKeys = {HID_KEY_1, HID_KEY_SPACE, HID_KEY_BACKSPACE, HID_KEY_ENTER}
127 };
128 ret = g_hidDdk->CreateDevice(hidDevice, hidEventProp, deviceId);
129 ASSERT_EQ(0, ret);
130
131 std::vector<struct Hid_EmitItem> items = {
132 {1, 0x14a, 108},
133 {3, 0, 50 },
134 {3, 1, 50 }
135 };
136
137 for (auto _ : state) {
138 ret = g_hidDdk->EmitEvent(deviceId, items);
139 }
140 ASSERT_EQ(0, ret);
141 }
142
143 BENCHMARK_REGISTER_F(HidDdkBenchmarkTest, EmitEvent_benchmark)->
144 Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
145
146 BENCHMARK_MAIN();