1 /*
2 * Copyright (c) 2024-2025 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 <iostream>
16 #include <string>
17 #include <vector>
18
19 #include "HdfUsbdBenchmarkManagerInterfaceTest.h"
20 #include "hdf_log.h"
21 #include "v1_0/iusb_interface.h"
22 #include "v1_0/usb_types.h"
23
24 using namespace benchmark::internal;
25 using namespace std;
26 using namespace OHOS;
27 using namespace OHOS::USB;
28 using namespace OHOS::HDI::Usb::V1_0;
29
30 const int SLEEP_TIME = 3;
31 const uint8_t INTERFACEID_OK_NEW = 0;
32 const uint8_t INTERFACEID_INVALID = 255;
33 constexpr int32_t ITERATION_FREQUENCY = 100;
34 constexpr int32_t REPETITION_FREQUENCY = 3;
35
36 namespace {
37 UsbDev HdfUsbdBenchmarkManagerInterfaceTest::dev_ = {0, 0};
38 sptr<UsbSubscriberTest> HdfUsbdBenchmarkManagerInterfaceTest::subscriber_ = nullptr;
39 sptr<IUsbInterface> g_usbInterface = nullptr;
40
SetUp(const::benchmark::State & state)41 void HdfUsbdBenchmarkManagerInterfaceTest::SetUp(const ::benchmark::State& state)
42 {
43 g_usbInterface = IUsbInterface::Get();
44 if (g_usbInterface == nullptr) {
45 HDF_LOGE("%{public}s:IUsbInterface::Get() failed.", __func__);
46 exit(0);
47 }
48 auto ret = g_usbInterface->SetPortRole(DEFAULT_PORT_ID, POWER_ROLE_SOURCE, DATA_ROLE_HOST);
49 sleep(SLEEP_TIME);
50 HDF_LOGI("HdfUsbdBenchmarkManagerInterfaceTest::[Device] %{public}d SetPortRole=%{public}d", __LINE__, ret);
51 if (ret != 0) {
52 ASSERT_EQ(HDF_ERR_NOT_SUPPORT, ret);
53 } else {
54 ASSERT_EQ(0, ret);
55 }
56
57 subscriber_ = new UsbSubscriberTest();
58 if (g_usbInterface->BindUsbdSubscriber(subscriber_) != HDF_SUCCESS) {
59 HDF_LOGE("%{public}s: bind usbd subscriber_ failed", __func__);
60 exit(0);
61 }
62
63 dev_ = {subscriber_->busNum_, subscriber_->devAddr_};
64
65 ret = g_usbInterface->OpenDevice(dev_);
66 HDF_LOGI("UsbdManageInterfaceTest:: %{public}d OpenDevice=%{public}d", __LINE__, ret);
67 ASSERT_EQ(0, ret);
68 }
69
TearDown(const::benchmark::State & state)70 void HdfUsbdBenchmarkManagerInterfaceTest::TearDown(const ::benchmark::State& state)
71 {
72 g_usbInterface->UnbindUsbdSubscriber(subscriber_);
73 dev_ = {subscriber_->busNum_, subscriber_->devAddr_};
74 auto ret = g_usbInterface->CloseDevice(dev_);
75 HDF_LOGI("UsbdManageInterfaceTest:: %{public}d Close=%{public}d", __LINE__, ret);
76 ASSERT_EQ(0, ret);
77 }
78
79 /**
80 * @tc.name: SUB_USB_HostManager_HDI_Performance_2800
81 * @tc.desc: Test functions to OpenDevice benchmark test
82 * @tc.desc: int32_t OpenDevice(const UsbDev &dev);
83 * @tc.desc: Positive test: parameters correctly
84 * @tc.type: FUNC
85 */
BENCHMARK_F(HdfUsbdBenchmarkManagerInterfaceTest,SUB_USB_HostManager_HDI_Performance_2800)86 BENCHMARK_F(HdfUsbdBenchmarkManagerInterfaceTest, SUB_USB_HostManager_HDI_Performance_2800)
87 (benchmark::State& st)
88 {
89 uint8_t interfaceId = INTERFACEID_OK_NEW;
90 struct UsbDev dev = dev_;
91 int32_t ret = -1;
92 for (auto _ : st) {
93 for (; interfaceId < INTERFACEID_INVALID; interfaceId++) {
94 ret = g_usbInterface->ManageInterface(dev, interfaceId, true);
95 if (ret == 0) {
96 break;
97 }
98 }
99 }
100 ASSERT_EQ(0, ret);
101 }
102
103 BENCHMARK_REGISTER_F(HdfUsbdBenchmarkManagerInterfaceTest, SUB_USB_HostManager_HDI_Performance_2800)
104 ->Iterations(ITERATION_FREQUENCY)
105 ->Repetitions(REPETITION_FREQUENCY)
106 ->ReportAggregatesOnly();
107
108 } // namespace
109
110 BENCHMARK_MAIN();
111