1060ff233Sopenharmony_ci/*
2060ff233Sopenharmony_ci * Copyright (c) 2022-2023 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
16060ff233Sopenharmony_ci#include <benchmark/benchmark.h>
17060ff233Sopenharmony_ci#include "accesstoken_kit.h"
18060ff233Sopenharmony_ci#include "nativetoken_kit.h"
19060ff233Sopenharmony_ci#include "softbus_bus_center.h"
20060ff233Sopenharmony_ci#include "token_setproc.h"
21060ff233Sopenharmony_ci
22060ff233Sopenharmony_cinamespace OHOS {
23060ff233Sopenharmony_cistatic int32_t g_subscribeId = 0;
24060ff233Sopenharmony_cistatic int32_t g_publishId = 0;
25060ff233Sopenharmony_cistatic const char *g_pkgName = "Softbus_Kits";
26060ff233Sopenharmony_cistatic bool g_flag = true;
27060ff233Sopenharmony_civoid AddPermission()
28060ff233Sopenharmony_ci{
29060ff233Sopenharmony_ci    if (g_flag) {
30060ff233Sopenharmony_ci        uint64_t tokenId;
31060ff233Sopenharmony_ci        const char *perms[2];
32060ff233Sopenharmony_ci        perms[0] = OHOS_PERMISSION_DISTRIBUTED_DATASYNC;
33060ff233Sopenharmony_ci        perms[1] = OHOS_PERMISSION_DISTRIBUTED_SOFTBUS_CENTER;
34060ff233Sopenharmony_ci        NativeTokenInfoParams infoInstance = {
35060ff233Sopenharmony_ci            .dcapsNum = 0,
36060ff233Sopenharmony_ci            .permsNum = 2,
37060ff233Sopenharmony_ci            .aclsNum = 0,
38060ff233Sopenharmony_ci            .dcaps = NULL,
39060ff233Sopenharmony_ci            .perms = perms,
40060ff233Sopenharmony_ci            .acls = NULL,
41060ff233Sopenharmony_ci            .processName = "Softbus_Kits",
42060ff233Sopenharmony_ci            .aplStr = "normal",
43060ff233Sopenharmony_ci        };
44060ff233Sopenharmony_ci        tokenId = GetAccessTokenId(&infoInstance);
45060ff233Sopenharmony_ci        SetSelfTokenID(tokenId);
46060ff233Sopenharmony_ci        OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
47060ff233Sopenharmony_ci        g_flag = false;
48060ff233Sopenharmony_ci    }
49060ff233Sopenharmony_ci}
50060ff233Sopenharmony_ci
51060ff233Sopenharmony_ciclass DiscoveryTest : public benchmark::Fixture {
52060ff233Sopenharmony_cipublic:
53060ff233Sopenharmony_ci    DiscoveryTest()
54060ff233Sopenharmony_ci    {
55060ff233Sopenharmony_ci        Iterations(iterations);
56060ff233Sopenharmony_ci        Repetitions(repetitions);
57060ff233Sopenharmony_ci        ReportAggregatesOnly();
58060ff233Sopenharmony_ci    }
59060ff233Sopenharmony_ci    ~DiscoveryTest() override = default;
60060ff233Sopenharmony_ci    static void SetUpTestCase(void);
61060ff233Sopenharmony_ci    static void TearDownTestCase(void);
62060ff233Sopenharmony_ci    void SetUp(const ::benchmark::State &state) override
63060ff233Sopenharmony_ci    {
64060ff233Sopenharmony_ci        AddPermission();
65060ff233Sopenharmony_ci    }
66060ff233Sopenharmony_ci    void TearDown(const ::benchmark::State &state) override
67060ff233Sopenharmony_ci    {}
68060ff233Sopenharmony_ci
69060ff233Sopenharmony_ciprotected:
70060ff233Sopenharmony_ci    const int32_t repetitions = 3;
71060ff233Sopenharmony_ci    const int32_t iterations = 1000;
72060ff233Sopenharmony_ci};
73060ff233Sopenharmony_ci
74060ff233Sopenharmony_civoid DiscoveryTest::SetUpTestCase(void)
75060ff233Sopenharmony_ci{}
76060ff233Sopenharmony_ci
77060ff233Sopenharmony_civoid DiscoveryTest::TearDownTestCase(void)
78060ff233Sopenharmony_ci{}
79060ff233Sopenharmony_ci
80060ff233Sopenharmony_cistatic int32_t GetSubscribeId(void)
81060ff233Sopenharmony_ci{
82060ff233Sopenharmony_ci    g_subscribeId++;
83060ff233Sopenharmony_ci    return g_subscribeId;
84060ff233Sopenharmony_ci}
85060ff233Sopenharmony_ci
86060ff233Sopenharmony_cistatic int32_t GetPublishId(void)
87060ff233Sopenharmony_ci{
88060ff233Sopenharmony_ci    g_publishId++;
89060ff233Sopenharmony_ci    return g_publishId;
90060ff233Sopenharmony_ci}
91060ff233Sopenharmony_ci
92060ff233Sopenharmony_cistatic SubscribeInfo g_sInfo = {
93060ff233Sopenharmony_ci    .subscribeId = 1,
94060ff233Sopenharmony_ci    .mode = DISCOVER_MODE_ACTIVE,
95060ff233Sopenharmony_ci    .medium = COAP,
96060ff233Sopenharmony_ci    .freq = MID,
97060ff233Sopenharmony_ci    .isSameAccount = true,
98060ff233Sopenharmony_ci    .isWakeRemote = false,
99060ff233Sopenharmony_ci    .capability = "dvKit",
100060ff233Sopenharmony_ci    .capabilityData = (unsigned char *)"capdata3",
101060ff233Sopenharmony_ci    .dataLen = strlen("capdata3")
102060ff233Sopenharmony_ci};
103060ff233Sopenharmony_ci
104060ff233Sopenharmony_cistatic PublishInfo g_pInfo = {
105060ff233Sopenharmony_ci    .publishId = 1,
106060ff233Sopenharmony_ci    .mode = DISCOVER_MODE_ACTIVE,
107060ff233Sopenharmony_ci    .medium = COAP,
108060ff233Sopenharmony_ci    .freq = MID,
109060ff233Sopenharmony_ci    .capability = "dvKit",
110060ff233Sopenharmony_ci    .capabilityData = (unsigned char *)"capdata4",
111060ff233Sopenharmony_ci    .dataLen = strlen("capdata4")
112060ff233Sopenharmony_ci};
113060ff233Sopenharmony_ci
114060ff233Sopenharmony_cistatic void TestDeviceFound(const DeviceInfo *device)
115060ff233Sopenharmony_ci{}
116060ff233Sopenharmony_ci
117060ff233Sopenharmony_cistatic void TestDiscoverResult(int32_t refreshId, RefreshResult reason)
118060ff233Sopenharmony_ci{}
119060ff233Sopenharmony_ci
120060ff233Sopenharmony_cistatic void TestPublishResult(int32_t publishId, PublishResult reason)
121060ff233Sopenharmony_ci{}
122060ff233Sopenharmony_ci
123060ff233Sopenharmony_cistatic IRefreshCallback g_refreshCb = {
124060ff233Sopenharmony_ci    .OnDeviceFound = TestDeviceFound,
125060ff233Sopenharmony_ci    .OnDiscoverResult = TestDiscoverResult
126060ff233Sopenharmony_ci};
127060ff233Sopenharmony_ci
128060ff233Sopenharmony_cistatic IPublishCb g_publishCb = {
129060ff233Sopenharmony_ci    .OnPublishResult = TestPublishResult
130060ff233Sopenharmony_ci};
131060ff233Sopenharmony_ci
132060ff233Sopenharmony_ci/**
133060ff233Sopenharmony_ci * @tc.name: PublishLNNTestCase
134060ff233Sopenharmony_ci * @tc.desc: PublishService Performance Testing
135060ff233Sopenharmony_ci * @tc.type: FUNC
136060ff233Sopenharmony_ci * @tc.require: PublishService normal operation
137060ff233Sopenharmony_ci */
138060ff233Sopenharmony_ciBENCHMARK_F(DiscoveryTest, PublishLNNTestCase)(benchmark::State &state)
139060ff233Sopenharmony_ci{
140060ff233Sopenharmony_ci    while (state.KeepRunning()) {
141060ff233Sopenharmony_ci        g_pInfo.publishId = GetPublishId();
142060ff233Sopenharmony_ci        state.ResumeTiming();
143060ff233Sopenharmony_ci        int32_t ret = PublishLNN(g_pkgName, &g_pInfo, &g_publishCb);
144060ff233Sopenharmony_ci        if (ret != 0) {
145060ff233Sopenharmony_ci            state.SkipWithError("PublishLNNTestCase failed.");
146060ff233Sopenharmony_ci        }
147060ff233Sopenharmony_ci        state.PauseTiming();
148060ff233Sopenharmony_ci        ret = StopPublishLNN(g_pkgName, g_pInfo.publishId);
149060ff233Sopenharmony_ci        if (ret != 0) {
150060ff233Sopenharmony_ci            state.SkipWithError("StopPublishLNNTestCase failed.");
151060ff233Sopenharmony_ci        }
152060ff233Sopenharmony_ci    }
153060ff233Sopenharmony_ci}
154060ff233Sopenharmony_ciBENCHMARK_REGISTER_F(DiscoveryTest, PublishLNNTestCase);
155060ff233Sopenharmony_ci
156060ff233Sopenharmony_ci/**
157060ff233Sopenharmony_ci * @tc.name: StopPublishLNNTestCase
158060ff233Sopenharmony_ci * @tc.desc: UnPublishService Performance Testing
159060ff233Sopenharmony_ci * @tc.type: FUNC
160060ff233Sopenharmony_ci * @tc.require: UnPublishService normal operation
161060ff233Sopenharmony_ci */
162060ff233Sopenharmony_ciBENCHMARK_F(DiscoveryTest, StopPublishLNNTestCase)(benchmark::State &state)
163060ff233Sopenharmony_ci{
164060ff233Sopenharmony_ci    while (state.KeepRunning()) {
165060ff233Sopenharmony_ci        g_pInfo.publishId = GetPublishId();
166060ff233Sopenharmony_ci        state.PauseTiming();
167060ff233Sopenharmony_ci        int32_t ret = PublishLNN(g_pkgName, &g_pInfo, &g_publishCb);
168060ff233Sopenharmony_ci        if (ret != 0) {
169060ff233Sopenharmony_ci            state.SkipWithError("PublishLNNTestCase failed.");
170060ff233Sopenharmony_ci        }
171060ff233Sopenharmony_ci        state.ResumeTiming();
172060ff233Sopenharmony_ci        ret = StopPublishLNN(g_pkgName, g_pInfo.publishId);
173060ff233Sopenharmony_ci        if (ret != 0) {
174060ff233Sopenharmony_ci            state.SkipWithError("StopPublishLNNTestCase failed.");
175060ff233Sopenharmony_ci        }
176060ff233Sopenharmony_ci    }
177060ff233Sopenharmony_ci}
178060ff233Sopenharmony_ciBENCHMARK_REGISTER_F(DiscoveryTest, StopPublishLNNTestCase);
179060ff233Sopenharmony_ci
180060ff233Sopenharmony_ci/**
181060ff233Sopenharmony_ci * @tc.name: RefreshLNNTestCase
182060ff233Sopenharmony_ci * @tc.desc: StartDiscovery Performance Testing
183060ff233Sopenharmony_ci * @tc.type: FUNC
184060ff233Sopenharmony_ci * @tc.require: StartDiscovery normal operation
185060ff233Sopenharmony_ci */
186060ff233Sopenharmony_ciBENCHMARK_F(DiscoveryTest, RefreshLNNTestCase)(benchmark::State &state)
187060ff233Sopenharmony_ci{
188060ff233Sopenharmony_ci    while (state.KeepRunning()) {
189060ff233Sopenharmony_ci        g_sInfo.subscribeId = GetSubscribeId();
190060ff233Sopenharmony_ci        state.ResumeTiming();
191060ff233Sopenharmony_ci        int32_t ret = RefreshLNN(g_pkgName, &g_sInfo, &g_refreshCb);
192060ff233Sopenharmony_ci        if (ret != 0) {
193060ff233Sopenharmony_ci            state.SkipWithError("RefreshLNNTestCase failed.");
194060ff233Sopenharmony_ci        }
195060ff233Sopenharmony_ci        state.PauseTiming();
196060ff233Sopenharmony_ci        ret = StopRefreshLNN(g_pkgName, g_sInfo.subscribeId);
197060ff233Sopenharmony_ci        if (ret != 0) {
198060ff233Sopenharmony_ci            state.SkipWithError("StoptRefreshLNNTestCase failed.");
199060ff233Sopenharmony_ci        }
200060ff233Sopenharmony_ci    }
201060ff233Sopenharmony_ci}
202060ff233Sopenharmony_ciBENCHMARK_REGISTER_F(DiscoveryTest, RefreshLNNTestCase);
203060ff233Sopenharmony_ci
204060ff233Sopenharmony_ci/**
205060ff233Sopenharmony_ci * @tc.name: StoptRefreshLNNTestCase
206060ff233Sopenharmony_ci * @tc.desc: StoptDiscovery Performance Testing
207060ff233Sopenharmony_ci * @tc.type: FUNC
208060ff233Sopenharmony_ci * @tc.require: StoptDiscovery normal operation
209060ff233Sopenharmony_ci */
210060ff233Sopenharmony_ciBENCHMARK_F(DiscoveryTest, StoptRefreshLNNTestCase)(benchmark::State &state)
211060ff233Sopenharmony_ci{
212060ff233Sopenharmony_ci    while (state.KeepRunning()) {
213060ff233Sopenharmony_ci        g_sInfo.subscribeId = GetSubscribeId();
214060ff233Sopenharmony_ci        state.PauseTiming();
215060ff233Sopenharmony_ci        int32_t ret = RefreshLNN(g_pkgName, &g_sInfo, &g_refreshCb);
216060ff233Sopenharmony_ci        if (ret != 0) {
217060ff233Sopenharmony_ci            state.SkipWithError("RefreshLNNTestCase failed.");
218060ff233Sopenharmony_ci        }
219060ff233Sopenharmony_ci        state.ResumeTiming();
220060ff233Sopenharmony_ci        ret = StopRefreshLNN(g_pkgName, g_sInfo.subscribeId);
221060ff233Sopenharmony_ci        if (ret != 0) {
222060ff233Sopenharmony_ci            state.SkipWithError("StoptRefreshLNNTestCase failed.");
223060ff233Sopenharmony_ci        }
224060ff233Sopenharmony_ci    }
225060ff233Sopenharmony_ci}
226060ff233Sopenharmony_ciBENCHMARK_REGISTER_F(DiscoveryTest, StoptRefreshLNNTestCase);
227060ff233Sopenharmony_ci}
228060ff233Sopenharmony_ci
229060ff233Sopenharmony_ci// Run the benchmark
230060ff233Sopenharmony_ciBENCHMARK_MAIN();