• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <benchmark/benchmark.h>
17 #include <gtest/gtest.h>
18 #include "hdf_log.h"
19 #include "usbd_port.h"
20 #include "usbd_function.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 
SetUp(const::benchmark::State & state)40 void UsbBenchmarkFunctionTest::SetUp(const ::benchmark::State& state)
41 {
42     g_usbInterface = IUsbInterface::Get();
43     ASSERT_NE(g_usbInterface, nullptr);
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 }
48 
TearDown(const::benchmark::State & state)49 void UsbBenchmarkFunctionTest::TearDown(const ::benchmark::State& state) {}
50 
51 /**
52  * @tc.name: GetCurrentFunctions
53  * @tc.desc: Test functions to GetCurrentFunctions benchmark test
54  * @tc.desc: int32_t GetCurrentFunctions(int32_t &funcs);
55  * @tc.desc: Positive test: parameters correctly
56  * @tc.type: FUNC
57  */
BENCHMARK_F(UsbBenchmarkFunctionTest,GetCurrentFunctions)58 BENCHMARK_F(UsbBenchmarkFunctionTest, GetCurrentFunctions)(benchmark::State &state)
59 {
60     ASSERT_TRUE(g_usbInterface != nullptr);
61     auto ret = 0;
62     int32_t funcs = USB_FUNCTION_NONE;;
63     for (auto _ : state) {
64         ret = g_usbInterface->GetCurrentFunctions(funcs);
65     }
66     ASSERT_EQ(0, ret);
67 }
68 
69 BENCHMARK_REGISTER_F(UsbBenchmarkFunctionTest, GetCurrentFunctions)->
70     Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
71 
72 /**
73  * @tc.name: SetCurrentFunctions
74  * @tc.desc: Test functions to SetCurrentFunctions benchmark test
75  * @tc.desc: int32_t SetCurrentFunctions(int32_t funcs)
76  * @tc.desc: Positive test: parameters correctly
77  * @tc.type: FUNC
78  */
BENCHMARK_F(UsbBenchmarkFunctionTest,SetCurrentFunctions)79 BENCHMARK_F(UsbBenchmarkFunctionTest, SetCurrentFunctions)(benchmark::State &state)
80 {
81     ASSERT_TRUE(g_usbInterface != nullptr);
82     auto ret = 0;
83     for (auto _ : state) {
84         ret = g_usbInterface->SetCurrentFunctions(USB_FUNCTION_ACM);
85     }
86     ASSERT_EQ(0, ret);
87 }
88 
89 BENCHMARK_REGISTER_F(UsbBenchmarkFunctionTest, SetCurrentFunctions)->
90     Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
91 
92 /**
93  * @tc.name: SetPortRole
94  * @tc.desc: Test functions to SetPortRole benchmark test
95  * @tc.desc: int32_t SetPortRole(int32_t portId,int32_t powerRole,int32_t dataRole)
96  * @tc.desc: Positive test: parameters correctly
97  * @tc.type: FUNC
98  */
BENCHMARK_F(UsbBenchmarkFunctionTest,SetPortRole)99 BENCHMARK_F(UsbBenchmarkFunctionTest, SetPortRole)(benchmark::State &state)
100 {
101     ASSERT_TRUE(g_usbInterface != nullptr);
102     auto ret = 0;
103     for (auto _ : state) {
104         ret = g_usbInterface->SetPortRole(DEFAULT_PORT_ID, POWER_ROLE_SOURCE, DATA_ROLE_HOST);
105     }
106     ASSERT_EQ(0, ret);
107 }
108 
109 BENCHMARK_REGISTER_F(UsbBenchmarkFunctionTest, SetPortRole)->
110     Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
111 
112 /**
113  * @tc.name: QueryPort
114  * @tc.desc: Test functions to QueryPort benchmark test
115  * @tc.desc: int32_t QueryPort(int32_t &portId, int32_t &powerRole, int32_t &dataRole, int32_t &mode);
116  * @tc.desc: Positive test: parameters correctly
117  * @tc.type: FUNC
118  */
119 
BENCHMARK_F(UsbBenchmarkFunctionTest,QueryPort)120 BENCHMARK_F(UsbBenchmarkFunctionTest, QueryPort)(benchmark::State &state)
121 {
122     ASSERT_TRUE(g_usbInterface != nullptr);
123     int32_t portId = DEFAULT_PORT_ID;
124     int32_t powerRole = POWER_ROLE_NONE;
125     int32_t dataRole = DATA_ROLE_NONE;
126     int32_t mode = PORT_MODE_NONE;
127     auto ret = 0;
128     for (auto _ : state) {
129         ret = g_usbInterface->QueryPort(portId, powerRole, dataRole, mode);
130     }
131     ASSERT_EQ(0, ret);
132 }
133 
134 BENCHMARK_REGISTER_F(UsbBenchmarkFunctionTest, QueryPort)->
135     Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
136 }
137 
138 BENCHMARK_MAIN();
139