19762338dSopenharmony_ci/*
29762338dSopenharmony_ci * Copyright (c) 2022-2024 Huawei Device Co., Ltd.
39762338dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
49762338dSopenharmony_ci * you may not use this file except in compliance with the License.
59762338dSopenharmony_ci * You may obtain a copy of the License at
69762338dSopenharmony_ci *
79762338dSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
89762338dSopenharmony_ci *
99762338dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
109762338dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
119762338dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
129762338dSopenharmony_ci * See the License for the specific language governing permissions and
139762338dSopenharmony_ci * limitations under the License.
149762338dSopenharmony_ci */
159762338dSopenharmony_ci#include <iostream>
169762338dSopenharmony_ci#include <string>
179762338dSopenharmony_ci#include <vector>
189762338dSopenharmony_ci
199762338dSopenharmony_ci#include "HdfUsbdBenchmarkFunctionTest.h"
209762338dSopenharmony_ci#include "hdf_log.h"
219762338dSopenharmony_ci#include "if_system_ability_manager.h"
229762338dSopenharmony_ci#include "system_ability_definition.h"
239762338dSopenharmony_ci#include "v1_0/iusb_interface.h"
249762338dSopenharmony_ci#include "v1_0/usb_types.h"
259762338dSopenharmony_ci
269762338dSopenharmony_ciusing namespace benchmark::internal;
279762338dSopenharmony_ciusing namespace OHOS;
289762338dSopenharmony_ciusing namespace std;
299762338dSopenharmony_ciusing namespace OHOS::HDI::Usb::V1_0;
309762338dSopenharmony_ci
319762338dSopenharmony_ciconstexpr int32_t SLEEP_TIME = 3;
329762338dSopenharmony_ciconstexpr int32_t ITERATION_FREQUENCY = 100;
339762338dSopenharmony_ciconstexpr int32_t REPETITION_FREQUENCY = 3;
349762338dSopenharmony_ci
359762338dSopenharmony_cinamespace {
369762338dSopenharmony_cisptr<IUsbInterface> g_usbInterface = nullptr;
379762338dSopenharmony_ci
389762338dSopenharmony_ciint32_t SwitchErrCode(int32_t ret)
399762338dSopenharmony_ci{
409762338dSopenharmony_ci    return ret == HDF_ERR_NOT_SUPPORT ? HDF_SUCCESS : ret;
419762338dSopenharmony_ci}
429762338dSopenharmony_ci
439762338dSopenharmony_civoid HdfUsbdBenchmarkFunctionTest::SetUp(const ::benchmark::State& state)
449762338dSopenharmony_ci{
459762338dSopenharmony_ci    g_usbInterface = IUsbInterface::Get();
469762338dSopenharmony_ci    if (g_usbInterface == nullptr) {
479762338dSopenharmony_ci        exit(0);
489762338dSopenharmony_ci    }
499762338dSopenharmony_ci    auto ret = g_usbInterface->SetPortRole(DEFAULT_PORT_ID, POWER_ROLE_SINK, DATA_ROLE_DEVICE);
509762338dSopenharmony_ci    sleep(SLEEP_TIME);
519762338dSopenharmony_ci    ret = SwitchErrCode(ret);
529762338dSopenharmony_ci    ASSERT_EQ(0, ret);
539762338dSopenharmony_ci    if (ret != 0) {
549762338dSopenharmony_ci        exit(0);
559762338dSopenharmony_ci    }
569762338dSopenharmony_ci}
579762338dSopenharmony_ci
589762338dSopenharmony_civoid HdfUsbdBenchmarkFunctionTest::TearDown(const ::benchmark::State& state) {}
599762338dSopenharmony_ci
609762338dSopenharmony_ci/**
619762338dSopenharmony_ci * @tc.name: SUB_USB_DeviceManager_HDI_Performance_0100
629762338dSopenharmony_ci * @tc.desc: Test functions to GetCurrentFunctions benchmark test
639762338dSopenharmony_ci * @tc.desc: int32_t GetCurrentFunctions(int32_t &funcs);
649762338dSopenharmony_ci * @tc.desc: Positive test: parameters correctly
659762338dSopenharmony_ci * @tc.type: FUNC
669762338dSopenharmony_ci */
679762338dSopenharmony_ciBENCHMARK_F(HdfUsbdBenchmarkFunctionTest, SUB_USB_DeviceManager_HDI_Performance_0100)
689762338dSopenharmony_ci(benchmark::State& st)
699762338dSopenharmony_ci{
709762338dSopenharmony_ci    int32_t funcs = USB_FUNCTION_NONE;
719762338dSopenharmony_ci    auto ret = -1;
729762338dSopenharmony_ci    for (auto _ : st) {
739762338dSopenharmony_ci        ret = g_usbInterface->GetCurrentFunctions(funcs);
749762338dSopenharmony_ci    }
759762338dSopenharmony_ci    ASSERT_EQ(0, ret);
769762338dSopenharmony_ci}
779762338dSopenharmony_ci
789762338dSopenharmony_ciBENCHMARK_REGISTER_F(HdfUsbdBenchmarkFunctionTest, SUB_USB_DeviceManager_HDI_Performance_0100)
799762338dSopenharmony_ci    ->Iterations(ITERATION_FREQUENCY)
809762338dSopenharmony_ci    ->Repetitions(REPETITION_FREQUENCY)
819762338dSopenharmony_ci    ->ReportAggregatesOnly();
829762338dSopenharmony_ci
839762338dSopenharmony_ci/**
849762338dSopenharmony_ci * @tc.name: SUB_USB_DeviceManager_HDI_Performance_0200
859762338dSopenharmony_ci * @tc.desc: Test functions to SetCurrentFunctions benchmark test
869762338dSopenharmony_ci * @tc.desc: int32_t SetCurrentFunctions(int32_t funcs)
879762338dSopenharmony_ci * @tc.desc: Positive test: parameters correctly
889762338dSopenharmony_ci * @tc.type: FUNC
899762338dSopenharmony_ci */
909762338dSopenharmony_ciBENCHMARK_F(HdfUsbdBenchmarkFunctionTest, SUB_USB_DeviceManager_HDI_Performance_0200)
919762338dSopenharmony_ci(benchmark::State& st)
929762338dSopenharmony_ci{
939762338dSopenharmony_ci    int32_t funcs = USB_FUNCTION_ACM;
949762338dSopenharmony_ci    auto ret = -1;
959762338dSopenharmony_ci    for (auto _ : st) {
969762338dSopenharmony_ci        ret = g_usbInterface->SetCurrentFunctions(funcs);
979762338dSopenharmony_ci    }
989762338dSopenharmony_ci    ASSERT_EQ(0, ret);
999762338dSopenharmony_ci}
1009762338dSopenharmony_ci
1019762338dSopenharmony_ciBENCHMARK_REGISTER_F(HdfUsbdBenchmarkFunctionTest, SUB_USB_DeviceManager_HDI_Performance_0200)
1029762338dSopenharmony_ci    ->Iterations(ITERATION_FREQUENCY)
1039762338dSopenharmony_ci    ->Repetitions(REPETITION_FREQUENCY)
1049762338dSopenharmony_ci    ->ReportAggregatesOnly();
1059762338dSopenharmony_ci
1069762338dSopenharmony_ci/**
1079762338dSopenharmony_ci * @tc.name: SUB_USB_PortManager_HDI_Performance_0100
1089762338dSopenharmony_ci * @tc.desc: Test functions to SetPortRole benchmark test
1099762338dSopenharmony_ci * @tc.desc: int32_t SetPortRole(int32_t portId,int32_t powerRole,int32_t dataRole)
1109762338dSopenharmony_ci * @tc.desc: Positive test: parameters correctly
1119762338dSopenharmony_ci * @tc.type: FUNC
1129762338dSopenharmony_ci */
1139762338dSopenharmony_ciBENCHMARK_F(HdfUsbdBenchmarkFunctionTest, SUB_USB_PortManager_HDI_Performance_0100)
1149762338dSopenharmony_ci(benchmark::State& st)
1159762338dSopenharmony_ci{
1169762338dSopenharmony_ci    auto ret = -1;
1179762338dSopenharmony_ci    for (auto _ : st) {
1189762338dSopenharmony_ci        ret = g_usbInterface->SetPortRole(DEFAULT_PORT_ID, POWER_ROLE_SOURCE, DATA_ROLE_HOST);
1199762338dSopenharmony_ci    }
1209762338dSopenharmony_ci    ret = SwitchErrCode(ret);
1219762338dSopenharmony_ci    ASSERT_EQ(0, ret);
1229762338dSopenharmony_ci}
1239762338dSopenharmony_ci
1249762338dSopenharmony_ciBENCHMARK_REGISTER_F(HdfUsbdBenchmarkFunctionTest, SUB_USB_PortManager_HDI_Performance_0100)
1259762338dSopenharmony_ci    ->Iterations(ITERATION_FREQUENCY)
1269762338dSopenharmony_ci    ->Repetitions(REPETITION_FREQUENCY)
1279762338dSopenharmony_ci    ->ReportAggregatesOnly();
1289762338dSopenharmony_ci
1299762338dSopenharmony_ci/**
1309762338dSopenharmony_ci * @tc.name: SUB_USB_PortManager_HDI_Performance_0200
1319762338dSopenharmony_ci * @tc.desc: Test functions to QueryPort benchmark test
1329762338dSopenharmony_ci * @tc.desc: int32_t QueryPort(int32_t &portId, int32_t &powerRole, int32_t &dataRole, int32_t &mode);
1339762338dSopenharmony_ci * @tc.desc: Positive test: parameters correctly
1349762338dSopenharmony_ci * @tc.type: FUNC
1359762338dSopenharmony_ci */
1369762338dSopenharmony_ciBENCHMARK_F(HdfUsbdBenchmarkFunctionTest, SUB_USB_PortManager_HDI_Performance_0200)
1379762338dSopenharmony_ci(benchmark::State& st)
1389762338dSopenharmony_ci{
1399762338dSopenharmony_ci    int32_t portId = DEFAULT_PORT_ID;
1409762338dSopenharmony_ci    int32_t powerRole = POWER_ROLE_NONE;
1419762338dSopenharmony_ci    int32_t dataRole = DATA_ROLE_NONE;
1429762338dSopenharmony_ci    int32_t mode = PORT_MODE_NONE;
1439762338dSopenharmony_ci    auto ret = -1;
1449762338dSopenharmony_ci    for (auto _ : st) {
1459762338dSopenharmony_ci        ret = g_usbInterface->QueryPort(portId, powerRole, dataRole, mode);
1469762338dSopenharmony_ci    }
1479762338dSopenharmony_ci    ASSERT_EQ(0, ret);
1489762338dSopenharmony_ci}
1499762338dSopenharmony_ci
1509762338dSopenharmony_ciBENCHMARK_REGISTER_F(HdfUsbdBenchmarkFunctionTest, SUB_USB_PortManager_HDI_Performance_0200)
1519762338dSopenharmony_ci    ->Iterations(ITERATION_FREQUENCY)
1529762338dSopenharmony_ci    ->Repetitions(REPETITION_FREQUENCY)
1539762338dSopenharmony_ci    ->ReportAggregatesOnly();
1549762338dSopenharmony_ci} // namespace
1559762338dSopenharmony_ci
1569762338dSopenharmony_ciBENCHMARK_MAIN();
157