/* * Copyright (c) 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include #include #include "hdf_base.h" #include #include #include "v1_0/ihid_ddk.h" #include "accesstoken_kit.h" #include "nativetoken_kit.h" #include "token_setproc.h" using namespace OHOS::HDI::Input::Ddk::V1_0; namespace { constexpr int32_t ITERATION_FREQUENCY = 100; constexpr int32_t REPETITION_FREQUENCY = 3; sptr g_hidDdk = nullptr; } class HidDdkBenchmarkTest : public benchmark::Fixture { public: void SetUp(const ::benchmark::State &state); void TearDown(const ::benchmark::State &state); static void MockPermission(); }; void HidDdkBenchmarkTest::SetUp(const ::benchmark::State &state) { HidDdkBenchmarkTest::MockPermission(); g_hidDdk = IHidDdk::Get(); } void HidDdkBenchmarkTest::TearDown(const ::benchmark::State &state) { g_hidDdk = nullptr; } void HidDdkBenchmarkTest::MockPermission() { const char *permissions[] = { "ohos.permission.ACCESS_DDK_HID" }; NativeTokenInfoParams infoInstance = { .dcapsNum = 0, .permsNum = 1, .aclsNum = 0, .dcaps = nullptr, .perms = permissions, .acls = nullptr, .processName = "hidDdkTestCase", .aplStr = "system_core", }; uint64_t tokenId = GetAccessTokenId(&infoInstance); EXPECT_EQ(0, SetSelfTokenID(tokenId)); OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo(); } /** * @tc.name: CreateDevice_benchmark * @tc.desc: Benchmarktest for interface CreateDevice and DestroyDevice. * @tc.type: FUNC */ BENCHMARK_F(HidDdkBenchmarkTest, CreateDevice_benchmark)(benchmark::State &state) { ASSERT_TRUE(g_hidDdk != nullptr); auto ret = 0; uint32_t deviceId = 0; struct Hid_Device hidDevice = { .deviceName = "VSoC keyboard", .vendorId = 0x6006, .productId = 0x6008, .version = 1, .bustype = BUS_USB }; struct Hid_EventProperties hidEventProp = { .hidEventTypes = {HID_EV_KEY}, .hidKeys = {HID_KEY_1, HID_KEY_SPACE, HID_KEY_BACKSPACE, HID_KEY_ENTER} }; for (auto _ : state) { ret = g_hidDdk->CreateDevice(hidDevice, hidEventProp, deviceId); ret = g_hidDdk->DestroyDevice(deviceId); } ASSERT_EQ(0, ret); } BENCHMARK_REGISTER_F(HidDdkBenchmarkTest, CreateDevice_benchmark)-> Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly(); /** * @tc.name: EmitEvent_benchmark * @tc.desc: Benchmarktest for interface EmitEvent. * @tc.type: FUNC */ BENCHMARK_F(HidDdkBenchmarkTest, EmitEvent_benchmark)(benchmark::State &state) { ASSERT_TRUE(g_hidDdk != nullptr); auto ret = 0; uint32_t deviceId = 0; struct Hid_Device hidDevice = { .deviceName = "VSoC keyboard", .vendorId = 0x6006, .productId = 0x6008, .version = 1, .bustype = BUS_USB }; struct Hid_EventProperties hidEventProp = { .hidEventTypes = {HID_EV_KEY}, .hidKeys = {HID_KEY_1, HID_KEY_SPACE, HID_KEY_BACKSPACE, HID_KEY_ENTER} }; ret = g_hidDdk->CreateDevice(hidDevice, hidEventProp, deviceId); ASSERT_EQ(0, ret); std::vector items = { {1, 0x14a, 108}, {3, 0, 50 }, {3, 1, 50 } }; for (auto _ : state) { ret = g_hidDdk->EmitEvent(deviceId, items); } ASSERT_EQ(0, ret); } BENCHMARK_REGISTER_F(HidDdkBenchmarkTest, EmitEvent_benchmark)-> Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly(); BENCHMARK_MAIN();