1094332d3Sopenharmony_ci/*
2094332d3Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4094332d3Sopenharmony_ci * you may not use this file except in compliance with the License.
5094332d3Sopenharmony_ci * You may obtain a copy of the License at
6094332d3Sopenharmony_ci *
7094332d3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8094332d3Sopenharmony_ci *
9094332d3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and
13094332d3Sopenharmony_ci * limitations under the License.
14094332d3Sopenharmony_ci */
15094332d3Sopenharmony_ci
16094332d3Sopenharmony_ci#include <benchmark/benchmark.h>
17094332d3Sopenharmony_ci#include <climits>
18094332d3Sopenharmony_ci#include <gtest/gtest.h>
19094332d3Sopenharmony_ci#include "hdf_base.h"
20094332d3Sopenharmony_ci#include "hdf_log.h"
21094332d3Sopenharmony_ci#include "v1_0/effect_types.h"
22094332d3Sopenharmony_ci#include "v1_0/ieffect_control.h"
23094332d3Sopenharmony_ci#include "v1_0/ieffect_model.h"
24094332d3Sopenharmony_ci#include "osal_mem.h"
25094332d3Sopenharmony_ci
26094332d3Sopenharmony_ciusing namespace std;
27094332d3Sopenharmony_ciusing namespace testing::ext;
28094332d3Sopenharmony_ciconstexpr bool IS_DIRECTLY_CALL = false;
29094332d3Sopenharmony_ci/* the input buffer len of the send command */
30094332d3Sopenharmony_ciconstexpr uint32_t SEND_COMMAND_LEN = 10;
31094332d3Sopenharmony_ci/* the output buffer len of the command */
32094332d3Sopenharmony_ciconstexpr uint32_t GET_BUFFER_LEN = 10;
33094332d3Sopenharmony_ci# define AUDIO_EFFECT_COMMAND_INVALID_LARGE 20
34094332d3Sopenharmony_ci
35094332d3Sopenharmony_cinamespace {
36094332d3Sopenharmony_ciconst int32_t ITERATION_FREQUENCY = 100;
37094332d3Sopenharmony_ciconst int32_t REPETITION_FREQUENCY = 3;
38094332d3Sopenharmony_ci
39094332d3Sopenharmony_ciclass AudioEffectControlBenchmarkTest : public benchmark::Fixture {
40094332d3Sopenharmony_cipublic:
41094332d3Sopenharmony_ci    struct IEffectControl *controller_ = nullptr;
42094332d3Sopenharmony_ci    struct IEffectModel *model_ = nullptr;
43094332d3Sopenharmony_ci    struct ControllerId contollerId_;
44094332d3Sopenharmony_ci    virtual void SetUp(const ::benchmark::State &state);
45094332d3Sopenharmony_ci    virtual void TearDown(const ::benchmark::State &state);
46094332d3Sopenharmony_ci    char *libName_ = nullptr;
47094332d3Sopenharmony_ci    char *effectId_ = nullptr;
48094332d3Sopenharmony_ci};
49094332d3Sopenharmony_ci
50094332d3Sopenharmony_civoid EffectControllerReleaseDesc(struct EffectControllerDescriptor *desc)
51094332d3Sopenharmony_ci{
52094332d3Sopenharmony_ci    if (desc == nullptr) {
53094332d3Sopenharmony_ci        return;
54094332d3Sopenharmony_ci    }
55094332d3Sopenharmony_ci
56094332d3Sopenharmony_ci    OsalMemFree(desc->effectId);
57094332d3Sopenharmony_ci    desc->effectId = nullptr;
58094332d3Sopenharmony_ci
59094332d3Sopenharmony_ci    OsalMemFree(desc->effectName);
60094332d3Sopenharmony_ci    desc->effectName = nullptr;
61094332d3Sopenharmony_ci
62094332d3Sopenharmony_ci    OsalMemFree(desc->libName);
63094332d3Sopenharmony_ci    desc->libName = nullptr;
64094332d3Sopenharmony_ci
65094332d3Sopenharmony_ci    OsalMemFree(desc->supplier);
66094332d3Sopenharmony_ci    desc->supplier = nullptr;
67094332d3Sopenharmony_ci}
68094332d3Sopenharmony_ci
69094332d3Sopenharmony_civoid AudioEffectControlBenchmarkTest::SetUp(const ::benchmark::State &state)
70094332d3Sopenharmony_ci{
71094332d3Sopenharmony_ci    // input testcase setup step,setup invoked before each testcases
72094332d3Sopenharmony_ci    libName_ = strdup("libmock_effect_lib");
73094332d3Sopenharmony_ci    effectId_ = strdup("aaaabbbb-8888-9999-6666-aabbccdd9966ff");
74094332d3Sopenharmony_ci    struct EffectInfo info = {
75094332d3Sopenharmony_ci        .libName = libName_,
76094332d3Sopenharmony_ci        .effectId = effectId_,
77094332d3Sopenharmony_ci        .ioDirection = 1,
78094332d3Sopenharmony_ci    };
79094332d3Sopenharmony_ci
80094332d3Sopenharmony_ci    model_ = IEffectModelGet(IS_DIRECTLY_CALL);
81094332d3Sopenharmony_ci    ASSERT_NE(model_, nullptr);
82094332d3Sopenharmony_ci
83094332d3Sopenharmony_ci    int32_t ret = model_->CreateEffectController(model_, &info, &controller_, &contollerId_);
84094332d3Sopenharmony_ci    ASSERT_EQ(ret, HDF_SUCCESS);
85094332d3Sopenharmony_ci    ASSERT_NE(controller_, nullptr);
86094332d3Sopenharmony_ci}
87094332d3Sopenharmony_ci
88094332d3Sopenharmony_civoid AudioEffectControlBenchmarkTest::TearDown(const ::benchmark::State &state)
89094332d3Sopenharmony_ci{
90094332d3Sopenharmony_ci    // input testcase teardown step,teardown invoked after each testcases
91094332d3Sopenharmony_ci    if (libName_ != nullptr) {
92094332d3Sopenharmony_ci        free(libName_);
93094332d3Sopenharmony_ci        libName_ = nullptr;
94094332d3Sopenharmony_ci    }
95094332d3Sopenharmony_ci
96094332d3Sopenharmony_ci    if (effectId_ != nullptr) {
97094332d3Sopenharmony_ci        free(effectId_);
98094332d3Sopenharmony_ci        effectId_ = nullptr;
99094332d3Sopenharmony_ci    }
100094332d3Sopenharmony_ci
101094332d3Sopenharmony_ci    if (controller_ != nullptr && model_ != nullptr) {
102094332d3Sopenharmony_ci        int32_t ret = model_->DestroyEffectController(model_, &contollerId_);
103094332d3Sopenharmony_ci        EXPECT_EQ(ret, HDF_SUCCESS);
104094332d3Sopenharmony_ci    }
105094332d3Sopenharmony_ci
106094332d3Sopenharmony_ci    if (model_ != nullptr) {
107094332d3Sopenharmony_ci        IEffectModelRelease(model_, IS_DIRECTLY_CALL);
108094332d3Sopenharmony_ci    }
109094332d3Sopenharmony_ci}
110094332d3Sopenharmony_ci
111094332d3Sopenharmony_ciBENCHMARK_F(AudioEffectControlBenchmarkTest, EffectProcess)(benchmark::State &state)
112094332d3Sopenharmony_ci{
113094332d3Sopenharmony_ci    ASSERT_NE(controller_, nullptr);
114094332d3Sopenharmony_ci    int32_t ret;
115094332d3Sopenharmony_ci    struct AudioEffectBuffer input = {0};
116094332d3Sopenharmony_ci    struct AudioEffectBuffer output = {0};
117094332d3Sopenharmony_ci
118094332d3Sopenharmony_ci    for (auto _ : state) {
119094332d3Sopenharmony_ci        ret = controller_->EffectProcess(controller_, &input, &output);
120094332d3Sopenharmony_ci        EXPECT_EQ(HDF_SUCCESS, ret);
121094332d3Sopenharmony_ci    }
122094332d3Sopenharmony_ci}
123094332d3Sopenharmony_ci
124094332d3Sopenharmony_ciBENCHMARK_REGISTER_F(AudioEffectControlBenchmarkTest, EffectProcess)->
125094332d3Sopenharmony_ci    Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
126094332d3Sopenharmony_ci
127094332d3Sopenharmony_ciBENCHMARK_F(AudioEffectControlBenchmarkTest, SendCommand)(benchmark::State &state)
128094332d3Sopenharmony_ci{
129094332d3Sopenharmony_ci    ASSERT_NE(controller_, nullptr);
130094332d3Sopenharmony_ci    int32_t ret;
131094332d3Sopenharmony_ci    int8_t input[SEND_COMMAND_LEN] = {0};
132094332d3Sopenharmony_ci    int8_t output[GET_BUFFER_LEN] = {0};
133094332d3Sopenharmony_ci    uint32_t replyLen = GET_BUFFER_LEN;
134094332d3Sopenharmony_ci
135094332d3Sopenharmony_ci    for (auto _ : state) {
136094332d3Sopenharmony_ci        ret = controller_->SendCommand(controller_, AUDIO_EFFECT_COMMAND_INIT_CONTOLLER,
137094332d3Sopenharmony_ci                                       input, SEND_COMMAND_LEN, output, &replyLen);
138094332d3Sopenharmony_ci        EXPECT_EQ(HDF_SUCCESS, ret);
139094332d3Sopenharmony_ci    }
140094332d3Sopenharmony_ci}
141094332d3Sopenharmony_ci
142094332d3Sopenharmony_ciBENCHMARK_REGISTER_F(AudioEffectControlBenchmarkTest, SendCommand)->
143094332d3Sopenharmony_ci    Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
144094332d3Sopenharmony_ci
145094332d3Sopenharmony_ciBENCHMARK_F(AudioEffectControlBenchmarkTest, GetEffectDescriptor)(benchmark::State &state)
146094332d3Sopenharmony_ci{
147094332d3Sopenharmony_ci    ASSERT_NE(controller_, nullptr);
148094332d3Sopenharmony_ci    int32_t ret;
149094332d3Sopenharmony_ci    struct EffectControllerDescriptor desc;
150094332d3Sopenharmony_ci
151094332d3Sopenharmony_ci    for (auto _ : state) {
152094332d3Sopenharmony_ci        ret = controller_->GetEffectDescriptor(controller_, &desc);
153094332d3Sopenharmony_ci        EXPECT_EQ(HDF_SUCCESS, ret);
154094332d3Sopenharmony_ci    }
155094332d3Sopenharmony_ci    EXPECT_STREQ(desc.effectId, effectId_);
156094332d3Sopenharmony_ci    EXPECT_STREQ(desc.effectName, "mock_effect");
157094332d3Sopenharmony_ci    EXPECT_STREQ(desc.libName, libName_);
158094332d3Sopenharmony_ci    EXPECT_STREQ(desc.supplier, "mock");
159094332d3Sopenharmony_ci    EffectControllerReleaseDesc(&desc);
160094332d3Sopenharmony_ci}
161094332d3Sopenharmony_ci
162094332d3Sopenharmony_ciBENCHMARK_REGISTER_F(AudioEffectControlBenchmarkTest, GetEffectDescriptor)->
163094332d3Sopenharmony_ci    Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
164094332d3Sopenharmony_ci
165094332d3Sopenharmony_ciBENCHMARK_F(AudioEffectControlBenchmarkTest, EffectReverse)(benchmark::State &state)
166094332d3Sopenharmony_ci{
167094332d3Sopenharmony_ci    ASSERT_NE(controller_, nullptr);
168094332d3Sopenharmony_ci    int32_t ret;
169094332d3Sopenharmony_ci    struct AudioEffectBuffer input = {0};
170094332d3Sopenharmony_ci    struct AudioEffectBuffer output = {0};
171094332d3Sopenharmony_ci
172094332d3Sopenharmony_ci    for (auto _ : state) {
173094332d3Sopenharmony_ci        ret = controller_->EffectReverse(controller_, &input, &output);
174094332d3Sopenharmony_ci        EXPECT_EQ(HDF_SUCCESS, ret);
175094332d3Sopenharmony_ci    }
176094332d3Sopenharmony_ci}
177094332d3Sopenharmony_ci
178094332d3Sopenharmony_ciBENCHMARK_REGISTER_F(AudioEffectControlBenchmarkTest, EffectReverse)->
179094332d3Sopenharmony_ci    Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
180094332d3Sopenharmony_ci}
181