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