1 /*
2 * Copyright (c) 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
16 #include <benchmark/benchmark.h>
17 #include <unistd.h>
18 #include <vector>
19
20 #include "device_manager.h"
21 #include "dm_app_image_info.h"
22 #include "dm_constants.h"
23
24 using namespace std;
25 using namespace OHOS;
26 using namespace OHOS::DistributedHardware;
27
28 namespace {
29 class BenchmarkDmInit : public DmInitCallback {
30 public:
BenchmarkDmInit()31 BenchmarkDmInit() : DmInitCallback() {}
~BenchmarkDmInit()32 virtual ~BenchmarkDmInit() override {}
OnRemoteDied()33 virtual void OnRemoteDied() override {}
34 };
35 class DeviceManagerTest : public benchmark::Fixture {
36 public:
DeviceManagerTest()37 DeviceManagerTest()
38 {
39 Iterations(iterations);
40 Repetitions(repetitions);
41 ReportAggregatesOnly();
42 }
43
44 ~DeviceManagerTest() override = default;
45
SetUp(const::benchmark::State & state)46 void SetUp(const ::benchmark::State &state) override
47 {
48 std::shared_ptr<BenchmarkDmInit> callback = std::make_shared<BenchmarkDmInit>();
49 DeviceManager::GetInstance().InitDeviceManager(pkgName, callback);
50 }
51
TearDown(const::benchmark::State & state)52 void TearDown(const ::benchmark::State &state) override
53 {
54 }
55 protected:
56 const string pkgName = "ohos.distributedhardware.devicemanager";
57 const int32_t repetitions = 3;
58 const int32_t iterations = 1000;
59 };
60
61 // InitDeviceManager
BENCHMARK_F(DeviceManagerTest,InitDeviceManagerTestCase)62 BENCHMARK_F(DeviceManagerTest, InitDeviceManagerTestCase)(benchmark::State &state)
63 {
64 while (state.KeepRunning()) {
65 std::shared_ptr<BenchmarkDmInit> callback = std::make_shared<BenchmarkDmInit>();
66 int32_t ret = DeviceManager::GetInstance().InitDeviceManager(pkgName, callback);
67 if (ret != DM_OK) {
68 state.SkipWithError("InitDeviceManagerTestCase failed.");
69 }
70 }
71 }
72
73 // GetFaParam
BENCHMARK_F(DeviceManagerTest,GetFaParamTestCase)74 BENCHMARK_F(DeviceManagerTest, GetFaParamTestCase)(benchmark::State &state)
75 {
76 while (state.KeepRunning()) {
77 DmAuthParam authParam;
78 int32_t ret = DeviceManager::GetInstance().GetFaParam(pkgName, authParam);
79 if (ret != DM_OK) {
80 state.SkipWithError("GetFaParamTestCase. failed");
81 }
82 }
83 }
84
85 // SetUserOperation
BENCHMARK_F(DeviceManagerTest,SetUserOperationTestCase)86 BENCHMARK_F(DeviceManagerTest, SetUserOperationTestCase)(benchmark::State &state)
87 {
88 while (state.KeepRunning()) {
89 int32_t action = 0;
90 const std::string params = "extra";
91 int32_t ret = DeviceManager::GetInstance().SetUserOperation(pkgName, action, params);
92 if (ret != DM_OK) {
93 state.SkipWithError("SetUserOperationTestCase failed.");
94 }
95 }
96 }
97
98 // GetUdidByNetworkId
BENCHMARK_F(DeviceManagerTest,GetUdidByNetworkIdTestCase)99 BENCHMARK_F(DeviceManagerTest, GetUdidByNetworkIdTestCase)(benchmark::State &state)
100 {
101 while (state.KeepRunning()) {
102 std::string netWorkId = "netWorkId_";
103 std::string udid = "udid_";
104 int32_t ret = DeviceManager::GetInstance().GetUdidByNetworkId(pkgName, netWorkId, udid);
105 if (ret != DM_OK) {
106 state.SkipWithError("SetUserOperationTestCase failed.");
107 }
108 }
109 }
110
111 // GetUuidByNetworkId
BENCHMARK_F(DeviceManagerTest,GetUuidByNetworkIdTestCase)112 BENCHMARK_F(DeviceManagerTest, GetUuidByNetworkIdTestCase)(benchmark::State &state)
113 {
114 while (state.KeepRunning()) {
115 std::string netWorkId = "netWorkId_";
116 std::string uuid = "uuid_";
117 int32_t ret = DeviceManager::GetInstance().GetUuidByNetworkId(pkgName, netWorkId, uuid);
118 if (ret != DM_OK) {
119 state.SkipWithError("SetUserOperationTestCase failed.");
120 }
121 }
122 }
123 }
124
125 // Run the benchmark
126 BENCHMARK_MAIN();
127