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