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 "osal_mem.h"
21 #include "v2_0/iaudio_manager.h"
22
23 using namespace testing::ext;
24 using namespace std;
25
26 namespace {
27 static const uint32_t g_audioAdapterNumMax = 5;
28 const int32_t ITERATION_FREQUENCY = 100;
29 const int32_t REPETITION_FREQUENCY = 3;
30
31 class AudioManagerBenchmarkTest : public benchmark::Fixture {
32 public:
33 struct IAudioManager *manager_ = nullptr;
34 struct AudioAdapterDescriptor *adapterDescs_ = nullptr;
35 void SetUp(const ::benchmark::State &state);
36 void TearDown(const ::benchmark::State &state);
37 void AudioAdapterDescriptorFree(struct AudioAdapterDescriptor *dataBlock, bool freeSelf);
38 void ReleaseAdapterDescs(struct AudioAdapterDescriptor **descs, uint32_t descsLen);
39 };
40
AudioAdapterDescriptorFree(struct AudioAdapterDescriptor * dataBlock,bool freeSelf)41 void AudioManagerBenchmarkTest::AudioAdapterDescriptorFree(struct AudioAdapterDescriptor *dataBlock, bool freeSelf)
42 {
43 if (dataBlock == nullptr) {
44 return;
45 }
46
47 if (dataBlock->adapterName != nullptr) {
48 OsalMemFree(dataBlock->adapterName);
49 dataBlock->adapterName = nullptr;
50 }
51
52 if (dataBlock->ports != nullptr) {
53 OsalMemFree(dataBlock->ports);
54 }
55
56 if (freeSelf) {
57 OsalMemFree(dataBlock);
58 }
59 }
60
ReleaseAdapterDescs(struct AudioAdapterDescriptor ** descs,uint32_t descsLen)61 void AudioManagerBenchmarkTest::ReleaseAdapterDescs(struct AudioAdapterDescriptor **descs, uint32_t descsLen)
62 {
63 if ((descsLen > 0) && (descs != nullptr) && ((*descs) != nullptr)) {
64 for (uint32_t i = 0; i < descsLen; i++) {
65 AudioAdapterDescriptorFree(&(*descs)[i], false);
66 }
67 OsalMemFree(*descs);
68 *descs = nullptr;
69 }
70 }
71
SetUp(const::benchmark::State & state)72 void AudioManagerBenchmarkTest::SetUp(const ::benchmark::State &state)
73 {
74 manager_ = IAudioManagerGet(false);
75 ASSERT_NE(manager_, nullptr);
76 }
77
TearDown(const::benchmark::State & state)78 void AudioManagerBenchmarkTest::TearDown(const ::benchmark::State &state)
79 {
80 IAudioManagerRelease(manager_, false);
81 }
82
BENCHMARK_F(AudioManagerBenchmarkTest,GetAllAdapters)83 BENCHMARK_F(AudioManagerBenchmarkTest, GetAllAdapters)(benchmark::State &state)
84 {
85 int32_t ret;
86 uint32_t size = g_audioAdapterNumMax;
87 adapterDescs_ = (struct AudioAdapterDescriptor *)OsalMemCalloc(
88 sizeof(struct AudioAdapterDescriptor) * (g_audioAdapterNumMax));
89 ASSERT_NE(adapterDescs_, nullptr);
90
91 for (auto _ : state) {
92 ret = manager_->GetAllAdapters(manager_, adapterDescs_, &size);
93 }
94 EXPECT_EQ(HDF_SUCCESS, ret);
95 ReleaseAdapterDescs(&adapterDescs_, g_audioAdapterNumMax);
96 }
97
98 BENCHMARK_REGISTER_F(AudioManagerBenchmarkTest, GetAllAdapters)->
99 Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
100
BENCHMARK_F(AudioManagerBenchmarkTest,LoadAndUnloadAdapter)101 BENCHMARK_F(AudioManagerBenchmarkTest, LoadAndUnloadAdapter)(benchmark::State &state)
102 {
103 int32_t ret;
104 uint32_t size = g_audioAdapterNumMax;
105 struct IAudioAdapter *adapter = nullptr;
106 adapterDescs_ = (struct AudioAdapterDescriptor *)OsalMemCalloc(
107 sizeof(struct AudioAdapterDescriptor) * (g_audioAdapterNumMax));
108 ASSERT_NE(adapterDescs_, nullptr);
109 ret = manager_->GetAllAdapters(manager_, adapterDescs_, &size);
110 EXPECT_EQ(HDF_SUCCESS, ret);
111
112 for (auto _ : state) {
113 ret = manager_->LoadAdapter(manager_, &adapterDescs_[0], &adapter);
114 }
115 EXPECT_EQ(HDF_SUCCESS, ret);
116
117 for (auto _ : state) {
118 ret = manager_->UnloadAdapter(manager_, adapterDescs_[0].adapterName);
119 }
120 EXPECT_EQ(HDF_SUCCESS, ret);
121 ReleaseAdapterDescs(&adapterDescs_, g_audioAdapterNumMax);
122 }
123
124 BENCHMARK_REGISTER_F(AudioManagerBenchmarkTest, LoadAndUnloadAdapter)->
125 Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
126 }
127
128 BENCHMARK_MAIN();
129