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 <securec.h>
18 #include "nativetoken_kit.h"
19 #include "softbus_bus_center.h"
20 #include "softbus_common.h"
21 #include "token_setproc.h"
22
23 namespace OHOS {
24 constexpr char TEST_PKG_NAME[] = "com.softbus.test";
25 static int32_t g_subscribeId = 0;
26 static int32_t g_publishId = 0;
27 static bool flag = true;
AddPermission()28 void AddPermission()
29 {
30 if (flag) {
31 uint64_t tokenId;
32 const char *perms[2];
33 perms[0] = OHOS_PERMISSION_DISTRIBUTED_DATASYNC;
34 perms[1] = OHOS_PERMISSION_DISTRIBUTED_SOFTBUS_CENTER;
35 NativeTokenInfoParams infoInstance = {
36 .dcapsNum = 0,
37 .permsNum = 2,
38 .aclsNum = 0,
39 .dcaps = NULL,
40 .perms = perms,
41 .acls = NULL,
42 .processName = "com.softbus.test",
43 .aplStr = "normal",
44 };
45 tokenId = GetAccessTokenId(&infoInstance);
46 SetSelfTokenID(tokenId);
47 flag = false;
48 }
49 }
50
51 class BusCenterTest : public benchmark::Fixture {
52 public:
BusCenterTest()53 BusCenterTest()
54 {
55 Iterations(iterations);
56 Repetitions(repetitions);
57 ReportAggregatesOnly();
58 }
59 ~BusCenterTest() override = default;
60 static void SetUpTestCase(void);
61 static void TearDownTestCase(void);
SetUp(const::benchmark::State & state)62 void SetUp(const ::benchmark::State &state) override
63 {
64 AddPermission();
65 }
66
67 protected:
68 const int32_t repetitions = 3;
69 const int32_t iterations = 1000;
70 };
71
SetUpTestCase(void)72 void BusCenterTest::SetUpTestCase(void)
73 {
74 }
75
TearDownTestCase(void)76 void BusCenterTest::TearDownTestCase(void)
77 {}
78
OnNodeOnline(NodeBasicInfo * info)79 static void OnNodeOnline(NodeBasicInfo *info)
80 {
81 (void)info;
82 }
83
84 static INodeStateCb g_nodeStateCb = {
85 .events = EVENT_NODE_STATE_ONLINE,
86 .onNodeOnline = OnNodeOnline,
87 };
88
GetPublishId(void)89 static int32_t GetPublishId(void)
90 {
91 g_publishId++;
92 return g_publishId;
93 }
94
95 static PublishInfo g_pInfo = {
96 .publishId = 1,
97 .mode = DISCOVER_MODE_ACTIVE,
98 .medium = COAP,
99 .freq = MID,
100 .capability = "dvKit",
101 .capabilityData = (unsigned char *)"capdata4",
102 .dataLen = sizeof("capdata4")
103 };
104
TestPublishResult(int publishId,PublishResult reason)105 static void TestPublishResult(int publishId, PublishResult reason)
106 {}
107
108 static IPublishCb g_publishCb = {
109 .OnPublishResult = TestPublishResult
110 };
111
GetSubscribeId(void)112 static int32_t GetSubscribeId(void)
113 {
114 g_subscribeId++;
115 return g_subscribeId;
116 }
117
118 static SubscribeInfo g_sInfo = {
119 .subscribeId = 1,
120 .mode = DISCOVER_MODE_ACTIVE,
121 .medium = COAP,
122 .freq = MID,
123 .isSameAccount = true,
124 .isWakeRemote = false,
125 .capability = "dvKit",
126 .capabilityData = (unsigned char *)"capdata3",
127 .dataLen = sizeof("capdata3")
128 };
129
TestDeviceFound(const DeviceInfo * device)130 static void TestDeviceFound(const DeviceInfo *device)
131 {}
132
TestDiscoverResult(int32_t refreshId,RefreshResult reason)133 static void TestDiscoverResult(int32_t refreshId, RefreshResult reason)
134 {}
135
136 static IRefreshCallback g_refreshCb = {
137 .OnDeviceFound = TestDeviceFound,
138 .OnDiscoverResult = TestDiscoverResult
139 };
140
141 /**
142 * @tc.name: RegNodeDeviceStateCbTestCase
143 * @tc.desc: RegNodeDeviceStateCb Performance Testing
144 * @tc.type: FUNC
145 * @tc.require: RegNodeDeviceStateCb normal operation
146 */
BENCHMARK_F(BusCenterTest,RegNodeDeviceStateCbTestCase)147 BENCHMARK_F(BusCenterTest, RegNodeDeviceStateCbTestCase)(benchmark::State &state)
148 {
149 while (state.KeepRunning()) {
150 state.ResumeTiming();
151 int ret = RegNodeDeviceStateCb(TEST_PKG_NAME, &g_nodeStateCb);
152 if (ret != 0) {
153 state.SkipWithError("RegNodeDeviceStateCbTestCase failed.");
154 }
155 state.PauseTiming();
156 UnregNodeDeviceStateCb(&g_nodeStateCb);
157 }
158 }
159 BENCHMARK_REGISTER_F(BusCenterTest, RegNodeDeviceStateCbTestCase);
160
161 /**
162 * @tc.name: UnregNodeDeviceStateCbTestCase
163 * @tc.desc: UnregNodeDeviceStateCb Performance Testing
164 * @tc.type: FUNC
165 * @tc.require: UnregNodeDeviceStateCb normal operation
166 */
BENCHMARK_F(BusCenterTest,UnregNodeDeviceStateCbTestCase)167 BENCHMARK_F(BusCenterTest, UnregNodeDeviceStateCbTestCase)(benchmark::State &state)
168 {
169 while (state.KeepRunning()) {
170 state.PauseTiming();
171 RegNodeDeviceStateCb(TEST_PKG_NAME, &g_nodeStateCb);
172 state.ResumeTiming();
173 int ret = UnregNodeDeviceStateCb(&g_nodeStateCb);
174 if (ret != 0) {
175 state.SkipWithError("UnregNodeDeviceStateCbTestCase failed.");
176 }
177 }
178 }
179 BENCHMARK_REGISTER_F(BusCenterTest, UnregNodeDeviceStateCbTestCase);
180
181 /**
182 * @tc.name: GetAllNodeDeviceInfoTestCase
183 * @tc.desc: GetAllNodeDeviceInfo Performance Testing
184 * @tc.type: FUNC
185 * @tc.require: GetAllNodeDeviceInfo normal operation
186 */
BENCHMARK_F(BusCenterTest,GetAllNodeDeviceInfoTestCase)187 BENCHMARK_F(BusCenterTest, GetAllNodeDeviceInfoTestCase)(benchmark::State &state)
188 {
189 while (state.KeepRunning()) {
190 NodeBasicInfo *info = nullptr;
191 int infoNum;
192
193 int ret = GetAllNodeDeviceInfo(TEST_PKG_NAME, &info, &infoNum);
194 if (ret != 0) {
195 state.SkipWithError("GetAllNodeDeviceInfoTestCase failed.");
196 }
197 FreeNodeInfo(info);
198 }
199 }
200 BENCHMARK_REGISTER_F(BusCenterTest, GetAllNodeDeviceInfoTestCase);
201
202 /**
203 * @tc.name: GetLocalNodeDeviceInfoTestCase
204 * @tc.desc: GetLocalNodeDeviceInfo Performance Testing
205 * @tc.type: FUNC
206 * @tc.require: GetLocalNodeDeviceInfo normal operation
207 */
BENCHMARK_F(BusCenterTest,GetLocalNodeDeviceInfoTestCase)208 BENCHMARK_F(BusCenterTest, GetLocalNodeDeviceInfoTestCase)(benchmark::State &state)
209 {
210 while (state.KeepRunning()) {
211 int ret;
212 NodeBasicInfo localNode;
213
214 ret = GetLocalNodeDeviceInfo(TEST_PKG_NAME, &localNode);
215 if (ret != 0) {
216 state.SkipWithError("GetLocalNodeDeviceInfoTestCase failed.");
217 }
218 }
219 }
220 BENCHMARK_REGISTER_F(BusCenterTest, GetLocalNodeDeviceInfoTestCase);
221
222 /**
223 * @tc.name: GetNodeKeyInfoTestCase
224 * @tc.desc: GetNodeKeyInfo Performance Testing
225 * @tc.type: FUNC
226 * @tc.require: GetNodeKeyInfo normal operation
227 */
BENCHMARK_F(BusCenterTest,GetNodeKeyInfoTestCase)228 BENCHMARK_F(BusCenterTest, GetNodeKeyInfoTestCase)(benchmark::State &state)
229 {
230 while (state.KeepRunning()) {
231 int ret;
232 NodeBasicInfo info;
233 char udid[UDID_BUF_LEN] = {0};
234 (void)memset_s(&info, sizeof(NodeBasicInfo), 0, sizeof(NodeBasicInfo));
235 state.PauseTiming();
236 GetLocalNodeDeviceInfo(TEST_PKG_NAME, &info);
237 state.ResumeTiming();
238 ret = GetNodeKeyInfo(TEST_PKG_NAME, info.networkId, NODE_KEY_UDID,
239 (uint8_t *)udid, UDID_BUF_LEN);
240 if (ret != 0) {
241 state.SkipWithError("GetNodeKeyInfoTestCase failed.");
242 }
243 }
244 }
245 BENCHMARK_REGISTER_F(BusCenterTest, GetNodeKeyInfoTestCase);
246
247 /**
248 * @tc.name: PublishLNNTestCase
249 * @tc.desc: PublishLNN Performance Testing
250 * @tc.type: FUNC
251 * @tc.require: PublishLNN normal operation
252 */
BENCHMARK_F(BusCenterTest,PublishLNNTestCase)253 BENCHMARK_F(BusCenterTest, PublishLNNTestCase)(benchmark::State &state)
254 {
255 while (state.KeepRunning()) {
256 int ret;
257 g_pInfo.publishId = GetPublishId();
258 state.ResumeTiming();
259 ret = PublishLNN(TEST_PKG_NAME, &g_pInfo, &g_publishCb);
260 if (ret != 0) {
261 state.SkipWithError("PublishLNNTestCase failed.");
262 }
263 state.PauseTiming();
264 StopPublishLNN(TEST_PKG_NAME, g_pInfo.publishId);
265 }
266 }
267 BENCHMARK_REGISTER_F(BusCenterTest, PublishLNNTestCase);
268
269 /**
270 * @tc.name: StopPublishLNNTestCase
271 * @tc.desc: StopPublishLNN Performance Testing
272 * @tc.type: FUNC
273 * @tc.require: StopPublishLNN normal operation
274 */
BENCHMARK_F(BusCenterTest,StopPublishLNNTestCase)275 BENCHMARK_F(BusCenterTest, StopPublishLNNTestCase)(benchmark::State &state)
276 {
277 while (state.KeepRunning()) {
278 int ret;
279 g_pInfo.publishId = GetPublishId();
280
281 state.PauseTiming();
282 PublishLNN(TEST_PKG_NAME, &g_pInfo, &g_publishCb);
283 state.ResumeTiming();
284 ret = StopPublishLNN(TEST_PKG_NAME, g_pInfo.publishId);
285 if (ret != 0) {
286 state.SkipWithError("StopPublishLNNTestCase failed.");
287 }
288 }
289 }
290 BENCHMARK_REGISTER_F(BusCenterTest, StopPublishLNNTestCase);
291
292 /**
293 * @tc.name: RefreshLNNTestCase
294 * @tc.desc: RefreshLNN Performance Testing
295 * @tc.type: FUNC
296 * @tc.require: RefreshLNN normal operation
297 */
BENCHMARK_F(BusCenterTest,RefreshLNNTestCase)298 BENCHMARK_F(BusCenterTest, RefreshLNNTestCase)(benchmark::State &state)
299 {
300 while (state.KeepRunning()) {
301 int ret;
302 g_sInfo.subscribeId = GetSubscribeId();
303
304 state.ResumeTiming();
305 ret = RefreshLNN(TEST_PKG_NAME, &g_sInfo, &g_refreshCb);
306 if (ret != 0) {
307 state.SkipWithError("RefreshLNNTestCase failed.");
308 }
309 state.PauseTiming();
310 StopRefreshLNN(TEST_PKG_NAME, g_sInfo.subscribeId);
311 }
312 }
313 BENCHMARK_REGISTER_F(BusCenterTest, RefreshLNNTestCase);
314
315 /**
316 * @tc.name:StopRefreshLNNTestCase
317 * @tc.desc: StopRefreshLNN Performance Testing
318 * @tc.type: FUNC
319 * @tc.require: StopRefreshLNN normal operation
320 */
BENCHMARK_F(BusCenterTest,StopRefreshLNNTestCase)321 BENCHMARK_F(BusCenterTest, StopRefreshLNNTestCase)(benchmark::State &state)
322 {
323 while (state.KeepRunning()) {
324 int ret;
325 g_sInfo.subscribeId = GetSubscribeId();
326
327 state.PauseTiming();
328 RefreshLNN(TEST_PKG_NAME, &g_sInfo, &g_refreshCb);
329 state.ResumeTiming();
330 ret = StopRefreshLNN(TEST_PKG_NAME, g_sInfo.subscribeId);
331 if (ret != 0) {
332 state.SkipWithError("StopRefreshLNNTestCase failed.");
333 }
334 }
335 }
336 BENCHMARK_REGISTER_F(BusCenterTest, StopRefreshLNNTestCase);
337 }
338
339 // Run the benchmark
340 BENCHMARK_MAIN();