• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
SetUp(const::benchmark::State & state)42 void UsbBenchmarkDeviceTest::SetUp(const ::benchmark::State &state)
43 {
44     g_usbInterface = IUsbInterface::Get();
45     ASSERT_NE(g_usbInterface, nullptr);
46     auto ret = g_usbInterface->SetPortRole(DEFAULT_PORT_ID, POWER_ROLE_SINK, DATA_ROLE_DEVICE);
47     sleep(SLEEP_TIME);
48     ASSERT_EQ(0, ret);
49 }
50 
TearDown(const::benchmark::State & state)51 void UsbBenchmarkDeviceTest::TearDown(const ::benchmark::State &state) {}
52 
53 /**
54  * @tc.name: OpenDevice
55  * @tc.desc: Test functions to OpenDevice benchmark test
56  * @tc.desc: int32_t OpenDevice(const UsbDev &dev);
57  * @tc.desc: Positive test: parameters correctly
58  * @tc.type: FUNC
59  */
BENCHMARK_F(UsbBenchmarkDeviceTest,OpenDevice)60 BENCHMARK_F(UsbBenchmarkDeviceTest, OpenDevice)(benchmark::State &state)
61 {
62     ASSERT_NE(g_usbInterface, nullptr);
63     sptr<UsbSubscriberTest> subscriber = new UsbSubscriberTest();
64     ASSERT_NE(subscriber, nullptr);
65     if (g_usbInterface->BindUsbdSubscriber(subscriber) != HDF_SUCCESS) {
66         HDF_LOGW("%{public}s: bind usbd subscriber failed\n", __func__);
67     }
68     g_dev = {subscriber->busNum_, subscriber->devAddr_};
69     int32_t ret;
70     for (auto _ : state) {
71         ret = g_usbInterface->OpenDevice(g_dev);
72     }
73     EXPECT_EQ(0, ret);
74     ret = g_usbInterface->UnbindUsbdSubscriber(subscriber);
75     EXPECT_EQ(0, ret);
76 }
77 BENCHMARK_REGISTER_F(UsbBenchmarkDeviceTest, OpenDevice)->
78     Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
79 
80 /**
81  * @tc.name: CloseDevice
82  * @tc.desc: Test functions to CloseDevice benchmark test
83  * @tc.desc: int32_t CloseDevice(const UsbDev &dev);
84  * @tc.desc: Positive test: parameters correctly
85  * @tc.type: FUNC
86  */
BENCHMARK_F(UsbBenchmarkDeviceTest,CloseDevice)87 BENCHMARK_F(UsbBenchmarkDeviceTest, CloseDevice)(benchmark::State &state)
88 {
89     ASSERT_NE(g_usbInterface, nullptr);
90     sptr<UsbSubscriberTest> subscriber = new UsbSubscriberTest();
91     ASSERT_NE(subscriber, nullptr);
92     if (g_usbInterface->BindUsbdSubscriber(subscriber) != HDF_SUCCESS) {
93         HDF_LOGW("%{public}s: bind usbd subscriber failed\n", __func__);
94     }
95     g_dev = {subscriber->busNum_, subscriber->devAddr_};
96     int32_t ret;
97     for (auto _ : state) {
98         ret = g_usbInterface->OpenDevice(g_dev);
99         EXPECT_EQ(0, ret);
100         ret = g_usbInterface->CloseDevice(g_dev);
101         EXPECT_EQ(0, ret);
102     }
103     ret = g_usbInterface->UnbindUsbdSubscriber(subscriber);
104     EXPECT_EQ(0, ret);
105 }
106 BENCHMARK_REGISTER_F(UsbBenchmarkDeviceTest, CloseDevice)->
107     Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
108 } // namespace
109 BENCHMARK_MAIN();
110