1 /*
2  * Copyright (c) 2023 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 
16 #include <benchmark/benchmark.h>
17 #include <gtest/gtest.h>
18 #include "hdf_log.h"
19 #include "usbd_port.h"
20 #include "UsbSubscriberTest.h"
21 #include "v1_0/iusb_interface.h"
22 
23 using namespace OHOS::HDI::Usb::V1_0;
24 using namespace testing::ext;
25 using namespace OHOS::USB;
26 using namespace std;
27 
28 namespace {
29     struct UsbDev g_dev = {0, 0};
30     sptr<IUsbInterface> g_usbInterface = nullptr;
31 
32     const int SLEEP_TIME = 3;
33     constexpr int32_t ITERATION_FREQUENCY = 100;
34     constexpr int32_t REPETITION_FREQUENCY = 3;
35 
36 class UsbBenchmarkDeviceTest : public benchmark::Fixture {
37 public:
38     void SetUp(const ::benchmark::State &state);
39     void TearDown(const ::benchmark::State &state);
40 };
41 
SwitchErrCode(int32_t ret)42 int32_t SwitchErrCode(int32_t ret)
43 {
44     return ret == HDF_ERR_NOT_SUPPORT ? HDF_SUCCESS : ret;
45 }
46 
SetUp(const::benchmark::State & state)47 void UsbBenchmarkDeviceTest::SetUp(const ::benchmark::State &state)
48 {
49     g_usbInterface = IUsbInterface::Get();
50     ASSERT_NE(g_usbInterface, nullptr);
51     auto ret = g_usbInterface->SetPortRole(DEFAULT_PORT_ID, POWER_ROLE_SINK, DATA_ROLE_DEVICE);
52     sleep(SLEEP_TIME);
53     ret = SwitchErrCode(ret);
54     ASSERT_EQ(0, ret);
55 }
56 
TearDown(const::benchmark::State & state)57 void UsbBenchmarkDeviceTest::TearDown(const ::benchmark::State &state) {}
58 
59 /**
60  * @tc.name: OpenDevice
61  * @tc.desc: Test functions to OpenDevice benchmark test
62  * @tc.desc: int32_t OpenDevice(const UsbDev &dev);
63  * @tc.desc: Positive test: parameters correctly
64  * @tc.type: FUNC
65  */
BENCHMARK_F(UsbBenchmarkDeviceTest,OpenDevice)66 BENCHMARK_F(UsbBenchmarkDeviceTest, OpenDevice)(benchmark::State &state)
67 {
68     ASSERT_NE(g_usbInterface, nullptr);
69     sptr<UsbSubscriberTest> subscriber = new UsbSubscriberTest();
70     ASSERT_NE(subscriber, nullptr);
71     if (g_usbInterface->BindUsbdSubscriber(subscriber) != HDF_SUCCESS) {
72         HDF_LOGW("%{public}s: bind usbd subscriber failed", __func__);
73     }
74     g_dev = {subscriber->busNum_, subscriber->devAddr_};
75     int32_t ret;
76     for (auto _ : state) {
77         ret = g_usbInterface->OpenDevice(g_dev);
78     }
79     EXPECT_EQ(0, ret);
80     ret = g_usbInterface->UnbindUsbdSubscriber(subscriber);
81     EXPECT_EQ(0, ret);
82 }
83 BENCHMARK_REGISTER_F(UsbBenchmarkDeviceTest, OpenDevice)->
84     Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
85 
86 /**
87  * @tc.name: CloseDevice
88  * @tc.desc: Test functions to CloseDevice benchmark test
89  * @tc.desc: int32_t CloseDevice(const UsbDev &dev);
90  * @tc.desc: Positive test: parameters correctly
91  * @tc.type: FUNC
92  */
BENCHMARK_F(UsbBenchmarkDeviceTest,CloseDevice)93 BENCHMARK_F(UsbBenchmarkDeviceTest, CloseDevice)(benchmark::State &state)
94 {
95     ASSERT_NE(g_usbInterface, nullptr);
96     sptr<UsbSubscriberTest> subscriber = new UsbSubscriberTest();
97     ASSERT_NE(subscriber, nullptr);
98     if (g_usbInterface->BindUsbdSubscriber(subscriber) != HDF_SUCCESS) {
99         HDF_LOGW("%{public}s: bind usbd subscriber failed", __func__);
100     }
101     g_dev = {subscriber->busNum_, subscriber->devAddr_};
102     int32_t ret;
103     for (auto _ : state) {
104         ret = g_usbInterface->OpenDevice(g_dev);
105         EXPECT_EQ(0, ret);
106         ret = g_usbInterface->CloseDevice(g_dev);
107         EXPECT_EQ(0, ret);
108     }
109     ret = g_usbInterface->UnbindUsbdSubscriber(subscriber);
110     EXPECT_EQ(0, ret);
111 }
112 BENCHMARK_REGISTER_F(UsbBenchmarkDeviceTest, CloseDevice)->
113     Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
114 } // namespace
115 BENCHMARK_MAIN();
116