• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef LOG_TAG
16 #define LOG_TAG "BenchmarkAudiomanagerTest"
17 #endif
18 
19 #include <benchmark/benchmark.h>
20 #include <string>
21 #include <vector>
22 #include "audio_info.h"
23 #include "audio_errors.h"
24 #include "audio_system_manager.h"
25 using namespace std;
26 using namespace OHOS;
27 using namespace OHOS::AudioStandard;
28 
29 namespace {
30     class BenchmarkAudiomanagerTest : public benchmark::Fixture {
31     public:
BenchmarkAudiomanagerTest()32         BenchmarkAudiomanagerTest()
33         {
34             Iterations(iterations);
35             Repetitions(repetitions);
36             ReportAggregatesOnly();
37         }
38 
39         ~BenchmarkAudiomanagerTest() override = default;
40 
SetUp(const::benchmark::State & state)41         void SetUp(const ::benchmark::State &state) override
42         {
43             sleep(1);
44             instance = AudioSystemManager::GetInstance();
45             sleep(1);
46         }
47 
TearDown(const::benchmark::State & state)48         void TearDown(const ::benchmark::State &state) override
49         {
50         }
51 
52     protected:
53         AudioSystemManager *instance;
54         const int32_t RENDERER_FLAG = 0;
55         const int32_t repetitions = 3;
56         const int32_t iterations = 300;
57         const int32_t MAX_VOL = 15;
58         const int32_t MIN_VOL = 0;
59     };
60 
61     // SetDeviceActiveAbility
BENCHMARK_F(BenchmarkAudiomanagerTest,SetDeviceActiveAbilityTestCase)62     BENCHMARK_F(BenchmarkAudiomanagerTest, SetDeviceActiveAbilityTestCase)
63     (
64         benchmark::State &state)
65     {
66         while (state.KeepRunning())
67         {
68             auto ret = instance->SetDeviceActive(DeviceType::DEVICE_TYPE_SPEAKER, true);
69             if (ret != SUCCESS)
70             {
71                 state.SkipWithError("SetDeviceActiveAbilityTestCase audioManager SetDeviceActive true failed.");
72             }
73             state.PauseTiming();
74             ret = instance->SetDeviceActive(DeviceType::DEVICE_TYPE_SPEAKER, false);
75             if (ret != SUCCESS)
76             {
77                 state.SkipWithError("SetDeviceActiveAbilityTestCase audioManager SetDeviceActive false failed.");
78             }
79             state.ResumeTiming();
80         }
81     }
82 
83     // SetVolumeAbility
BENCHMARK_F(BenchmarkAudiomanagerTest,SetVolumeAbilityTestCase)84     BENCHMARK_F(BenchmarkAudiomanagerTest, SetVolumeAbilityTestCase)
85     (
86         benchmark::State &state)
87     {
88         while (state.KeepRunning())
89         {
90             auto ret = instance->SetVolume(AudioVolumeType::STREAM_RING, MAX_VOL);
91             if (ret != SUCCESS)
92             {
93                 state.SkipWithError("SetVolumeAbilityTestCase audioManager SetVolume MAX_VOL failed.");
94             }
95             state.PauseTiming();
96             ret = instance->SetVolume(AudioVolumeType::STREAM_RING, MIN_VOL);
97             if (ret != SUCCESS)
98             {
99                 state.SkipWithError("SetVolumeAbilityTestCase audioManager SetVolume MIN_VOL failed.");
100             }
101             state.ResumeTiming();
102         }
103     }
104 
105     // SetMuteAbility
BENCHMARK_F(BenchmarkAudiomanagerTest,SetMuteAbilityTestCase)106     BENCHMARK_F(BenchmarkAudiomanagerTest, SetMuteAbilityTestCase)
107     (
108         benchmark::State &state)
109     {
110         while (state.KeepRunning())
111         {
112             state.PauseTiming();
113             int32_t ret = instance->SetMute(AudioVolumeType::STREAM_RING, true);
114             if (ret != SUCCESS)
115             {
116                 state.SkipWithError("SetVolumeAbilityTestCase audioManager SetDeviceActive false failed.");
117             }
118             state.ResumeTiming();
119             ret = instance->SetMute(AudioVolumeType::STREAM_RING, false);
120             if (ret != SUCCESS)
121             {
122                 state.SkipWithError("SetVolumeAbilityTestCase audioManager SetDeviceActive false failed.");
123             }
124         }
125     }
126 
127     // GetPreferredInputDeviceForCapturerInfo
BENCHMARK_F(BenchmarkAudiomanagerTest,GetPreferredInputDeviceForCapturerInfoTestCase)128     BENCHMARK_F(BenchmarkAudiomanagerTest, GetPreferredInputDeviceForCapturerInfoTestCase)
129     (
130         benchmark::State &state)
131     {
132         while (state.KeepRunning())
133         {
134             int32_t ret = instance->GetPreferredInputDeviceDescriptors();
135             if (ret != SUCCESS)
136             {
137                 state.SkipWithError("Get preferred input device for capturer info failed.");
138             }
139         }
140     }
141 
142 }
143 
144 // Run the benchmark
145 BENCHMARK_MAIN();