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 #include <securec.h>
20 #include <cstdlib>
21
22 #include "accesstoken_kit.h"
23 #include "device_manager.h"
24 #include "dm_app_image_info.h"
25 #include "dm_subscribe_info.h"
26 #include "device_manager_callback.h"
27 #include "dm_constants.h"
28 #include "nativetoken_kit.h"
29 #include "system_ability_definition.h"
30 #include "softbus_common.h"
31 #include "token_setproc.h"
32
33 using namespace std;
34 using namespace OHOS;
35 using namespace OHOS::DistributedHardware;
36
37 namespace {
38 class DeviceDiscoveryCallbackTest : public DiscoveryCallback {
39 public:
DeviceDiscoveryCallbackTest()40 DeviceDiscoveryCallbackTest() : DiscoveryCallback() {}
~DeviceDiscoveryCallbackTest()41 virtual ~DeviceDiscoveryCallbackTest() {}
OnDiscoverySuccess(uint16_t subscribeId)42 void OnDiscoverySuccess(uint16_t subscribeId) override {}
OnDiscoveryFailed(uint16_t subscribeId,int32_t failedReason)43 void OnDiscoveryFailed(uint16_t subscribeId, int32_t failedReason) override {}
OnDeviceFound(uint16_t subscribeId,const DmDeviceInfo & deviceInfo)44 void OnDeviceFound(uint16_t subscribeId, const DmDeviceInfo &deviceInfo) override {}
45 };
46
47 class DevicePublishCallbackTest : public PublishCallback {
48 public:
DevicePublishCallbackTest()49 DevicePublishCallbackTest() : PublishCallback() {}
~DevicePublishCallbackTest()50 virtual ~DevicePublishCallbackTest() {}
OnPublishResult(int32_t publishId,int32_t failedReason)51 void OnPublishResult(int32_t publishId, int32_t failedReason) override {}
52 };
53
54 class BenchmarkDmInit : public DmInitCallback {
55 public:
BenchmarkDmInit()56 BenchmarkDmInit() : DmInitCallback() {}
~BenchmarkDmInit()57 virtual ~BenchmarkDmInit() override {}
OnRemoteDied()58 void OnRemoteDied() override {}
59 };
60
61 class DeviceStateCallbackTest : public DeviceStateCallback {
62 public:
DeviceStateCallbackTest()63 DeviceStateCallbackTest() : DeviceStateCallback() {}
~DeviceStateCallbackTest()64 virtual ~DeviceStateCallbackTest() {}
OnDeviceOnline(const DmDeviceInfo & deviceInfo)65 void OnDeviceOnline(const DmDeviceInfo &deviceInfo) override {}
OnDeviceReady(const DmDeviceInfo & deviceInfo)66 void OnDeviceReady(const DmDeviceInfo &deviceInfo) override {}
OnDeviceOffline(const DmDeviceInfo & deviceInfo)67 void OnDeviceOffline(const DmDeviceInfo &deviceInfo) override {}
OnDeviceChanged(const DmDeviceInfo & deviceInfo)68 void OnDeviceChanged(const DmDeviceInfo &deviceInfo) override {}
69 };
70
71 class DeviceManagerFaCallbackTest : public DeviceManagerUiCallback {
72 public:
DeviceManagerFaCallbackTest()73 DeviceManagerFaCallbackTest() : DeviceManagerUiCallback() {}
~DeviceManagerFaCallbackTest()74 virtual ~DeviceManagerFaCallbackTest() {}
OnCall(const std::string & paramJson)75 void OnCall(const std::string ¶mJson) override {}
76 };
77
78 class DeviceManagerFaTest : public benchmark::Fixture {
79 public:
DeviceManagerFaTest()80 DeviceManagerFaTest()
81 {
82 Iterations(iterations);
83 Repetitions(repetitions);
84 ReportAggregatesOnly();
85 }
86
87 ~DeviceManagerFaTest() override = default;
88
SetUp(const::benchmark::State & state)89 void SetUp(const ::benchmark::State &state) override
90 {
91 std::shared_ptr<BenchmarkDmInit> callback = std::make_shared<BenchmarkDmInit>();
92 DeviceManager::GetInstance().InitDeviceManager(pkgName, callback);
93 }
94
TearDown(const::benchmark::State & state)95 void TearDown(const ::benchmark::State &state) override
96 {
97 }
98 protected:
99 const int32_t repetitions = 3;
100 const int32_t iterations = 1000;
101 // sleep 1000ms
102 const int32_t usleepTime = 1000 * 1000;
103 const string pkgName = "com.ohos.devicemanager";
104 const string extra = "extra_";
105 const string bundleName = "bundleName_";
106 const string extraString = "extraString_";
107 const string packageName = "com.ohos.devicemanager";
108 const int32_t authType = 1;
109 };
110
111 class GetTrustedDeviceListTest : public DeviceManagerFaTest {
112 public:
SetUp(const::benchmark::State & state)113 void SetUp(const ::benchmark::State &state) override
114 {
115 }
TearDown(const::benchmark::State & state)116 void TearDown(const ::benchmark::State &state) override
117 {
118 }
119 };
120
121 class GetLocalDeviceInfoTest : public DeviceManagerFaTest {
122 public:
SetUp(const::benchmark::State & state)123 void SetUp(const ::benchmark::State &state) override
124 {
125 }
TearDown(const::benchmark::State & state)126 void TearDown(const ::benchmark::State &state) override
127 {
128 }
129 };
130
131 class DeviceDiscoveryTest : public DeviceManagerFaTest {
132 public:
SetUp(const::benchmark::State & state)133 void SetUp(const ::benchmark::State &state) override
134 {
135 const int32_t permsNum = 3;
136 const int32_t indexZero = 0;
137 const int32_t indexOne = 1;
138 const int32_t indexTwo = 2;
139 uint64_t tokenId;
140 const char *perms[permsNum];
141 perms[indexZero] = "ohos.permission.ACCESS_SERVICE_DM";
142 perms[indexOne] = "ohos.permission.DISTRIBUTED_DATASYNC";
143 perms[indexTwo] = "ohos.permission.DISTRIBUTED_SOFTBUS_CENTER";
144 NativeTokenInfoParams infoInstance = {
145 .dcapsNum = 0,
146 .permsNum = permsNum,
147 .aclsNum = 0,
148 .dcaps = NULL,
149 .perms = perms,
150 .acls = NULL,
151 .processName = "device_manager",
152 .aplStr = "system_core",
153 };
154 tokenId = GetAccessTokenId(&infoInstance);
155 SetSelfTokenID(tokenId);
156 OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
157 }
TearDown(const::benchmark::State & state)158 void TearDown(const ::benchmark::State &state) override
159 {
160 }
161 };
162
163 class RegisterDeviceManagerFaTest : public DeviceManagerFaTest {
164 public:
TearDown(const::benchmark::State & state)165 void TearDown(const ::benchmark::State &state) override
166 {
167 DeviceManager::GetInstance().UnRegisterDeviceManagerFaCallback(pkgName);
168 usleep(usleepTime);
169 }
170 };
171
172 class UnRegisterDeviceManagerFaTest : public DeviceManagerFaTest {
173 public:
SetUp(const::benchmark::State & state)174 void SetUp(const ::benchmark::State &state) override
175 {
176 std::shared_ptr<DeviceManagerFaCallbackTest> callback = std::make_shared<DeviceManagerFaCallbackTest>();
177 DeviceManager::GetInstance().RegisterDeviceManagerFaCallback(pkgName, callback);
178 usleep(usleepTime);
179 }
180 };
181
182 class RegisterDevStateTest : public DeviceManagerFaTest {
183 public:
TearDown(const::benchmark::State & state)184 void TearDown(const ::benchmark::State &state) override
185 {
186 DeviceManager::GetInstance().UnRegisterDevStateCallback(pkgName);
187 usleep(usleepTime);
188 }
189 };
190
191 class UnRegisterDevStateTest : public DeviceManagerFaTest {
192 public:
SetUp(const::benchmark::State & state)193 void SetUp(const ::benchmark::State &state) override
194 {
195 }
TearDown(const::benchmark::State & state)196 void TearDown(const ::benchmark::State &state) override
197 {
198 }
199 };
200
201 // GetTrustedDeviceList
BENCHMARK_F(GetTrustedDeviceListTest,GetTrustedDeviceListTestCase)202 BENCHMARK_F(GetTrustedDeviceListTest, GetTrustedDeviceListTestCase)(
203 benchmark::State &state)
204 {
205 while (state.KeepRunning()) {
206 state.PauseTiming();
207 std::shared_ptr<BenchmarkDmInit> callback = std::make_shared<BenchmarkDmInit>();
208 DeviceManager::GetInstance().InitDeviceManager(pkgName, callback);
209 state.ResumeTiming();
210 std::vector<DmDeviceInfo> devList {};
211 DmDeviceInfo deviceInfo;
212 string deviceInfoId = "12345678";
213 string deviceInfoName = "com.OHOS";
214 deviceInfo.deviceTypeId = 0;
215 string deviceNetworkId = "com.OHOS.app";
216 strncpy_s(deviceInfo.deviceId, DM_MAX_DEVICE_ID_LEN, deviceInfoId.c_str(), deviceInfoId.length());
217 strncpy_s(deviceInfo.deviceName, DM_MAX_DEVICE_NAME_LEN, deviceInfoName.c_str(), deviceInfoName.length());
218 strncpy_s(deviceInfo.networkId, DM_MAX_DEVICE_ID_LEN, deviceNetworkId.c_str(), deviceNetworkId.length());
219 int32_t ret = DeviceManager::GetInstance().GetTrustedDeviceList(pkgName, extra, devList);
220 if (ret != DM_OK) {
221 state.SkipWithError("GetTrustedDeviceListTestCase failed.");
222 }
223 }
224 }
225
226 // GetLocalDeviceInfo
BENCHMARK_F(GetLocalDeviceInfoTest,GetLocalDeviceInfoTestCase)227 BENCHMARK_F(GetLocalDeviceInfoTest, GetLocalDeviceInfoTestCase)(
228 benchmark::State &state)
229 {
230 while (state.KeepRunning()) {
231 state.PauseTiming();
232 std::shared_ptr<BenchmarkDmInit> callback = std::make_shared<BenchmarkDmInit>();
233 DeviceManager::GetInstance().InitDeviceManager(pkgName, callback);
234 state.ResumeTiming();
235 std::vector<DmDeviceInfo> devList {};
236 DmDeviceInfo deviceInfo;
237 string deviceInfoId = "12345678";
238 string deviceInfoName = "com.OHOS";
239 deviceInfo.deviceTypeId = 0;
240 string deviceNetworkId = "com.OHOS.app";
241 strncpy_s(deviceInfo.deviceId, DM_MAX_DEVICE_ID_LEN, deviceInfoId.c_str(), deviceInfoId.length());
242 strncpy_s(deviceInfo.deviceName, DM_MAX_DEVICE_NAME_LEN, deviceInfoName.c_str(), deviceInfoName.length());
243 strncpy_s(deviceInfo.networkId, DM_MAX_DEVICE_ID_LEN, deviceNetworkId.c_str(), deviceNetworkId.length());
244 int32_t ret = DeviceManager::GetInstance().GetLocalDeviceInfo(pkgName, deviceInfo);
245 if (ret != DM_OK) {
246 state.SkipWithError("GetLocalDeviceInfoTestCase failed.");
247 }
248 }
249 }
250
251 // StartDeviceDiscovery
BENCHMARK_F(DeviceDiscoveryTest,StartDeviceDiscoveryTestCase)252 BENCHMARK_F(DeviceDiscoveryTest, StartDeviceDiscoveryTestCase)(
253 benchmark::State &state)
254 {
255 while (state.KeepRunning()) {
256 string packageName = "com.devicemanager";
257 state.PauseTiming();
258 std::shared_ptr<BenchmarkDmInit> callback_ = std::make_shared<BenchmarkDmInit>();
259 DeviceManager::GetInstance().InitDeviceManager(packageName, callback_);
260 state.ResumeTiming();
261 DmSubscribeInfo subInfo;
262 subInfo.subscribeId = DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID;
263 subInfo.mode = DM_DISCOVER_MODE_ACTIVE;
264 subInfo.medium = DM_AUTO;
265 subInfo.freq = DM_HIGH;
266 subInfo.isSameAccount = false;
267 subInfo.isWakeRemote = false;
268 strcpy_s(subInfo.capability, DM_MAX_DEVICE_CAPABILITY_LEN, DM_CAPABILITY_OSD);
269 std::shared_ptr<DiscoveryCallback> callback = std::make_shared<DeviceDiscoveryCallbackTest>();
270 std::string str;
271 int32_t ret = DeviceManager::GetInstance().StartDeviceDiscovery(packageName, subInfo, str, callback);
272 if (ret != DM_OK) {
273 state.SkipWithError("StartDeviceDiscoveryTestCase failed.");
274 }
275 state.PauseTiming();
276 uint16_t subscribeId = DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID;
277 DeviceManager::GetInstance().StopDeviceDiscovery(packageName, subscribeId);
278 }
279 }
280
281 // StopDeviceDiscovery
BENCHMARK_F(DeviceDiscoveryTest,StoptDeviceDiscoveryTestCase)282 BENCHMARK_F(DeviceDiscoveryTest, StoptDeviceDiscoveryTestCase)(
283 benchmark::State &state)
284 {
285 while (state.KeepRunning()) {
286 string packageName = "com.devicemanager";
287 state.PauseTiming();
288 std::shared_ptr<BenchmarkDmInit> callback_ = std::make_shared<BenchmarkDmInit>();
289 DeviceManager::GetInstance().InitDeviceManager(packageName, callback_);
290 DmSubscribeInfo subInfo;
291 subInfo.subscribeId = DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID;
292 subInfo.mode = DM_DISCOVER_MODE_ACTIVE;
293 subInfo.medium = DM_AUTO;
294 subInfo.freq = DM_HIGH;
295 subInfo.isSameAccount = false;
296 subInfo.isWakeRemote = false;
297 strcpy_s(subInfo.capability, DM_MAX_DEVICE_CAPABILITY_LEN, DM_CAPABILITY_OSD);
298 std::shared_ptr<DiscoveryCallback> callback = std::make_shared<DeviceDiscoveryCallbackTest>();
299 std::string str;
300 int32_t ret = DeviceManager::GetInstance().StartDeviceDiscovery(pkgName,
301 subInfo, str, callback);
302 if (ret != DM_OK) {
303 state.SkipWithError("StopDeviceDiscoveryTestCase failed.");
304 }
305 state.ResumeTiming();
306 uint16_t subscribeId = DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID;
307 ret =DeviceManager::GetInstance().StopDeviceDiscovery(packageName, subscribeId);
308 if (ret != DM_OK) {
309 state.SkipWithError("StopDeviceDiscoveryTestCase failed.");
310 }
311 }
312 }
313
314 // RegisterDeviceManagerFaCallback
BENCHMARK_F(RegisterDeviceManagerFaTest,RegisterDeviceManagerFaCallbackTestCase)315 BENCHMARK_F(RegisterDeviceManagerFaTest, RegisterDeviceManagerFaCallbackTestCase)(
316 benchmark::State &state)
317 {
318 while (state.KeepRunning()) {
319 std::shared_ptr<DeviceManagerFaCallbackTest> callback = std::make_shared<DeviceManagerFaCallbackTest>();
320 int32_t ret = DeviceManager::GetInstance().RegisterDeviceManagerFaCallback(packageName, callback);
321 if (ret != DM_OK) {
322 state.SkipWithError("AuthenticateDeviceTestCase failed.");
323 }
324 }
325 }
326
327 // UnRegisterDeviceManagerFaCallback
BENCHMARK_F(UnRegisterDeviceManagerFaTest,UnRegisterDeviceManagerFaCallbackTestCase)328 BENCHMARK_F(UnRegisterDeviceManagerFaTest, UnRegisterDeviceManagerFaCallbackTestCase)(
329 benchmark::State &state)
330 {
331 while (state.KeepRunning()) {
332 state.PauseTiming();
333 std::shared_ptr<DeviceManagerFaCallbackTest> callback = std::make_shared<DeviceManagerFaCallbackTest>();
334 int32_t ret = DeviceManager::GetInstance().RegisterDeviceManagerFaCallback(packageName, callback);
335 if (ret != DM_OK) {
336 state.SkipWithError("AuthenticateDeviceTestCase failed.");
337 }
338 state.ResumeTiming();
339 ret = DeviceManager::GetInstance().UnRegisterDeviceManagerFaCallback(packageName);
340 if (ret != DM_OK) {
341 state.SkipWithError("UnRegisterDeviceManagerFaCallbackTestCase failed.");
342 }
343 }
344 }
345 }
346
347 // Run the benchmark
348 BENCHMARK_MAIN();
349