1094332d3Sopenharmony_ci/*
2094332d3Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4094332d3Sopenharmony_ci * you may not use this file except in compliance with the License.
5094332d3Sopenharmony_ci * You may obtain a copy of the License at
6094332d3Sopenharmony_ci *
7094332d3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8094332d3Sopenharmony_ci *
9094332d3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and
13094332d3Sopenharmony_ci * limitations under the License.
14094332d3Sopenharmony_ci */
15094332d3Sopenharmony_ci
16094332d3Sopenharmony_ci#include <benchmark/benchmark.h>
17094332d3Sopenharmony_ci#include <gtest/gtest.h>
18094332d3Sopenharmony_ci#include "hdf_log.h"
19094332d3Sopenharmony_ci#include "usbd_port.h"
20094332d3Sopenharmony_ci#include "UsbSubscriberTest.h"
21094332d3Sopenharmony_ci#include "v1_0/iusb_interface.h"
22094332d3Sopenharmony_ci
23094332d3Sopenharmony_ciusing namespace OHOS::HDI::Usb::V1_0;
24094332d3Sopenharmony_ciusing namespace testing::ext;
25094332d3Sopenharmony_ciusing namespace OHOS::USB;
26094332d3Sopenharmony_ciusing namespace std;
27094332d3Sopenharmony_ci
28094332d3Sopenharmony_cinamespace {
29094332d3Sopenharmony_ci    struct UsbDev g_dev = {0, 0};
30094332d3Sopenharmony_ci    sptr<IUsbInterface> g_usbInterface = nullptr;
31094332d3Sopenharmony_ci
32094332d3Sopenharmony_ci    const int SLEEP_TIME = 3;
33094332d3Sopenharmony_ci    constexpr int32_t ITERATION_FREQUENCY = 100;
34094332d3Sopenharmony_ci    constexpr int32_t REPETITION_FREQUENCY = 3;
35094332d3Sopenharmony_ci
36094332d3Sopenharmony_ciclass UsbBenchmarkDeviceTest : public benchmark::Fixture {
37094332d3Sopenharmony_cipublic:
38094332d3Sopenharmony_ci    void SetUp(const ::benchmark::State &state);
39094332d3Sopenharmony_ci    void TearDown(const ::benchmark::State &state);
40094332d3Sopenharmony_ci};
41094332d3Sopenharmony_ci
42094332d3Sopenharmony_ciint32_t SwitchErrCode(int32_t ret)
43094332d3Sopenharmony_ci{
44094332d3Sopenharmony_ci    return ret == HDF_ERR_NOT_SUPPORT ? HDF_SUCCESS : ret;
45094332d3Sopenharmony_ci}
46094332d3Sopenharmony_ci
47094332d3Sopenharmony_civoid UsbBenchmarkDeviceTest::SetUp(const ::benchmark::State &state)
48094332d3Sopenharmony_ci{
49094332d3Sopenharmony_ci    g_usbInterface = IUsbInterface::Get();
50094332d3Sopenharmony_ci    ASSERT_NE(g_usbInterface, nullptr);
51094332d3Sopenharmony_ci    auto ret = g_usbInterface->SetPortRole(DEFAULT_PORT_ID, POWER_ROLE_SINK, DATA_ROLE_DEVICE);
52094332d3Sopenharmony_ci    sleep(SLEEP_TIME);
53094332d3Sopenharmony_ci    ret = SwitchErrCode(ret);
54094332d3Sopenharmony_ci    ASSERT_EQ(0, ret);
55094332d3Sopenharmony_ci}
56094332d3Sopenharmony_ci
57094332d3Sopenharmony_civoid UsbBenchmarkDeviceTest::TearDown(const ::benchmark::State &state) {}
58094332d3Sopenharmony_ci
59094332d3Sopenharmony_ci/**
60094332d3Sopenharmony_ci * @tc.name: OpenDevice
61094332d3Sopenharmony_ci * @tc.desc: Test functions to OpenDevice benchmark test
62094332d3Sopenharmony_ci * @tc.desc: int32_t OpenDevice(const UsbDev &dev);
63094332d3Sopenharmony_ci * @tc.desc: Positive test: parameters correctly
64094332d3Sopenharmony_ci * @tc.type: FUNC
65094332d3Sopenharmony_ci */
66094332d3Sopenharmony_ciBENCHMARK_F(UsbBenchmarkDeviceTest, OpenDevice)(benchmark::State &state)
67094332d3Sopenharmony_ci{
68094332d3Sopenharmony_ci    ASSERT_NE(g_usbInterface, nullptr);
69094332d3Sopenharmony_ci    sptr<UsbSubscriberTest> subscriber = new UsbSubscriberTest();
70094332d3Sopenharmony_ci    ASSERT_NE(subscriber, nullptr);
71094332d3Sopenharmony_ci    if (g_usbInterface->BindUsbdSubscriber(subscriber) != HDF_SUCCESS) {
72094332d3Sopenharmony_ci        HDF_LOGW("%{public}s: bind usbd subscriber failed", __func__);
73094332d3Sopenharmony_ci    }
74094332d3Sopenharmony_ci    g_dev = {subscriber->busNum_, subscriber->devAddr_};
75094332d3Sopenharmony_ci    int32_t ret;
76094332d3Sopenharmony_ci    for (auto _ : state) {
77094332d3Sopenharmony_ci        ret = g_usbInterface->OpenDevice(g_dev);
78094332d3Sopenharmony_ci    }
79094332d3Sopenharmony_ci    EXPECT_EQ(0, ret);
80094332d3Sopenharmony_ci    ret = g_usbInterface->UnbindUsbdSubscriber(subscriber);
81094332d3Sopenharmony_ci    EXPECT_EQ(0, ret);
82094332d3Sopenharmony_ci}
83094332d3Sopenharmony_ciBENCHMARK_REGISTER_F(UsbBenchmarkDeviceTest, OpenDevice)->
84094332d3Sopenharmony_ci    Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
85094332d3Sopenharmony_ci
86094332d3Sopenharmony_ci/**
87094332d3Sopenharmony_ci * @tc.name: CloseDevice
88094332d3Sopenharmony_ci * @tc.desc: Test functions to CloseDevice benchmark test
89094332d3Sopenharmony_ci * @tc.desc: int32_t CloseDevice(const UsbDev &dev);
90094332d3Sopenharmony_ci * @tc.desc: Positive test: parameters correctly
91094332d3Sopenharmony_ci * @tc.type: FUNC
92094332d3Sopenharmony_ci */
93094332d3Sopenharmony_ciBENCHMARK_F(UsbBenchmarkDeviceTest, CloseDevice)(benchmark::State &state)
94094332d3Sopenharmony_ci{
95094332d3Sopenharmony_ci    ASSERT_NE(g_usbInterface, nullptr);
96094332d3Sopenharmony_ci    sptr<UsbSubscriberTest> subscriber = new UsbSubscriberTest();
97094332d3Sopenharmony_ci    ASSERT_NE(subscriber, nullptr);
98094332d3Sopenharmony_ci    if (g_usbInterface->BindUsbdSubscriber(subscriber) != HDF_SUCCESS) {
99094332d3Sopenharmony_ci        HDF_LOGW("%{public}s: bind usbd subscriber failed", __func__);
100094332d3Sopenharmony_ci    }
101094332d3Sopenharmony_ci    g_dev = {subscriber->busNum_, subscriber->devAddr_};
102094332d3Sopenharmony_ci    int32_t ret;
103094332d3Sopenharmony_ci    for (auto _ : state) {
104094332d3Sopenharmony_ci        ret = g_usbInterface->OpenDevice(g_dev);
105094332d3Sopenharmony_ci        EXPECT_EQ(0, ret);
106094332d3Sopenharmony_ci        ret = g_usbInterface->CloseDevice(g_dev);
107094332d3Sopenharmony_ci        EXPECT_EQ(0, ret);
108094332d3Sopenharmony_ci    }
109094332d3Sopenharmony_ci    ret = g_usbInterface->UnbindUsbdSubscriber(subscriber);
110094332d3Sopenharmony_ci    EXPECT_EQ(0, ret);
111094332d3Sopenharmony_ci}
112094332d3Sopenharmony_ciBENCHMARK_REGISTER_F(UsbBenchmarkDeviceTest, CloseDevice)->
113094332d3Sopenharmony_ci    Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
114094332d3Sopenharmony_ci} // namespace
115094332d3Sopenharmony_ciBENCHMARK_MAIN();
116