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_info.h" 20 #include "audio_errors.h" 21 #include "audio_system_manager.h" 22 using namespace std; 23 using namespace OHOS; 24 using namespace OHOS::AudioStandard; 25 26 namespace { 27 class BenchmarkAudiomanagerTest : public benchmark::Fixture { 28 public: BenchmarkAudiomanagerTest()29 BenchmarkAudiomanagerTest() 30 { 31 Iterations(iterations); 32 Repetitions(repetitions); 33 ReportAggregatesOnly(); 34 } 35 36 ~BenchmarkAudiomanagerTest() override = default; 37 SetUp(const::benchmark::State & state)38 void SetUp(const ::benchmark::State &state) override 39 { 40 sleep(1); 41 instance = AudioSystemManager::GetInstance(); 42 sleep(1); 43 } 44 TearDown(const::benchmark::State & state)45 void TearDown(const ::benchmark::State &state) override 46 { 47 } 48 49 protected: 50 AudioSystemManager *instance; 51 const int32_t RENDERER_FLAG = 0; 52 const int32_t repetitions = 3; 53 const int32_t iterations = 300; 54 const int32_t MAX_VOL = 15; 55 const int32_t MIN_VOL = 0; 56 }; 57 58 // SetDeviceActiveAbility BENCHMARK_F(BenchmarkAudiomanagerTest,SetDeviceActiveAbilityTestCase)59 BENCHMARK_F(BenchmarkAudiomanagerTest, SetDeviceActiveAbilityTestCase) 60 ( 61 benchmark::State &state) 62 { 63 while (state.KeepRunning()) 64 { 65 auto ret = instance->SetDeviceActive(ActiveDeviceType::SPEAKER, true); 66 if (ret != SUCCESS) 67 { 68 state.SkipWithError("SetDeviceActiveAbilityTestCase audioManager SetDeviceActive true failed."); 69 } 70 state.PauseTiming(); 71 ret = instance->SetDeviceActive(ActiveDeviceType::SPEAKER, false); 72 if (ret != SUCCESS) 73 { 74 state.SkipWithError("SetDeviceActiveAbilityTestCase audioManager SetDeviceActive false failed."); 75 } 76 state.ResumeTiming(); 77 } 78 } 79 80 // SetVolumeAbility BENCHMARK_F(BenchmarkAudiomanagerTest,SetVolumeAbilityTestCase)81 BENCHMARK_F(BenchmarkAudiomanagerTest, SetVolumeAbilityTestCase) 82 ( 83 benchmark::State &state) 84 { 85 while (state.KeepRunning()) 86 { 87 auto ret = instance->SetVolume(AudioVolumeType::STREAM_RING, MAX_VOL); 88 if (ret != SUCCESS) 89 { 90 state.SkipWithError("SetVolumeAbilityTestCase audioManager SetVolume MAX_VOL failed."); 91 } 92 state.PauseTiming(); 93 ret = instance->SetVolume(AudioVolumeType::STREAM_RING, MIN_VOL); 94 if (ret != SUCCESS) 95 { 96 state.SkipWithError("SetVolumeAbilityTestCase audioManager SetVolume MIN_VOL failed."); 97 } 98 state.ResumeTiming(); 99 } 100 } 101 102 // SetMuteAbility BENCHMARK_F(BenchmarkAudiomanagerTest,SetMuteAbilityTestCase)103 BENCHMARK_F(BenchmarkAudiomanagerTest, SetMuteAbilityTestCase) 104 ( 105 benchmark::State &state) 106 { 107 while (state.KeepRunning()) 108 { 109 state.PauseTiming(); 110 int32_t ret = instance->SetMute(AudioVolumeType::STREAM_RING, true); 111 if (ret != SUCCESS) 112 { 113 state.SkipWithError("SetVolumeAbilityTestCase audioManager SetDeviceActive false failed."); 114 } 115 state.ResumeTiming(); 116 ret = instance->SetMute(AudioVolumeType::STREAM_RING, false); 117 if (ret != SUCCESS) 118 { 119 state.SkipWithError("SetVolumeAbilityTestCase audioManager SetDeviceActive false failed."); 120 } 121 } 122 } 123 } 124 125 // Run the benchmark 126 BENCHMARK_MAIN();