• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 "HdfUsbdBenchmarkDeviceTest.h"
20 #include "UsbSubscriberTest.h"
21 #include "hdf_log.h"
22 #include "v1_0/iusb_interface.h"
23 #include "v1_0/usb_types.h"
24 
25 using namespace benchmark::internal;
26 using namespace OHOS;
27 using namespace OHOS::USB;
28 using namespace std;
29 using namespace OHOS::HDI::Usb::V1_0;
30 
31 const int SLEEP_TIME = 3;
32 
33 namespace {
34 sptr<IUsbInterface> g_usbInterface = nullptr;
35 
36 struct UsbDev HdfUsbdBenchmarkDeviceTest::dev_ = { 0, 0 };
37 
SetUp(const::benchmark::State & state)38 void HdfUsbdBenchmarkDeviceTest::SetUp(const ::benchmark::State& state)
39 {
40     g_usbInterface = IUsbInterface::Get();
41     if (g_usbInterface == nullptr) {
42         exit(0);
43     }
44     auto ret = g_usbInterface->SetPortRole(1, 1, 1);
45     sleep(SLEEP_TIME);
46     ASSERT_EQ(0, ret);
47     if (ret != 0) {
48         exit(0);
49     }
50 
51     sptr<UsbSubscriberTest> subscriber = new UsbSubscriberTest();
52     if (g_usbInterface->BindUsbdSubscriber(subscriber) != HDF_SUCCESS) {
53         exit(0);
54     }
55     dev_ = { subscriber->busNum_, subscriber->devAddr_ };
56 }
57 
TearDown(const::benchmark::State & state)58 void HdfUsbdBenchmarkDeviceTest::TearDown(const ::benchmark::State& state) {}
59 
60 /**
61  * @tc.name: SUB_USB_HDI_Benchmark_0010
62  * @tc.desc: Test functions to OpenDevice benchmark test
63  * @tc.desc: int32_t OpenDevice(const UsbDev &dev);
64  * @tc.desc: Positive test: parameters correctly
65  * @tc.type: FUNC
66  */
BENCHMARK_F(HdfUsbdBenchmarkDeviceTest,SUB_USB_HDI_Benchmark_0010)67 BENCHMARK_F(HdfUsbdBenchmarkDeviceTest, SUB_USB_HDI_Benchmark_0010)
68 (benchmark::State& st)
69 {
70     struct UsbDev dev = dev_;
71     auto ret = 0;
72     for (auto _ : st) {
73         ret = g_usbInterface->OpenDevice(dev);
74     }
75     ASSERT_EQ(0, ret);
76 }
77 
78 BENCHMARK_REGISTER_F(HdfUsbdBenchmarkDeviceTest, SUB_USB_HDI_Benchmark_0010)
79     ->Iterations(100)
80     ->Repetitions(3)
81     ->ReportAggregatesOnly();
82 
83 /**
84  * @tc.name: SUB_USB_HDI_Benchmark_0020
85  * @tc.desc: Test functions to CloseDevice benchmark test
86  * @tc.desc: int32_t CloseDevice(const UsbDev &dev);
87  * @tc.desc: Positive test: parameters correctly
88  * @tc.type: FUNC
89  */
BENCHMARK_F(HdfUsbdBenchmarkDeviceTest,SUB_USB_HDI_Benchmark_0020)90 BENCHMARK_F(HdfUsbdBenchmarkDeviceTest, SUB_USB_HDI_Benchmark_0020)
91 (benchmark::State& st)
92 {
93     struct UsbDev dev = dev_;
94     auto ret = g_usbInterface->OpenDevice(dev);
95     ASSERT_EQ(0, ret);
96     for (auto _ : st) {
97         ret = g_usbInterface->CloseDevice(dev);
98     }
99     ASSERT_EQ(0, ret);
100 }
101 
102 BENCHMARK_REGISTER_F(HdfUsbdBenchmarkDeviceTest, SUB_USB_HDI_Benchmark_0020)
103     ->Iterations(100)
104     ->Repetitions(3)
105     ->ReportAggregatesOnly();
106 } // namespace
107 
108 BENCHMARK_MAIN();
109