1 /*
2 * Copyright (c) 2025 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 "HdfUsbdBenchmarkFunctionTest.h"
20 #include "hdf_log.h"
21 #include "if_system_ability_manager.h"
22 #include "system_ability_definition.h"
23 #include "v2_0/iusb_device_interface.h"
24 #include "v2_0/iusb_port_interface.h"
25 #include "v2_0/usb_types.h"
26
27 using namespace benchmark::internal;
28 using namespace OHOS;
29 using namespace std;
30 using namespace OHOS::HDI::Usb::V2_0;
31
32 constexpr int32_t SLEEP_TIME = 3;
33 constexpr int32_t ITERATION_FREQUENCY = 100;
34 constexpr int32_t REPETITION_FREQUENCY = 3;
35
36 namespace {
37 sptr<IUsbPortInterface> g_usbPortInterface = nullptr;
38 sptr<IUsbDeviceInterface> g_usbDeviceInterface = nullptr;
39
SetUp(const::benchmark::State & state)40 void HdfUsbdBenchmarkFunctionTest::SetUp(const ::benchmark::State& state)
41 {
42 g_usbDeviceInterface = IUsbDeviceInterface::Get();
43 g_usbPortInterface = IUsbPortInterface::Get();
44 if (g_usbDeviceInterface == nullptr || g_usbPortInterface == nullptr) {
45 exit(0);
46 }
47 auto ret = g_usbPortInterface->SetPortRole(DEFAULT_PORT_ID, POWER_ROLE_SINK, DATA_ROLE_DEVICE);
48 sleep(SLEEP_TIME);
49 if (ret != 0) {
50 ASSERT_EQ(HDF_ERR_NOT_SUPPORT, ret);
51 } else {
52 ASSERT_EQ(0, ret);
53 }
54 }
55
TearDown(const::benchmark::State & state)56 void HdfUsbdBenchmarkFunctionTest::TearDown(const ::benchmark::State& state) {}
57
58 /**
59 * @tc.name: SUB_USB_DeviceManager_HDI_Performance_0100
60 * @tc.desc: Test functions to GetCurrentFunctions benchmark test
61 * @tc.desc: int32_t GetCurrentFunctions(int32_t &funcs);
62 * @tc.desc: Positive test: parameters correctly
63 * @tc.type: FUNC
64 */
BENCHMARK_F(HdfUsbdBenchmarkFunctionTest,SUB_USB_DeviceManager_HDI_Performance_0100)65 BENCHMARK_F(HdfUsbdBenchmarkFunctionTest, SUB_USB_DeviceManager_HDI_Performance_0100)
66 (benchmark::State& st)
67 {
68 int32_t funcs = USB_FUNCTION_NONE;
69 auto ret = -1;
70 for (auto _ : st) {
71 ret = g_usbDeviceInterface->GetCurrentFunctions(funcs);
72 }
73 ASSERT_EQ(0, ret);
74 }
75
76 BENCHMARK_REGISTER_F(HdfUsbdBenchmarkFunctionTest, SUB_USB_DeviceManager_HDI_Performance_0100)
77 ->Iterations(ITERATION_FREQUENCY)
78 ->Repetitions(REPETITION_FREQUENCY)
79 ->ReportAggregatesOnly();
80
81 /**
82 * @tc.name: SUB_USB_DeviceManager_HDI_Performance_0200
83 * @tc.desc: Test functions to SetCurrentFunctions benchmark test
84 * @tc.desc: int32_t SetCurrentFunctions(int32_t funcs)
85 * @tc.desc: Positive test: parameters correctly
86 * @tc.type: FUNC
87 */
BENCHMARK_F(HdfUsbdBenchmarkFunctionTest,SUB_USB_DeviceManager_HDI_Performance_0200)88 BENCHMARK_F(HdfUsbdBenchmarkFunctionTest, SUB_USB_DeviceManager_HDI_Performance_0200)
89 (benchmark::State& st)
90 {
91 int32_t funcs = USB_FUNCTION_ACM;
92 auto ret = -1;
93 for (auto _ : st) {
94 ret = g_usbDeviceInterface->SetCurrentFunctions(funcs);
95 }
96 ASSERT_EQ(0, ret);
97 }
98
99 BENCHMARK_REGISTER_F(HdfUsbdBenchmarkFunctionTest, SUB_USB_DeviceManager_HDI_Performance_0200)
100 ->Iterations(ITERATION_FREQUENCY)
101 ->Repetitions(REPETITION_FREQUENCY)
102 ->ReportAggregatesOnly();
103
104 /**
105 * @tc.name: SUB_USB_PortManager_HDI_Performance_0100
106 * @tc.desc: Test functions to SetPortRole benchmark test
107 * @tc.desc: int32_t SetPortRole(int32_t portId,int32_t powerRole,int32_t dataRole)
108 * @tc.desc: Positive test: parameters correctly
109 * @tc.type: FUNC
110 */
BENCHMARK_F(HdfUsbdBenchmarkFunctionTest,SUB_USB_PortManager_HDI_Performance_0100)111 BENCHMARK_F(HdfUsbdBenchmarkFunctionTest, SUB_USB_PortManager_HDI_Performance_0100)
112 (benchmark::State& st)
113 {
114 auto ret = -1;
115 for (auto _ : st) {
116 ret = g_usbPortInterface->SetPortRole(DEFAULT_PORT_ID, POWER_ROLE_SOURCE, DATA_ROLE_HOST);
117 }
118 if (ret != 0) {
119 ASSERT_EQ(HDF_ERR_NOT_SUPPORT, ret);
120 } else {
121 ASSERT_EQ(0, ret);
122 }
123 }
124
125 BENCHMARK_REGISTER_F(HdfUsbdBenchmarkFunctionTest, SUB_USB_PortManager_HDI_Performance_0100)
126 ->Iterations(ITERATION_FREQUENCY)
127 ->Repetitions(REPETITION_FREQUENCY)
128 ->ReportAggregatesOnly();
129
130 /**
131 * @tc.name: SUB_USB_PortManager_HDI_Performance_0200
132 * @tc.desc: Test functions to QueryPort benchmark test
133 * @tc.desc: int32_t QueryPort(int32_t &portId, int32_t &powerRole, int32_t &dataRole, int32_t &mode);
134 * @tc.desc: Positive test: parameters correctly
135 * @tc.type: FUNC
136 */
BENCHMARK_F(HdfUsbdBenchmarkFunctionTest,SUB_USB_PortManager_HDI_Performance_0200)137 BENCHMARK_F(HdfUsbdBenchmarkFunctionTest, SUB_USB_PortManager_HDI_Performance_0200)
138 (benchmark::State& st)
139 {
140 int32_t portId = DEFAULT_PORT_ID;
141 int32_t powerRole = POWER_ROLE_NONE;
142 int32_t dataRole = DATA_ROLE_NONE;
143 int32_t mode = PORT_MODE_NONE;
144 auto ret = -1;
145 for (auto _ : st) {
146 ret = g_usbPortInterface->QueryPort(portId, powerRole, dataRole, mode);
147 }
148 ASSERT_EQ(0, ret);
149 }
150
151 BENCHMARK_REGISTER_F(HdfUsbdBenchmarkFunctionTest, SUB_USB_PortManager_HDI_Performance_0200)
152 ->Iterations(ITERATION_FREQUENCY)
153 ->Repetitions(REPETITION_FREQUENCY)
154 ->ReportAggregatesOnly();
155 } // namespace
156
157 BENCHMARK_MAIN();
158