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