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 "hdf_log.h"
17 #include "usbd_function.h"
18 #include "usbd_port.h"
19 #include <benchmark/benchmark.h>
20 #include <gtest/gtest.h>
21
22 using namespace benchmark::internal;
23 using namespace OHOS;
24 using namespace std;
25 using namespace OHOS::HDI::Usb::V1_0;
26
27 namespace {
28 sptr<IUsbInterface> g_usbInterface = nullptr;
29
30 constexpr int32_t SLEEP_TIME = 3;
31 constexpr int32_t ITERATION_FREQUENCY = 100;
32 constexpr int32_t REPETITION_FREQUENCY = 3;
33
34 class UsbBenchmarkFunctionTest : public benchmark::Fixture {
35 public:
36 void SetUp(const ::benchmark::State &state);
37 void TearDown(const ::benchmark::State &state);
38 };
39
SwitchErrCode(int32_t ret)40 int32_t SwitchErrCode(int32_t ret)
41 {
42 return ret == HDF_ERR_NOT_SUPPORT ? HDF_SUCCESS : ret;
43 }
44
SetUp(const::benchmark::State & state)45 void UsbBenchmarkFunctionTest::SetUp(const ::benchmark::State &state)
46 {
47 g_usbInterface = IUsbInterface::Get();
48 ASSERT_NE(g_usbInterface, nullptr);
49 auto ret = g_usbInterface->SetPortRole(DEFAULT_PORT_ID, POWER_ROLE_SINK, DATA_ROLE_DEVICE);
50 sleep(SLEEP_TIME);
51 ret = SwitchErrCode(ret);
52 ASSERT_EQ(0, ret);
53 }
54
TearDown(const::benchmark::State & state)55 void UsbBenchmarkFunctionTest::TearDown(const ::benchmark::State& state) {}
56
57 /**
58 * @tc.name: GetCurrentFunctions
59 * @tc.desc: Test functions to GetCurrentFunctions benchmark test
60 * @tc.desc: int32_t GetCurrentFunctions(int32_t &funcs);
61 * @tc.desc: Positive test: parameters correctly
62 * @tc.type: FUNC
63 */
BENCHMARK_F(UsbBenchmarkFunctionTest,GetCurrentFunctions)64 BENCHMARK_F(UsbBenchmarkFunctionTest, GetCurrentFunctions)(benchmark::State &state)
65 {
66 ASSERT_TRUE(g_usbInterface != nullptr);
67 auto ret = 0;
68 int32_t funcs = USB_FUNCTION_NONE;
69 for (auto _ : state) {
70 ret = g_usbInterface->GetCurrentFunctions(funcs);
71 }
72 ASSERT_EQ(0, ret);
73 }
74
75 BENCHMARK_REGISTER_F(UsbBenchmarkFunctionTest, GetCurrentFunctions)->
76 Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
77
78 /**
79 * @tc.name: SetCurrentFunctions
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(UsbBenchmarkFunctionTest,SetCurrentFunctions)85 BENCHMARK_F(UsbBenchmarkFunctionTest, SetCurrentFunctions)(benchmark::State &state)
86 {
87 ASSERT_TRUE(g_usbInterface != nullptr);
88 auto ret = 0;
89 for (auto _ : state) {
90 ret = g_usbInterface->SetCurrentFunctions(USB_FUNCTION_ACM);
91 }
92 ASSERT_EQ(0, ret);
93 }
94
95 BENCHMARK_REGISTER_F(UsbBenchmarkFunctionTest, SetCurrentFunctions)->
96 Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
97
98 /**
99 * @tc.name: SetPortRole
100 * @tc.desc: Test functions to SetPortRole benchmark test
101 * @tc.desc: int32_t SetPortRole(int32_t portId,int32_t powerRole,int32_t dataRole)
102 * @tc.desc: Positive test: parameters correctly
103 * @tc.type: FUNC
104 */
BENCHMARK_F(UsbBenchmarkFunctionTest,SetPortRole)105 BENCHMARK_F(UsbBenchmarkFunctionTest, SetPortRole)(benchmark::State &state)
106 {
107 ASSERT_TRUE(g_usbInterface != nullptr);
108 auto ret = 0;
109 for (auto _ : state) {
110 ret = g_usbInterface->SetPortRole(DEFAULT_PORT_ID, POWER_ROLE_SOURCE, DATA_ROLE_HOST);
111 }
112 ret = SwitchErrCode(ret);
113 ASSERT_EQ(0, ret);
114 }
115
116 BENCHMARK_REGISTER_F(UsbBenchmarkFunctionTest, SetPortRole)->
117 Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
118
119 /**
120 * @tc.name: QueryPort
121 * @tc.desc: Test functions to QueryPort benchmark test
122 * @tc.desc: int32_t QueryPort(int32_t &portId, int32_t &powerRole, int32_t &dataRole, int32_t &mode);
123 * @tc.desc: Positive test: parameters correctly
124 * @tc.type: FUNC
125 */
126
BENCHMARK_F(UsbBenchmarkFunctionTest,QueryPort)127 BENCHMARK_F(UsbBenchmarkFunctionTest, QueryPort)(benchmark::State &state)
128 {
129 ASSERT_TRUE(g_usbInterface != nullptr);
130 int32_t portId = DEFAULT_PORT_ID;
131 int32_t powerRole = POWER_ROLE_NONE;
132 int32_t dataRole = DATA_ROLE_NONE;
133 int32_t mode = PORT_MODE_NONE;
134 auto ret = 0;
135 for (auto _ : state) {
136 ret = g_usbInterface->QueryPort(portId, powerRole, dataRole, mode);
137 }
138 ASSERT_EQ(0, ret);
139 }
140
141 BENCHMARK_REGISTER_F(UsbBenchmarkFunctionTest, QueryPort)->
142 Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
143 }
144
145 BENCHMARK_MAIN();
146