19762338dSopenharmony_ci/*
29762338dSopenharmony_ci * Copyright (c) 2022-2024 Huawei Device Co., Ltd.
39762338dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
49762338dSopenharmony_ci * you may not use this file except in compliance with the License.
59762338dSopenharmony_ci * You may obtain a copy of the License at
69762338dSopenharmony_ci *
79762338dSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
89762338dSopenharmony_ci *
99762338dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
109762338dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
119762338dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
129762338dSopenharmony_ci * See the License for the specific language governing permissions and
139762338dSopenharmony_ci * limitations under the License.
149762338dSopenharmony_ci */
159762338dSopenharmony_ci#include <iostream>
169762338dSopenharmony_ci#include <string>
179762338dSopenharmony_ci#include <vector>
189762338dSopenharmony_ci
199762338dSopenharmony_ci#include "HdfUsbdBenchmarkDeviceTest.h"
209762338dSopenharmony_ci#include "hdf_log.h"
219762338dSopenharmony_ci#include "v1_1/iusb_interface.h"
229762338dSopenharmony_ci
239762338dSopenharmony_ciusing namespace benchmark::internal;
249762338dSopenharmony_ciusing namespace std;
259762338dSopenharmony_ciusing namespace OHOS;
269762338dSopenharmony_ciusing namespace OHOS::USB;
279762338dSopenharmony_ciusing namespace OHOS::HDI::Usb::V1_0;
289762338dSopenharmony_ciusing namespace OHOS::HDI::Usb::V1_1;
299762338dSopenharmony_ci
309762338dSopenharmony_ciconst int SLEEP_TIME = 3;
319762338dSopenharmony_ciconstexpr int32_t ITERATION_FREQUENCY = 100;
329762338dSopenharmony_ciconstexpr int32_t REPETITION_FREQUENCY = 3;
339762338dSopenharmony_ciconst uint8_t INTERFACEID_INVALID = 255;
349762338dSopenharmony_ciconst uint8_t INTERFACEID_OK_NEW = 0;
359762338dSopenharmony_ci
369762338dSopenharmony_cinamespace {
379762338dSopenharmony_cisptr<OHOS::HDI::Usb::V1_1::IUsbInterface> g_usbInterface = nullptr;
389762338dSopenharmony_ci
399762338dSopenharmony_cistruct UsbDev HdfUsbdBenchmarkDeviceTest::dev_ = { 0, 0 };
409762338dSopenharmony_ci
419762338dSopenharmony_ciint32_t SwitchErrCode(int32_t ret)
429762338dSopenharmony_ci{
439762338dSopenharmony_ci    return ret == HDF_ERR_NOT_SUPPORT ? HDF_SUCCESS : ret;
449762338dSopenharmony_ci}
459762338dSopenharmony_ci
469762338dSopenharmony_civoid HdfUsbdBenchmarkDeviceTest::SetUp(const ::benchmark::State& state)
479762338dSopenharmony_ci{
489762338dSopenharmony_ci    g_usbInterface = OHOS::HDI::Usb::V1_1::IUsbInterface::Get();
499762338dSopenharmony_ci    ASSERT_NE(g_usbInterface, nullptr);
509762338dSopenharmony_ci    auto ret = g_usbInterface->SetPortRole(DEFAULT_PORT_ID, POWER_ROLE_SOURCE, DATA_ROLE_HOST);
519762338dSopenharmony_ci    sleep(SLEEP_TIME);
529762338dSopenharmony_ci    ret = SwitchErrCode(ret);
539762338dSopenharmony_ci    ASSERT_EQ(0, ret);
549762338dSopenharmony_ci}
559762338dSopenharmony_ci
569762338dSopenharmony_civoid HdfUsbdBenchmarkDeviceTest::TearDown(const ::benchmark::State& state){}
579762338dSopenharmony_ci
589762338dSopenharmony_civoid HdfUsbdBenchmarkDeviceTest::InitPara(const sptr<UsbSubscriberTest> &subscriber)
599762338dSopenharmony_ci{
609762338dSopenharmony_ci    auto ret = g_usbInterface->BindUsbdSubscriber(subscriber);
619762338dSopenharmony_ci    ASSERT_EQ(0, ret);
629762338dSopenharmony_ci    dev_ = {subscriber->busNum_, subscriber->devAddr_};
639762338dSopenharmony_ci    ret = g_usbInterface->OpenDevice(dev_);
649762338dSopenharmony_ci    ASSERT_EQ(0, ret);
659762338dSopenharmony_ci}
669762338dSopenharmony_ci
679762338dSopenharmony_civoid HdfUsbdBenchmarkDeviceTest::ReleasePara(const sptr<UsbSubscriberTest> &subscriber)
689762338dSopenharmony_ci{
699762338dSopenharmony_ci    ASSERT_TRUE(g_usbInterface != nullptr);
709762338dSopenharmony_ci    auto ret = g_usbInterface->UnbindUsbdSubscriber(subscriber);
719762338dSopenharmony_ci    EXPECT_EQ(0, ret);
729762338dSopenharmony_ci    ret = g_usbInterface->CloseDevice(dev_);
739762338dSopenharmony_ci    ASSERT_EQ(0, ret);
749762338dSopenharmony_ci}
759762338dSopenharmony_ci
769762338dSopenharmony_ci/**
779762338dSopenharmony_ci * @tc.name: SUB_USB_HostManager_HDI_Performance_1100
789762338dSopenharmony_ci * @tc.desc: Test functions to OpenDevice benchmark test
799762338dSopenharmony_ci * @tc.desc: int32_t OpenDevice(const UsbDev &dev);
809762338dSopenharmony_ci * @tc.desc: Positive test: parameters correctly
819762338dSopenharmony_ci * @tc.type: FUNC
829762338dSopenharmony_ci */
839762338dSopenharmony_ciBENCHMARK_F(HdfUsbdBenchmarkDeviceTest, SUB_USB_HostManager_HDI_Performance_1100)
849762338dSopenharmony_ci(benchmark::State& st)
859762338dSopenharmony_ci{
869762338dSopenharmony_ci    ASSERT_NE(g_usbInterface, nullptr);
879762338dSopenharmony_ci    sptr<UsbSubscriberTest> subscriber = new UsbSubscriberTest();
889762338dSopenharmony_ci    ASSERT_NE(subscriber, nullptr);
899762338dSopenharmony_ci    InitPara(subscriber);
909762338dSopenharmony_ci    struct UsbDev dev = dev_;
919762338dSopenharmony_ci    auto ret = -1;
929762338dSopenharmony_ci    for (auto _ : st) {
939762338dSopenharmony_ci        ret = g_usbInterface->OpenDevice(dev);
949762338dSopenharmony_ci    }
959762338dSopenharmony_ci    ASSERT_EQ(0, ret);
969762338dSopenharmony_ci    ReleasePara(subscriber);
979762338dSopenharmony_ci}
989762338dSopenharmony_ci
999762338dSopenharmony_ciBENCHMARK_REGISTER_F(HdfUsbdBenchmarkDeviceTest, SUB_USB_HostManager_HDI_Performance_1100)
1009762338dSopenharmony_ci    ->Iterations(ITERATION_FREQUENCY)
1019762338dSopenharmony_ci    ->Repetitions(REPETITION_FREQUENCY)
1029762338dSopenharmony_ci    ->ReportAggregatesOnly();
1039762338dSopenharmony_ci
1049762338dSopenharmony_ci/**
1059762338dSopenharmony_ci * @tc.name: SUB_USB_HostManager_HDI_Performance_1200
1069762338dSopenharmony_ci * @tc.desc: Test functions to CloseDevice benchmark test
1079762338dSopenharmony_ci * @tc.desc: int32_t CloseDevice(const UsbDev &dev);
1089762338dSopenharmony_ci * @tc.desc: Positive test: parameters correctly
1099762338dSopenharmony_ci * @tc.type: FUNC
1109762338dSopenharmony_ci */
1119762338dSopenharmony_ciBENCHMARK_F(HdfUsbdBenchmarkDeviceTest, SUB_USB_HostManager_HDI_Performance_1200)
1129762338dSopenharmony_ci(benchmark::State& st)
1139762338dSopenharmony_ci{
1149762338dSopenharmony_ci    ASSERT_NE(g_usbInterface, nullptr);
1159762338dSopenharmony_ci    sptr<UsbSubscriberTest> subscriber = new UsbSubscriberTest();
1169762338dSopenharmony_ci    ASSERT_NE(subscriber, nullptr);
1179762338dSopenharmony_ci    InitPara(subscriber);
1189762338dSopenharmony_ci    struct UsbDev dev = dev_;
1199762338dSopenharmony_ci    int32_t ret;
1209762338dSopenharmony_ci    for (auto _ : st) {
1219762338dSopenharmony_ci        ret = g_usbInterface->OpenDevice(dev);
1229762338dSopenharmony_ci        ASSERT_EQ(0, ret);
1239762338dSopenharmony_ci        ret = g_usbInterface->CloseDevice(dev);
1249762338dSopenharmony_ci        ASSERT_EQ(0, ret);
1259762338dSopenharmony_ci    }
1269762338dSopenharmony_ci    ReleasePara(subscriber);
1279762338dSopenharmony_ci}
1289762338dSopenharmony_ci
1299762338dSopenharmony_ciBENCHMARK_REGISTER_F(HdfUsbdBenchmarkDeviceTest, SUB_USB_HostManager_HDI_Performance_1200)
1309762338dSopenharmony_ci    ->Iterations(ITERATION_FREQUENCY)
1319762338dSopenharmony_ci    ->Repetitions(REPETITION_FREQUENCY)
1329762338dSopenharmony_ci    ->ReportAggregatesOnly();
1339762338dSopenharmony_ci
1349762338dSopenharmony_ci/**
1359762338dSopenharmony_ci * @tc.number   : SUB_USB_HostManager_HDI_Performance_3100
1369762338dSopenharmony_ci * @tc.name     : GetDeviceSpeedBenchmarkTest
1379762338dSopenharmony_ci * @tc.desc     : int32_t GetDeviceSpeed(const UsbDev &dev, uint8_t interfaceId, uint8_t speed)
1389762338dSopenharmony_ci * @tc.desc     : Positive test: parameters correctly
1399762338dSopenharmony_ci * @tc.size     : MediumTest
1409762338dSopenharmony_ci * @tc.type     : Function
1419762338dSopenharmony_ci * @tc.level    : Level 3
1429762338dSopenharmony_ci */
1439762338dSopenharmony_ciBENCHMARK_F(HdfUsbdBenchmarkDeviceTest, GetDeviceSpeedBenchmarkTest)
1449762338dSopenharmony_ci(benchmark::State& st)
1459762338dSopenharmony_ci{
1469762338dSopenharmony_ci    ASSERT_NE(g_usbInterface, nullptr);
1479762338dSopenharmony_ci    sptr<UsbSubscriberTest> subscriber = new UsbSubscriberTest();
1489762338dSopenharmony_ci    ASSERT_NE(subscriber, nullptr);
1499762338dSopenharmony_ci    InitPara(subscriber);
1509762338dSopenharmony_ci    struct UsbDev dev = dev_;
1519762338dSopenharmony_ci    int32_t ret = -1;
1529762338dSopenharmony_ci    uint8_t speed = 0;
1539762338dSopenharmony_ci    for (auto _ : st) {
1549762338dSopenharmony_ci        ret = g_usbInterface->GetDeviceSpeed(dev, speed);
1559762338dSopenharmony_ci    }
1569762338dSopenharmony_ci    ASSERT_EQ(0, ret);
1579762338dSopenharmony_ci    ReleasePara(subscriber);
1589762338dSopenharmony_ci}
1599762338dSopenharmony_ci
1609762338dSopenharmony_ciBENCHMARK_REGISTER_F(HdfUsbdBenchmarkDeviceTest, GetDeviceSpeedBenchmarkTest)
1619762338dSopenharmony_ci    ->Iterations(ITERATION_FREQUENCY)
1629762338dSopenharmony_ci    ->Repetitions(REPETITION_FREQUENCY)
1639762338dSopenharmony_ci    ->ReportAggregatesOnly();
1649762338dSopenharmony_ci
1659762338dSopenharmony_ci/**
1669762338dSopenharmony_ci * @tc.number   : SUB_USB_HostManager_HDI_Performance_3200
1679762338dSopenharmony_ci * @tc.name     : GetInterfaceActiveStatusBenchmarkTest
1689762338dSopenharmony_ci * @tc.desc     : int32_t GetInterfaceActiveStatus(const UsbDev &dev, uint8_t interfaceId, bool unactived)
1699762338dSopenharmony_ci * @tc.desc     : Positive test: parameters correctly
1709762338dSopenharmony_ci * @tc.size     : MediumTest
1719762338dSopenharmony_ci * @tc.type     : Function
1729762338dSopenharmony_ci * @tc.level    : Level 3
1739762338dSopenharmony_ci */
1749762338dSopenharmony_ciBENCHMARK_F(HdfUsbdBenchmarkDeviceTest, GetInterfaceActiveStatusBenchmarkTest)
1759762338dSopenharmony_ci(benchmark::State& st)
1769762338dSopenharmony_ci{
1779762338dSopenharmony_ci    ASSERT_NE(g_usbInterface, nullptr);
1789762338dSopenharmony_ci    sptr<UsbSubscriberTest> subscriber = new UsbSubscriberTest();
1799762338dSopenharmony_ci    ASSERT_NE(subscriber, nullptr);
1809762338dSopenharmony_ci    InitPara(subscriber);
1819762338dSopenharmony_ci    uint8_t interfaceId = INTERFACEID_OK_NEW;
1829762338dSopenharmony_ci    struct UsbDev dev = dev_;
1839762338dSopenharmony_ci    int32_t ret = -1;
1849762338dSopenharmony_ci    bool unactived = 1;
1859762338dSopenharmony_ci    ret = g_usbInterface->ClaimInterface(dev, interfaceId, 1);
1869762338dSopenharmony_ci    ASSERT_EQ(0, ret);
1879762338dSopenharmony_ci    for (auto _ : st) {
1889762338dSopenharmony_ci        for (; interfaceId < INTERFACEID_INVALID; interfaceId++) {
1899762338dSopenharmony_ci            ret = g_usbInterface->GetInterfaceActiveStatus(dev, interfaceId, unactived);
1909762338dSopenharmony_ci            if (ret == 0) {
1919762338dSopenharmony_ci                break;
1929762338dSopenharmony_ci            }
1939762338dSopenharmony_ci        }
1949762338dSopenharmony_ci    }
1959762338dSopenharmony_ci    ASSERT_EQ(0, ret);
1969762338dSopenharmony_ci    ReleasePara(subscriber);
1979762338dSopenharmony_ci}
1989762338dSopenharmony_ci
1999762338dSopenharmony_ciBENCHMARK_REGISTER_F(HdfUsbdBenchmarkDeviceTest, GetInterfaceActiveStatusBenchmarkTest)
2009762338dSopenharmony_ci    ->Iterations(ITERATION_FREQUENCY)
2019762338dSopenharmony_ci    ->Repetitions(REPETITION_FREQUENCY)
2029762338dSopenharmony_ci    ->ReportAggregatesOnly();
2039762338dSopenharmony_ci
2049762338dSopenharmony_ci} // namespace
2059762338dSopenharmony_ci
2069762338dSopenharmony_ciBENCHMARK_MAIN();
207