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_ciconstexpr uint32_t MAX_DESCRIPTOR_NUM = 20;
30094332d3Sopenharmony_ci
31094332d3Sopenharmony_cinamespace {
32094332d3Sopenharmony_ciconst int32_t ITERATION_FREQUENCY = 100;
33094332d3Sopenharmony_ciconst int32_t REPETITION_FREQUENCY = 3;
34094332d3Sopenharmony_ciconst int32_t HDF_EFFECT_NUM_MAX = 32;
35094332d3Sopenharmony_ci
36094332d3Sopenharmony_ciclass AudioEffectModelBenchmarkTest : public benchmark::Fixture {
37094332d3Sopenharmony_cipublic:
38094332d3Sopenharmony_ci    struct IEffectModel *model_ = nullptr;
39094332d3Sopenharmony_ci    struct ControllerId contollerId_;
40094332d3Sopenharmony_ci    char *libName_ = nullptr;
41094332d3Sopenharmony_ci    char *effectId_ = nullptr;
42094332d3Sopenharmony_ci    virtual void SetUp(const ::benchmark::State &state);
43094332d3Sopenharmony_ci    virtual void TearDown(const ::benchmark::State &state);
44094332d3Sopenharmony_ci};
45094332d3Sopenharmony_ci
46094332d3Sopenharmony_civoid EffectControllerReleaseDesc(struct EffectControllerDescriptor *desc)
47094332d3Sopenharmony_ci{
48094332d3Sopenharmony_ci    if (desc == nullptr) {
49094332d3Sopenharmony_ci        return;
50094332d3Sopenharmony_ci    }
51094332d3Sopenharmony_ci
52094332d3Sopenharmony_ci    OsalMemFree(desc->effectId);
53094332d3Sopenharmony_ci    desc->effectId = nullptr;
54094332d3Sopenharmony_ci
55094332d3Sopenharmony_ci    OsalMemFree(desc->effectName);
56094332d3Sopenharmony_ci    desc->effectName = nullptr;
57094332d3Sopenharmony_ci
58094332d3Sopenharmony_ci    OsalMemFree(desc->libName);
59094332d3Sopenharmony_ci    desc->libName = nullptr;
60094332d3Sopenharmony_ci
61094332d3Sopenharmony_ci    OsalMemFree(desc->supplier);
62094332d3Sopenharmony_ci    desc->supplier = nullptr;
63094332d3Sopenharmony_ci}
64094332d3Sopenharmony_ci
65094332d3Sopenharmony_civoid EffectControllerReleaseDescs(struct EffectControllerDescriptor *descs, const uint32_t *descsLen)
66094332d3Sopenharmony_ci{
67094332d3Sopenharmony_ci    if (descs == nullptr || descsLen == nullptr || *descsLen == 0 || *descsLen > HDF_EFFECT_NUM_MAX) {
68094332d3Sopenharmony_ci        return;
69094332d3Sopenharmony_ci    }
70094332d3Sopenharmony_ci
71094332d3Sopenharmony_ci    for (uint32_t i = 0; i < *descsLen; i++) {
72094332d3Sopenharmony_ci        EffectControllerReleaseDesc(&descs[i]);
73094332d3Sopenharmony_ci    }
74094332d3Sopenharmony_ci}
75094332d3Sopenharmony_ci
76094332d3Sopenharmony_civoid AudioEffectModelBenchmarkTest::SetUp(const ::benchmark::State &state)
77094332d3Sopenharmony_ci{
78094332d3Sopenharmony_ci    // input testcase setup step,setup invoked before each testcases
79094332d3Sopenharmony_ci    libName_ = strdup("libmock_effect_lib");
80094332d3Sopenharmony_ci    effectId_ = strdup("aaaabbbb-8888-9999-6666-aabbccdd9966ff");
81094332d3Sopenharmony_ci    model_ = IEffectModelGet(IS_DIRECTLY_CALL);
82094332d3Sopenharmony_ci    ASSERT_NE(nullptr, model_);
83094332d3Sopenharmony_ci}
84094332d3Sopenharmony_ci
85094332d3Sopenharmony_civoid AudioEffectModelBenchmarkTest::TearDown(const ::benchmark::State &state)
86094332d3Sopenharmony_ci{
87094332d3Sopenharmony_ci    // input testcase teardown step,teardown invoked after each testcases
88094332d3Sopenharmony_ci    if (libName_ != nullptr) {
89094332d3Sopenharmony_ci        free(libName_);
90094332d3Sopenharmony_ci        libName_ = nullptr;
91094332d3Sopenharmony_ci    }
92094332d3Sopenharmony_ci
93094332d3Sopenharmony_ci    if (effectId_ != nullptr) {
94094332d3Sopenharmony_ci        free(effectId_);
95094332d3Sopenharmony_ci        effectId_ = nullptr;
96094332d3Sopenharmony_ci    }
97094332d3Sopenharmony_ci
98094332d3Sopenharmony_ci    if (model_ != nullptr) {
99094332d3Sopenharmony_ci        IEffectModelRelease(model_, IS_DIRECTLY_CALL);
100094332d3Sopenharmony_ci    }
101094332d3Sopenharmony_ci}
102094332d3Sopenharmony_ci
103094332d3Sopenharmony_ciBENCHMARK_F(AudioEffectModelBenchmarkTest, IsSupplyEffectLibs)(benchmark::State &state)
104094332d3Sopenharmony_ci{
105094332d3Sopenharmony_ci    ASSERT_NE(model_, nullptr);
106094332d3Sopenharmony_ci    int32_t ret;
107094332d3Sopenharmony_ci    bool isSupport = false;
108094332d3Sopenharmony_ci
109094332d3Sopenharmony_ci    for (auto _ : state) {
110094332d3Sopenharmony_ci        ret = model_->IsSupplyEffectLibs(model_, &isSupport);
111094332d3Sopenharmony_ci        EXPECT_EQ(HDF_SUCCESS, ret);
112094332d3Sopenharmony_ci    }
113094332d3Sopenharmony_ci}
114094332d3Sopenharmony_ci
115094332d3Sopenharmony_ciBENCHMARK_REGISTER_F(AudioEffectModelBenchmarkTest, IsSupplyEffectLibs)->
116094332d3Sopenharmony_ci    Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
117094332d3Sopenharmony_ci
118094332d3Sopenharmony_ciBENCHMARK_F(AudioEffectModelBenchmarkTest, GetAllEffectDescriptors)(benchmark::State &state)
119094332d3Sopenharmony_ci{
120094332d3Sopenharmony_ci    ASSERT_NE(model_, nullptr);
121094332d3Sopenharmony_ci    int32_t ret;
122094332d3Sopenharmony_ci    uint32_t descsLen = MAX_DESCRIPTOR_NUM;
123094332d3Sopenharmony_ci    struct EffectControllerDescriptor descs[MAX_DESCRIPTOR_NUM];
124094332d3Sopenharmony_ci
125094332d3Sopenharmony_ci    for (auto _ : state) {
126094332d3Sopenharmony_ci        ret = model_->GetAllEffectDescriptors(model_, descs, &descsLen);
127094332d3Sopenharmony_ci        EXPECT_EQ(HDF_SUCCESS, ret);
128094332d3Sopenharmony_ci    }
129094332d3Sopenharmony_ci    EXPECT_GE(MAX_DESCRIPTOR_NUM, descsLen);
130094332d3Sopenharmony_ci
131094332d3Sopenharmony_ci    for (uint32_t i = 0; i < descsLen; i++) {
132094332d3Sopenharmony_ci        EXPECT_NE(nullptr, descs[i].effectId);
133094332d3Sopenharmony_ci    }
134094332d3Sopenharmony_ci
135094332d3Sopenharmony_ci    EffectControllerReleaseDescs(descs, &descsLen);
136094332d3Sopenharmony_ci}
137094332d3Sopenharmony_ci
138094332d3Sopenharmony_ciBENCHMARK_REGISTER_F(AudioEffectModelBenchmarkTest, GetAllEffectDescriptors)->
139094332d3Sopenharmony_ci    Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
140094332d3Sopenharmony_ci
141094332d3Sopenharmony_ciBENCHMARK_F(AudioEffectModelBenchmarkTest, CreateAndDestroyEffectController)(benchmark::State &state)
142094332d3Sopenharmony_ci{
143094332d3Sopenharmony_ci    ASSERT_NE(model_, nullptr);
144094332d3Sopenharmony_ci    int32_t ret;
145094332d3Sopenharmony_ci    struct EffectInfo info = {
146094332d3Sopenharmony_ci        .libName = libName_,
147094332d3Sopenharmony_ci        .effectId = effectId_,
148094332d3Sopenharmony_ci        .ioDirection = 1,
149094332d3Sopenharmony_ci    };
150094332d3Sopenharmony_ci    struct IEffectControl *contoller = NULL;
151094332d3Sopenharmony_ci
152094332d3Sopenharmony_ci    for (auto _ : state) {
153094332d3Sopenharmony_ci        ret = model_->CreateEffectController(model_, &info, &contoller, &contollerId_);
154094332d3Sopenharmony_ci        EXPECT_EQ(HDF_SUCCESS, ret);
155094332d3Sopenharmony_ci        ret = model_->DestroyEffectController(model_, &contollerId_);
156094332d3Sopenharmony_ci        EXPECT_EQ(HDF_SUCCESS, ret);
157094332d3Sopenharmony_ci    }
158094332d3Sopenharmony_ci}
159094332d3Sopenharmony_ci
160094332d3Sopenharmony_ciBENCHMARK_REGISTER_F(AudioEffectModelBenchmarkTest, CreateAndDestroyEffectController)->
161094332d3Sopenharmony_ci    Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
162094332d3Sopenharmony_ci
163094332d3Sopenharmony_ciBENCHMARK_F(AudioEffectModelBenchmarkTest, GetEffectDescriptor)(benchmark::State &state)
164094332d3Sopenharmony_ci{
165094332d3Sopenharmony_ci    ASSERT_NE(model_, nullptr);
166094332d3Sopenharmony_ci    int32_t ret;
167094332d3Sopenharmony_ci    struct EffectControllerDescriptor desc;
168094332d3Sopenharmony_ci
169094332d3Sopenharmony_ci    for (auto _ : state) {
170094332d3Sopenharmony_ci        ret = model_->GetEffectDescriptor(model_, effectId_, &desc);
171094332d3Sopenharmony_ci        EXPECT_EQ(HDF_SUCCESS, ret);
172094332d3Sopenharmony_ci    }
173094332d3Sopenharmony_ci    EXPECT_STREQ(desc.effectId, effectId_);
174094332d3Sopenharmony_ci    EXPECT_STREQ(desc.effectName, "mock_effect");
175094332d3Sopenharmony_ci    EXPECT_STREQ(desc.libName, libName_);
176094332d3Sopenharmony_ci    EXPECT_STREQ(desc.supplier, "mock");
177094332d3Sopenharmony_ci    EffectControllerReleaseDesc(&desc);
178094332d3Sopenharmony_ci}
179094332d3Sopenharmony_ci
180094332d3Sopenharmony_ciBENCHMARK_REGISTER_F(AudioEffectModelBenchmarkTest, GetEffectDescriptor)->
181094332d3Sopenharmony_ci    Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
182094332d3Sopenharmony_ci}
183