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 <gtest/gtest.h>
18 #include <string>
19 #include <vector>
20
21 #define private public
22 #include "advanced_notification_service.h"
23 #include "ans_const_define.h"
24 #include "ans_inner_errors.h"
25 #include "mock_ipc_skeleton.h"
26 #include "notification.h"
27 #include "notification_subscriber.h"
28
29
30 using namespace OHOS;
31 using namespace OHOS::Notification;
32 using namespace OHOS::AbilityRuntime;
33
34 namespace {
35 class TestAnsSubscriber : public NotificationSubscriber {
36 public:
OnConnected()37 void OnConnected() override
38 {}
OnDisconnected()39 void OnDisconnected() override
40 {}
OnDied()41 void OnDied() override
42 {}
OnUpdate(const std::shared_ptr<NotificationSortingMap> & sortingMap)43 void OnUpdate(const std::shared_ptr<NotificationSortingMap> &sortingMap) override
44 {}
OnDoNotDisturbDateChange(const std::shared_ptr<NotificationDoNotDisturbDate> & date)45 void OnDoNotDisturbDateChange(const std::shared_ptr<NotificationDoNotDisturbDate> &date) override
46 {}
OnEnabledNotificationChanged(const std::shared_ptr<EnabledNotificationCallbackData> & callbackData)47 void OnEnabledNotificationChanged(
48 const std::shared_ptr<EnabledNotificationCallbackData> &callbackData) override
49 {}
OnCanceled(const std::shared_ptr<OHOS::Notification::Notification> & request,const std::shared_ptr<NotificationSortingMap> & sortingMap,int deleteReason)50 void OnCanceled(const std::shared_ptr<OHOS::Notification::Notification> &request,
51 const std::shared_ptr<NotificationSortingMap> &sortingMap, int deleteReason) override
52 {}
OnConsumed(const std::shared_ptr<OHOS::Notification::Notification> & request)53 void OnConsumed(const std::shared_ptr<OHOS::Notification::Notification> &request) override
54 {}
OnConsumed(const std::shared_ptr<OHOS::Notification::Notification> & request,const std::shared_ptr<NotificationSortingMap> & sortingMap)55 void OnConsumed(const std::shared_ptr<OHOS::Notification::Notification> &request,
56 const std::shared_ptr<NotificationSortingMap> &sortingMap) override
57 {}
58 };
59
60 class BenchmarkNotificationService : public benchmark::Fixture {
61 public:
BenchmarkNotificationService()62 BenchmarkNotificationService()
63 {
64 Iterations(iterations);
65 Repetitions(repetitions);
66 ReportAggregatesOnly();
67 }
68
69 ~BenchmarkNotificationService() override = default;
70
SetUp(const::benchmark::State & state)71 void SetUp(const ::benchmark::State &state) override
72 {}
TearDown(const::benchmark::State & state)73 void TearDown(const ::benchmark::State &state) override
74 {}
75
76 protected:
77 const int32_t repetitions = 3;
78 const int32_t iterations = 100;
79 static sptr<AdvancedNotificationService> advancedNotificationService_;
80 };
81
82 sptr<AdvancedNotificationService> BenchmarkNotificationService::advancedNotificationService_ =
83 AdvancedNotificationService::GetInstance();
84
85 /**
86 * @tc.name: AddSlotTestCase
87 * @tc.desc: AddSlot
88 * @tc.type: FUNC
89 * @tc.require:
90 */
BENCHMARK_F(BenchmarkNotificationService,AddSlotTestCase)91 BENCHMARK_F(BenchmarkNotificationService, AddSlotTestCase)(benchmark::State &state)
92 {
93 std::vector<sptr<NotificationSlot>> slots;
94 sptr<NotificationSlot> slot = new NotificationSlot(NotificationConstant::SlotType::CUSTOM);
95 slots.push_back(slot);
96 while (state.KeepRunning()) {
97 ErrCode errCode = advancedNotificationService_->AddSlots(slots);
98 if (errCode != ERR_OK) {
99 state.SkipWithError("AddSlotTestCase failed.");
100 }
101 }
102 }
103
104 /**
105 * @tc.name: RemoveSlotByTypeTestCase
106 * @tc.desc: RemoveSlotByType
107 * @tc.type: FUNC
108 * @tc.require:
109 */
BENCHMARK_F(BenchmarkNotificationService,RemoveSlotByTypeTestCase)110 BENCHMARK_F(BenchmarkNotificationService, RemoveSlotByTypeTestCase)(benchmark::State &state)
111 {
112 std::vector<sptr<NotificationSlot>> slots;
113 sptr<NotificationSlot> slot = new NotificationSlot(NotificationConstant::SlotType::CUSTOM);
114 slots.push_back(slot);
115 while (state.KeepRunning()) {
116 ErrCode errCode = advancedNotificationService_->AddSlots(slots);
117 if (errCode != ERR_OK) {
118 state.SkipWithError("RemoveSlotByTypeTestCase add failed.");
119 }
120
121 errCode = advancedNotificationService_->RemoveSlotByType(NotificationConstant::SlotType::CUSTOM);
122 if (errCode != ERR_OK) {
123 state.SkipWithError("RemoveSlotByTypeTestCase remove failed.");
124 }
125 }
126 }
127
128 /**
129 * @tc.name: SubscribeTestCase
130 * @tc.desc: Subscribe
131 * @tc.type: FUNC
132 * @tc.require:
133 */
BENCHMARK_F(BenchmarkNotificationService,SubscribeTestCase)134 BENCHMARK_F(BenchmarkNotificationService, SubscribeTestCase)(benchmark::State &state)
135 {
136 auto subscriber = new TestAnsSubscriber();
137 sptr<NotificationSubscribeInfo> info = new NotificationSubscribeInfo();
138 while (state.KeepRunning()) {
139 ErrCode errCode = advancedNotificationService_->Subscribe(subscriber->GetImpl(), info);
140 if (errCode != ERR_OK) {
141 state.SkipWithError("SubscribeTestCase failed.");
142 }
143 }
144 }
145
146 /**
147 * @tc.name: PublishNotificationTestCase001
148 * @tc.desc: Publish a normal text type notification.
149 * @tc.type: FUNC
150 * @tc.require:
151 */
BENCHMARK_F(BenchmarkNotificationService,PublishNotificationTestCase001)152 BENCHMARK_F(BenchmarkNotificationService, PublishNotificationTestCase001)(benchmark::State &state)
153 {
154 sptr<NotificationRequest> req = new (std::nothrow) NotificationRequest(1);
155 EXPECT_NE(req, nullptr);
156 req->SetSlotType(NotificationConstant::SlotType::OTHER);
157 req->SetLabel("req's label");
158 std::string label = "publish's label";
159 std::shared_ptr<NotificationNormalContent> normalContent = std::make_shared<NotificationNormalContent>();
160 EXPECT_NE(normalContent, nullptr);
161 normalContent->SetText("normalContent's text");
162 normalContent->SetTitle("normalContent's title");
163 std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(normalContent);
164 EXPECT_NE(content, nullptr);
165 req->SetContent(content);
166
167 while (state.KeepRunning()) {
168 ErrCode errCode = advancedNotificationService_->Publish(label, req);
169 if (errCode != ERR_OK) {
170 state.SkipWithError("PublishNotificationTestCase001 failed.");
171 }
172 }
173 }
174
175 /**
176 * @tc.name: CancelNotificationTestCase001
177 * @tc.desc: Cancel a normal text type notification.
178 * @tc.type: FUNC
179 * @tc.require:
180 */
BENCHMARK_F(BenchmarkNotificationService,CancelNotificationTestCase001)181 BENCHMARK_F(BenchmarkNotificationService, CancelNotificationTestCase001)(benchmark::State &state)
182 {
183 sptr<NotificationRequest> req = new (std::nothrow) NotificationRequest(0);
184 EXPECT_NE(req, nullptr);
185 req->SetSlotType(NotificationConstant::SlotType::OTHER);
186 req->SetLabel("req's label");
187 std::string label = "publish's label";
188 std::shared_ptr<NotificationNormalContent> normalContent = std::make_shared<NotificationNormalContent>();
189 EXPECT_NE(normalContent, nullptr);
190 normalContent->SetText("normalContent's text");
191 normalContent->SetTitle("normalContent's title");
192 std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(normalContent);
193 EXPECT_NE(content, nullptr);
194 req->SetContent(content);
195
196 int id = 0;
197 while (state.KeepRunning()) {
198 req->SetNotificationId(id);
199 ErrCode errCode = advancedNotificationService_->Publish(label, req);
200 if (errCode != ERR_OK) {
201 state.SkipWithError("CancelNotificationTestCase001 publish failed.");
202 }
203 advancedNotificationService_->Cancel(id, label);
204 }
205 }
206
207 /**
208 * @tc.name: SetNotificationBadgeNumTestCase
209 * @tc.desc: SetNotificationBadgeNum
210 * @tc.type: FUNC
211 * @tc.require:
212 */
BENCHMARK_F(BenchmarkNotificationService,SetNotificationBadgeNumTestCase)213 BENCHMARK_F(BenchmarkNotificationService, SetNotificationBadgeNumTestCase)(benchmark::State &state)
214 {
215 while (state.KeepRunning()) {
216 ErrCode errCode = advancedNotificationService_->SetNotificationBadgeNum(2);
217 if (errCode != ERR_OK) {
218 state.SkipWithError("SetNotificationBadgeNumTestCase failed.");
219 }
220 }
221 }
222
223 /**
224 * @tc.name: GetBundleImportanceTestCase
225 * @tc.desc: GetBundleImportance
226 * @tc.type: FUNC
227 * @tc.require:
228 */
BENCHMARK_F(BenchmarkNotificationService,GetBundleImportanceTestCase)229 BENCHMARK_F(BenchmarkNotificationService, GetBundleImportanceTestCase)(benchmark::State &state)
230 {
231 int importance = 0;
232 while (state.KeepRunning()) {
233 ErrCode errCode = advancedNotificationService_->GetBundleImportance(importance);
234 if (errCode != ERR_OK) {
235 state.SkipWithError("GetBundleImportanceTestCase failed.");
236 }
237 }
238 }
239
240 /**
241 * @tc.name: SetPrivateNotificationsAllowedTestCase
242 * @tc.desc: SetPrivateNotificationsAllowed
243 * @tc.type: FUNC
244 * @tc.require:
245 */
BENCHMARK_F(BenchmarkNotificationService,SetPrivateNotificationsAllowedTestCase)246 BENCHMARK_F(BenchmarkNotificationService, SetPrivateNotificationsAllowedTestCase)(benchmark::State &state)
247 {
248 while (state.KeepRunning()) {
249 ErrCode errCode = advancedNotificationService_->SetPrivateNotificationsAllowed(true);
250 if (errCode != ERR_OK) {
251 state.SkipWithError("SetPrivateNotificationsAllowedTestCase failed.");
252 }
253 }
254 }
255
256 /**
257 * @tc.name: GetPrivateNotificationsAllowedTestCase
258 * @tc.desc: GetPrivateNotificationsAllowed
259 * @tc.type: FUNC
260 * @tc.require:
261 */
BENCHMARK_F(BenchmarkNotificationService,GetPrivateNotificationsAllowedTestCase)262 BENCHMARK_F(BenchmarkNotificationService, GetPrivateNotificationsAllowedTestCase)(benchmark::State &state)
263 {
264 while (state.KeepRunning()) {
265 ErrCode errCode = advancedNotificationService_->SetPrivateNotificationsAllowed(true);
266 if (errCode != ERR_OK) {
267 state.SkipWithError("GetPrivateNotificationsAllowed set failed.");
268 }
269
270 bool allow = false;
271 errCode = advancedNotificationService_->GetPrivateNotificationsAllowed(allow);
272 if (!allow || errCode != ERR_OK) {
273 state.SkipWithError("GetPrivateNotificationsAllowed get failed.");
274 }
275 }
276 }
277
278 /**
279 * @tc.name: SetShowBadgeEnabledForBundleTestCase
280 * @tc.desc: SetShowBadgeEnabledForBundle
281 * @tc.type: FUNC
282 * @tc.require:
283 */
BENCHMARK_F(BenchmarkNotificationService,SetShowBadgeEnabledForBundleTestCase)284 BENCHMARK_F(BenchmarkNotificationService, SetShowBadgeEnabledForBundleTestCase)(benchmark::State &state)
285 {
286 sptr<NotificationBundleOption> bundleOption = new NotificationBundleOption("bundleName", 1000);
287 while (state.KeepRunning()) {
288 ErrCode errCode = advancedNotificationService_->SetShowBadgeEnabledForBundle(bundleOption, true);
289 if (errCode != ERR_OK) {
290 state.SkipWithError("SetShowBadgeEnabledForBundleTestCase failed.");
291 }
292 }
293 }
294
295 /**
296 * @tc.name: GetShowBadgeEnabledForBundleTestCase
297 * @tc.desc: GetShowBadgeEnabledForBundle
298 * @tc.type: FUNC
299 * @tc.require:
300 */
BENCHMARK_F(BenchmarkNotificationService,GetShowBadgeEnabledForBundleTestCase)301 BENCHMARK_F(BenchmarkNotificationService, GetShowBadgeEnabledForBundleTestCase)(benchmark::State &state)
302 {
303 sptr<NotificationBundleOption> bundleOption = new NotificationBundleOption("bundleName", 1000);
304 while (state.KeepRunning()) {
305 ErrCode errCode = advancedNotificationService_->SetShowBadgeEnabledForBundle(bundleOption, true);
306 if (errCode != ERR_OK) {
307 state.SkipWithError("GetShowBadgeEnabledForBundleTestCase set failed.");
308 }
309
310 bool allow = false;
311 errCode = advancedNotificationService_->GetShowBadgeEnabledForBundle(bundleOption, allow);
312 if (!allow || errCode != ERR_OK) {
313 state.SkipWithError("GetShowBadgeEnabledForBundleTestCase get failed.");
314 }
315 }
316 }
317
318 /**
319 * @tc.name: GetAllActiveNotificationsTestCase
320 * @tc.desc: GetAllActiveNotifications
321 * @tc.type: FUNC
322 * @tc.require:
323 */
BENCHMARK_F(BenchmarkNotificationService,GetAllActiveNotificationsTestCase)324 BENCHMARK_F(BenchmarkNotificationService, GetAllActiveNotificationsTestCase)(benchmark::State &state)
325 {
326 std::vector<sptr<OHOS::Notification::Notification>> notifications;
327 while (state.KeepRunning()) {
328 ErrCode errCode = advancedNotificationService_->GetAllActiveNotifications(notifications);
329 if (errCode != ERR_OK) {
330 state.SkipWithError("GetAllActiveNotificationsTestCase failed.");
331 }
332 }
333 }
334
335 /**
336 * @tc.name: SetNotificationsEnabledForAllBundlesTestCase
337 * @tc.desc: SetNotificationsEnabledForAllBundles
338 * @tc.type: FUNC
339 * @tc.require:
340 */
BENCHMARK_F(BenchmarkNotificationService,SetNotificationsEnabledForAllBundlesTestCase)341 BENCHMARK_F(BenchmarkNotificationService, SetNotificationsEnabledForAllBundlesTestCase)(benchmark::State &state)
342 {
343 std::vector<sptr<OHOS::Notification::Notification>> notifications;
344 while (state.KeepRunning()) {
345 ErrCode errCode = advancedNotificationService_->SetNotificationsEnabledForAllBundles(std::string(), true);
346 if (errCode != ERR_OK) {
347 state.SkipWithError("SetNotificationsEnabledForAllBundlesTestCase failed.");
348 }
349 }
350 }
351
352 /**
353 * @tc.name: IsAllowedNotifyTestCase
354 * @tc.desc: IsAllowedNotify
355 * @tc.type: FUNC
356 * @tc.require:
357 */
BENCHMARK_F(BenchmarkNotificationService,IsAllowedNotifyTestCase)358 BENCHMARK_F(BenchmarkNotificationService, IsAllowedNotifyTestCase)(benchmark::State &state)
359 {
360 std::vector<sptr<OHOS::Notification::Notification>> notifications;
361 while (state.KeepRunning()) {
362 ErrCode errCode = advancedNotificationService_->SetNotificationsEnabledForAllBundles(std::string(), true);
363 if (errCode != ERR_OK) {
364 state.SkipWithError("IsAllowedNotifyTestCase set failed.");
365 }
366
367 bool allowed = false;
368 errCode = advancedNotificationService_->IsAllowedNotify(allowed);
369 if (!allowed || errCode != ERR_OK) {
370 state.SkipWithError("IsAllowedNotifyTestCase get failed.");
371 }
372 }
373 }
374
375 /**
376 * @tc.name: SetNotificationsEnabledForSpecialBundleTestCase
377 * @tc.desc: SetNotificationsEnabledForSpecialBundle
378 * @tc.type: FUNC
379 * @tc.require:
380 */
BENCHMARK_F(BenchmarkNotificationService,SetNotificationsEnabledForSpecialBundleTestCase)381 BENCHMARK_F(BenchmarkNotificationService, SetNotificationsEnabledForSpecialBundleTestCase)(benchmark::State &state)
382 {
383 sptr<NotificationBundleOption> bundleOption = new NotificationBundleOption("bundleName", 1000);
384 while (state.KeepRunning()) {
385 ErrCode errCode = advancedNotificationService_->SetNotificationsEnabledForSpecialBundle(
386 std::string(), bundleOption, true);
387 if (errCode != ERR_OK) {
388 state.SkipWithError("SetNotificationsEnabledForSpecialBundleTestCase failed.");
389 }
390 }
391 }
392 }
393
394 // Run the benchmark
395 BENCHMARK_MAIN();