• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 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 "accesstoken_kit.h"
18 #include "discovery_service.h"
19 #include "nativetoken_kit.h"
20 #include "softbus_bus_center.h"
21 #include "token_setproc.h"
22 
23 namespace OHOS {
24 static int g_subscribeId = 0;
25 static int g_publishId = 0;
26 static const char *g_pkgName = "Softbus_Kits";
27 static bool g_flag = true;
AddPermission()28 void AddPermission()
29 {
30     if (g_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 = "Softbus_Kits",
43             .aplStr = "normal",
44         };
45         tokenId = GetAccessTokenId(&infoInstance);
46         SetSelfTokenID(tokenId);
47         OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
48         g_flag = false;
49     }
50 }
51 
52 class DiscoveryTest : public benchmark::Fixture {
53 public:
DiscoveryTest()54     DiscoveryTest()
55     {
56         Iterations(iterations);
57         Repetitions(repetitions);
58         ReportAggregatesOnly();
59     }
60     ~DiscoveryTest() override = default;
61     static void SetUpTestCase(void);
62     static void TearDownTestCase(void);
SetUp(const::benchmark::State & state)63     void SetUp(const ::benchmark::State &state) override
64     {
65         AddPermission();
66     }
TearDown(const::benchmark::State & state)67     void TearDown(const ::benchmark::State &state) override
68     {}
69 
70 protected:
71     const int32_t repetitions = 3;
72     const int32_t iterations = 1000;
73 };
74 
SetUpTestCase(void)75 void DiscoveryTest::SetUpTestCase(void)
76 {}
77 
TearDownTestCase(void)78 void DiscoveryTest::TearDownTestCase(void)
79 {}
80 
GetSubscribeId(void)81 static int GetSubscribeId(void)
82 {
83     g_subscribeId++;
84     return g_subscribeId;
85 }
86 
GetPublishId(void)87 static int GetPublishId(void)
88 {
89     g_publishId++;
90     return g_publishId;
91 }
92 
93 static SubscribeInfo g_sInfo = {
94     .subscribeId = 1,
95     .mode = DISCOVER_MODE_ACTIVE,
96     .medium = COAP,
97     .freq = MID,
98     .isSameAccount = true,
99     .isWakeRemote = false,
100     .capability = "dvKit",
101     .capabilityData = (unsigned char *)"capdata3",
102     .dataLen = strlen("capdata3")
103 };
104 
105 static PublishInfo g_pInfo = {
106     .publishId = 1,
107     .mode = DISCOVER_MODE_ACTIVE,
108     .medium = COAP,
109     .freq = MID,
110     .capability = "dvKit",
111     .capabilityData = (unsigned char *)"capdata4",
112     .dataLen = strlen("capdata4")
113 };
114 
TestDeviceFound(const DeviceInfo * device)115 static void TestDeviceFound(const DeviceInfo *device)
116 {}
117 
TestDiscoverResult(int32_t refreshId,RefreshResult reason)118 static void TestDiscoverResult(int32_t refreshId, RefreshResult reason)
119 {}
120 
TestPublishResult(int publishId,PublishResult reason)121 static void TestPublishResult(int publishId, PublishResult reason)
122 {}
123 
124 static IRefreshCallback g_refreshCb = {
125     .OnDeviceFound = TestDeviceFound,
126     .OnDiscoverResult = TestDiscoverResult
127 };
128 
129 static IPublishCb g_publishCb = {
130     .OnPublishResult = TestPublishResult
131 };
132 
133 /**
134  * @tc.name: PublishLNNTestCase
135  * @tc.desc: PublishService Performance Testing
136  * @tc.type: FUNC
137  * @tc.require: PublishService normal operation
138  */
BENCHMARK_F(DiscoveryTest,PublishLNNTestCase)139 BENCHMARK_F(DiscoveryTest, PublishLNNTestCase)(benchmark::State &state)
140 {
141     while (state.KeepRunning()) {
142         g_pInfo.publishId = GetPublishId();
143         state.ResumeTiming();
144         int ret = PublishLNN(g_pkgName, &g_pInfo, &g_publishCb);
145         if (ret != 0) {
146             state.SkipWithError("PublishLNNTestCase failed.");
147         }
148         state.PauseTiming();
149         ret = StopPublishLNN(g_pkgName, g_pInfo.publishId);
150         if (ret != 0) {
151             state.SkipWithError("StopPublishLNNTestCase failed.");
152         }
153     }
154 }
155 BENCHMARK_REGISTER_F(DiscoveryTest, PublishLNNTestCase);
156 
157 /**
158  * @tc.name: StopPublishLNNTestCase
159  * @tc.desc: UnPublishService Performance Testing
160  * @tc.type: FUNC
161  * @tc.require: UnPublishService normal operation
162  */
BENCHMARK_F(DiscoveryTest,StopPublishLNNTestCase)163 BENCHMARK_F(DiscoveryTest, StopPublishLNNTestCase)(benchmark::State &state)
164 {
165     while (state.KeepRunning()) {
166         g_pInfo.publishId = GetPublishId();
167         state.PauseTiming();
168         int ret = PublishLNN(g_pkgName, &g_pInfo, &g_publishCb);
169         if (ret != 0) {
170             state.SkipWithError("PublishLNNTestCase failed.");
171         }
172         state.ResumeTiming();
173         ret = StopPublishLNN(g_pkgName, g_pInfo.publishId);
174         if (ret != 0) {
175             state.SkipWithError("StopPublishLNNTestCase failed.");
176         }
177     }
178 }
179 BENCHMARK_REGISTER_F(DiscoveryTest, StopPublishLNNTestCase);
180 
181 /**
182  * @tc.name: RefreshLNNTestCase
183  * @tc.desc: StartDiscovery Performance Testing
184  * @tc.type: FUNC
185  * @tc.require: StartDiscovery normal operation
186  */
BENCHMARK_F(DiscoveryTest,RefreshLNNTestCase)187 BENCHMARK_F(DiscoveryTest, RefreshLNNTestCase)(benchmark::State &state)
188 {
189     while (state.KeepRunning()) {
190         g_sInfo.subscribeId = GetSubscribeId();
191         state.ResumeTiming();
192         int ret = RefreshLNN(g_pkgName, &g_sInfo, &g_refreshCb);
193         if (ret != 0) {
194             state.SkipWithError("RefreshLNNTestCase failed.");
195         }
196         state.PauseTiming();
197         ret = StopRefreshLNN(g_pkgName, g_sInfo.subscribeId);
198         if (ret != 0) {
199             state.SkipWithError("StoptRefreshLNNTestCase failed.");
200         }
201     }
202 }
203 BENCHMARK_REGISTER_F(DiscoveryTest, RefreshLNNTestCase);
204 
205 /**
206  * @tc.name: StoptRefreshLNNTestCase
207  * @tc.desc: StoptDiscovery Performance Testing
208  * @tc.type: FUNC
209  * @tc.require: StoptDiscovery normal operation
210  */
BENCHMARK_F(DiscoveryTest,StoptRefreshLNNTestCase)211 BENCHMARK_F(DiscoveryTest, StoptRefreshLNNTestCase)(benchmark::State &state)
212 {
213     while (state.KeepRunning()) {
214         g_sInfo.subscribeId = GetSubscribeId();
215         state.PauseTiming();
216         int ret = RefreshLNN(g_pkgName, &g_sInfo, &g_refreshCb);
217         if (ret != 0) {
218             state.SkipWithError("RefreshLNNTestCase failed.");
219         }
220         state.ResumeTiming();
221         ret = StopRefreshLNN(g_pkgName, g_sInfo.subscribeId);
222         if (ret != 0) {
223             state.SkipWithError("StoptRefreshLNNTestCase failed.");
224         }
225     }
226 }
227 BENCHMARK_REGISTER_F(DiscoveryTest, StoptRefreshLNNTestCase);
228 }
229 
230 // Run the benchmark
231 BENCHMARK_MAIN();