• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include <benchmark/benchmark.h>
17 #include <climits>
18 #include <gtest/gtest.h>
19 #include "hdf_base.h"
20 #include "hdf_log.h"
21 #include "v1_0/effect_types.h"
22 #include "v1_0/ieffect_control.h"
23 #include "v1_0/ieffect_model.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 const int32_t HDF_EFFECT_NUM_MAX = 32;
35 
36 class AudioEffectModelBenchmarkTest : public benchmark::Fixture {
37 public:
38     struct IEffectModel *model_ = nullptr;
39     struct ControllerId contollerId_;
40     char *libName_ = nullptr;
41     char *effectId_ = nullptr;
42     virtual void SetUp(const ::benchmark::State &state);
43     virtual void TearDown(const ::benchmark::State &state);
44 };
45 
EffectControllerReleaseDesc(struct EffectControllerDescriptor * desc)46 void EffectControllerReleaseDesc(struct EffectControllerDescriptor *desc)
47 {
48     if (desc == nullptr) {
49         return;
50     }
51 
52     OsalMemFree(desc->effectId);
53     desc->effectId = nullptr;
54 
55     OsalMemFree(desc->effectName);
56     desc->effectName = nullptr;
57 
58     OsalMemFree(desc->libName);
59     desc->libName = nullptr;
60 
61     OsalMemFree(desc->supplier);
62     desc->supplier = nullptr;
63 }
64 
EffectControllerReleaseDescs(struct EffectControllerDescriptor * descs,const uint32_t * descsLen)65 void EffectControllerReleaseDescs(struct EffectControllerDescriptor *descs, const uint32_t *descsLen)
66 {
67     if (descs == nullptr || descsLen == nullptr || *descsLen == 0 || *descsLen > HDF_EFFECT_NUM_MAX) {
68         return;
69     }
70 
71     for (uint32_t i = 0; i < *descsLen; i++) {
72         EffectControllerReleaseDesc(&descs[i]);
73     }
74 }
75 
SetUp(const::benchmark::State & state)76 void AudioEffectModelBenchmarkTest::SetUp(const ::benchmark::State &state)
77 {
78     // input testcase setup step,setup invoked before each testcases
79     libName_ = strdup("libmock_effect_lib");
80     effectId_ = strdup("aaaabbbb-8888-9999-6666-aabbccdd9966ff");
81     model_ = IEffectModelGet(IS_DIRECTLY_CALL);
82     ASSERT_NE(nullptr, model_);
83 }
84 
TearDown(const::benchmark::State & state)85 void AudioEffectModelBenchmarkTest::TearDown(const ::benchmark::State &state)
86 {
87     // input testcase teardown step,teardown invoked after each testcases
88     if (libName_ != nullptr) {
89         free(libName_);
90         libName_ = nullptr;
91     }
92 
93     if (effectId_ != nullptr) {
94         free(effectId_);
95         effectId_ = nullptr;
96     }
97 
98     if (model_ != nullptr) {
99         IEffectModelRelease(model_, IS_DIRECTLY_CALL);
100     }
101 }
102 
BENCHMARK_F(AudioEffectModelBenchmarkTest,IsSupplyEffectLibs)103 BENCHMARK_F(AudioEffectModelBenchmarkTest, IsSupplyEffectLibs)(benchmark::State &state)
104 {
105     ASSERT_NE(model_, nullptr);
106     int32_t ret;
107     bool isSupport = false;
108 
109     for (auto _ : state) {
110         ret = model_->IsSupplyEffectLibs(model_, &isSupport);
111         EXPECT_EQ(HDF_SUCCESS, ret);
112     }
113 }
114 
115 BENCHMARK_REGISTER_F(AudioEffectModelBenchmarkTest, IsSupplyEffectLibs)->
116     Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
117 
BENCHMARK_F(AudioEffectModelBenchmarkTest,GetAllEffectDescriptors)118 BENCHMARK_F(AudioEffectModelBenchmarkTest, GetAllEffectDescriptors)(benchmark::State &state)
119 {
120     ASSERT_NE(model_, nullptr);
121     int32_t ret;
122     uint32_t descsLen = MAX_DESCRIPTOR_NUM;
123     struct EffectControllerDescriptor descs[MAX_DESCRIPTOR_NUM];
124 
125     for (auto _ : state) {
126         ret = model_->GetAllEffectDescriptors(model_, descs, &descsLen);
127         EXPECT_EQ(HDF_SUCCESS, ret);
128     }
129     EXPECT_GE(MAX_DESCRIPTOR_NUM, descsLen);
130 
131     for (uint32_t i = 0; i < descsLen; i++) {
132         EXPECT_NE(nullptr, descs[i].effectId);
133     }
134 
135     EffectControllerReleaseDescs(descs, &descsLen);
136 }
137 
138 BENCHMARK_REGISTER_F(AudioEffectModelBenchmarkTest, GetAllEffectDescriptors)->
139     Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
140 
BENCHMARK_F(AudioEffectModelBenchmarkTest,CreateAndDestroyEffectController)141 BENCHMARK_F(AudioEffectModelBenchmarkTest, CreateAndDestroyEffectController)(benchmark::State &state)
142 {
143     ASSERT_NE(model_, nullptr);
144     int32_t ret;
145     struct EffectInfo info = {
146         .libName = libName_,
147         .effectId = effectId_,
148         .ioDirection = 1,
149     };
150     struct IEffectControl *contoller = NULL;
151 
152     for (auto _ : state) {
153         ret = model_->CreateEffectController(model_, &info, &contoller, &contollerId_);
154         EXPECT_EQ(HDF_SUCCESS, ret);
155         ret = model_->DestroyEffectController(model_, &contollerId_);
156         EXPECT_EQ(HDF_SUCCESS, ret);
157     }
158 }
159 
160 BENCHMARK_REGISTER_F(AudioEffectModelBenchmarkTest, CreateAndDestroyEffectController)->
161     Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
162 
BENCHMARK_F(AudioEffectModelBenchmarkTest,GetEffectDescriptor)163 BENCHMARK_F(AudioEffectModelBenchmarkTest, GetEffectDescriptor)(benchmark::State &state)
164 {
165     ASSERT_NE(model_, nullptr);
166     int32_t ret;
167     struct EffectControllerDescriptor desc;
168 
169     for (auto _ : state) {
170         ret = model_->GetEffectDescriptor(model_, effectId_, &desc);
171         EXPECT_EQ(HDF_SUCCESS, ret);
172     }
173     EXPECT_STREQ(desc.effectId, effectId_);
174     EXPECT_STREQ(desc.effectName, "mock_effect");
175     EXPECT_STREQ(desc.libName, libName_);
176     EXPECT_STREQ(desc.supplier, "mock");
177     EffectControllerReleaseDesc(&desc);
178 }
179 
180 BENCHMARK_REGISTER_F(AudioEffectModelBenchmarkTest, GetEffectDescriptor)->
181     Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
182 }
183