1 /*
2 * Copyright (c) 2022-2024 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 "accesstoken_kit.h"
21 #include "device_manager.h"
22 #include "dm_app_image_info.h"
23 #include "dm_constants.h"
24 #include "nativetoken_kit.h"
25 #include "softbus_common.h"
26 #include "token_setproc.h"
27
28 using namespace std;
29 using namespace OHOS;
30 using namespace OHOS::DistributedHardware;
31
32 namespace {
33 class BenchmarkDmInit : public DmInitCallback {
34 public:
BenchmarkDmInit()35 BenchmarkDmInit() : DmInitCallback() {}
~BenchmarkDmInit()36 virtual ~BenchmarkDmInit() {}
OnRemoteDied()37 void OnRemoteDied() override {}
38 };
39 class DeviceManagerTest : public benchmark::Fixture {
40 public:
DeviceManagerTest()41 DeviceManagerTest()
42 {
43 Iterations(iterations);
44 Repetitions(repetitions);
45 ReportAggregatesOnly();
46 }
47
48 ~DeviceManagerTest() override = default;
49
SetUp(const::benchmark::State & state)50 void SetUp(const ::benchmark::State &state) override
51 {
52 const int32_t permsNum = 3;
53 const int32_t indexZero = 0;
54 const int32_t indexOne = 1;
55 const int32_t indexTwo = 2;
56 uint64_t tokenId;
57 const char *perms[permsNum];
58 perms[indexZero] = "ohos.permission.ACCESS_SERVICE_DM";
59 perms[indexOne] = "ohos.permission.DISTRIBUTED_DATASYNC";
60 perms[indexTwo] = "ohos.permission.DISTRIBUTED_SOFTBUS_CENTER";
61 NativeTokenInfoParams infoInstance = {
62 .dcapsNum = 0,
63 .permsNum = permsNum,
64 .aclsNum = 0,
65 .dcaps = NULL,
66 .perms = perms,
67 .acls = NULL,
68 .processName = "device_manager",
69 .aplStr = "system_core",
70 };
71 tokenId = GetAccessTokenId(&infoInstance);
72 SetSelfTokenID(tokenId);
73 OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
74 std::shared_ptr<BenchmarkDmInit> callback = std::make_shared<BenchmarkDmInit>();
75 DeviceManager::GetInstance().InitDeviceManager(pkgName, callback);
76 }
77
TearDown(const::benchmark::State & state)78 void TearDown(const ::benchmark::State &state) override
79 {
80 }
81 protected:
82 const string pkgName = "ohos.distributedhardware.devicemanager";
83 const int32_t repetitions = 3;
84 const int32_t iterations = 1000;
85 };
86
87 // InitDeviceManager
BENCHMARK_F(DeviceManagerTest,InitDeviceManagerTestCase)88 BENCHMARK_F(DeviceManagerTest, InitDeviceManagerTestCase)(benchmark::State &state)
89 {
90 while (state.KeepRunning()) {
91 std::shared_ptr<BenchmarkDmInit> callback = std::make_shared<BenchmarkDmInit>();
92 int32_t ret = DeviceManager::GetInstance().InitDeviceManager(pkgName, callback);
93 if (ret != DM_OK) {
94 state.SkipWithError("InitDeviceManagerTestCase failed.");
95 }
96 }
97 }
98
99 // SetUserOperation
BENCHMARK_F(DeviceManagerTest,SetUserOperationTestCase)100 BENCHMARK_F(DeviceManagerTest, SetUserOperationTestCase)(benchmark::State &state)
101 {
102 while (state.KeepRunning()) {
103 int32_t action = 0;
104 const std::string params = "extra";
105 int32_t ret = DeviceManager::GetInstance().SetUserOperation(pkgName, action, params);
106 if (ret != DM_OK) {
107 state.SkipWithError("SetUserOperationTestCase failed.");
108 }
109 }
110 }
111
112 // GetUdidByNetworkId
BENCHMARK_F(DeviceManagerTest,GetUdidByNetworkIdTestCase)113 BENCHMARK_F(DeviceManagerTest, GetUdidByNetworkIdTestCase)(benchmark::State &state)
114 {
115 while (state.KeepRunning()) {
116 std::string netWorkId = "netWorkId_";
117 std::string udid = "udid_";
118 int32_t ret = DeviceManager::GetInstance().GetUdidByNetworkId(pkgName, netWorkId, udid);
119 if (ret != DM_OK) {
120 state.SkipWithError("SetUserOperationTestCase failed.");
121 }
122 }
123 }
124
125 // GetUuidByNetworkId
BENCHMARK_F(DeviceManagerTest,GetUuidByNetworkIdTestCase)126 BENCHMARK_F(DeviceManagerTest, GetUuidByNetworkIdTestCase)(benchmark::State &state)
127 {
128 while (state.KeepRunning()) {
129 std::string netWorkId = "netWorkId_";
130 std::string uuid = "uuid_";
131 int32_t ret = DeviceManager::GetInstance().GetUuidByNetworkId(pkgName, netWorkId, uuid);
132 if (ret != DM_OK) {
133 state.SkipWithError("SetUserOperationTestCase failed.");
134 }
135 }
136 }
137 }
138
139 // Run the benchmark
140 BENCHMARK_MAIN();
141