• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 
18 #include <meta/api/object.h>
19 #include <meta/base/namespace.h>
20 
21 #include "property_utils.h"
22 
23 META_BEGIN_NAMESPACE()
24 namespace benchmarks {
25 
26 template<typename Type>
GettingPropertyValue(benchmark::State & state,Type && setValue)27 void GettingPropertyValue(benchmark::State& state, Type&& setValue)
28 {
29     const auto value = setValue;
30     for (auto _ : state) {
31         state.PauseTiming();
32         auto property = ConstructProperty<Type>("Property");
33         property->SetValue(value);
34         state.ResumeTiming();
35         benchmark::DoNotOptimize(property->GetValue());
36     }
37 }
38 
39 template<typename Type>
SettingPropertyValue(benchmark::State & state,Type && setValue)40 void SettingPropertyValue(benchmark::State& state, Type&& setValue)
41 {
42     const auto value = setValue;
43     for (auto _ : state) {
44         state.PauseTiming();
45         auto property = ConstructProperty<Type>("Property");
46         state.ResumeTiming();
47         property->SetValue(value);
48         state.PauseTiming();
49         benchmark::DoNotOptimize(property->GetValue());
50         state.ResumeTiming();
51     }
52 }
53 
54 template<typename Type>
SettingPropertyValueWithDependency(benchmark::State & state,Type && setValue)55 void SettingPropertyValueWithDependency(benchmark::State& state, Type&& setValue)
56 {
57     const auto value = setValue;
58     for (auto _ : state) {
59         state.PauseTiming();
60         auto property = ConstructProperty<Type>("Property");
61         auto property_dep = ConstructProperty<Type>("Property Dep");
62         property_dep->SetBind(property);
63         state.ResumeTiming();
64         property->SetValue(value);
65     }
66 }
67 
68 template<typename Type>
SettingPropertyValueWithDependencyChain(benchmark::State & state,Type && setValue)69 void SettingPropertyValueWithDependencyChain(benchmark::State& state, Type&& setValue)
70 {
71     const auto value = setValue;
72     for (auto _ : state) {
73         state.PauseTiming();
74         auto property1 = ConstructProperty<Type>("Property 1");
75         auto property2 = ConstructProperty<Type>("Property 2");
76         auto property3 = ConstructProperty<Type>("Property 3");
77         auto property4 = ConstructProperty<Type>("Property 4");
78 
79         property2->SetBind(property1);
80         property3->SetBind(property2);
81         property4->SetBind(property3);
82 
83         state.ResumeTiming();
84         property1->SetValue(value);
85     }
86 }
87 
88 template<typename Type, typename Validator>
SettingPropertyValueWithValidator(benchmark::State & state,Type && setValue,BASE_NS::shared_ptr<Validator> validator)89 void SettingPropertyValueWithValidator(
90     benchmark::State& state, Type&& setValue, BASE_NS::shared_ptr<Validator> validator)
91 {
92     const auto value = setValue;
93     for (auto _ : state) {
94         state.PauseTiming();
95         auto property = ConstructProperty<Type>("Property");
96         property->SetValidator(validator);
97 
98         state.ResumeTiming();
99         property->SetValue(value);
100         benchmark::DoNotOptimize(property->GetValue());
101     }
102 }
103 
104 template<typename Type>
BindingProperty(benchmark::State & state,Type && p1Value)105 void BindingProperty(benchmark::State& state, Type&& p1Value)
106 {
107     for (auto _ : state) {
108         state.PauseTiming();
109 
110         auto property1 = ConstructProperty<Type>("Property 1", p1Value);
111         auto property2 = ConstructProperty<Type>("Property 2");
112 
113         state.ResumeTiming();
114         property2->SetBind(property1);
115     }
116 }
117 
118 template<typename Type>
BindingPropertyChain(benchmark::State & state,Type && p1Value)119 void BindingPropertyChain(benchmark::State& state, Type&& p1Value)
120 {
121     for (auto _ : state) {
122         state.PauseTiming();
123 
124         auto property1 = ConstructProperty<Type>("Property 1", p1Value);
125         auto property2 = ConstructProperty<Type>("Property 2");
126         auto property3 = ConstructProperty<Type>("Property 3");
127         auto property4 = ConstructProperty<Type>("Property 4");
128 
129         property3->SetBind(property4);
130         property2->SetBind(property3);
131 
132         state.ResumeTiming();
133         property1->SetBind(property2);
134     }
135 }
136 
137 template<typename Type>
EvaluatingProperty(benchmark::State & state,Type && setValue)138 void EvaluatingProperty(benchmark::State& state, Type&& setValue)
139 {
140     for (auto _ : state) {
141         state.PauseTiming();
142 
143         auto property1 = ConstructProperty<Type>("Property 1");
144         auto property2 = ConstructProperty<Type>("Property 2");
145 
146         property2->SetBind(property1);
147         property1->SetValue(setValue);
148 
149         state.ResumeTiming();
150         benchmark::DoNotOptimize(property2->GetValue());
151     }
152 }
153 
154 template<typename Type>
EvaluatingPropertyChain(benchmark::State & state,Type && setValue)155 void EvaluatingPropertyChain(benchmark::State& state, Type&& setValue)
156 {
157     for (auto _ : state) {
158         state.PauseTiming();
159 
160         auto property1 = ConstructProperty<Type>("Property 1");
161         auto property2 = ConstructProperty<Type>("Property 2");
162         auto property3 = ConstructProperty<Type>("Property 3");
163         auto property4 = ConstructProperty<Type>("Property 4");
164 
165         property2->SetBind(property1);
166         property3->SetBind(property2);
167         property4->SetBind(property3);
168         property1->SetValue(setValue);
169 
170         state.ResumeTiming();
171         benchmark::DoNotOptimize(property4->GetValue());
172     }
173 }
174 
175 // Integer
176 BENCHMARK_CAPTURE(GettingPropertyValue, Integer, 10);
177 BENCHMARK_CAPTURE(SettingPropertyValue, Integer, 10);
178 BENCHMARK_CAPTURE(SettingPropertyValueWithDependency, Integer, 10);
179 BENCHMARK_CAPTURE(SettingPropertyValueWithDependencyChain, Integer, 10);
180 BENCHMARK_CAPTURE(BindingProperty, Integer, 10);
181 BENCHMARK_CAPTURE(BindingPropertyChain, Integer, 10);
182 BENCHMARK_CAPTURE(EvaluatingProperty, Integer, 10);
183 BENCHMARK_CAPTURE(EvaluatingPropertyChain, Integer, 10);
184 
185 // IObject
186 BENCHMARK_CAPTURE(GettingPropertyValue, IObject, Object().GetIObject());
187 BENCHMARK_CAPTURE(SettingPropertyValue, IObject, Object().GetIObject());
188 BENCHMARK_CAPTURE(SettingPropertyValueWithDependency, IObject, Object().GetIObject());
189 BENCHMARK_CAPTURE(SettingPropertyValueWithDependencyChain, IObject, Object().GetIObject());
190 BENCHMARK_CAPTURE(BindingProperty, IObject, Object().GetIObject());
191 BENCHMARK_CAPTURE(BindingPropertyChain, IObject, Object().GetIObject());
192 BENCHMARK_CAPTURE(EvaluatingProperty, IObject, Object().GetIObject());
193 BENCHMARK_CAPTURE(EvaluatingPropertyChain, IObject, Object().GetIObject());
194 
195 // Custom interface
196 BENCHMARK_CAPTURE(GettingPropertyValue, IBenchmarkType, CreateBenchmarkType());
197 BENCHMARK_CAPTURE(SettingPropertyValue, IBenchmarkType, CreateBenchmarkType());
198 BENCHMARK_CAPTURE(SettingPropertyValueWithDependency, IBenchmarkType, CreateBenchmarkType());
199 BENCHMARK_CAPTURE(SettingPropertyValueWithDependencyChain, IBenchmarkType, CreateBenchmarkType());
200 BENCHMARK_CAPTURE(BindingProperty, IBenchmarkType, CreateBenchmarkType());
201 BENCHMARK_CAPTURE(BindingPropertyChain, IBenchmarkType, CreateBenchmarkType());
202 BENCHMARK_CAPTURE(EvaluatingProperty, IBenchmarkType, CreateBenchmarkType());
203 BENCHMARK_CAPTURE(EvaluatingPropertyChain, IBenchmarkType, CreateBenchmarkType());
204 
205 // Custom non-interface type
206 BENCHMARK_CAPTURE(GettingPropertyValue, BenchmarkEnum, BenchmarkEnumA);
207 BENCHMARK_CAPTURE(SettingPropertyValue, BenchmarkEnum, BenchmarkEnumA);
208 BENCHMARK_CAPTURE(SettingPropertyValueWithDependency, BenchmarkEnum, BenchmarkEnumA);
209 BENCHMARK_CAPTURE(SettingPropertyValueWithDependencyChain, BenchmarkEnum, BenchmarkEnumA);
210 BENCHMARK_CAPTURE(BindingProperty, BenchmarkEnum, BenchmarkEnumA);
211 BENCHMARK_CAPTURE(BindingPropertyChain, BenchmarkEnum, BenchmarkEnumA);
212 BENCHMARK_CAPTURE(EvaluatingProperty, BenchmarkEnum, BenchmarkEnumA);
213 BENCHMARK_CAPTURE(EvaluatingPropertyChain, BenchmarkEnum, BenchmarkEnumA);
214 } // namespace benchmarks
215 META_END_NAMESPACE()
216