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