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 #define AUDIO_CHANNELCOUNT 2
27 #define AUDIO_SAMPLE_RATE_48K 48000
28 #define DEEP_BUFFER_RENDER_PERIOD_SIZE 4096
29 #define INT_32_MAX 0x7fffffff
30 #define PCM_16_BIT 16
31 #define PCM_8_BIT 8
32
33 namespace {
34 static const uint32_t g_audioAdapterNumMax = 5;
35 const int32_t AUDIO_ADAPTER_BUF_TEST = 1024;
36 const int32_t ITERATION_FREQUENCY = 100;
37 const int32_t REPETITION_FREQUENCY = 3;
38
39 class AudioAdapterBenchmarkTest : public benchmark::Fixture {
40 public:
41 struct IAudioManager *manager_ = nullptr;
42 struct IAudioAdapter *adapter_ = nullptr;
43 struct AudioAdapterDescriptor *adapterDescs_ = nullptr;
44 uint32_t renderId_ = 0;
45 uint32_t captureId_ = 0;
46 void SetUp(const ::benchmark::State &state);
47 void TearDown(const ::benchmark::State &state);
48 void InitAttrs(struct AudioSampleAttributes &attrs);
49 void InitDevDesc(struct AudioDeviceDescriptor &devDesc);
50 void AudioAdapterDescriptorFree(struct AudioAdapterDescriptor *dataBlock, bool freeSelf);
51 void ReleaseAdapterDescs(struct AudioAdapterDescriptor **descs, uint32_t descsLen);
52 };
53
AudioAdapterDescriptorFree(struct AudioAdapterDescriptor * dataBlock,bool freeSelf)54 void AudioAdapterBenchmarkTest::AudioAdapterDescriptorFree(struct AudioAdapterDescriptor *dataBlock, bool freeSelf)
55 {
56 if (dataBlock == nullptr) {
57 return;
58 }
59
60 if (dataBlock->adapterName != nullptr) {
61 OsalMemFree(dataBlock->adapterName);
62 dataBlock->adapterName = nullptr;
63 }
64
65 if (dataBlock->ports != nullptr) {
66 OsalMemFree(dataBlock->ports);
67 }
68
69 if (freeSelf) {
70 OsalMemFree(dataBlock);
71 }
72 }
73
ReleaseAdapterDescs(struct AudioAdapterDescriptor ** descs,uint32_t descsLen)74 void AudioAdapterBenchmarkTest::ReleaseAdapterDescs(struct AudioAdapterDescriptor **descs, uint32_t descsLen)
75 {
76 if ((descsLen > 0) && (descs != nullptr) && ((*descs) == nullptr)) {
77 return;
78 }
79
80 for (uint32_t i = 0; i < descsLen; i++) {
81 AudioAdapterDescriptorFree(&(*descs)[i], false);
82 }
83 OsalMemFree(*descs);
84 *descs = nullptr;
85 }
86
InitAttrs(struct AudioSampleAttributes & attrs)87 void AudioAdapterBenchmarkTest::InitAttrs(struct AudioSampleAttributes &attrs)
88 {
89 attrs.format = AUDIO_FORMAT_TYPE_PCM_16_BIT;
90 attrs.channelCount = AUDIO_CHANNELCOUNT;
91 attrs.sampleRate = AUDIO_SAMPLE_RATE_48K;
92 attrs.interleaved = 1;
93 attrs.type = AUDIO_IN_MEDIA;
94 attrs.period = DEEP_BUFFER_RENDER_PERIOD_SIZE;
95 attrs.frameSize = PCM_16_BIT * attrs.channelCount / PCM_8_BIT;
96 attrs.isBigEndian = false;
97 attrs.isSignedData = true;
98 attrs.startThreshold = DEEP_BUFFER_RENDER_PERIOD_SIZE / (attrs.frameSize);
99 attrs.stopThreshold = INT_32_MAX;
100 }
101
InitDevDesc(struct AudioDeviceDescriptor & devDesc)102 void AudioAdapterBenchmarkTest::InitDevDesc(struct AudioDeviceDescriptor &devDesc)
103 {
104 ASSERT_NE(adapterDescs_, nullptr);
105 ASSERT_NE(adapterDescs_->ports, nullptr);
106 for (uint32_t index = 0; index < adapterDescs_->portsLen; index++) {
107 if (adapterDescs_->ports[index].dir == PORT_OUT) {
108 devDesc.portId = adapterDescs_->ports[index].portId;
109 return;
110 }
111 }
112 }
113
SetUp(const::benchmark::State & state)114 void AudioAdapterBenchmarkTest::SetUp(const ::benchmark::State &state)
115 {
116 uint32_t size = g_audioAdapterNumMax;
117 manager_ = IAudioManagerGet(false);
118 ASSERT_NE(manager_, nullptr);
119
120 adapterDescs_ = (struct AudioAdapterDescriptor *)OsalMemCalloc(
121 sizeof(struct AudioAdapterDescriptor) * (g_audioAdapterNumMax));
122 ASSERT_NE(adapterDescs_, nullptr);
123
124 ASSERT_EQ(HDF_SUCCESS, manager_->GetAllAdapters(manager_, adapterDescs_, &size));
125 if (size > g_audioAdapterNumMax) {
126 ReleaseAdapterDescs(&adapterDescs_, g_audioAdapterNumMax);
127 ASSERT_LT(size, g_audioAdapterNumMax);
128 }
129
130 if (manager_->LoadAdapter(manager_, &adapterDescs_[0], &adapter_) != HDF_SUCCESS) {
131 ReleaseAdapterDescs(&adapterDescs_, g_audioAdapterNumMax);
132 ASSERT_TRUE(false);
133 }
134
135 if (adapter_ == nullptr) {
136 ReleaseAdapterDescs(&adapterDescs_, g_audioAdapterNumMax);
137 ASSERT_TRUE(false);
138 }
139 }
140
TearDown(const::benchmark::State & state)141 void AudioAdapterBenchmarkTest::TearDown(const ::benchmark::State &state)
142 {
143 ASSERT_NE(manager_, nullptr);
144 ASSERT_NE(adapter_, nullptr);
145
146 manager_->UnloadAdapter(manager_, adapterDescs_[0].adapterName);
147 ReleaseAdapterDescs(&adapterDescs_, g_audioAdapterNumMax);
148 adapter_ = nullptr;
149 IAudioManagerRelease(manager_, false);
150 manager_ = nullptr;
151 }
152
BENCHMARK_F(AudioAdapterBenchmarkTest,InitAllPorts)153 BENCHMARK_F(AudioAdapterBenchmarkTest, InitAllPorts)(benchmark::State &state)
154 {
155 int32_t ret;
156 for (auto _ : state) {
157 ret = adapter_->InitAllPorts(adapter_);
158 }
159 EXPECT_EQ(HDF_SUCCESS, ret);
160 }
161
162 BENCHMARK_REGISTER_F(AudioAdapterBenchmarkTest, InitAllPorts)->
163 Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
164
BENCHMARK_F(AudioAdapterBenchmarkTest,CreateRender)165 BENCHMARK_F(AudioAdapterBenchmarkTest, CreateRender)(benchmark::State &state)
166 {
167 int32_t ret;
168 struct IAudioRender *render = nullptr;
169 struct AudioDeviceDescriptor devicedesc = {};
170 struct AudioSampleAttributes attrs = {};
171 InitDevDesc(devicedesc);
172 devicedesc.desc = const_cast<char*>("primary");
173 devicedesc.pins = PIN_OUT_SPEAKER;
174 InitAttrs(attrs);
175 attrs.silenceThreshold = 0;
176 attrs.streamId = 0;
177
178 for (auto _ : state) {
179 ret = adapter_->CreateRender(adapter_, &devicedesc, &attrs, &render, &renderId_);
180 if (ret != HDF_SUCCESS) {
181 attrs.format = AUDIO_FORMAT_TYPE_PCM_32_BIT;
182 ASSERT_EQ(HDF_SUCCESS, adapter_->CreateRender(adapter_, &devicedesc, &attrs, &render, &renderId_));
183 }
184 ret = adapter_->DestroyRender(adapter_, renderId_);
185 }
186 EXPECT_EQ(HDF_SUCCESS, ret);
187 }
188
189 BENCHMARK_REGISTER_F(AudioAdapterBenchmarkTest, CreateRender)->
190 Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
191
BENCHMARK_F(AudioAdapterBenchmarkTest,CreateCapture)192 BENCHMARK_F(AudioAdapterBenchmarkTest, CreateCapture)(benchmark::State &state)
193 {
194 int32_t ret;
195 struct IAudioCapture *capture = nullptr;
196 struct AudioDeviceDescriptor devicedesc = {};
197 struct AudioSampleAttributes attrs = {};
198 InitDevDesc(devicedesc);
199 devicedesc.desc = const_cast<char*>("primary");
200 devicedesc.pins = PIN_IN_MIC;
201 InitAttrs(attrs);
202 attrs.silenceThreshold = DEEP_BUFFER_RENDER_PERIOD_SIZE;
203
204 for (auto _ : state) {
205 ret = adapter_->CreateCapture(adapter_, &devicedesc, &attrs, &capture, &captureId_);
206 if (ret != HDF_SUCCESS) {
207 attrs.format = AUDIO_FORMAT_TYPE_PCM_32_BIT;
208 ASSERT_EQ(HDF_SUCCESS, adapter_->CreateCapture(adapter_, &devicedesc, &attrs, &capture, &captureId_));
209 }
210 ret = adapter_->DestroyCapture(adapter_, captureId_);
211 }
212 EXPECT_EQ(HDF_SUCCESS, ret);
213 }
214
215 BENCHMARK_REGISTER_F(AudioAdapterBenchmarkTest, CreateCapture)->
216 Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
217
BENCHMARK_F(AudioAdapterBenchmarkTest,GetPortCapability)218 BENCHMARK_F(AudioAdapterBenchmarkTest, GetPortCapability)(benchmark::State &state)
219 {
220 int32_t ret;
221 struct AudioPort port = {};
222 struct AudioPortCapability capability = {};
223 port.dir = PORT_OUT;
224 port.portId = 0;
225 port.portName = const_cast<char*>("primary");
226
227 for (auto _ : state) {
228 ret = adapter_->GetPortCapability(adapter_, &port, &capability);
229 }
230 ASSERT_TRUE(ret == HDF_SUCCESS || ret == HDF_ERR_NOT_SUPPORT);
231 }
232
233 BENCHMARK_REGISTER_F(AudioAdapterBenchmarkTest, GetPortCapability)->
234 Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
235
BENCHMARK_F(AudioAdapterBenchmarkTest,SetPassthroughMode)236 BENCHMARK_F(AudioAdapterBenchmarkTest, SetPassthroughMode)(benchmark::State &state)
237 {
238 int32_t ret;
239 struct AudioPort port = {};
240 port.dir = PORT_OUT;
241 port.portId = 0;
242 port.portName = const_cast<char*>("primary");
243 enum AudioPortPassthroughMode mode = PORT_PASSTHROUGH_LPCM;
244
245 for (auto _ : state) {
246 ret = adapter_->SetPassthroughMode(adapter_, &port, mode);
247 }
248 ASSERT_TRUE(ret == HDF_SUCCESS || ret == HDF_FAILURE);
249 }
250
251 BENCHMARK_REGISTER_F(AudioAdapterBenchmarkTest, SetPassthroughMode)->
252 Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
253
BENCHMARK_F(AudioAdapterBenchmarkTest,GetPassthroughMode)254 BENCHMARK_F(AudioAdapterBenchmarkTest, GetPassthroughMode)(benchmark::State &state)
255 {
256 int32_t ret;
257 struct AudioPort port = {};
258 port.dir = PORT_OUT;
259 port.portId = 0;
260 port.portName = const_cast<char*>("primary");
261 enum AudioPortPassthroughMode mode;
262
263 for (auto _ : state) {
264 ret = adapter_->GetPassthroughMode(adapter_, &port, &mode);
265 }
266 ASSERT_TRUE(ret == HDF_SUCCESS || ret == HDF_FAILURE);
267 }
268
269 BENCHMARK_REGISTER_F(AudioAdapterBenchmarkTest, GetPassthroughMode)->
270 Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
271
BENCHMARK_F(AudioAdapterBenchmarkTest,GetDeviceStatus)272 BENCHMARK_F(AudioAdapterBenchmarkTest, GetDeviceStatus)(benchmark::State &state)
273 {
274 int32_t ret;
275 struct AudioDeviceStatus status = {};
276
277 for (auto _ : state) {
278 ret = adapter_->GetDeviceStatus(adapter_, &status);
279 }
280 EXPECT_EQ(HDF_SUCCESS, ret);
281 }
282
283 BENCHMARK_REGISTER_F(AudioAdapterBenchmarkTest, GetDeviceStatus)->
284 Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
285
BENCHMARK_F(AudioAdapterBenchmarkTest,SetMicMute)286 BENCHMARK_F(AudioAdapterBenchmarkTest, SetMicMute)(benchmark::State &state)
287 {
288 int32_t ret;
289 bool mute = false;
290
291 for (auto _ : state) {
292 ret = adapter_->SetMicMute(adapter_, mute);
293 }
294 ASSERT_TRUE(ret == HDF_SUCCESS || ret == HDF_ERR_NOT_SUPPORT);
295 }
296
297 BENCHMARK_REGISTER_F(AudioAdapterBenchmarkTest, SetMicMute)->
298 Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
299
BENCHMARK_F(AudioAdapterBenchmarkTest,GetMicMute)300 BENCHMARK_F(AudioAdapterBenchmarkTest, GetMicMute)(benchmark::State &state)
301 {
302 int32_t ret;
303 bool mute = false;
304
305 for (auto _ : state) {
306 ret = adapter_->GetMicMute(adapter_, &mute);
307 }
308 ASSERT_TRUE(ret == HDF_SUCCESS || ret == HDF_ERR_NOT_SUPPORT);
309 }
310
311 BENCHMARK_REGISTER_F(AudioAdapterBenchmarkTest, GetMicMute)->
312 Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
313
BENCHMARK_F(AudioAdapterBenchmarkTest,SetVoiceVolume)314 BENCHMARK_F(AudioAdapterBenchmarkTest, SetVoiceVolume)(benchmark::State &state)
315 {
316 int32_t ret;
317 float volume = 0;
318
319 for (auto _ : state) {
320 ret = adapter_->SetVoiceVolume(adapter_, volume);
321 }
322 ASSERT_TRUE(ret == HDF_SUCCESS || ret == HDF_ERR_NOT_SUPPORT);
323 }
324
325 BENCHMARK_REGISTER_F(AudioAdapterBenchmarkTest, SetVoiceVolume)->
326 Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
327
BENCHMARK_F(AudioAdapterBenchmarkTest,SetExtraParams)328 BENCHMARK_F(AudioAdapterBenchmarkTest, SetExtraParams)(benchmark::State &state)
329 {
330 int32_t ret;
331 enum AudioExtParamKey key = AUDIO_EXT_PARAM_KEY_LOWPOWER;
332 char condition[AUDIO_ADAPTER_BUF_TEST];
333 const char *value = "sup_sampling_rates=4800;sup_channels=1;sup_formats=2;";
334
335 for (auto _ : state) {
336 ret = adapter_->SetExtraParams(adapter_, key, condition, value);
337 }
338 ASSERT_TRUE(ret == HDF_SUCCESS || ret == HDF_ERR_NOT_SUPPORT);
339 }
340
341 BENCHMARK_REGISTER_F(AudioAdapterBenchmarkTest, SetExtraParams)->
342 Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
343
BENCHMARK_F(AudioAdapterBenchmarkTest,GetExtraParams)344 BENCHMARK_F(AudioAdapterBenchmarkTest, GetExtraParams)(benchmark::State &state)
345 {
346 int32_t ret;
347 enum AudioExtParamKey key = AUDIO_EXT_PARAM_KEY_NONE;
348 char condition[AUDIO_ADAPTER_BUF_TEST];
349 char value[AUDIO_ADAPTER_BUF_TEST];
350 uint32_t valueLen = AUDIO_ADAPTER_BUF_TEST;
351 for (auto _ : state) {
352 ret = adapter_->GetExtraParams(adapter_, key, condition, value, valueLen);
353 }
354 ASSERT_TRUE(ret == HDF_SUCCESS || ret == HDF_ERR_NOT_SUPPORT);
355 }
356
357 BENCHMARK_REGISTER_F(AudioAdapterBenchmarkTest, GetExtraParams)->
358 Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
359
360
BENCHMARK_F(AudioAdapterBenchmarkTest,UpdateAudioRoute)361 BENCHMARK_F(AudioAdapterBenchmarkTest, UpdateAudioRoute)(benchmark::State &state)
362 {
363 ASSERT_NE(adapter_, nullptr);
364 int32_t ret;
365 struct AudioRoute route = {};
366 int32_t routeHandle = 0;
367
368 for (auto _ : state) {
369 ret = adapter_->UpdateAudioRoute(adapter_, &route, &routeHandle);
370 EXPECT_NE(HDF_SUCCESS, ret);
371 }
372 }
373
374 BENCHMARK_REGISTER_F(AudioAdapterBenchmarkTest, UpdateAudioRoute)->
375 Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
376
BENCHMARK_F(AudioAdapterBenchmarkTest,ReleaseAudioRoute)377 BENCHMARK_F(AudioAdapterBenchmarkTest, ReleaseAudioRoute)(benchmark::State &state)
378 {
379 ASSERT_NE(adapter_, nullptr);
380 int32_t ret;
381 int32_t routeHandle = 0;
382
383 for (auto _ : state) {
384 ret = adapter_->ReleaseAudioRoute(adapter_, routeHandle);
385 EXPECT_NE(HDF_SUCCESS, ret);
386 }
387 }
388
389 BENCHMARK_REGISTER_F(AudioAdapterBenchmarkTest, ReleaseAudioRoute)->
390 Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
391
BENCHMARK_F(AudioAdapterBenchmarkTest,RegExtraParamObserver)392 BENCHMARK_F(AudioAdapterBenchmarkTest, RegExtraParamObserver)(benchmark::State &state)
393 {
394 ASSERT_NE(adapter_, nullptr);
395 int32_t ret;
396 int8_t cookie = 0;
397 struct IAudioCallback *audioCallback = nullptr;
398
399 for (auto _ : state) {
400 ret = adapter_->RegExtraParamObserver(adapter_, audioCallback, cookie);
401 ASSERT_TRUE(ret == HDF_SUCCESS || ret == HDF_ERR_INVALID_PARAM);
402 }
403 }
404
405 BENCHMARK_REGISTER_F(AudioAdapterBenchmarkTest, RegExtraParamObserver)->
406 Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
407 }
408