• 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 #include <deque>
18 
19 #include <meta/api/object.h>
20 #include <meta/base/namespace.h>
21 
22 #include "property_utils.h"
23 
24 META_BEGIN_NAMESPACE()
25 namespace benchmarks {
26 
ConstructObject(benchmark::State & state)27 void ConstructObject(benchmark::State& state)
28 {
29     auto& objr = GetObjectRegistry();
30     for (auto _ : state) {
31         auto obj = objr.Create(ClassId::BenchmarkType);
32         state.PauseTiming();
33         benchmark::DoNotOptimize(obj->GetName());
34         state.ResumeTiming();
35     }
36 }
37 
ConstructProperty(benchmark::State & state)38 void ConstructProperty(benchmark::State& state)
39 {
40     for (auto _ : state) {
41         auto property = META_NS::ConstructProperty<int>("Property");
42         state.PauseTiming();
43         benchmark::DoNotOptimize(property->GetValue());
44         state.ResumeTiming();
45     }
46 }
47 
ManyObjects(benchmark::State & state)48 void ManyObjects(benchmark::State& state)
49 {
50     constexpr size_t objectCount = 50000;
51     auto& objr = GetObjectRegistry();
52     objr.Purge(); // make sure there is nothing from other tests
53     for (auto _ : state) {
54         std::deque<IObject::Ptr> objects;
55         for (size_t i = 0; i != objectCount; ++i) {
56             objects.push_back(objr.Create(ClassId::BenchmarkType));
57         }
58     }
59 }
60 
ManyObjectsWithAddAndRemove(benchmark::State & state)61 void ManyObjectsWithAddAndRemove(benchmark::State& state)
62 {
63     constexpr size_t objectCount = 50000;
64     auto& objr = GetObjectRegistry();
65     objr.Purge();
66     std::deque<IObject::Ptr> objects;
67     for (size_t i = 0; i != objectCount; ++i) {
68         // reverse, so when we remove from the back, it is the last one created
69         objects.push_front(objr.Create(ClassId::BenchmarkType));
70     }
71     for (auto _ : state) {
72         for (size_t i = 0; i != objectCount; ++i) {
73             objects.pop_back();
74             objects.push_back(objr.Create(ClassId::BenchmarkType));
75         }
76     }
77 }
78 
79 BENCHMARK(ConstructObject);
80 BENCHMARK(ConstructProperty);
81 BENCHMARK(ManyObjects);
82 BENCHMARK(ManyObjectsWithAddAndRemove);
83 
84 } // namespace benchmarks
85 META_END_NAMESPACE()
86