1/*
2 * Copyright (c) 2022-2023 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 "nativetoken_kit.h"
19#include "softbus_bus_center.h"
20#include "token_setproc.h"
21
22namespace OHOS {
23static int32_t g_subscribeId = 0;
24static int32_t g_publishId = 0;
25static const char *g_pkgName = "Softbus_Kits";
26static bool g_flag = true;
27void 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
51class DiscoveryTest : public benchmark::Fixture {
52public:
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);
62    void SetUp(const ::benchmark::State &state) override
63    {
64        AddPermission();
65    }
66    void TearDown(const ::benchmark::State &state) override
67    {}
68
69protected:
70    const int32_t repetitions = 3;
71    const int32_t iterations = 1000;
72};
73
74void DiscoveryTest::SetUpTestCase(void)
75{}
76
77void DiscoveryTest::TearDownTestCase(void)
78{}
79
80static int32_t GetSubscribeId(void)
81{
82    g_subscribeId++;
83    return g_subscribeId;
84}
85
86static int32_t GetPublishId(void)
87{
88    g_publishId++;
89    return g_publishId;
90}
91
92static 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
104static 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
114static void TestDeviceFound(const DeviceInfo *device)
115{}
116
117static void TestDiscoverResult(int32_t refreshId, RefreshResult reason)
118{}
119
120static void TestPublishResult(int32_t publishId, PublishResult reason)
121{}
122
123static IRefreshCallback g_refreshCb = {
124    .OnDeviceFound = TestDeviceFound,
125    .OnDiscoverResult = TestDiscoverResult
126};
127
128static IPublishCb g_publishCb = {
129    .OnPublishResult = TestPublishResult
130};
131
132/**
133 * @tc.name: PublishLNNTestCase
134 * @tc.desc: PublishService Performance Testing
135 * @tc.type: FUNC
136 * @tc.require: PublishService normal operation
137 */
138BENCHMARK_F(DiscoveryTest, PublishLNNTestCase)(benchmark::State &state)
139{
140    while (state.KeepRunning()) {
141        g_pInfo.publishId = GetPublishId();
142        state.ResumeTiming();
143        int32_t ret = PublishLNN(g_pkgName, &g_pInfo, &g_publishCb);
144        if (ret != 0) {
145            state.SkipWithError("PublishLNNTestCase failed.");
146        }
147        state.PauseTiming();
148        ret = StopPublishLNN(g_pkgName, g_pInfo.publishId);
149        if (ret != 0) {
150            state.SkipWithError("StopPublishLNNTestCase failed.");
151        }
152    }
153}
154BENCHMARK_REGISTER_F(DiscoveryTest, PublishLNNTestCase);
155
156/**
157 * @tc.name: StopPublishLNNTestCase
158 * @tc.desc: UnPublishService Performance Testing
159 * @tc.type: FUNC
160 * @tc.require: UnPublishService normal operation
161 */
162BENCHMARK_F(DiscoveryTest, StopPublishLNNTestCase)(benchmark::State &state)
163{
164    while (state.KeepRunning()) {
165        g_pInfo.publishId = GetPublishId();
166        state.PauseTiming();
167        int32_t ret = PublishLNN(g_pkgName, &g_pInfo, &g_publishCb);
168        if (ret != 0) {
169            state.SkipWithError("PublishLNNTestCase failed.");
170        }
171        state.ResumeTiming();
172        ret = StopPublishLNN(g_pkgName, g_pInfo.publishId);
173        if (ret != 0) {
174            state.SkipWithError("StopPublishLNNTestCase failed.");
175        }
176    }
177}
178BENCHMARK_REGISTER_F(DiscoveryTest, StopPublishLNNTestCase);
179
180/**
181 * @tc.name: RefreshLNNTestCase
182 * @tc.desc: StartDiscovery Performance Testing
183 * @tc.type: FUNC
184 * @tc.require: StartDiscovery normal operation
185 */
186BENCHMARK_F(DiscoveryTest, RefreshLNNTestCase)(benchmark::State &state)
187{
188    while (state.KeepRunning()) {
189        g_sInfo.subscribeId = GetSubscribeId();
190        state.ResumeTiming();
191        int32_t ret = RefreshLNN(g_pkgName, &g_sInfo, &g_refreshCb);
192        if (ret != 0) {
193            state.SkipWithError("RefreshLNNTestCase failed.");
194        }
195        state.PauseTiming();
196        ret = StopRefreshLNN(g_pkgName, g_sInfo.subscribeId);
197        if (ret != 0) {
198            state.SkipWithError("StoptRefreshLNNTestCase failed.");
199        }
200    }
201}
202BENCHMARK_REGISTER_F(DiscoveryTest, RefreshLNNTestCase);
203
204/**
205 * @tc.name: StoptRefreshLNNTestCase
206 * @tc.desc: StoptDiscovery Performance Testing
207 * @tc.type: FUNC
208 * @tc.require: StoptDiscovery normal operation
209 */
210BENCHMARK_F(DiscoveryTest, StoptRefreshLNNTestCase)(benchmark::State &state)
211{
212    while (state.KeepRunning()) {
213        g_sInfo.subscribeId = GetSubscribeId();
214        state.PauseTiming();
215        int32_t ret = RefreshLNN(g_pkgName, &g_sInfo, &g_refreshCb);
216        if (ret != 0) {
217            state.SkipWithError("RefreshLNNTestCase failed.");
218        }
219        state.ResumeTiming();
220        ret = StopRefreshLNN(g_pkgName, g_sInfo.subscribeId);
221        if (ret != 0) {
222            state.SkipWithError("StoptRefreshLNNTestCase failed.");
223        }
224    }
225}
226BENCHMARK_REGISTER_F(DiscoveryTest, StoptRefreshLNNTestCase);
227}
228
229// Run the benchmark
230BENCHMARK_MAIN();