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 #include <benchmark/benchmark.h>
16 #include <cstring>
17 #include <ctime>
18 #include <securec.h>
19 #include <string>
20 #include <unordered_set>
21 #include <unistd.h>
22 #include "nativetoken_kit.h"
23 #include "session.h"
24 #include "softbus_common.h"
25 #include "accesstoken_kit.h"
26 #include "token_setproc.h"
27
28
29 namespace OHOS {
30 const char *g_pkgName = "dms";
31 char g_sessionName[] = "ohos.distributedschedule.dms.test";
32 char g_networkid[] = "ABCDEF00ABCDEF00ABCDEF00ABCDEF00ABCDEF00ABCDEF00ABCDEF00ABCDEF00";
33
34 const char *RECV_ROOT_PATH = "/data/";
35 static bool flag = true;
36
AddPermission()37 static void AddPermission()
38 {
39 if (flag) {
40 uint64_t tokenId;
41 const char *perms[2];
42 perms[0] = OHOS_PERMISSION_DISTRIBUTED_DATASYNC;
43 perms[1] = OHOS_PERMISSION_DISTRIBUTED_SOFTBUS_CENTER;
44 NativeTokenInfoParams infoInstance = {
45 .dcapsNum = 0,
46 .permsNum = 2,
47 .aclsNum = 0,
48 .dcaps = NULL,
49 .perms = perms,
50 .acls = NULL,
51 .processName = "dms",
52 .aplStr = "normal",
53 };
54 tokenId = GetAccessTokenId(&infoInstance);
55 SetSelfTokenID(tokenId);
56 OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
57 flag = false;
58 }
59 }
60
OnSendFileProcess(int sessionId,uint64_t bytesUpload,uint64_t bytesTotal)61 int OnSendFileProcess(int sessionId, uint64_t bytesUpload, uint64_t bytesTotal)
62 {
63 return 0;
64 }
65
OnSendFileFinished(int sessionId,const char * firstFile)66 int OnSendFileFinished(int sessionId, const char *firstFile)
67 {
68 return 0;
69 }
70
OnFileTransError(int sessionId)71 void OnFileTransError(int sessionId)
72 {}
73
74 static IFileSendListener g_fileSendListener = {
75 .OnSendFileProcess = OnSendFileProcess,
76 .OnSendFileFinished = OnSendFileFinished,
77 .OnFileTransError = OnFileTransError,
78 };
79
OnReceiveFileStarted(int sessionId,const char * files,int fileCnt)80 int OnReceiveFileStarted(int sessionId, const char *files, int fileCnt)
81 {
82 return 0;
83 }
84
OnReceiveFileFinished(int sessionId,const char * files,int fileCnt)85 void OnReceiveFileFinished(int sessionId, const char *files, int fileCnt)
86 {}
87
OnReceiveFileProcess(int sessionId,const char * firstFile,uint64_t bytesUpload,uint64_t bytesTotal)88 int OnReceiveFileProcess(int sessionId, const char *firstFile, uint64_t bytesUpload, uint64_t bytesTotal)
89 {
90 return 0;
91 }
92 static const IFileReceiveListener g_fileRecvListener = {
93 .OnReceiveFileStarted = OnReceiveFileStarted,
94 .OnReceiveFileFinished = OnReceiveFileFinished,
95 .OnReceiveFileProcess = OnReceiveFileProcess,
96 .OnFileTransError = OnFileTransError,
97 };
98
99 class TransTest : public benchmark::Fixture {
100 public:
TransTest()101 TransTest()
102 {
103 Iterations(iterations);
104 Repetitions(repetitions);
105 ReportAggregatesOnly();
106 }
107 ~TransTest() override = default;
108 static void SetUpTestCase(void);
109 static void TearDownTestCase(void);
SetUp(const::benchmark::State & state)110 void SetUp(const ::benchmark::State &state) override
111 {
112 AddPermission();
113 }
114
115 protected:
116 const int32_t repetitions = 3;
117 const int32_t iterations = 1000;
118 };
119
SetUpTestCase(void)120 void TransTest::SetUpTestCase(void)
121 {}
122
TearDownTestCase(void)123 void TransTest::TearDownTestCase(void)
124 {}
125
OnSessionOpened(int sessionId,int result)126 static int OnSessionOpened(int sessionId, int result)
127 {
128 return 0;
129 }
130
OnSessionClosed(int sessionId)131 static void OnSessionClosed(int sessionId)
132 {}
133
OnBytesReceived(int sessionId,const void * data,unsigned int len)134 static void OnBytesReceived(int sessionId, const void *data, unsigned int len)
135 {}
136
OnMessageReceived(int sessionId,const void * data,unsigned int len)137 static void OnMessageReceived(int sessionId, const void *data, unsigned int len)
138 {}
139
140 static ISessionListener g_sessionlistener = {
141 .OnSessionOpened = OnSessionOpened,
142 .OnSessionClosed = OnSessionClosed,
143 .OnBytesReceived = OnBytesReceived,
144 .OnMessageReceived = OnMessageReceived,
145 };
146
147
148 /**
149 * @tc.name: CreateSessionServerTestCase
150 * @tc.desc: CreateSessionServer Performance Testing
151 * @tc.type: FUNC
152 * @tc.require: CreateSessionServer normal operation
153 */
BENCHMARK_F(TransTest,CreateSessionServerTestCase)154 BENCHMARK_F(TransTest, CreateSessionServerTestCase)(benchmark::State &state)
155 {
156 while (state.KeepRunning()) {
157 int ret = CreateSessionServer(g_pkgName, g_sessionName, &g_sessionlistener);
158 if (ret != 0) {
159 state.SkipWithError("CreateSessionServerTestCase failed.");
160 }
161 state.PauseTiming();
162 RemoveSessionServer(g_pkgName, g_sessionName);
163 state.ResumeTiming();
164 }
165 }
166 BENCHMARK_REGISTER_F(TransTest, CreateSessionServerTestCase);
167
168 /**
169 * @tc.name:RemoveSessionServerTestCase
170 * @tc.desc: RemoveSessionServer Performance Testing
171 * @tc.type: FUNC
172 * @tc.require: RemoveSessionServer normal operation
173 */
BENCHMARK_F(TransTest,RemoveSessionServerTestCase)174 BENCHMARK_F(TransTest, RemoveSessionServerTestCase)(benchmark::State &state)
175 {
176 while (state.KeepRunning()) {
177 state.PauseTiming();
178 int ret = CreateSessionServer(g_pkgName, g_sessionName, &g_sessionlistener);
179 state.ResumeTiming();
180 ret = RemoveSessionServer(g_pkgName, g_sessionName);
181 if (ret != 0) {
182 state.SkipWithError("RemoveSessionServerTestCase failed.");
183 }
184 }
185 }
186 BENCHMARK_REGISTER_F(TransTest, RemoveSessionServerTestCase);
187
188 /**
189 * @tc.name: SetFileReceiveListenerTestCase
190 * @tc.desc: SetFileReceiveListener Performance Testing
191 * @tc.type: FUNC
192 * @tc.require: SetFileReceiveListener normal operation
193 */
BENCHMARK_F(TransTest,SetFileReceiveListenerTestCase)194 BENCHMARK_F(TransTest, SetFileReceiveListenerTestCase)(benchmark::State &state)
195 {
196 while (state.KeepRunning()) {
197 state.PauseTiming();
198 int ret = CreateSessionServer(g_pkgName, g_sessionName, &g_sessionlistener);
199 state.ResumeTiming();
200 ret = SetFileReceiveListener(g_pkgName, g_sessionName, &g_fileRecvListener, RECV_ROOT_PATH);
201 if (ret != 0) {
202 state.SkipWithError("SetFileReceiveListenerTestCase failed");
203 }
204 state.PauseTiming();
205 RemoveSessionServer(g_pkgName, g_sessionName);
206 }
207 }
208 BENCHMARK_REGISTER_F(TransTest, SetFileReceiveListenerTestCase);
209
210 /**
211 * @tc.name: SetFileSendListenerTestCase
212 * @tc.desc: SetFileSendListener Performance Testing
213 * @tc.type: FUNC
214 * @tc.require: SetFileSendListener normal operation
215 */
BENCHMARK_F(TransTest,SetFileSendListenerTestCase)216 BENCHMARK_F(TransTest, SetFileSendListenerTestCase)(benchmark::State &state)
217 {
218 while (state.KeepRunning()) {
219 state.PauseTiming();
220 int ret = CreateSessionServer(g_pkgName, g_sessionName, &g_sessionlistener);
221 state.ResumeTiming();
222 ret = SetFileSendListener(g_pkgName, g_sessionName, &g_fileSendListener);
223 if (ret != 0) {
224 state.SkipWithError("SetFileSendListenerTestCase failed");
225 }
226 state.PauseTiming();
227 RemoveSessionServer(g_pkgName, g_sessionName);
228 }
229 }
230 BENCHMARK_REGISTER_F(TransTest, SetFileSendListenerTestCase);
231 } // namespace OHOS
232
233 // Run the benchmark
234 BENCHMARK_MAIN();