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 = nullptr,
45 .perms = perms,
46 .acls = nullptr,
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
TearDownTestCase(void)80 void BusCenterTest::TearDownTestCase(void) { }
81
OnNodeOnline(NodeBasicInfo * info)82 static void OnNodeOnline(NodeBasicInfo *info)
83 {
84 (void)info;
85 }
86
87 static INodeStateCb g_nodeStateCb = {
88 .events = EVENT_NODE_STATE_ONLINE,
89 .onNodeOnline = OnNodeOnline,
90 };
91
GetPublishId(void)92 static int32_t GetPublishId(void)
93 {
94 g_publishId++;
95 return g_publishId;
96 }
97
98 static PublishInfo g_pInfo = { .publishId = 1,
99 .mode = DISCOVER_MODE_ACTIVE,
100 .medium = COAP,
101 .freq = MID,
102 .capability = "dvKit",
103 .capabilityData = (unsigned char *)CAPABILITY_4,
104 .dataLen = strlen(CAPABILITY_4) };
105
TestPublishResult(int32_t publishId,PublishResult reason)106 static void TestPublishResult(int32_t publishId, PublishResult reason) { }
107
108 static IPublishCb g_publishCb = { .OnPublishResult = TestPublishResult };
109
GetSubscribeId(void)110 static int32_t GetSubscribeId(void)
111 {
112 g_subscribeId++;
113 return g_subscribeId;
114 }
115
116 static SubscribeInfo g_sInfo = { .subscribeId = 1,
117 .mode = DISCOVER_MODE_ACTIVE,
118 .medium = COAP,
119 .freq = MID,
120 .isSameAccount = true,
121 .isWakeRemote = false,
122 .capability = "dvKit",
123 .capabilityData = (unsigned char *)CAPABILITY_3,
124 .dataLen = strlen(CAPABILITY_3) };
125
TestDeviceFound(const DeviceInfo * device)126 static void TestDeviceFound(const DeviceInfo *device) { }
127
TestDiscoverResult(int32_t refreshId,RefreshResult reason)128 static void TestDiscoverResult(int32_t refreshId, RefreshResult reason) { }
129
130 static IRefreshCallback g_refreshCb = { .OnDeviceFound = TestDeviceFound, .OnDiscoverResult = TestDiscoverResult };
131
132 /**
133 * @tc.name: RegNodeDeviceStateCbTestCase
134 * @tc.desc: RegNodeDeviceStateCb Performance Testing
135 * @tc.type: FUNC
136 * @tc.require: RegNodeDeviceStateCb normal operation
137 */
BENCHMARK_F(BusCenterTest,RegNodeDeviceStateCbTestCase)138 BENCHMARK_F(BusCenterTest, RegNodeDeviceStateCbTestCase)(benchmark::State &state)
139 {
140 while (state.KeepRunning()) {
141 state.ResumeTiming();
142 int32_t ret = RegNodeDeviceStateCb(TEST_PKG_NAME, &g_nodeStateCb);
143 if (ret != 0) {
144 state.SkipWithError("RegNodeDeviceStateCbTestCase failed.");
145 }
146 state.PauseTiming();
147 UnregNodeDeviceStateCb(&g_nodeStateCb);
148 }
149 }
150 BENCHMARK_REGISTER_F(BusCenterTest, RegNodeDeviceStateCbTestCase);
151
152 /**
153 * @tc.name: UnregNodeDeviceStateCbTestCase
154 * @tc.desc: UnregNodeDeviceStateCb Performance Testing
155 * @tc.type: FUNC
156 * @tc.require: UnregNodeDeviceStateCb normal operation
157 */
BENCHMARK_F(BusCenterTest,UnregNodeDeviceStateCbTestCase)158 BENCHMARK_F(BusCenterTest, UnregNodeDeviceStateCbTestCase)(benchmark::State &state)
159 {
160 while (state.KeepRunning()) {
161 state.PauseTiming();
162 RegNodeDeviceStateCb(TEST_PKG_NAME, &g_nodeStateCb);
163 state.ResumeTiming();
164 int32_t ret = UnregNodeDeviceStateCb(&g_nodeStateCb);
165 if (ret != 0) {
166 state.SkipWithError("UnregNodeDeviceStateCbTestCase failed.");
167 }
168 }
169 }
170 BENCHMARK_REGISTER_F(BusCenterTest, UnregNodeDeviceStateCbTestCase);
171
172 /**
173 * @tc.name: GetAllNodeDeviceInfoTestCase
174 * @tc.desc: GetAllNodeDeviceInfo Performance Testing
175 * @tc.type: FUNC
176 * @tc.require: GetAllNodeDeviceInfo normal operation
177 */
BENCHMARK_F(BusCenterTest,GetAllNodeDeviceInfoTestCase)178 BENCHMARK_F(BusCenterTest, GetAllNodeDeviceInfoTestCase)(benchmark::State &state)
179 {
180 while (state.KeepRunning()) {
181 NodeBasicInfo *info = nullptr;
182 int32_t infoNum;
183
184 int32_t ret = GetAllNodeDeviceInfo(TEST_PKG_NAME, &info, &infoNum);
185 if (ret != 0) {
186 state.SkipWithError("GetAllNodeDeviceInfoTestCase failed.");
187 }
188 FreeNodeInfo(info);
189 }
190 }
191 BENCHMARK_REGISTER_F(BusCenterTest, GetAllNodeDeviceInfoTestCase);
192
193 /**
194 * @tc.name: GetLocalNodeDeviceInfoTestCase
195 * @tc.desc: GetLocalNodeDeviceInfo Performance Testing
196 * @tc.type: FUNC
197 * @tc.require: GetLocalNodeDeviceInfo normal operation
198 */
BENCHMARK_F(BusCenterTest,GetLocalNodeDeviceInfoTestCase)199 BENCHMARK_F(BusCenterTest, GetLocalNodeDeviceInfoTestCase)(benchmark::State &state)
200 {
201 while (state.KeepRunning()) {
202 int32_t ret;
203 NodeBasicInfo localNode;
204
205 ret = GetLocalNodeDeviceInfo(TEST_PKG_NAME, &localNode);
206 if (ret != 0) {
207 state.SkipWithError("GetLocalNodeDeviceInfoTestCase failed.");
208 }
209 }
210 }
211 BENCHMARK_REGISTER_F(BusCenterTest, GetLocalNodeDeviceInfoTestCase);
212
213 /**
214 * @tc.name: GetNodeKeyInfoTestCase
215 * @tc.desc: GetNodeKeyInfo Performance Testing
216 * @tc.type: FUNC
217 * @tc.require: GetNodeKeyInfo normal operation
218 */
BENCHMARK_F(BusCenterTest,GetNodeKeyInfoTestCase)219 BENCHMARK_F(BusCenterTest, GetNodeKeyInfoTestCase)(benchmark::State &state)
220 {
221 while (state.KeepRunning()) {
222 int32_t ret;
223 NodeBasicInfo info;
224 char udid[UDID_BUF_LEN] = { 0 };
225 (void)memset_s(&info, sizeof(NodeBasicInfo), 0, sizeof(NodeBasicInfo));
226 state.PauseTiming();
227 GetLocalNodeDeviceInfo(TEST_PKG_NAME, &info);
228 state.ResumeTiming();
229 ret = GetNodeKeyInfo(TEST_PKG_NAME, info.networkId, NODE_KEY_UDID, (uint8_t *)udid, UDID_BUF_LEN);
230 if (ret != 0) {
231 state.SkipWithError("GetNodeKeyInfoTestCase failed.");
232 }
233 }
234 }
235 BENCHMARK_REGISTER_F(BusCenterTest, GetNodeKeyInfoTestCase);
236
237 /**
238 * @tc.name: PublishLNNTestCase
239 * @tc.desc: PublishLNN Performance Testing
240 * @tc.type: FUNC
241 * @tc.require: PublishLNN normal operation
242 */
BENCHMARK_F(BusCenterTest,PublishLNNTestCase)243 BENCHMARK_F(BusCenterTest, PublishLNNTestCase)(benchmark::State &state)
244 {
245 while (state.KeepRunning()) {
246 int32_t ret;
247 g_pInfo.publishId = GetPublishId();
248 state.ResumeTiming();
249 ret = PublishLNN(TEST_PKG_NAME, &g_pInfo, &g_publishCb);
250 if (ret != 0) {
251 state.SkipWithError("PublishLNNTestCase failed.");
252 }
253 state.PauseTiming();
254 StopPublishLNN(TEST_PKG_NAME, g_pInfo.publishId);
255 }
256 }
257 BENCHMARK_REGISTER_F(BusCenterTest, PublishLNNTestCase);
258
259 /**
260 * @tc.name: StopPublishLNNTestCase
261 * @tc.desc: StopPublishLNN Performance Testing
262 * @tc.type: FUNC
263 * @tc.require: StopPublishLNN normal operation
264 */
BENCHMARK_F(BusCenterTest,StopPublishLNNTestCase)265 BENCHMARK_F(BusCenterTest, StopPublishLNNTestCase)(benchmark::State &state)
266 {
267 while (state.KeepRunning()) {
268 int32_t ret;
269 g_pInfo.publishId = GetPublishId();
270
271 state.PauseTiming();
272 PublishLNN(TEST_PKG_NAME, &g_pInfo, &g_publishCb);
273 state.ResumeTiming();
274 ret = StopPublishLNN(TEST_PKG_NAME, g_pInfo.publishId);
275 if (ret != 0) {
276 state.SkipWithError("StopPublishLNNTestCase failed.");
277 }
278 }
279 }
280 BENCHMARK_REGISTER_F(BusCenterTest, StopPublishLNNTestCase);
281
282 /**
283 * @tc.name: RefreshLNNTestCase
284 * @tc.desc: RefreshLNN Performance Testing
285 * @tc.type: FUNC
286 * @tc.require: RefreshLNN normal operation
287 */
BENCHMARK_F(BusCenterTest,RefreshLNNTestCase)288 BENCHMARK_F(BusCenterTest, RefreshLNNTestCase)(benchmark::State &state)
289 {
290 while (state.KeepRunning()) {
291 int32_t ret;
292 g_sInfo.subscribeId = GetSubscribeId();
293
294 state.ResumeTiming();
295 ret = RefreshLNN(TEST_PKG_NAME, &g_sInfo, &g_refreshCb);
296 if (ret != 0) {
297 state.SkipWithError("RefreshLNNTestCase failed.");
298 }
299 state.PauseTiming();
300 StopRefreshLNN(TEST_PKG_NAME, g_sInfo.subscribeId);
301 }
302 }
303 BENCHMARK_REGISTER_F(BusCenterTest, RefreshLNNTestCase);
304
305 /**
306 * @tc.name:StopRefreshLNNTestCase
307 * @tc.desc: StopRefreshLNN Performance Testing
308 * @tc.type: FUNC
309 * @tc.require: StopRefreshLNN normal operation
310 */
BENCHMARK_F(BusCenterTest,StopRefreshLNNTestCase)311 BENCHMARK_F(BusCenterTest, StopRefreshLNNTestCase)(benchmark::State &state)
312 {
313 while (state.KeepRunning()) {
314 int32_t ret;
315 g_sInfo.subscribeId = GetSubscribeId();
316
317 state.PauseTiming();
318 RefreshLNN(TEST_PKG_NAME, &g_sInfo, &g_refreshCb);
319 state.ResumeTiming();
320 ret = StopRefreshLNN(TEST_PKG_NAME, g_sInfo.subscribeId);
321 if (ret != 0) {
322 state.SkipWithError("StopRefreshLNNTestCase failed.");
323 }
324 }
325 }
326 BENCHMARK_REGISTER_F(BusCenterTest, StopRefreshLNNTestCase);
327 } // namespace OHOS
328
329 // Run the benchmark
330 BENCHMARK_MAIN();