• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "v2_0/iusb_host_interface.h"
22 #include "v2_0/iusb_port_interface.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::V2_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<IUsbHostInterface> g_usbHostInterface = nullptr;
40 sptr<IUsbPortInterface> g_usbPortInterface = nullptr;
41 
SetUp(const::benchmark::State & state)42 void HdfUsbdBenchmarkManagerInterfaceTest::SetUp(const ::benchmark::State& state)
43 {
44     g_usbHostInterface = IUsbHostInterface::Get(true);
45     g_usbPortInterface = IUsbPortInterface::Get();
46     if (g_usbHostInterface == nullptr || g_usbPortInterface == nullptr) {
47         HDF_LOGE("%{public}s:IUsbInterface::Get() failed.", __func__);
48         exit(0);
49     }
50     auto ret = g_usbPortInterface->SetPortRole(DEFAULT_PORT_ID, POWER_ROLE_SOURCE, DATA_ROLE_HOST);
51     sleep(SLEEP_TIME);
52     HDF_LOGI("HdfUsbdBenchmarkManagerInterfaceTest::[Device] %{public}d SetPortRole=%{public}d", __LINE__, ret);
53     if (ret != 0) {
54         ASSERT_EQ(HDF_ERR_NOT_SUPPORT, ret);
55     } else {
56         ASSERT_EQ(0, ret);
57     }
58 
59     subscriber_ = new UsbSubscriberTest();
60     if (g_usbHostInterface->BindUsbdHostSubscriber(subscriber_) != HDF_SUCCESS) {
61         HDF_LOGE("%{public}s: bind usbd subscriber_ failed", __func__);
62         exit(0);
63     }
64 
65     dev_ = {subscriber_->busNum_, subscriber_->devAddr_};
66 
67     ret = g_usbHostInterface->OpenDevice(dev_);
68     HDF_LOGI("UsbdManageInterfaceTest:: %{public}d OpenDevice=%{public}d", __LINE__, ret);
69     ASSERT_EQ(0, ret);
70 }
71 
TearDown(const::benchmark::State & state)72 void HdfUsbdBenchmarkManagerInterfaceTest::TearDown(const ::benchmark::State& state)
73 {
74     g_usbHostInterface->UnbindUsbdHostSubscriber(subscriber_);
75     dev_ = {subscriber_->busNum_, subscriber_->devAddr_};
76     auto ret = g_usbHostInterface->CloseDevice(dev_);
77     HDF_LOGI("UsbdManageInterfaceTest:: %{public}d Close=%{public}d", __LINE__, ret);
78     ASSERT_EQ(0, ret);
79 }
80 
81 /**
82  * @tc.name: SUB_USB_HostManager_HDI_Performance_2800
83  * @tc.desc: Test functions to OpenDevice benchmark test
84  * @tc.desc: int32_t OpenDevice(const UsbDev &dev);
85  * @tc.desc: Positive test: parameters correctly
86  * @tc.type: FUNC
87  */
BENCHMARK_F(HdfUsbdBenchmarkManagerInterfaceTest,SUB_USB_HostManager_HDI_Performance_2800)88 BENCHMARK_F(HdfUsbdBenchmarkManagerInterfaceTest, SUB_USB_HostManager_HDI_Performance_2800)
89 (benchmark::State& st)
90 {
91     uint8_t interfaceId = INTERFACEID_OK_NEW;
92     struct UsbDev dev = dev_;
93     int32_t ret = -1;
94     for (auto _ : st) {
95         for (; interfaceId < INTERFACEID_INVALID; interfaceId++) {
96             ret = g_usbHostInterface->ManageInterface(dev, interfaceId, true);
97             if (ret == 0) {
98                 break;
99             }
100         }
101     }
102     ASSERT_EQ(0, ret);
103 }
104 
105 BENCHMARK_REGISTER_F(HdfUsbdBenchmarkManagerInterfaceTest, SUB_USB_HostManager_HDI_Performance_2800)
106     ->Iterations(ITERATION_FREQUENCY)
107     ->Repetitions(REPETITION_FREQUENCY)
108     ->ReportAggregatesOnly();
109 
110 } // namespace
111 
112 BENCHMARK_MAIN();
113