1060ff233Sopenharmony_ci/*
2060ff233Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3060ff233Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4060ff233Sopenharmony_ci * you may not use this file except in compliance with the License.
5060ff233Sopenharmony_ci * You may obtain a copy of the License at
6060ff233Sopenharmony_ci *
7060ff233Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8060ff233Sopenharmony_ci *
9060ff233Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10060ff233Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11060ff233Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12060ff233Sopenharmony_ci * See the License for the specific language governing permissions and
13060ff233Sopenharmony_ci * limitations under the License.
14060ff233Sopenharmony_ci */
15060ff233Sopenharmony_ci#include <benchmark/benchmark.h>
16060ff233Sopenharmony_ci#include <cstring>
17060ff233Sopenharmony_ci#include <ctime>
18060ff233Sopenharmony_ci#include <securec.h>
19060ff233Sopenharmony_ci#include <string>
20060ff233Sopenharmony_ci#include <unordered_set>
21060ff233Sopenharmony_ci#include <unistd.h>
22060ff233Sopenharmony_ci#include "nativetoken_kit.h"
23060ff233Sopenharmony_ci#include "session.h"
24060ff233Sopenharmony_ci#include "softbus_common.h"
25060ff233Sopenharmony_ci#include "accesstoken_kit.h"
26060ff233Sopenharmony_ci#include "token_setproc.h"
27060ff233Sopenharmony_ci
28060ff233Sopenharmony_ci
29060ff233Sopenharmony_cinamespace OHOS {
30060ff233Sopenharmony_ciconst char *g_pkgName = "dms";
31060ff233Sopenharmony_cichar g_sessionName[] = "ohos.distributedschedule.dms.test";
32060ff233Sopenharmony_cichar g_networkid[] = "ABCDEF00ABCDEF00ABCDEF00ABCDEF00ABCDEF00ABCDEF00ABCDEF00ABCDEF00";
33060ff233Sopenharmony_ci
34060ff233Sopenharmony_ciconst char *RECV_ROOT_PATH = "/data/";
35060ff233Sopenharmony_cistatic bool flag = true;
36060ff233Sopenharmony_ci
37060ff233Sopenharmony_cistatic void AddPermission()
38060ff233Sopenharmony_ci{
39060ff233Sopenharmony_ci    if (flag) {
40060ff233Sopenharmony_ci        uint64_t tokenId;
41060ff233Sopenharmony_ci        const char *perms[2];
42060ff233Sopenharmony_ci        perms[0] = OHOS_PERMISSION_DISTRIBUTED_DATASYNC;
43060ff233Sopenharmony_ci        perms[1] = OHOS_PERMISSION_DISTRIBUTED_SOFTBUS_CENTER;
44060ff233Sopenharmony_ci        NativeTokenInfoParams infoInstance = {
45060ff233Sopenharmony_ci            .dcapsNum = 0,
46060ff233Sopenharmony_ci            .permsNum = 2,
47060ff233Sopenharmony_ci            .aclsNum = 0,
48060ff233Sopenharmony_ci            .dcaps = NULL,
49060ff233Sopenharmony_ci            .perms = perms,
50060ff233Sopenharmony_ci            .acls = NULL,
51060ff233Sopenharmony_ci            .processName = "dms",
52060ff233Sopenharmony_ci            .aplStr = "normal",
53060ff233Sopenharmony_ci        };
54060ff233Sopenharmony_ci        tokenId = GetAccessTokenId(&infoInstance);
55060ff233Sopenharmony_ci        SetSelfTokenID(tokenId);
56060ff233Sopenharmony_ci        OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
57060ff233Sopenharmony_ci        flag = false;
58060ff233Sopenharmony_ci    }
59060ff233Sopenharmony_ci}
60060ff233Sopenharmony_ci
61060ff233Sopenharmony_ciint32_t OnSendFileProcess(int32_t sessionId, uint64_t bytesUpload, uint64_t bytesTotal)
62060ff233Sopenharmony_ci{
63060ff233Sopenharmony_ci    return 0;
64060ff233Sopenharmony_ci}
65060ff233Sopenharmony_ci
66060ff233Sopenharmony_ciint32_t OnSendFileFinished(int32_t sessionId, const char *firstFile)
67060ff233Sopenharmony_ci{
68060ff233Sopenharmony_ci    return 0;
69060ff233Sopenharmony_ci}
70060ff233Sopenharmony_ci
71060ff233Sopenharmony_civoid OnFileTransError(int32_t sessionId)
72060ff233Sopenharmony_ci{}
73060ff233Sopenharmony_ci
74060ff233Sopenharmony_cistatic IFileSendListener g_fileSendListener = {
75060ff233Sopenharmony_ci    .OnSendFileProcess = OnSendFileProcess,
76060ff233Sopenharmony_ci    .OnSendFileFinished = OnSendFileFinished,
77060ff233Sopenharmony_ci    .OnFileTransError = OnFileTransError,
78060ff233Sopenharmony_ci};
79060ff233Sopenharmony_ci
80060ff233Sopenharmony_ciint32_t OnReceiveFileStarted(int32_t sessionId, const char *files, int32_t fileCnt)
81060ff233Sopenharmony_ci{
82060ff233Sopenharmony_ci    return 0;
83060ff233Sopenharmony_ci}
84060ff233Sopenharmony_ci
85060ff233Sopenharmony_civoid OnReceiveFileFinished(int32_t sessionId, const char *files, int32_t fileCnt)
86060ff233Sopenharmony_ci{}
87060ff233Sopenharmony_ci
88060ff233Sopenharmony_ciint32_t OnReceiveFileProcess(int32_t sessionId, const char *firstFile, uint64_t bytesUpload, uint64_t bytesTotal)
89060ff233Sopenharmony_ci{
90060ff233Sopenharmony_ci    return 0;
91060ff233Sopenharmony_ci}
92060ff233Sopenharmony_cistatic const IFileReceiveListener g_fileRecvListener = {
93060ff233Sopenharmony_ci    .OnReceiveFileStarted = OnReceiveFileStarted,
94060ff233Sopenharmony_ci    .OnReceiveFileFinished = OnReceiveFileFinished,
95060ff233Sopenharmony_ci    .OnReceiveFileProcess = OnReceiveFileProcess,
96060ff233Sopenharmony_ci    .OnFileTransError = OnFileTransError,
97060ff233Sopenharmony_ci};
98060ff233Sopenharmony_ci
99060ff233Sopenharmony_ciclass TransTest : public benchmark::Fixture {
100060ff233Sopenharmony_cipublic:
101060ff233Sopenharmony_ci    TransTest()
102060ff233Sopenharmony_ci    {
103060ff233Sopenharmony_ci        Iterations(iterations);
104060ff233Sopenharmony_ci        Repetitions(repetitions);
105060ff233Sopenharmony_ci        ReportAggregatesOnly();
106060ff233Sopenharmony_ci    }
107060ff233Sopenharmony_ci    ~TransTest() override = default;
108060ff233Sopenharmony_ci    static void SetUpTestCase(void);
109060ff233Sopenharmony_ci    static void TearDownTestCase(void);
110060ff233Sopenharmony_ci    void SetUp(const ::benchmark::State &state) override
111060ff233Sopenharmony_ci    {
112060ff233Sopenharmony_ci        AddPermission();
113060ff233Sopenharmony_ci    }
114060ff233Sopenharmony_ci
115060ff233Sopenharmony_ciprotected:
116060ff233Sopenharmony_ci    const int32_t repetitions = 3;
117060ff233Sopenharmony_ci    const int32_t iterations = 1000;
118060ff233Sopenharmony_ci};
119060ff233Sopenharmony_ci
120060ff233Sopenharmony_civoid TransTest::SetUpTestCase(void)
121060ff233Sopenharmony_ci{}
122060ff233Sopenharmony_ci
123060ff233Sopenharmony_civoid TransTest::TearDownTestCase(void)
124060ff233Sopenharmony_ci{}
125060ff233Sopenharmony_ci
126060ff233Sopenharmony_cistatic int32_t OnSessionOpened(int32_t sessionId, int32_t result)
127060ff233Sopenharmony_ci{
128060ff233Sopenharmony_ci    return 0;
129060ff233Sopenharmony_ci}
130060ff233Sopenharmony_ci
131060ff233Sopenharmony_cistatic void OnSessionClosed(int32_t sessionId)
132060ff233Sopenharmony_ci{}
133060ff233Sopenharmony_ci
134060ff233Sopenharmony_cistatic void OnBytesReceived(int32_t sessionId, const void *data, unsigned int len)
135060ff233Sopenharmony_ci{}
136060ff233Sopenharmony_ci
137060ff233Sopenharmony_cistatic void OnMessageReceived(int32_t sessionId, const void *data, unsigned int len)
138060ff233Sopenharmony_ci{}
139060ff233Sopenharmony_ci
140060ff233Sopenharmony_cistatic ISessionListener g_sessionlistener = {
141060ff233Sopenharmony_ci    .OnSessionOpened = OnSessionOpened,
142060ff233Sopenharmony_ci    .OnSessionClosed = OnSessionClosed,
143060ff233Sopenharmony_ci    .OnBytesReceived = OnBytesReceived,
144060ff233Sopenharmony_ci    .OnMessageReceived = OnMessageReceived,
145060ff233Sopenharmony_ci};
146060ff233Sopenharmony_ci
147060ff233Sopenharmony_ci
148060ff233Sopenharmony_ci/**
149060ff233Sopenharmony_ci * @tc.name: CreateSessionServerTestCase
150060ff233Sopenharmony_ci * @tc.desc: CreateSessionServer Performance Testing
151060ff233Sopenharmony_ci * @tc.type: FUNC
152060ff233Sopenharmony_ci * @tc.require: CreateSessionServer normal operation
153060ff233Sopenharmony_ci */
154060ff233Sopenharmony_ciBENCHMARK_F(TransTest, CreateSessionServerTestCase)(benchmark::State &state)
155060ff233Sopenharmony_ci{
156060ff233Sopenharmony_ci    while (state.KeepRunning()) {
157060ff233Sopenharmony_ci        int32_t ret = CreateSessionServer(g_pkgName, g_sessionName, &g_sessionlistener);
158060ff233Sopenharmony_ci        if (ret != 0) {
159060ff233Sopenharmony_ci            state.SkipWithError("CreateSessionServerTestCase failed.");
160060ff233Sopenharmony_ci        }
161060ff233Sopenharmony_ci        state.PauseTiming();
162060ff233Sopenharmony_ci        RemoveSessionServer(g_pkgName, g_sessionName);
163060ff233Sopenharmony_ci        state.ResumeTiming();
164060ff233Sopenharmony_ci    }
165060ff233Sopenharmony_ci}
166060ff233Sopenharmony_ciBENCHMARK_REGISTER_F(TransTest, CreateSessionServerTestCase);
167060ff233Sopenharmony_ci
168060ff233Sopenharmony_ci/**
169060ff233Sopenharmony_ci * @tc.name:RemoveSessionServerTestCase
170060ff233Sopenharmony_ci * @tc.desc: RemoveSessionServer Performance Testing
171060ff233Sopenharmony_ci * @tc.type: FUNC
172060ff233Sopenharmony_ci * @tc.require: RemoveSessionServer normal operation
173060ff233Sopenharmony_ci */
174060ff233Sopenharmony_ciBENCHMARK_F(TransTest, RemoveSessionServerTestCase)(benchmark::State &state)
175060ff233Sopenharmony_ci{
176060ff233Sopenharmony_ci    while (state.KeepRunning()) {
177060ff233Sopenharmony_ci        state.PauseTiming();
178060ff233Sopenharmony_ci        int32_t ret = CreateSessionServer(g_pkgName, g_sessionName, &g_sessionlistener);
179060ff233Sopenharmony_ci        state.ResumeTiming();
180060ff233Sopenharmony_ci        ret = RemoveSessionServer(g_pkgName, g_sessionName);
181060ff233Sopenharmony_ci        if (ret != 0) {
182060ff233Sopenharmony_ci            state.SkipWithError("RemoveSessionServerTestCase failed.");
183060ff233Sopenharmony_ci        }
184060ff233Sopenharmony_ci    }
185060ff233Sopenharmony_ci}
186060ff233Sopenharmony_ciBENCHMARK_REGISTER_F(TransTest, RemoveSessionServerTestCase);
187060ff233Sopenharmony_ci
188060ff233Sopenharmony_ci/**
189060ff233Sopenharmony_ci * @tc.name: SetFileReceiveListenerTestCase
190060ff233Sopenharmony_ci * @tc.desc: SetFileReceiveListener Performance Testing
191060ff233Sopenharmony_ci * @tc.type: FUNC
192060ff233Sopenharmony_ci * @tc.require: SetFileReceiveListener normal operation
193060ff233Sopenharmony_ci */
194060ff233Sopenharmony_ciBENCHMARK_F(TransTest, SetFileReceiveListenerTestCase)(benchmark::State &state)
195060ff233Sopenharmony_ci{
196060ff233Sopenharmony_ci    while (state.KeepRunning()) {
197060ff233Sopenharmony_ci        state.PauseTiming();
198060ff233Sopenharmony_ci        int32_t ret = CreateSessionServer(g_pkgName, g_sessionName, &g_sessionlistener);
199060ff233Sopenharmony_ci        state.ResumeTiming();
200060ff233Sopenharmony_ci        ret = SetFileReceiveListener(g_pkgName, g_sessionName,  &g_fileRecvListener, RECV_ROOT_PATH);
201060ff233Sopenharmony_ci        if (ret != 0) {
202060ff233Sopenharmony_ci            state.SkipWithError("SetFileReceiveListenerTestCase failed");
203060ff233Sopenharmony_ci        }
204060ff233Sopenharmony_ci        state.PauseTiming();
205060ff233Sopenharmony_ci        RemoveSessionServer(g_pkgName, g_sessionName);
206060ff233Sopenharmony_ci    }
207060ff233Sopenharmony_ci}
208060ff233Sopenharmony_ciBENCHMARK_REGISTER_F(TransTest, SetFileReceiveListenerTestCase);
209060ff233Sopenharmony_ci
210060ff233Sopenharmony_ci/**
211060ff233Sopenharmony_ci * @tc.name: SetFileSendListenerTestCase
212060ff233Sopenharmony_ci * @tc.desc: SetFileSendListener Performance Testing
213060ff233Sopenharmony_ci * @tc.type: FUNC
214060ff233Sopenharmony_ci * @tc.require: SetFileSendListener normal operation
215060ff233Sopenharmony_ci */
216060ff233Sopenharmony_ciBENCHMARK_F(TransTest, SetFileSendListenerTestCase)(benchmark::State &state)
217060ff233Sopenharmony_ci{
218060ff233Sopenharmony_ci    while (state.KeepRunning()) {
219060ff233Sopenharmony_ci        state.PauseTiming();
220060ff233Sopenharmony_ci        int32_t ret = CreateSessionServer(g_pkgName, g_sessionName, &g_sessionlistener);
221060ff233Sopenharmony_ci        state.ResumeTiming();
222060ff233Sopenharmony_ci        ret = SetFileSendListener(g_pkgName, g_sessionName,  &g_fileSendListener);
223060ff233Sopenharmony_ci        if (ret != 0) {
224060ff233Sopenharmony_ci            state.SkipWithError("SetFileSendListenerTestCase failed");
225060ff233Sopenharmony_ci        }
226060ff233Sopenharmony_ci        state.PauseTiming();
227060ff233Sopenharmony_ci        RemoveSessionServer(g_pkgName, g_sessionName);
228060ff233Sopenharmony_ci    }
229060ff233Sopenharmony_ci}
230060ff233Sopenharmony_ciBENCHMARK_REGISTER_F(TransTest, SetFileSendListenerTestCase);
231060ff233Sopenharmony_ci} // namespace OHOS
232060ff233Sopenharmony_ci
233060ff233Sopenharmony_ci// Run the benchmark
234060ff233Sopenharmony_ciBENCHMARK_MAIN();