1094332d3Sopenharmony_ci/*
2094332d3Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4094332d3Sopenharmony_ci * you may not use this file except in compliance with the License.
5094332d3Sopenharmony_ci * You may obtain a copy of the License at
6094332d3Sopenharmony_ci *
7094332d3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8094332d3Sopenharmony_ci *
9094332d3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and
13094332d3Sopenharmony_ci * limitations under the License.
14094332d3Sopenharmony_ci */
15094332d3Sopenharmony_ci
16094332d3Sopenharmony_ci#include "hdf_log.h"
17094332d3Sopenharmony_ci#include "usbd_function.h"
18094332d3Sopenharmony_ci#include "usbd_port.h"
19094332d3Sopenharmony_ci#include <benchmark/benchmark.h>
20094332d3Sopenharmony_ci#include <gtest/gtest.h>
21094332d3Sopenharmony_ci
22094332d3Sopenharmony_ciusing namespace benchmark::internal;
23094332d3Sopenharmony_ciusing namespace OHOS;
24094332d3Sopenharmony_ciusing namespace std;
25094332d3Sopenharmony_ciusing namespace OHOS::HDI::Usb::V1_0;
26094332d3Sopenharmony_ci
27094332d3Sopenharmony_cinamespace {
28094332d3Sopenharmony_cisptr<IUsbInterface> g_usbInterface = nullptr;
29094332d3Sopenharmony_ci
30094332d3Sopenharmony_ciconstexpr int32_t SLEEP_TIME = 3;
31094332d3Sopenharmony_ciconstexpr int32_t ITERATION_FREQUENCY = 100;
32094332d3Sopenharmony_ciconstexpr int32_t REPETITION_FREQUENCY = 3;
33094332d3Sopenharmony_ci
34094332d3Sopenharmony_ciclass UsbBenchmarkFunctionTest : public benchmark::Fixture {
35094332d3Sopenharmony_cipublic:
36094332d3Sopenharmony_ci    void SetUp(const ::benchmark::State &state);
37094332d3Sopenharmony_ci    void TearDown(const ::benchmark::State &state);
38094332d3Sopenharmony_ci};
39094332d3Sopenharmony_ci
40094332d3Sopenharmony_ciint32_t SwitchErrCode(int32_t ret)
41094332d3Sopenharmony_ci{
42094332d3Sopenharmony_ci    return ret == HDF_ERR_NOT_SUPPORT ? HDF_SUCCESS : ret;
43094332d3Sopenharmony_ci}
44094332d3Sopenharmony_ci
45094332d3Sopenharmony_civoid UsbBenchmarkFunctionTest::SetUp(const ::benchmark::State &state)
46094332d3Sopenharmony_ci{
47094332d3Sopenharmony_ci    g_usbInterface = IUsbInterface::Get();
48094332d3Sopenharmony_ci    ASSERT_NE(g_usbInterface, nullptr);
49094332d3Sopenharmony_ci    auto ret = g_usbInterface->SetPortRole(DEFAULT_PORT_ID, POWER_ROLE_SINK, DATA_ROLE_DEVICE);
50094332d3Sopenharmony_ci    sleep(SLEEP_TIME);
51094332d3Sopenharmony_ci    ret = SwitchErrCode(ret);
52094332d3Sopenharmony_ci    ASSERT_EQ(0, ret);
53094332d3Sopenharmony_ci}
54094332d3Sopenharmony_ci
55094332d3Sopenharmony_civoid UsbBenchmarkFunctionTest::TearDown(const ::benchmark::State& state) {}
56094332d3Sopenharmony_ci
57094332d3Sopenharmony_ci/**
58094332d3Sopenharmony_ci * @tc.name: GetCurrentFunctions
59094332d3Sopenharmony_ci * @tc.desc: Test functions to GetCurrentFunctions benchmark test
60094332d3Sopenharmony_ci * @tc.desc: int32_t GetCurrentFunctions(int32_t &funcs);
61094332d3Sopenharmony_ci * @tc.desc: Positive test: parameters correctly
62094332d3Sopenharmony_ci * @tc.type: FUNC
63094332d3Sopenharmony_ci */
64094332d3Sopenharmony_ciBENCHMARK_F(UsbBenchmarkFunctionTest, GetCurrentFunctions)(benchmark::State &state)
65094332d3Sopenharmony_ci{
66094332d3Sopenharmony_ci    ASSERT_TRUE(g_usbInterface != nullptr);
67094332d3Sopenharmony_ci    auto ret = 0;
68094332d3Sopenharmony_ci    int32_t funcs = USB_FUNCTION_NONE;
69094332d3Sopenharmony_ci    for (auto _ : state) {
70094332d3Sopenharmony_ci        ret = g_usbInterface->GetCurrentFunctions(funcs);
71094332d3Sopenharmony_ci    }
72094332d3Sopenharmony_ci    ASSERT_EQ(0, ret);
73094332d3Sopenharmony_ci}
74094332d3Sopenharmony_ci
75094332d3Sopenharmony_ciBENCHMARK_REGISTER_F(UsbBenchmarkFunctionTest, GetCurrentFunctions)->
76094332d3Sopenharmony_ci    Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
77094332d3Sopenharmony_ci
78094332d3Sopenharmony_ci/**
79094332d3Sopenharmony_ci * @tc.name: SetCurrentFunctions
80094332d3Sopenharmony_ci * @tc.desc: Test functions to SetCurrentFunctions benchmark test
81094332d3Sopenharmony_ci * @tc.desc: int32_t SetCurrentFunctions(int32_t funcs)
82094332d3Sopenharmony_ci * @tc.desc: Positive test: parameters correctly
83094332d3Sopenharmony_ci * @tc.type: FUNC
84094332d3Sopenharmony_ci */
85094332d3Sopenharmony_ciBENCHMARK_F(UsbBenchmarkFunctionTest, SetCurrentFunctions)(benchmark::State &state)
86094332d3Sopenharmony_ci{
87094332d3Sopenharmony_ci    ASSERT_TRUE(g_usbInterface != nullptr);
88094332d3Sopenharmony_ci    auto ret = 0;
89094332d3Sopenharmony_ci    for (auto _ : state) {
90094332d3Sopenharmony_ci        ret = g_usbInterface->SetCurrentFunctions(USB_FUNCTION_ACM);
91094332d3Sopenharmony_ci    }
92094332d3Sopenharmony_ci    ASSERT_EQ(0, ret);
93094332d3Sopenharmony_ci}
94094332d3Sopenharmony_ci
95094332d3Sopenharmony_ciBENCHMARK_REGISTER_F(UsbBenchmarkFunctionTest, SetCurrentFunctions)->
96094332d3Sopenharmony_ci    Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
97094332d3Sopenharmony_ci
98094332d3Sopenharmony_ci/**
99094332d3Sopenharmony_ci * @tc.name: SetPortRole
100094332d3Sopenharmony_ci * @tc.desc: Test functions to SetPortRole benchmark test
101094332d3Sopenharmony_ci * @tc.desc: int32_t SetPortRole(int32_t portId,int32_t powerRole,int32_t dataRole)
102094332d3Sopenharmony_ci * @tc.desc: Positive test: parameters correctly
103094332d3Sopenharmony_ci * @tc.type: FUNC
104094332d3Sopenharmony_ci */
105094332d3Sopenharmony_ciBENCHMARK_F(UsbBenchmarkFunctionTest, SetPortRole)(benchmark::State &state)
106094332d3Sopenharmony_ci{
107094332d3Sopenharmony_ci    ASSERT_TRUE(g_usbInterface != nullptr);
108094332d3Sopenharmony_ci    auto ret = 0;
109094332d3Sopenharmony_ci    for (auto _ : state) {
110094332d3Sopenharmony_ci        ret = g_usbInterface->SetPortRole(DEFAULT_PORT_ID, POWER_ROLE_SOURCE, DATA_ROLE_HOST);
111094332d3Sopenharmony_ci    }
112094332d3Sopenharmony_ci    ret = SwitchErrCode(ret);
113094332d3Sopenharmony_ci    ASSERT_EQ(0, ret);
114094332d3Sopenharmony_ci}
115094332d3Sopenharmony_ci
116094332d3Sopenharmony_ciBENCHMARK_REGISTER_F(UsbBenchmarkFunctionTest, SetPortRole)->
117094332d3Sopenharmony_ci    Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
118094332d3Sopenharmony_ci
119094332d3Sopenharmony_ci/**
120094332d3Sopenharmony_ci * @tc.name: QueryPort
121094332d3Sopenharmony_ci * @tc.desc: Test functions to QueryPort benchmark test
122094332d3Sopenharmony_ci * @tc.desc: int32_t QueryPort(int32_t &portId, int32_t &powerRole, int32_t &dataRole, int32_t &mode);
123094332d3Sopenharmony_ci * @tc.desc: Positive test: parameters correctly
124094332d3Sopenharmony_ci * @tc.type: FUNC
125094332d3Sopenharmony_ci */
126094332d3Sopenharmony_ci
127094332d3Sopenharmony_ciBENCHMARK_F(UsbBenchmarkFunctionTest, QueryPort)(benchmark::State &state)
128094332d3Sopenharmony_ci{
129094332d3Sopenharmony_ci    ASSERT_TRUE(g_usbInterface != nullptr);
130094332d3Sopenharmony_ci    int32_t portId = DEFAULT_PORT_ID;
131094332d3Sopenharmony_ci    int32_t powerRole = POWER_ROLE_NONE;
132094332d3Sopenharmony_ci    int32_t dataRole = DATA_ROLE_NONE;
133094332d3Sopenharmony_ci    int32_t mode = PORT_MODE_NONE;
134094332d3Sopenharmony_ci    auto ret = 0;
135094332d3Sopenharmony_ci    for (auto _ : state) {
136094332d3Sopenharmony_ci        ret = g_usbInterface->QueryPort(portId, powerRole, dataRole, mode);
137094332d3Sopenharmony_ci    }
138094332d3Sopenharmony_ci    ASSERT_EQ(0, ret);
139094332d3Sopenharmony_ci}
140094332d3Sopenharmony_ci
141094332d3Sopenharmony_ciBENCHMARK_REGISTER_F(UsbBenchmarkFunctionTest, QueryPort)->
142094332d3Sopenharmony_ci    Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
143094332d3Sopenharmony_ci}
144094332d3Sopenharmony_ci
145094332d3Sopenharmony_ciBENCHMARK_MAIN();
146