• 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 #include <meta/ext/object_fwd.h>
22 
23 #include "property_utils.h"
24 
25 META_BEGIN_NAMESPACE()
26 namespace benchmarks {
27 namespace {
28 
29 #define TEST_INTERFACE(num)                                                                                   \
30     class ITestInterface##num : public CORE_NS::IInterface {                                                  \
31         META_INTERFACE(CORE_NS::IInterface, ITestInterface##num, MakeUid("ItestInterface" #num, "TestType")) \
32     };
33 
34 TEST_INTERFACE(1)
35 TEST_INTERFACE(2)
36 TEST_INTERFACE(3)
37 TEST_INTERFACE(4)
38 TEST_INTERFACE(5)
39 TEST_INTERFACE(6)
40 TEST_INTERFACE(7)
41 TEST_INTERFACE(8)
42 TEST_INTERFACE(9)
43 TEST_INTERFACE(10)
44 
45 META_REGISTER_CLASS(ManyInterfacesTest, "6ac0d536-d0d9-4b4d-9db1-9833252f39de", ObjectCategoryBits::NO_CATEGORY)
46 
47 struct ManyInterfacesTest
48     : IntroduceInterfaces<ObjectFwd, ITestInterface1, ITestInterface2, ITestInterface3, ITestInterface4,
49           ITestInterface5, ITestInterface6, ITestInterface7, ITestInterface8, ITestInterface9, ITestInterface10> {
50     META_OBJECT(ManyInterfacesTest, ClassId::ManyInterfacesTest, IntroduceInterfaces)
51 };
52 
53 } // namespace
54 
ObjectGetInterface(benchmark::State & state)55 void ObjectGetInterface(benchmark::State& state)
56 {
57     auto obj = CreateBenchmarkType();
58     for (auto _ : state) {
59         benchmark::DoNotOptimize(obj->GetInterface(IAttachment::UID));
60         benchmark::DoNotOptimize(obj->GetInterface(ILifecycle::UID));
61     }
62 }
63 
ObjectGetInterfaceMany(benchmark::State & state)64 void ObjectGetInterfaceMany(benchmark::State& state)
65 {
66     META_NS::RegisterObjectType<ManyInterfacesTest>();
67     auto obj = GetObjectRegistry().Create(ClassId::ManyInterfacesTest);
68     for (auto _ : state) {
69         benchmark::DoNotOptimize(obj->GetInterface(ITestInterface1::UID));
70         benchmark::DoNotOptimize(obj->GetInterface(ITestInterface10::UID));
71     }
72     META_NS::UnregisterObjectType<ManyInterfacesTest>();
73 }
74 
75 class IUnknownInterface : public CORE_NS::IInterface {
76     META_INTERFACE(CORE_NS::IInterface, IUnknownInterface, MakeUid("IUnknownInterface", "TestType"))
77 };
78 
ObjectGetInterfaceFail(benchmark::State & state)79 void ObjectGetInterfaceFail(benchmark::State& state)
80 {
81     META_NS::RegisterObjectType<ManyInterfacesTest>();
82     auto obj = GetObjectRegistry().Create(ClassId::ManyInterfacesTest);
83     for (auto _ : state) {
84         benchmark::DoNotOptimize(obj->GetInterface(IUnknownInterface::UID));
85     }
86     META_NS::UnregisterObjectType<ManyInterfacesTest>();
87 }
88 
89 BENCHMARK(ObjectGetInterface);
90 BENCHMARK(ObjectGetInterfaceMany);
91 BENCHMARK(ObjectGetInterfaceFail);
92 
93 } // namespace benchmarks
94 META_END_NAMESPACE()
95