• 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 "accesstoken_kit.h"
18 #include "discovery_service.h"
19 #include "nativetoken_kit.h"
20 #include "token_setproc.h"
21 
22 namespace OHOS {
23 static int g_subscribeId = 0;
24 static int g_publishId = 0;
25 static const char *g_pkgName = "Softbus_Kits";
26 static bool g_flag = true;
AddPermission()27 void AddPermission()
28 {
29     if (g_flag) {
30         uint64_t tokenId;
31         const char *perms[2];
32         perms[0] = OHOS_PERMISSION_DISTRIBUTED_DATASYNC;
33         perms[1] = OHOS_PERMISSION_DISTRIBUTED_SOFTBUS_CENTER;
34         NativeTokenInfoParams infoInstance = {
35             .dcapsNum = 0,
36             .permsNum = 2,
37             .aclsNum = 0,
38             .dcaps = NULL,
39             .perms = perms,
40             .acls = NULL,
41             .processName = "Softbus_Kits",
42             .aplStr = "normal",
43         };
44         tokenId = GetAccessTokenId(&infoInstance);
45         SetSelfTokenID(tokenId);
46         OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
47         g_flag = false;
48     }
49 }
50 
51 class DiscoveryTest : public benchmark::Fixture {
52 public:
DiscoveryTest()53     DiscoveryTest()
54     {
55         Iterations(iterations);
56         Repetitions(repetitions);
57         ReportAggregatesOnly();
58     }
59     ~DiscoveryTest() 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     }
TearDown(const::benchmark::State & state)66     void TearDown(const ::benchmark::State &state) override
67     {}
68 
69 protected:
70     const int32_t repetitions = 3;
71     const int32_t iterations = 1000;
72 };
73 
SetUpTestCase(void)74 void DiscoveryTest::SetUpTestCase(void)
75 {}
76 
TearDownTestCase(void)77 void DiscoveryTest::TearDownTestCase(void)
78 {}
79 
GetSubscribeId(void)80 static int GetSubscribeId(void)
81 {
82     g_subscribeId++;
83     return g_subscribeId;
84 }
85 
GetPublishId(void)86 static int GetPublishId(void)
87 {
88     g_publishId++;
89     return g_publishId;
90 }
91 
92 static SubscribeInfo g_sInfo = {
93     .subscribeId = 1,
94     .mode = DISCOVER_MODE_ACTIVE,
95     .medium = COAP,
96     .freq = MID,
97     .isSameAccount = true,
98     .isWakeRemote = false,
99     .capability = "dvKit",
100     .capabilityData = (unsigned char *)"capdata3",
101     .dataLen = strlen("capdata3")
102 };
103 
104 static PublishInfo g_pInfo = {
105     .publishId = 1,
106     .mode = DISCOVER_MODE_ACTIVE,
107     .medium = COAP,
108     .freq = MID,
109     .capability = "dvKit",
110     .capabilityData = (unsigned char *)"capdata4",
111     .dataLen = strlen("capdata4")
112 };
113 
TestDeviceFound(const DeviceInfo * device)114 static void TestDeviceFound(const DeviceInfo *device)
115 {}
116 
TestDiscoverFailed(int subscribeId,DiscoveryFailReason failReason)117 static void TestDiscoverFailed(int subscribeId, DiscoveryFailReason failReason)
118 {}
119 
TestDiscoverySuccess(int subscribeId)120 static void TestDiscoverySuccess(int subscribeId)
121 {}
122 
TestPublishSuccess(int publishId)123 static void TestPublishSuccess(int publishId)
124 {}
125 
TestPublishFail(int publishId,PublishFailReason reason)126 static void TestPublishFail(int publishId, PublishFailReason reason)
127 {}
128 
129 static IDiscoveryCallback g_subscribeCb = {
130     .OnDeviceFound = TestDeviceFound,
131     .OnDiscoverFailed = TestDiscoverFailed,
132     .OnDiscoverySuccess = TestDiscoverySuccess
133 };
134 
135 static IPublishCallback g_publishCb = {
136     .OnPublishSuccess = TestPublishSuccess,
137     .OnPublishFail = TestPublishFail
138 };
139 
140 /**
141  * @tc.name: PublishServiceTestCase
142  * @tc.desc: PublishService Performance Testing
143  * @tc.type: FUNC
144  * @tc.require: PublishService normal operation
145  */
BENCHMARK_F(DiscoveryTest,PublishServiceTestCase)146 BENCHMARK_F(DiscoveryTest, PublishServiceTestCase)(benchmark::State &state)
147 {
148     while (state.KeepRunning()) {
149         g_pInfo.publishId = GetPublishId();
150         state.ResumeTiming();
151         int ret = PublishService(g_pkgName, &g_pInfo, &g_publishCb);
152         if (ret != 0) {
153             state.SkipWithError("PublishServiceTestCase failed.");
154         }
155         state.PauseTiming();
156         ret = UnPublishService(g_pkgName, g_pInfo.publishId);
157     }
158 }
159 BENCHMARK_REGISTER_F(DiscoveryTest, PublishServiceTestCase);
160 
161 /**
162  * @tc.name: UnPublishServiceTestCase
163  * @tc.desc: UnPublishService Performance Testing
164  * @tc.type: FUNC
165  * @tc.require: UnPublishService normal operation
166  */
BENCHMARK_F(DiscoveryTest,UnPublishServiceTestCase)167 BENCHMARK_F(DiscoveryTest, UnPublishServiceTestCase)(benchmark::State &state)
168 {
169     while (state.KeepRunning()) {
170         g_pInfo.publishId = GetPublishId();
171         state.PauseTiming();
172         int ret = PublishService(g_pkgName, &g_pInfo, &g_publishCb);
173         if (ret != 0) {
174             state.SkipWithError("UnPublishServiceTestCase failed.");
175         }
176         state.ResumeTiming();
177         ret = UnPublishService(g_pkgName, g_pInfo.publishId);
178         if (ret != 0) {
179             state.SkipWithError("UnPublishServiceTestCase failed.");
180         }
181     }
182 }
183 BENCHMARK_REGISTER_F(DiscoveryTest, UnPublishServiceTestCase);
184 
185 /**
186  * @tc.name: StartDiscoveryTestCase
187  * @tc.desc: StartDiscovery Performance Testing
188  * @tc.type: FUNC
189  * @tc.require: StartDiscovery normal operation
190  */
BENCHMARK_F(DiscoveryTest,StartDiscoveryTestCase)191 BENCHMARK_F(DiscoveryTest, StartDiscoveryTestCase)(benchmark::State &state)
192 {
193     while (state.KeepRunning()) {
194         g_sInfo.subscribeId = GetSubscribeId();
195         state.ResumeTiming();
196         int ret = StartDiscovery(g_pkgName, &g_sInfo, &g_subscribeCb);
197         if (ret != 0) {
198             state.SkipWithError("StartDiscoveryTestCase failed.");
199         }
200         state.PauseTiming();
201         ret = StopDiscovery(g_pkgName, g_sInfo.subscribeId);
202     }
203 }
204 BENCHMARK_REGISTER_F(DiscoveryTest, StartDiscoveryTestCase);
205 
206 /**
207  * @tc.name: StopDiscoveryTestCase
208  * @tc.desc: StoptDiscovery Performance Testing
209  * @tc.type: FUNC
210  * @tc.require: StoptDiscovery normal operation
211  */
BENCHMARK_F(DiscoveryTest,StoptDiscoveryTestCase)212 BENCHMARK_F(DiscoveryTest, StoptDiscoveryTestCase)(benchmark::State &state)
213 {
214     while (state.KeepRunning()) {
215         g_sInfo.subscribeId = GetSubscribeId();
216         state.PauseTiming();
217         int ret = StartDiscovery(g_pkgName, &g_sInfo, &g_subscribeCb);
218         if (ret != 0) {
219             state.SkipWithError("StoptDiscoveryTestCase failed.");
220         }
221         state.ResumeTiming();
222         ret = StopDiscovery(g_pkgName, g_sInfo.subscribeId);
223         if (ret != 0) {
224             state.SkipWithError("StoptDiscoveryTestCase failed.");
225         }
226     }
227 }
228 BENCHMARK_REGISTER_F(DiscoveryTest, StoptDiscoveryTestCase);
229 }
230 
231 // Run the benchmark
232 BENCHMARK_MAIN();