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