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