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 <string>
18 #include <vector>
19 
20 #include "notification_action_button.h"
21 #include "notification_request.h"
22 #include "notification_content.h"
23 #include "notification_helper.h"
24 #include "want_agent_helper.h"
25 #include "want_agent_info.h"
26 
27 using namespace OHOS;
28 using namespace OHOS::Notification;
29 using namespace OHOS::AbilityRuntime;
30 
31 namespace {
32 class BenchmarkNotificationPublish : public benchmark::Fixture {
33 public:
BenchmarkNotificationPublish()34     BenchmarkNotificationPublish()
35     {
36         Iterations(iterations);
37         Repetitions(repetitions);
38         ReportAggregatesOnly();
39 
40         InitTextRequest();
41         InitWantAgentRequest();
42         InitButtonRequest();
43     }
44 
45     ~BenchmarkNotificationPublish() override = default;
46 
SetUp(const::benchmark::State & state)47     void SetUp(const ::benchmark::State &state) override
48     {}
49 
TearDown(const::benchmark::State & state)50     void TearDown(const ::benchmark::State &state) override
51     {}
52 
53 protected:
54     void InitTextRequest();
55     void InitWantAgentRequest();
56     void InitButtonRequest();
57 
58     const int32_t repetitions = 1;
59     const int32_t iterations = 100;
60 
61     NotificationRequest req_;
62     NotificationRequest reqWantAgent_;
63     NotificationRequest reqButton_;
64 };
65 
InitTextRequest()66 void BenchmarkNotificationPublish::InitTextRequest()
67 {
68     std::shared_ptr<NotificationMediaContent> mediaContent = std::make_shared<NotificationMediaContent>();
69     std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(mediaContent);
70     req_.SetContent(content);
71     req_.SetSlotType(NotificationConstant::OTHER);
72     std::vector<std::string> style;
73     style.push_back("style");
74     req_.SetNotificationUserInputHistory(style);
75     req_.SetOwnerBundleName("bundleName");
76     req_.SetLabel("Text");
77 }
78 
InitWantAgentRequest()79 void BenchmarkNotificationPublish::InitWantAgentRequest()
80 {
81     std::shared_ptr<NotificationMediaContent> mediaContent = std::make_shared<NotificationMediaContent>();
82     std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(mediaContent);
83     AbilityRuntime::WantAgent::WantAgentInfo paramsInfo;
84     std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> wantAgent =
85         AbilityRuntime::WantAgent::WantAgentHelper::GetWantAgent(paramsInfo);
86     reqWantAgent_.SetContent(content);
87     reqWantAgent_.SetSlotType(NotificationConstant::OTHER);
88     reqWantAgent_.SetWantAgent(wantAgent);
89     reqWantAgent_.SetOwnerBundleName("bundleName");
90     reqWantAgent_.SetLabel("Text");
91 }
92 
InitButtonRequest()93 void BenchmarkNotificationPublish::InitButtonRequest()
94 {
95     std::shared_ptr<NotificationMediaContent> mediaContent = std::make_shared<NotificationMediaContent>();
96     std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(mediaContent);
97     AbilityRuntime::WantAgent::WantAgentInfo paramsInfo;
98     std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> wantAgent =
99         AbilityRuntime::WantAgent::WantAgentHelper::GetWantAgent(paramsInfo);
100     std::shared_ptr<NotificationActionButton> actionButton =
101         NotificationActionButton::Create(nullptr, "title", wantAgent);
102     std::shared_ptr<NotificationUserInput> onlyUserInput = NotificationUserInput::Create("onlyUserInputKey");
103     AAFwk::WantParams additional;
104     actionButton->AddAdditionalData(additional);
105     actionButton->SetSemanticActionButton(NotificationConstant::NONE_ACTION_BUTTON);
106     actionButton->SetAutoCreatedReplies(true);
107     actionButton->AddMimeTypeOnlyUserInput(onlyUserInput);
108     actionButton->SetContextDependent(true);
109     reqButton_.SetContent(content);
110     reqButton_.SetSlotType(NotificationConstant::OTHER);
111     reqButton_.AddActionButton(actionButton);
112     reqButton_.SetOwnerBundleName("bundleName");
113     reqButton_.SetLabel("Text");
114 }
115 
116 /**
117 * @tc.name: PublishNotificationTestCase001
118 * @tc.desc: publish text notifcation
119 * @tc.type: FUNC
120 * @tc.require:
121 */
BENCHMARK_F(BenchmarkNotificationPublish,PublishNotificationTestCase001)122 BENCHMARK_F(BenchmarkNotificationPublish, PublishNotificationTestCase001)(benchmark::State &state)
123 {
124     int id = 0;
125     while (state.KeepRunning()) {
126         req_.SetNotificationId(id++);
127         NotificationHelper::PublishNotification(req_);
128     }
129 }
130 
131 /**
132 * @tc.name: CancelNotificationTestCase
133 * @tc.desc: cancel notification
134 * @tc.type: FUNC
135 * @tc.require:
136 */
BENCHMARK_F(BenchmarkNotificationPublish,CancelNotificationTestCase)137 BENCHMARK_F(BenchmarkNotificationPublish, CancelNotificationTestCase)(benchmark::State &state)
138 {
139     int id = 100;
140     while (state.KeepRunning()) {
141         id--;
142         NotificationHelper::CancelNotification(id);
143     }
144 }
145 
146 /**
147 * @tc.name: PublishNotificationTestCase002
148 * @tc.desc: publish wantAgent notifcation
149 * @tc.type: FUNC
150 * @tc.require:
151 */
BENCHMARK_F(BenchmarkNotificationPublish,PublishNotificationTestCase002)152 BENCHMARK_F(BenchmarkNotificationPublish, PublishNotificationTestCase002)(benchmark::State &state)
153 {
154     int id = 0;
155     while (state.KeepRunning()) {
156         reqWantAgent_.SetNotificationId(id++);
157         NotificationHelper::PublishNotification(reqWantAgent_);
158     }
159 }
160 
161 BENCHMARK_REGISTER_F(BenchmarkNotificationPublish, CancelNotificationTestCase)->Iterations(100)->
162     Repetitions(1)->ReportAggregatesOnly();
163 
164 /**
165 * @tc.name: PublishNotificationTestCase003
166 * @tc.desc: publish wantAgent notifcation
167 * @tc.type: FUNC
168 * @tc.require:
169 */
BENCHMARK_F(BenchmarkNotificationPublish,PublishNotificationTestCase003)170 BENCHMARK_F(BenchmarkNotificationPublish, PublishNotificationTestCase003)(benchmark::State &state)
171 {
172     int id = 0;
173     while (state.KeepRunning()) {
174         reqButton_.SetNotificationId(id++);
175         NotificationHelper::PublishNotification(reqButton_);
176     }
177 }
178 
179 BENCHMARK_REGISTER_F(BenchmarkNotificationPublish, CancelNotificationTestCase)->Iterations(100)->
180     Repetitions(1)->ReportAggregatesOnly();
181 
182 /**
183 * @tc.name: RemoveNotificationTestCase
184 * @tc.desc: remove notification
185 * @tc.type: FUNC
186 * @tc.require:
187 */
BENCHMARK_F(BenchmarkNotificationPublish,RemoveNotificationTestCase)188 BENCHMARK_F(BenchmarkNotificationPublish, RemoveNotificationTestCase)(benchmark::State &state)
189 {
190     int id = 100;
191     while (state.KeepRunning()) {
192         id--;
193         NotificationHelper::RemoveNotifications();
194     }
195 }
196 }
197 
198 // Run the benchmark
199 BENCHMARK_MAIN();