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 "common_event_constant.h"
21 #include "common_event_listener.h"
22 #include "common_event_manager.h"
23 #include "singleton.h"
24
25 using namespace OHOS::EventFwk;
26
27 namespace {
28 class BenchmarkCommonEventPublish : public benchmark::Fixture {
29 public:
BenchmarkCommonEventPublish()30 BenchmarkCommonEventPublish()
31 {
32 Iterations(iterations);
33 Repetitions(repetitions);
34 ReportAggregatesOnly();
35 }
36
37 ~BenchmarkCommonEventPublish() override = default;
38
SetUp(const::benchmark::State & state)39 void SetUp(const ::benchmark::State &state) override
40 {}
41
TearDown(const::benchmark::State & state)42 void TearDown(const ::benchmark::State &state) override
43 {}
44
45 protected:
46 const int32_t repetitions = 1;
47 const int32_t iterations = 100;
48 };
49
50 class CommonEventSubscriberBenchmark : public CommonEventSubscriber {
51 public:
CommonEventSubscriberBenchmark(const CommonEventSubscribeInfo & subscribeInfo)52 explicit CommonEventSubscriberBenchmark(const CommonEventSubscribeInfo &subscribeInfo) : CommonEventSubscriber(
53 subscribeInfo) {};
~CommonEventSubscriberBenchmark()54 virtual ~CommonEventSubscriberBenchmark() {};
OnReceiveEvent(const CommonEventData & data)55 virtual void OnReceiveEvent(const CommonEventData &data) {};
56 };
57
58 /**
59 * @tc.name: SubscribeCommonEventTestCase
60 * @tc.desc: SubscribeCommonEvent
61 * @tc.type: FUNC
62 * @tc.require:
63 */
BENCHMARK_F(BenchmarkCommonEventPublish,SubscribeCommonEventTestCase)64 BENCHMARK_F(BenchmarkCommonEventPublish, SubscribeCommonEventTestCase)(benchmark::State &state)
65 {
66 std::string eventName = "SUBSCRIBER_EVENT_BENCHMARK";
67 MatchingSkills matchingSkills;
68 matchingSkills.AddEvent(eventName);
69 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
70 auto subscriberPtr = std::make_shared<CommonEventSubscriberBenchmark>(subscribeInfo);
71 while (state.KeepRunning()) {
72 CommonEventManager::SubscribeCommonEvent(subscriberPtr);
73 }
74 }
75
76 /**
77 * @tc.name: UnsubscribeCommonEventTestCase
78 * @tc.desc: UnsubscribeCommonEvent
79 * @tc.type: FUNC
80 * @tc.require:
81 */
BENCHMARK_F(BenchmarkCommonEventPublish,UnsubscribeCommonEventTestCase)82 BENCHMARK_F(BenchmarkCommonEventPublish, UnsubscribeCommonEventTestCase)(benchmark::State &state)
83 {
84 std::string eventName = "UNSUBSCRIBER_EVENT_BENCHMARK";
85 MatchingSkills matchingSkills;
86 matchingSkills.AddEvent(eventName);
87 CommonEventSubscribeInfo subscribeInfo(matchingSkills);
88 subscribeInfo.SetPriority(1000);
89 auto subscriberPtr = std::make_shared<CommonEventSubscriberBenchmark>(subscribeInfo);
90
91 while (state.KeepRunning()) {
92 if (OHOS::EventFwk::CommonEventManager::SubscribeCommonEvent(subscriberPtr)) {
93 OHOS::EventFwk::CommonEventManager::UnSubscribeCommonEvent(subscriberPtr);
94 }
95 }
96 }
97
98 /**
99 * @tc.name: PublishCommonEventTestCase
100 * @tc.desc: PublishCommonEvent
101 * @tc.type: FUNC
102 * @tc.require:
103 */
BENCHMARK_F(BenchmarkCommonEventPublish,PublishCommonEventTestCase)104 BENCHMARK_F(BenchmarkCommonEventPublish, PublishCommonEventTestCase)(benchmark::State &state)
105 {
106 std::string eventAction = "PUBLISH_COMMON_EVENT_BENCHMARK";
107 std::string entity = "ADDENTITY";
108 bool stickty = false;
109 Want testWant;
110 testWant.SetAction(eventAction);
111 testWant.AddEntity(entity);
112 CommonEventData commonEventData(testWant);
113 CommonEventPublishInfo publishInfo;
114 publishInfo.SetSticky(stickty);
115
116 while (state.KeepRunning()) {
117 OHOS::EventFwk::CommonEventManager::PublishCommonEvent(commonEventData, publishInfo);
118 }
119 }
120 }
121
122 // Run the benchmark
123 BENCHMARK_MAIN();