1 /*
2 * Copyright (c) 2022-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 #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 ASSERT_EQ(0, ret);
47 if (ret != 0) {
48 exit(0);
49 }
50 }
51
TearDown(const::benchmark::State & state)52 void HdfUsbdBenchmarkFunctionTest::TearDown(const ::benchmark::State& state) {}
53
54 /**
55 * @tc.name: SUB_USB_HDI_Benchmark_0030
56 * @tc.desc: Test functions to GetCurrentFunctions benchmark test
57 * @tc.desc: int32_t GetCurrentFunctions(int32_t &funcs);
58 * @tc.desc: Positive test: parameters correctly
59 * @tc.type: FUNC
60 */
BENCHMARK_F(HdfUsbdBenchmarkFunctionTest,SUB_USB_HDI_Benchmark_0030)61 BENCHMARK_F(HdfUsbdBenchmarkFunctionTest, SUB_USB_HDI_Benchmark_0030)
62 (benchmark::State& st)
63 {
64 int32_t funcs = USB_FUNCTION_NONE;
65 auto ret = 0;
66 for (auto _ : st) {
67 ret = g_usbInterface->GetCurrentFunctions(funcs);
68 }
69 ASSERT_EQ(0, ret);
70 }
71
72 BENCHMARK_REGISTER_F(HdfUsbdBenchmarkFunctionTest, SUB_USB_HDI_Benchmark_0030)
73 ->Iterations(ITERATION_FREQUENCY)
74 ->Repetitions(REPETITION_FREQUENCY)
75 ->ReportAggregatesOnly();
76
77 /**
78 * @tc.name: SUB_USB_HDI_Benchmark_0040
79 * @tc.desc: Test functions to SetCurrentFunctions benchmark test
80 * @tc.desc: int32_t SetCurrentFunctions(int32_t funcs)
81 * @tc.desc: Positive test: parameters correctly
82 * @tc.type: FUNC
83 */
BENCHMARK_F(HdfUsbdBenchmarkFunctionTest,SUB_USB_HDI_Benchmark_0040)84 BENCHMARK_F(HdfUsbdBenchmarkFunctionTest, SUB_USB_HDI_Benchmark_0040)
85 (benchmark::State& st)
86 {
87 int32_t funcs = USB_FUNCTION_ACM;
88 auto ret = 0;
89 for (auto _ : st) {
90 ret = g_usbInterface->SetCurrentFunctions(funcs);
91 }
92 ASSERT_EQ(0, ret);
93 }
94
95 BENCHMARK_REGISTER_F(HdfUsbdBenchmarkFunctionTest, SUB_USB_HDI_Benchmark_0040)
96 ->Iterations(ITERATION_FREQUENCY)
97 ->Repetitions(REPETITION_FREQUENCY)
98 ->ReportAggregatesOnly();
99
100 /**
101 * @tc.name: SUB_USB_HDI_Benchmark_0050
102 * @tc.desc: Test functions to SetPortRole benchmark test
103 * @tc.desc: int32_t SetPortRole(int32_t portId,int32_t powerRole,int32_t dataRole)
104 * @tc.desc: Positive test: parameters correctly
105 * @tc.type: FUNC
106 */
BENCHMARK_F(HdfUsbdBenchmarkFunctionTest,SUB_USB_HDI_Benchmark_0050)107 BENCHMARK_F(HdfUsbdBenchmarkFunctionTest, SUB_USB_HDI_Benchmark_0050)
108 (benchmark::State& st)
109 {
110 auto ret = 0;
111 for (auto _ : st) {
112 ret = g_usbInterface->SetPortRole(DEFAULT_PORT_ID, POWER_ROLE_SOURCE, DATA_ROLE_HOST);
113 }
114 ASSERT_EQ(0, ret);
115 }
116
117 BENCHMARK_REGISTER_F(HdfUsbdBenchmarkFunctionTest, SUB_USB_HDI_Benchmark_0050)
118 ->Iterations(ITERATION_FREQUENCY)
119 ->Repetitions(REPETITION_FREQUENCY)
120 ->ReportAggregatesOnly();
121
122 /**
123 * @tc.name: SUB_USB_HDI_Benchmark_0060
124 * @tc.desc: Test functions to QueryPort benchmark test
125 * @tc.desc: int32_t QueryPort(int32_t &portId, int32_t &powerRole, int32_t &dataRole, int32_t &mode);
126 * @tc.desc: Positive test: parameters correctly
127 * @tc.type: FUNC
128 */
BENCHMARK_F(HdfUsbdBenchmarkFunctionTest,SUB_USB_HDI_Benchmark_0060)129 BENCHMARK_F(HdfUsbdBenchmarkFunctionTest, SUB_USB_HDI_Benchmark_0060)
130 (benchmark::State& st)
131 {
132 int32_t portId = DEFAULT_PORT_ID;
133 int32_t powerRole = POWER_ROLE_NONE;
134 int32_t dataRole = DATA_ROLE_NONE;
135 int32_t mode = PORT_MODE_NONE;
136 auto ret = 0;
137 for (auto _ : st) {
138 ret = g_usbInterface->QueryPort(portId, powerRole, dataRole, mode);
139 }
140 ASSERT_EQ(0, ret);
141 }
142
143 BENCHMARK_REGISTER_F(HdfUsbdBenchmarkFunctionTest, SUB_USB_HDI_Benchmark_0060)
144 ->Iterations(ITERATION_FREQUENCY)
145 ->Repetitions(REPETITION_FREQUENCY)
146 ->ReportAggregatesOnly();
147 } // namespace
148
149 BENCHMARK_MAIN();
150