1 /*
2  * Copyright (c) 2023 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 #include <gtest/gtest.h>
16 #include <benchmark/benchmark.h>
17 #include "v3_0/codec_callback_service.h"
18 #include "v3_0/icodec_callback.h"
19 #include "v3_0/icodec_component_manager.h"
20 using namespace std;
21 using namespace testing::ext;
22 using OHOS::sptr;
23 using namespace OHOS::HDI::Codec::V3_0;
24 constexpr int64_t APP_DATA = 3;
25 namespace {
26 const int32_t ITERATION_FREQUENCY = 100;
27 const int32_t REPETITION_FREQUENCY = 3;
28 class CodecBenchmarkManagerTest : public benchmark::Fixture {
29 public:
SetUp(const::benchmark::State & state)30     void SetUp(const ::benchmark::State &state)
31     {
32         manager_ = ICodecComponentManager::Get();
33         callback_ = new CodecCallbackService();
34     }
TearDown(const::benchmark::State & state)35     void TearDown(const ::benchmark::State &state)
36     {
37         manager_ = nullptr;
38         callback_ = nullptr;
39     }
40 
41 public:
42     sptr<ICodecComponentManager> manager_;
43     sptr<ICodecCallback> callback_;
44 };
45 
BENCHMARK_F(CodecBenchmarkManagerTest,GetComponentNum)46 BENCHMARK_F(CodecBenchmarkManagerTest, GetComponentNum)(benchmark::State &state)
47 {
48     ASSERT_TRUE(manager_ != nullptr);
49     int32_t count = 0;
50     int32_t ret;
51     for (auto _ : state) {
52         ret = manager_->GetComponentNum(count);
53         ASSERT_EQ(ret, HDF_SUCCESS);
54         EXPECT_TRUE(count >= 0);
55     }
56 }
57 
58 BENCHMARK_REGISTER_F(CodecBenchmarkManagerTest, GetComponentNum)->
59     Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
60 
BENCHMARK_F(CodecBenchmarkManagerTest,GetComponentCapabilityList)61 BENCHMARK_F(CodecBenchmarkManagerTest, GetComponentCapabilityList)(benchmark::State &state)
62 {
63     ASSERT_TRUE(manager_ != nullptr);
64     int32_t count = 0;
65     auto ret = manager_->GetComponentNum(count);
66     ASSERT_EQ(ret, HDF_SUCCESS);
67     ASSERT_TRUE(count > 0);
68 
69     std::vector<CodecCompCapability> capList;
70     for (auto _ : state) {
71         ret = manager_->GetComponentCapabilityList(capList, count);
72         ASSERT_EQ(ret, HDF_SUCCESS);
73     }
74 }
75 
76 BENCHMARK_REGISTER_F(CodecBenchmarkManagerTest, GetComponentCapabilityList)->
77     Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
78 
BENCHMARK_F(CodecBenchmarkManagerTest,CreateComponent)79 BENCHMARK_F(CodecBenchmarkManagerTest, CreateComponent)(benchmark::State &state)
80 {
81     ASSERT_TRUE(manager_ != nullptr);
82     std::string compName("");
83     int32_t count = 0;
84     auto ret = manager_->GetComponentNum(count);
85     ASSERT_EQ(ret, HDF_SUCCESS);
86     ASSERT_TRUE(count > 0);
87 
88     std::vector<CodecCompCapability> capList;
89     ret = manager_->GetComponentCapabilityList(capList, count);
90     ASSERT_EQ(ret, HDF_SUCCESS);
91 
92     compName = capList[0].compName;
93     ASSERT_FALSE(compName.empty());
94     sptr<ICodecComponent> component;
95     uint32_t componentId = 0;
96     for (auto _ : state) {
97         ret = manager_->CreateComponent(component, componentId, compName, APP_DATA, callback_);
98         ASSERT_EQ(ret, HDF_SUCCESS);
99         if (componentId != 0) {
100             manager_->DestroyComponent(componentId);
101         }
102     }
103 }
104 
105 BENCHMARK_REGISTER_F(CodecBenchmarkManagerTest, CreateComponent)->
106     Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
107 
BENCHMARK_F(CodecBenchmarkManagerTest,DestroyComponent)108 BENCHMARK_F(CodecBenchmarkManagerTest, DestroyComponent)(benchmark::State &state)
109 {
110     ASSERT_TRUE(manager_ != nullptr);
111     std::string compName("");
112     int32_t count = 0;
113     auto ret = manager_->GetComponentNum(count);
114     ASSERT_EQ(ret, HDF_SUCCESS);
115     ASSERT_TRUE(count > 0);
116 
117     std::vector<CodecCompCapability> capList;
118     ret = manager_->GetComponentCapabilityList(capList, count);
119     ASSERT_EQ(ret, HDF_SUCCESS);
120 
121     compName = capList[0].compName;
122     ASSERT_FALSE(compName.empty());
123     sptr<ICodecComponent> component;
124     uint32_t componentId = 0;
125     for (auto _ : state) {
126         ret = manager_->CreateComponent(component, componentId, compName, APP_DATA, callback_);
127         ASSERT_EQ(ret, HDF_SUCCESS);
128         if (componentId != 0) {
129             manager_->DestroyComponent(componentId);
130         }
131     }
132 }
133 
134 BENCHMARK_REGISTER_F(CodecBenchmarkManagerTest, DestroyComponent)->
135     Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
136 }  // namespace
137