1 /* 2 * Copyright (c) 2022 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 <string> 18 #include <vector> 19 #include "audio_errors.h" 20 #include "audio_info.h" 21 #include "audio_capturer.h" 22 using namespace std; 23 using namespace OHOS; 24 using namespace OHOS::AudioStandard; 25 26 namespace { 27 class BenchmarkAudiocapturerTest : public benchmark::Fixture { 28 public: BenchmarkAudiocapturerTest()29 BenchmarkAudiocapturerTest() 30 { 31 Iterations(iterations); 32 Repetitions(repetitions); 33 ReportAggregatesOnly(); 34 } 35 36 ~BenchmarkAudiocapturerTest() override = default; 37 SetUp(const::benchmark::State & state)38 void SetUp(const ::benchmark::State &state) override 39 { 40 sleep(1); 41 AudioCapturerOptions capturerOptions; 42 capturerOptions.streamInfo.samplingRate = AudioSamplingRate::SAMPLE_RATE_96000; 43 capturerOptions.streamInfo.encoding = AudioEncodingType::ENCODING_PCM; 44 capturerOptions.streamInfo.format = AudioSampleFormat::SAMPLE_U8; 45 capturerOptions.streamInfo.channels = AudioChannel::MONO; 46 capturerOptions.capturerInfo.sourceType = SourceType::SOURCE_TYPE_MIC; 47 capturerOptions.capturerInfo.capturerFlags = CAPTURER_FLAG; 48 audioCapturer = AudioCapturer::Create(capturerOptions); 49 sleep(1); 50 } 51 TearDown(const::benchmark::State & state)52 void TearDown(const ::benchmark::State &state) override 53 { 54 audioCapturer->Release(); 55 } 56 57 protected: 58 unique_ptr<AudioCapturer> audioCapturer; 59 const int32_t CAPTURER_FLAG = 0; 60 const int32_t repetitions = 3; 61 const int32_t iterations = 300; 62 }; 63 64 // StartAbility BENCHMARK_F(BenchmarkAudiocapturerTest,StartAbilityTestCase)65 BENCHMARK_F(BenchmarkAudiocapturerTest, StartAbilityTestCase) 66 ( 67 benchmark::State &state) 68 { 69 while (state.KeepRunning()) 70 { 71 bool isStarted = audioCapturer->Start(); 72 if (isStarted == false) 73 { 74 state.SkipWithError("StartAbilityTestCase audioCapturer start failed."); 75 } 76 77 state.PauseTiming(); 78 bool isStopped = audioCapturer->Stop(); 79 if (isStopped == false) 80 { 81 state.SkipWithError("StartAbilityTestCase audioCapturer start failed."); 82 } 83 state.ResumeTiming(); 84 } 85 } 86 87 // StopAbility BENCHMARK_F(BenchmarkAudiocapturerTest,StopAbilityTestCase)88 BENCHMARK_F(BenchmarkAudiocapturerTest, StopAbilityTestCase) 89 ( 90 benchmark::State &state) 91 { 92 while (state.KeepRunning()) 93 { 94 state.PauseTiming(); 95 bool isStarted = audioCapturer->Start(); 96 if (isStarted == false) 97 { 98 state.SkipWithError("StopAbilityTestCase audioCapturer start failed."); 99 } 100 state.ResumeTiming(); 101 102 bool isStopped = audioCapturer->Stop(); 103 if (isStopped == false) 104 { 105 state.SkipWithError("StopAbilityTestCase audioCapturer start failed."); 106 } 107 } 108 } 109 } 110 111 // Run the benchmark 112 BENCHMARK_MAIN();