1 /*
2 * Copyright (c) 2021-2022 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 const int SLEEP_TIME = 3;
32 const int TEST_PORT_ID = 1;
33 const int TEST_POWER_ROLE = 2;
34 const int TEST_DATAR_ROLE = 2;
35
36 namespace {
37 sptr<IUsbInterface> g_usbInterface = nullptr;
38
SetUp(const::benchmark::State & state)39 void HdfUsbdBenchmarkFunctionTest::SetUp(const ::benchmark::State& state)
40 {
41 g_usbInterface = IUsbInterface::Get();
42 if (g_usbInterface == nullptr) {
43 exit(0);
44 }
45 auto ret = g_usbInterface->SetPortRole(TEST_PORT_ID, TEST_POWER_ROLE, TEST_DATAR_ROLE);
46 sleep(SLEEP_TIME);
47 ASSERT_EQ(0, ret);
48 if (ret != 0) {
49 exit(0);
50 }
51 }
52
TearDown(const::benchmark::State & state)53 void HdfUsbdBenchmarkFunctionTest::TearDown(const ::benchmark::State& state) {}
54
55 /**
56 * @tc.name: SUB_USB_HDI_Benchmark_0030
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_HDI_Benchmark_0030)62 BENCHMARK_F(HdfUsbdBenchmarkFunctionTest, SUB_USB_HDI_Benchmark_0030)
63 (benchmark::State& st)
64 {
65 int32_t funcs = 0;
66 auto ret = 0;
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_HDI_Benchmark_0030)
74 ->Iterations(100)
75 ->Repetitions(3)
76 ->ReportAggregatesOnly();
77
78 /**
79 * @tc.name: SUB_USB_HDI_Benchmark_0040
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_HDI_Benchmark_0040)85 BENCHMARK_F(HdfUsbdBenchmarkFunctionTest, SUB_USB_HDI_Benchmark_0040)
86 (benchmark::State& st)
87 {
88 int32_t funcs = 1;
89 auto ret = 0;
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_HDI_Benchmark_0040)
97 ->Iterations(100)
98 ->Repetitions(3)
99 ->ReportAggregatesOnly();
100
101 /**
102 * @tc.name: SUB_USB_HDI_Benchmark_0050
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_HDI_Benchmark_0050)108 BENCHMARK_F(HdfUsbdBenchmarkFunctionTest, SUB_USB_HDI_Benchmark_0050)
109 (benchmark::State& st)
110 {
111 auto ret = 0;
112 for (auto _ : st) {
113 ret = g_usbInterface->SetPortRole(1, 1, 1);
114 }
115 ASSERT_EQ(0, ret);
116 }
117
118 BENCHMARK_REGISTER_F(HdfUsbdBenchmarkFunctionTest, SUB_USB_HDI_Benchmark_0050)
119 ->Iterations(100)
120 ->Repetitions(3)
121 ->ReportAggregatesOnly();
122
123 /**
124 * @tc.name: SUB_USB_HDI_Benchmark_0060
125 * @tc.desc: Test functions to QueryPort benchmark test
126 * @tc.desc: int32_t QueryPort(int32_t &portId, int32_t &powerRole, int32_t &dataRole, int32_t &mode);
127 * @tc.desc: Positive test: parameters correctly
128 * @tc.type: FUNC
129 */
BENCHMARK_F(HdfUsbdBenchmarkFunctionTest,SUB_USB_HDI_Benchmark_0060)130 BENCHMARK_F(HdfUsbdBenchmarkFunctionTest, SUB_USB_HDI_Benchmark_0060)
131 (benchmark::State& st)
132 {
133 int32_t portId = 0;
134 int32_t powerRole = 0;
135 int32_t dataRole = 0;
136 int32_t mode = 0;
137 auto ret = 0;
138 for (auto _ : st) {
139 ret = g_usbInterface->QueryPort(portId, powerRole, dataRole, mode);
140 }
141 ASSERT_EQ(0, ret);
142 }
143
144 BENCHMARK_REGISTER_F(HdfUsbdBenchmarkFunctionTest, SUB_USB_HDI_Benchmark_0060)
145 ->Iterations(100)
146 ->Repetitions(3)
147 ->ReportAggregatesOnly();
148 } // namespace
149
150 BENCHMARK_MAIN();
151