1 /*
2 * Copyright (c) 2025 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 "v1_1/iusb_ddk.h"
20 #include "usbd_wrapper.h"
21
22 using namespace OHOS::HDI::Usb::Ddk;
23 using namespace testing::ext;
24 using namespace std;
25
26 namespace {
27 V1_1::DriverAbilityInfo g_driverInfo;
28 OHOS::sptr<V1_1::IUsbDdk> g_usbDdk = nullptr;
29
30 constexpr int32_t ITERATION_FREQUENCY = 100;
31 constexpr int32_t REPETITION_FREQUENCY = 3;
32
33 class UsbBenchmarkDriverTest : public benchmark::Fixture {
34 public:
35 void SetUp(const ::benchmark::State &state);
36 void TearDown(const ::benchmark::State &state);
37 };
38
SetUp(const::benchmark::State & state)39 void UsbBenchmarkDriverTest::SetUp(const ::benchmark::State &state)
40 {
41 g_usbDdk = V1_1::IUsbDdk::Get();
42 ASSERT_NE(g_usbDdk, nullptr);
43 }
44
TearDown(const::benchmark::State & state)45 void UsbBenchmarkDriverTest::TearDown(const ::benchmark::State &state) {}
46
47 /**
48 * @tc.name: UpdateDriverInfo
49 * @tc.desc: Test functions to UpdateDriverInfo benchmark test
50 * @tc.desc: int32_t UpdateDriverInfo(const DriverAbilityInfo &driverInfo);
51 * @tc.desc: Positive test: parameters correctly
52 * @tc.type: FUNC
53 */
BENCHMARK_F(UsbBenchmarkDriverTest,UpdateDriverInfo)54 BENCHMARK_F(UsbBenchmarkDriverTest, UpdateDriverInfo)(benchmark::State &state)
55 {
56 ASSERT_NE(g_usbDdk, nullptr);
57 g_driverInfo.driverUid = "driverName-10001";
58 g_driverInfo.vids = { 1001, 1002 };
59 int32_t ret = 0;
60 for (auto _ : state) {
61 ret = g_usbDdk->UpdateDriverInfo(g_driverInfo);
62 EXPECT_EQ(0, ret);
63 }
64 }
65 BENCHMARK_REGISTER_F(UsbBenchmarkDriverTest, UpdateDriverInfo)->
66 Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
67
68 /**
69 * @tc.name: RemoveDriverInfo
70 * @tc.desc: Test functions to RemoveDriverInfo benchmark test
71 * @tc.desc: int32_t RemoveDriverInfo(const std::string &driverUid);
72 * @tc.desc: Positive test: parameters correctly
73 * @tc.type: FUNC
74 */
BENCHMARK_F(UsbBenchmarkDriverTest,RemoveDriverInfo)75 BENCHMARK_F(UsbBenchmarkDriverTest, RemoveDriverInfo)(benchmark::State &state)
76 {
77 ASSERT_NE(g_usbDdk, nullptr);
78 g_driverInfo.driverUid = "driverName-10001";
79 g_driverInfo.vids = { 1001, 1002 };
80 int32_t ret = 0;
81 for (auto _ : state) {
82 ret = g_usbDdk->UpdateDriverInfo(g_driverInfo);
83 EXPECT_EQ(0, ret);
84 ret = g_usbDdk->RemoveDriverInfo(g_driverInfo.driverUid);
85 EXPECT_EQ(0, ret);
86 }
87 }
88 BENCHMARK_REGISTER_F(UsbBenchmarkDriverTest, RemoveDriverInfo)->
89 Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
90 } // namespace
91 BENCHMARK_MAIN();
92