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 "accessibility_config.h" 18 #include "accesstoken_kit.h" 19 #include "ffrt_inner.h" 20 #include "nativetoken_kit.h" 21 #include "token_setproc.h" 22 23 using namespace OHOS::AccessibilityConfig; 24 using namespace OHOS::Security::AccessToken; 25 26 namespace { 27 class AccessibilityConfigObserverImpl : public OHOS::AccessibilityConfig::AccessibilityConfigObserver { 28 public: 29 AccessibilityConfigObserverImpl() = default; 30 OnConfigChanged(const CONFIG_ID id,const ConfigValue & value)31 void OnConfigChanged(const CONFIG_ID id, const ConfigValue &value) override 32 { 33 complete_.set_value(); 34 } 35 SetCompletePromise(ffrt::promise<void> & promise)36 void SetCompletePromise(ffrt::promise<void> &promise) 37 { 38 complete_ = std::move(promise); 39 } 40 41 private: 42 ffrt::promise<void> complete_; 43 }; 44 45 static bool g_flag = true; AddPermission()46 void AddPermission() 47 { 48 if (g_flag) { 49 const char *perms[2]; 50 perms[0] = OHOS::Accessibility::OHOS_PERMISSION_READ_ACCESSIBILITY_CONFIG.c_str(); 51 perms[1] = OHOS::Accessibility::OHOS_PERMISSION_WRITE_ACCESSIBILITY_CONFIG.c_str(); 52 NativeTokenInfoParams infoInstance = { 53 .dcapsNum = 0, 54 .permsNum = 2, 55 .aclsNum = 0, 56 .dcaps = nullptr, 57 .perms = perms, 58 .acls = nullptr, 59 .processName = "com.accessibility.config.benchmark.test", 60 .aplStr = "normal", 61 }; 62 uint64_t tokenId = GetAccessTokenId(&infoInstance); 63 SetSelfTokenID(tokenId); 64 AccessTokenKit::ReloadNativeTokenInfo(); 65 g_flag = false; 66 } 67 } 68 69 /** 70 * @tc.name: BenchmarkTestForSetScreenMagnificationState 71 * @tc.desc: Testcase for testing 'SetScreenMagnificationState' function. 72 * @tc.type: FUNC 73 * @tc.require: Issue Number 74 */ BenchmarkTestForSetScreenMagnificationState(benchmark::State & state)75 static void BenchmarkTestForSetScreenMagnificationState(benchmark::State &state) 76 { 77 bool value = false; 78 std::shared_ptr<AccessibilityConfigObserverImpl> configObserver = 79 std::make_shared<AccessibilityConfigObserverImpl>(); 80 auto &config = OHOS::AccessibilityConfig::AccessibilityConfig::GetInstance(); 81 (void)config.InitializeContext(); 82 AddPermission(); 83 84 config.SubscribeConfigObserver(CONFIG_SCREEN_MAGNIFICATION, configObserver, false); 85 config.GetScreenMagnificationState(value); 86 for (auto _ : state) { 87 /* @tc.steps: step1.call SetScreenMagnificationState in loop */ 88 ffrt::promise<void> complete; 89 ffrt::future syncFuture = complete.get_future(); 90 configObserver->SetCompletePromise(complete); 91 92 config.SetScreenMagnificationState(!value); 93 syncFuture.wait(); 94 value = !value; 95 } 96 } 97 98 /** 99 * @tc.name: BenchmarkTestForGetScreenMagnificationState 100 * @tc.desc: Testcase for testing 'GetScreenMagnificationState' function. 101 * @tc.type: FUNC 102 * @tc.require: Issue Number 103 */ BenchmarkTestForGetScreenMagnificationState(benchmark::State & state)104 static void BenchmarkTestForGetScreenMagnificationState(benchmark::State &state) 105 { 106 bool value = false; 107 auto &config = OHOS::AccessibilityConfig::AccessibilityConfig::GetInstance(); 108 (void)config.InitializeContext(); 109 AddPermission(); 110 for (auto _ : state) { 111 /* @tc.steps: step1.call GetScreenMagnificationState in loop */ 112 (void)config.GetScreenMagnificationState(value); 113 } 114 } 115 116 /** 117 * @tc.name: BenchmarkTestForSetShortkeyTarget 118 * @tc.desc: Testcase for testing 'SetShortkeyTarget' function. 119 * @tc.type: FUNC 120 * @tc.require: Issue Number 121 */ BenchmarkTestForSetShortkeyTarget(benchmark::State & state)122 static void BenchmarkTestForSetShortkeyTarget(benchmark::State &state) 123 { 124 std::string nameStr = "aaa"; 125 std::shared_ptr<AccessibilityConfigObserverImpl> configObserver = 126 std::make_shared<AccessibilityConfigObserverImpl>(); 127 auto &config = OHOS::AccessibilityConfig::AccessibilityConfig::GetInstance(); 128 (void)config.InitializeContext(); 129 AddPermission(); 130 131 config.SubscribeConfigObserver(CONFIG_SHORT_KEY_TARGET, configObserver, false); 132 133 for (auto _ : state) { 134 /* @tc.steps: step1.call SetShortkeyTarget in loop */ 135 ffrt::promise<void> complete; 136 ffrt::future syncFuture = complete.get_future(); 137 configObserver->SetCompletePromise(complete); 138 config.SetShortkeyTarget(nameStr); 139 syncFuture.wait(); 140 if (!nameStr.compare("aaa")) { 141 nameStr = "bbb"; 142 } else { 143 nameStr = "aaa"; 144 } 145 } 146 } 147 148 /** 149 * @tc.name: BenchmarkTestForGetShortkeyTarget 150 * @tc.desc: Testcase for testing 'GetShortkeyTarget' function. 151 * @tc.type: FUNC 152 * @tc.require: Issue Number 153 */ BenchmarkTestForGetShortkeyTarget(benchmark::State & state)154 static void BenchmarkTestForGetShortkeyTarget(benchmark::State &state) 155 { 156 std::string value = ""; 157 auto &config = OHOS::AccessibilityConfig::AccessibilityConfig::GetInstance(); 158 (void)config.InitializeContext(); 159 AddPermission(); 160 for (auto _ : state) { 161 /* @tc.steps: step1.call GetShortkeyTarget in loop */ 162 (void)config.GetShortkeyTarget(value); 163 } 164 } 165 166 /** 167 * @tc.name: BenchmarkTestForSetContentTimeout 168 * @tc.desc: Testcase for testing 'SetContentTimeout' function. 169 * @tc.type: FUNC 170 * @tc.require: Issue Number 171 */ BenchmarkTestForSetContentTimeout(benchmark::State & state)172 static void BenchmarkTestForSetContentTimeout(benchmark::State &state) 173 { 174 uint32_t value = 0; 175 std::shared_ptr<AccessibilityConfigObserverImpl> configObserver = 176 std::make_shared<AccessibilityConfigObserverImpl>(); 177 auto &config = OHOS::AccessibilityConfig::AccessibilityConfig::GetInstance(); 178 (void)config.InitializeContext(); 179 AddPermission(); 180 181 config.SubscribeConfigObserver(CONFIG_CONTENT_TIMEOUT, configObserver, false); 182 config.GetContentTimeout(value); 183 for (auto _ : state) { 184 /* @tc.steps: step1.call SetContentTimeout in loop */ 185 value++; 186 ffrt::promise<void> complete; 187 ffrt::future syncFuture = complete.get_future(); 188 configObserver->SetCompletePromise(complete); 189 190 config.SetContentTimeout(value); 191 syncFuture.wait(); 192 } 193 } 194 195 /** 196 * @tc.name: BenchmarkTestForGetContentTimeout 197 * @tc.desc: Testcase for testing 'GetContentTimeout' function. 198 * @tc.type: FUNC 199 * @tc.require: Issue Number 200 */ BenchmarkTestForGetContentTimeout(benchmark::State & state)201 static void BenchmarkTestForGetContentTimeout(benchmark::State &state) 202 { 203 uint32_t value = 0; 204 auto &config = OHOS::AccessibilityConfig::AccessibilityConfig::GetInstance(); 205 (void)config.InitializeContext(); 206 AddPermission(); 207 for (auto _ : state) { 208 /* @tc.steps: step1.call GetContentTimeout in loop */ 209 (void)config.GetContentTimeout(value); 210 } 211 } 212 213 /** 214 * @tc.name: BenchmarkTestForSubscribeConfigObserver 215 * @tc.desc: Testcase for testing 'SubscribeConfigObserver' and 'UnsubscribeConfigObserver' function. 216 * @tc.type: FUNC 217 * @tc.require: Issue Number 218 */ BenchmarkTestForSubscribeConfigObserver(benchmark::State & state)219 static void BenchmarkTestForSubscribeConfigObserver(benchmark::State &state) 220 { 221 std::shared_ptr<AccessibilityConfigObserverImpl> configObserver = 222 std::make_shared<AccessibilityConfigObserverImpl>(); 223 auto &config = OHOS::AccessibilityConfig::AccessibilityConfig::GetInstance(); 224 (void)config.InitializeContext(); 225 AddPermission(); 226 227 for (auto _ : state) { 228 /* @tc.steps: step1.call SubscribeConfigObserver in loop */ 229 config.SubscribeConfigObserver(CONFIG_SCREEN_MAGNIFICATION, configObserver, false); 230 config.UnsubscribeConfigObserver(CONFIG_SCREEN_MAGNIFICATION, configObserver); 231 } 232 } 233 234 BENCHMARK(BenchmarkTestForSetScreenMagnificationState)->Iterations(1000)->ReportAggregatesOnly(); 235 BENCHMARK(BenchmarkTestForGetScreenMagnificationState)->Iterations(1000)->ReportAggregatesOnly(); 236 BENCHMARK(BenchmarkTestForSetShortkeyTarget)->Iterations(1000)->ReportAggregatesOnly(); 237 BENCHMARK(BenchmarkTestForGetShortkeyTarget)->Iterations(1000)->ReportAggregatesOnly(); 238 BENCHMARK(BenchmarkTestForSetContentTimeout)->Iterations(1000)->ReportAggregatesOnly(); 239 BENCHMARK(BenchmarkTestForGetContentTimeout)->Iterations(1000)->ReportAggregatesOnly(); 240 BENCHMARK(BenchmarkTestForSubscribeConfigObserver)->Iterations(1000)->ReportAggregatesOnly(); 241 } 242 243 BENCHMARK_MAIN();