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