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
16 #include <benchmark/benchmark.h>
17 #include "discovery_service.h"
18
19 namespace OHOS {
20 static int g_subscribeId = 0;
21 static int g_publishId = 0;
22 static const char *g_pkgName = "Softbus_Kits";
23
24 class DiscoveryTest : public benchmark::Fixture {
25 public:
DiscoveryTest()26 DiscoveryTest()
27 {
28 Iterations(iterations);
29 Repetitions(repetitions);
30 ReportAggregatesOnly();
31 }
32 ~DiscoveryTest() override = default;
33 static void SetUpTestCase(void);
34 static void TearDownTestCase(void);
SetUp(const::benchmark::State & state)35 void SetUp(const ::benchmark::State &state) override
36 {}
TearDown(const::benchmark::State & state)37 void TearDown(const ::benchmark::State &state) override
38 {}
39
40 protected:
41 const int32_t repetitions = 3;
42 const int32_t iterations = 1000;
43 };
44
SetUpTestCase(void)45 void DiscoveryTest::SetUpTestCase(void)
46 {}
47
TearDownTestCase(void)48 void DiscoveryTest::TearDownTestCase(void)
49 {}
50
GetSubscribeId(void)51 static int GetSubscribeId(void)
52 {
53 g_subscribeId++;
54 return g_subscribeId;
55 }
56
GetPublishId(void)57 static int GetPublishId(void)
58 {
59 g_publishId++;
60 return g_publishId;
61 }
62
63 static SubscribeInfo g_sInfo = {
64 .subscribeId = 1,
65 .mode = DISCOVER_MODE_ACTIVE,
66 .medium = COAP,
67 .freq = MID,
68 .isSameAccount = true,
69 .isWakeRemote = false,
70 .capability = "dvKit",
71 .capabilityData = (unsigned char *)"capdata3",
72 .dataLen = sizeof("capdata3")
73 };
74
75 static PublishInfo g_pInfo = {
76 .publishId = 1,
77 .mode = DISCOVER_MODE_ACTIVE,
78 .medium = COAP,
79 .freq = MID,
80 .capability = "dvKit",
81 .capabilityData = (unsigned char *)"capdata4",
82 .dataLen = sizeof("capdata4")
83 };
84
TestDeviceFound(const DeviceInfo * device)85 static void TestDeviceFound(const DeviceInfo *device)
86 {}
87
TestDiscoverFailed(int subscribeId,DiscoveryFailReason failReason)88 static void TestDiscoverFailed(int subscribeId, DiscoveryFailReason failReason)
89 {}
90
TestDiscoverySuccess(int subscribeId)91 static void TestDiscoverySuccess(int subscribeId)
92 {}
93
TestPublishSuccess(int publishId)94 static void TestPublishSuccess(int publishId)
95 {}
96
TestPublishFail(int publishId,PublishFailReason reason)97 static void TestPublishFail(int publishId, PublishFailReason reason)
98 {}
99
100 static IDiscoveryCallback g_subscribeCb = {
101 .OnDeviceFound = TestDeviceFound,
102 .OnDiscoverFailed = TestDiscoverFailed,
103 .OnDiscoverySuccess = TestDiscoverySuccess
104 };
105
106 static IPublishCallback g_publishCb = {
107 .OnPublishSuccess = TestPublishSuccess,
108 .OnPublishFail = TestPublishFail
109 };
110
111 /**
112 * @tc.name: PublishServiceTestCase
113 * @tc.desc: PublishService Performance Testing
114 * @tc.type: FUNC
115 * @tc.require: PublishService normal operation
116 */
BENCHMARK_F(DiscoveryTest,PublishServiceTestCase)117 BENCHMARK_F(DiscoveryTest, PublishServiceTestCase)(benchmark::State &state)
118 {
119 while (state.KeepRunning()) {
120 g_pInfo.publishId = GetPublishId();
121 state.ResumeTiming();
122 int ret = PublishService(g_pkgName, &g_pInfo, &g_publishCb);
123 if (ret != 0) {
124 state.SkipWithError("PublishServiceTestCase failed.");
125 }
126 state.PauseTiming();
127 ret = UnPublishService(g_pkgName, g_pInfo.publishId);
128 }
129 }
130 BENCHMARK_REGISTER_F(DiscoveryTest, PublishServiceTestCase);
131
132 /**
133 * @tc.name: UnPublishServiceTestCase
134 * @tc.desc: UnPublishService Performance Testing
135 * @tc.type: FUNC
136 * @tc.require: UnPublishService normal operation
137 */
BENCHMARK_F(DiscoveryTest,UnPublishServiceTestCase)138 BENCHMARK_F(DiscoveryTest, UnPublishServiceTestCase)(benchmark::State &state)
139 {
140 while (state.KeepRunning()) {
141 g_pInfo.publishId = GetPublishId();
142 state.PauseTiming();
143 int ret = PublishService(g_pkgName, &g_pInfo, &g_publishCb);
144 if (ret != 0) {
145 state.SkipWithError("UnPublishServiceTestCase failed.");
146 }
147 state.ResumeTiming();
148 ret = UnPublishService(g_pkgName, g_pInfo.publishId);
149 if (ret != 0) {
150 state.SkipWithError("UnPublishServiceTestCase failed.");
151 }
152 }
153 }
154 BENCHMARK_REGISTER_F(DiscoveryTest, UnPublishServiceTestCase);
155
156 /**
157 * @tc.name: StartDiscoveryTestCase
158 * @tc.desc: StartDiscovery Performance Testing
159 * @tc.type: FUNC
160 * @tc.require: StartDiscovery normal operation
161 */
BENCHMARK_F(DiscoveryTest,StartDiscoveryTestCase)162 BENCHMARK_F(DiscoveryTest, StartDiscoveryTestCase)(benchmark::State &state)
163 {
164 while (state.KeepRunning()) {
165 g_sInfo.subscribeId = GetSubscribeId();
166 state.ResumeTiming();
167 int ret = StartDiscovery(g_pkgName, &g_sInfo, &g_subscribeCb);
168 if (ret != 0) {
169 state.SkipWithError("StartDiscoveryTestCase failed.");
170 }
171 state.PauseTiming();
172 ret = StopDiscovery(g_pkgName, g_sInfo.subscribeId);
173 }
174 }
175 BENCHMARK_REGISTER_F(DiscoveryTest, StartDiscoveryTestCase);
176
177 /**
178 * @tc.name: StopDiscoveryTestCase
179 * @tc.desc: StoptDiscovery Performance Testing
180 * @tc.type: FUNC
181 * @tc.require: StoptDiscovery normal operation
182 */
BENCHMARK_F(DiscoveryTest,StoptDiscoveryTestCase)183 BENCHMARK_F(DiscoveryTest, StoptDiscoveryTestCase)(benchmark::State &state)
184 {
185 while (state.KeepRunning()) {
186 g_sInfo.subscribeId = GetSubscribeId();
187 state.PauseTiming();
188 int ret = StartDiscovery(g_pkgName, &g_sInfo, &g_subscribeCb);
189 if (ret != 0) {
190 state.SkipWithError("StoptDiscoveryTestCase failed.");
191 }
192 state.ResumeTiming();
193 ret = StopDiscovery(g_pkgName, g_sInfo.subscribeId);
194 if (ret != 0) {
195 state.SkipWithError("StoptDiscoveryTestCase failed.");
196 }
197 }
198 }
199 BENCHMARK_REGISTER_F(DiscoveryTest, StoptDiscoveryTestCase);
200 }
201
202 // Run the benchmark
203 BENCHMARK_MAIN();