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 /* the input buffer len of the send command */
30 constexpr uint32_t SEND_COMMAND_LEN = 10;
31 /* the output buffer len of the command */
32 constexpr uint32_t GET_BUFFER_LEN = 10;
33 # define AUDIO_EFFECT_COMMAND_INVALID_LARGE 20
34 
35 namespace {
36 const int32_t ITERATION_FREQUENCY = 100;
37 const int32_t REPETITION_FREQUENCY = 3;
38 
39 class AudioEffectControlBenchmarkTest : public benchmark::Fixture {
40 public:
41     struct IEffectControl *controller_ = nullptr;
42     struct IEffectModel *model_ = nullptr;
43     struct ControllerId contollerId_;
44     virtual void SetUp(const ::benchmark::State &state);
45     virtual void TearDown(const ::benchmark::State &state);
46     char *libName_ = nullptr;
47     char *effectId_ = nullptr;
48 };
49 
EffectControllerReleaseDesc(struct EffectControllerDescriptor * desc)50 void EffectControllerReleaseDesc(struct EffectControllerDescriptor *desc)
51 {
52     if (desc == nullptr) {
53         return;
54     }
55 
56     OsalMemFree(desc->effectId);
57     desc->effectId = nullptr;
58 
59     OsalMemFree(desc->effectName);
60     desc->effectName = nullptr;
61 
62     OsalMemFree(desc->libName);
63     desc->libName = nullptr;
64 
65     OsalMemFree(desc->supplier);
66     desc->supplier = nullptr;
67 }
68 
SetUp(const::benchmark::State & state)69 void AudioEffectControlBenchmarkTest::SetUp(const ::benchmark::State &state)
70 {
71     // input testcase setup step,setup invoked before each testcases
72     libName_ = strdup("libmock_effect_lib");
73     effectId_ = strdup("aaaabbbb-8888-9999-6666-aabbccdd9966ff");
74     struct EffectInfo info = {
75         .libName = libName_,
76         .effectId = effectId_,
77         .ioDirection = 1,
78     };
79 
80     model_ = IEffectModelGet(IS_DIRECTLY_CALL);
81     ASSERT_NE(model_, nullptr);
82 
83     int32_t ret = model_->CreateEffectController(model_, &info, &controller_, &contollerId_);
84     ASSERT_EQ(ret, HDF_SUCCESS);
85     ASSERT_NE(controller_, nullptr);
86 }
87 
TearDown(const::benchmark::State & state)88 void AudioEffectControlBenchmarkTest::TearDown(const ::benchmark::State &state)
89 {
90     // input testcase teardown step,teardown invoked after each testcases
91     if (libName_ != nullptr) {
92         free(libName_);
93         libName_ = nullptr;
94     }
95 
96     if (effectId_ != nullptr) {
97         free(effectId_);
98         effectId_ = nullptr;
99     }
100 
101     if (controller_ != nullptr && model_ != nullptr) {
102         int32_t ret = model_->DestroyEffectController(model_, &contollerId_);
103         EXPECT_EQ(ret, HDF_SUCCESS);
104     }
105 
106     if (model_ != nullptr) {
107         IEffectModelRelease(model_, IS_DIRECTLY_CALL);
108     }
109 }
110 
BENCHMARK_F(AudioEffectControlBenchmarkTest,EffectProcess)111 BENCHMARK_F(AudioEffectControlBenchmarkTest, EffectProcess)(benchmark::State &state)
112 {
113     ASSERT_NE(controller_, nullptr);
114     int32_t ret;
115     struct AudioEffectBuffer input = {0};
116     struct AudioEffectBuffer output = {0};
117 
118     for (auto _ : state) {
119         ret = controller_->EffectProcess(controller_, &input, &output);
120         EXPECT_EQ(HDF_SUCCESS, ret);
121     }
122 }
123 
124 BENCHMARK_REGISTER_F(AudioEffectControlBenchmarkTest, EffectProcess)->
125     Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
126 
BENCHMARK_F(AudioEffectControlBenchmarkTest,SendCommand)127 BENCHMARK_F(AudioEffectControlBenchmarkTest, SendCommand)(benchmark::State &state)
128 {
129     ASSERT_NE(controller_, nullptr);
130     int32_t ret;
131     int8_t input[SEND_COMMAND_LEN] = {0};
132     int8_t output[GET_BUFFER_LEN] = {0};
133     uint32_t replyLen = GET_BUFFER_LEN;
134 
135     for (auto _ : state) {
136         ret = controller_->SendCommand(controller_, AUDIO_EFFECT_COMMAND_INIT_CONTOLLER,
137                                        input, SEND_COMMAND_LEN, output, &replyLen);
138         EXPECT_EQ(HDF_SUCCESS, ret);
139     }
140 }
141 
142 BENCHMARK_REGISTER_F(AudioEffectControlBenchmarkTest, SendCommand)->
143     Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
144 
BENCHMARK_F(AudioEffectControlBenchmarkTest,GetEffectDescriptor)145 BENCHMARK_F(AudioEffectControlBenchmarkTest, GetEffectDescriptor)(benchmark::State &state)
146 {
147     ASSERT_NE(controller_, nullptr);
148     int32_t ret;
149     struct EffectControllerDescriptor desc;
150 
151     for (auto _ : state) {
152         ret = controller_->GetEffectDescriptor(controller_, &desc);
153         EXPECT_EQ(HDF_SUCCESS, ret);
154     }
155     EXPECT_STREQ(desc.effectId, effectId_);
156     EXPECT_STREQ(desc.effectName, "mock_effect");
157     EXPECT_STREQ(desc.libName, libName_);
158     EXPECT_STREQ(desc.supplier, "mock");
159     EffectControllerReleaseDesc(&desc);
160 }
161 
162 BENCHMARK_REGISTER_F(AudioEffectControlBenchmarkTest, GetEffectDescriptor)->
163     Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
164 
BENCHMARK_F(AudioEffectControlBenchmarkTest,EffectReverse)165 BENCHMARK_F(AudioEffectControlBenchmarkTest, EffectReverse)(benchmark::State &state)
166 {
167     ASSERT_NE(controller_, nullptr);
168     int32_t ret;
169     struct AudioEffectBuffer input = {0};
170     struct AudioEffectBuffer output = {0};
171 
172     for (auto _ : state) {
173         ret = controller_->EffectReverse(controller_, &input, &output);
174         EXPECT_EQ(HDF_SUCCESS, ret);
175     }
176 }
177 
178 BENCHMARK_REGISTER_F(AudioEffectControlBenchmarkTest, EffectReverse)->
179     Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
180 }
181