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 <benchmark/benchmark.h>
16 #include <climits>
17 #include <gtest/gtest.h>
18 #include "hdf_base.h"
19 #include "hdf_log.h"
20 #include "v1_0/effect_types.h"
21 #include "v1_0/ieffect_control.h"
22 #include "v1_0/ieffect_model.h"
23 #include "include/effect_common.h"
24 #include "osal_mem.h"
25
26 using namespace std;
27 using namespace testing::ext;
28 constexpr bool IS_DIRECTLY_CALL = false;
29 constexpr uint32_t MAX_DESCRIPTOR_NUM = 20;
30
31 namespace {
32 const int32_t ITERATION_FREQUENCY = 100;
33 const int32_t REPETITION_FREQUENCY = 3;
34 class EffectModelBenchmarkTest : public benchmark::Fixture {
35 public:
36 struct IEffectModel *model_ = nullptr;
37 struct ControllerId contollerId_;
38 char *libName_ = nullptr;
39 char *effectId_ = nullptr;
40 virtual void SetUp(const ::benchmark::State &state);
41 virtual void TearDown(const ::benchmark::State &state);
42 };
43
SetUp(const::benchmark::State & state)44 void EffectModelBenchmarkTest::SetUp(const ::benchmark::State &state)
45 {
46 // input testcase setup step,setup invoked before each testcases
47 libName_ = strdup("libmock_effect_lib");
48 effectId_ = strdup("aaaabbbb-8888-9999-6666-aabbccdd9966ff");
49 model_ = IEffectModelGet(IS_DIRECTLY_CALL);
50 ASSERT_NE(nullptr, model_);
51 }
52
TearDown(const::benchmark::State & state)53 void EffectModelBenchmarkTest::TearDown(const ::benchmark::State &state)
54 {
55 // input testcase teardown step,teardown invoked after each testcases
56 if (libName_ != nullptr) {
57 free(libName_);
58 libName_ = nullptr;
59 }
60
61 if (effectId_ != nullptr) {
62 free(effectId_);
63 effectId_ = nullptr;
64 }
65
66 if (model_ != nullptr) {
67 IEffectModelRelease(model_, IS_DIRECTLY_CALL);
68 }
69 }
70
71 /**
72 * @tc.name: HdfAudioIsSupplyEffectLibs
73 * @tc.desc: Verify the EffectModelIsSupplyEffectLibs function.
74 */
BENCHMARK_F(EffectModelBenchmarkTest,HdfAudioIsSupplyEffectLibs)75 BENCHMARK_F(EffectModelBenchmarkTest, HdfAudioIsSupplyEffectLibs)(benchmark::State &state)
76 {
77 int32_t ret;
78 bool isSupport = false;
79 for (auto _ : state) {
80 ret = model_->IsSupplyEffectLibs(model_, &isSupport);
81 }
82 EXPECT_EQ(ret, HDF_SUCCESS);
83 }
84 BENCHMARK_REGISTER_F(EffectModelBenchmarkTest, HdfAudioIsSupplyEffectLibs)->
85 Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
86
87 /**
88 * @tc.name: HdfAudioGetAllEffectDescriptors
89 * @tc.desc: Verify the EffectModelGetAllEffectDescriptors function.
90 */
BENCHMARK_F(EffectModelBenchmarkTest,HdfAudioGetAllEffectDescriptors)91 BENCHMARK_F(EffectModelBenchmarkTest, HdfAudioGetAllEffectDescriptors)(benchmark::State &state)
92 {
93 int32_t ret;
94 uint32_t descsLen = MAX_DESCRIPTOR_NUM;
95 struct EffectControllerDescriptor descs[MAX_DESCRIPTOR_NUM];
96 for (auto _ : state) {
97 ret = model_->GetAllEffectDescriptors(model_, descs, &descsLen);
98 }
99 ASSERT_EQ(ret, HDF_SUCCESS);
100 EXPECT_GE(MAX_DESCRIPTOR_NUM, descsLen);
101 }
102 BENCHMARK_REGISTER_F(EffectModelBenchmarkTest, HdfAudioGetAllEffectDescriptors)->
103 Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
104 /**
105 * @tc.name: HdfAudioCreateDestroyController
106 * @tc.desc: Verify the EffectModelCreateEffectController and EffectModelDestroyEffectController function.
107 */
BENCHMARK_F(EffectModelBenchmarkTest,HdfAudioCreateDestroyController)108 BENCHMARK_F(EffectModelBenchmarkTest, HdfAudioCreateDestroyController)(benchmark::State &state)
109 {
110 int32_t ret;
111 struct EffectInfo info = {
112 .libName = libName_,
113 .effectId = effectId_,
114 .ioDirection = 1,
115 };
116 for (auto _ : state) {
117 struct IEffectControl *contoller = NULL;
118 ret = model_->CreateEffectController(model_, &info, &contoller, &contollerId_);
119 if (ret == HDF_SUCCESS) {
120 ASSERT_NE(contoller, nullptr);
121 }
122
123 if (contoller != nullptr) {
124 ret = model_->DestroyEffectController(model_, &contollerId_);
125 }
126 }
127 }
128 BENCHMARK_REGISTER_F(EffectModelBenchmarkTest, HdfAudioCreateDestroyController)->
129 Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
130
131 /**
132 * @tc.name: HdfAudioGetEffectDescriptor
133 * @tc.desc: Verify the EffectModelGetEffectDescriptor function.
134 */
BENCHMARK_F(EffectModelBenchmarkTest,HdfAudioGetEffectDescriptor)135 BENCHMARK_F(EffectModelBenchmarkTest, HdfAudioGetEffectDescriptor)(benchmark::State &state)
136 {
137 int32_t ret;
138 struct EffectControllerDescriptor desc;
139 for (auto _ : state) {
140 ret = model_->GetEffectDescriptor(model_, effectId_, &desc);
141 }
142 ASSERT_EQ(ret, HDF_SUCCESS);
143 EffectControllerReleaseDesc(&desc);
144 }
145 BENCHMARK_REGISTER_F(EffectModelBenchmarkTest, HdfAudioCreateDestroyController)->
146 Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
147 } // end of namespace
148 BENCHMARK_MAIN();