Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "benchmark/benchmark.h" |
| 18 | |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 19 | #include <aidl/android/hardware/vibrator/BnVibratorCallback.h> |
| 20 | #include <aidl/android/hardware/vibrator/IVibrator.h> |
| 21 | |
| 22 | #include <android/binder_manager.h> |
| 23 | #include <android/binder_process.h> |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 24 | #include <future> |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 25 | |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 26 | using ::benchmark::Counter; |
| 27 | using ::benchmark::Fixture; |
| 28 | using ::benchmark::kMicrosecond; |
| 29 | using ::benchmark::State; |
| 30 | using ::benchmark::internal::Benchmark; |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 31 | using ::ndk::enum_range; |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 32 | |
Lais Andrade | 6677c6f | 2025-03-21 09:48:28 -0700 | [diff] [blame] | 33 | using namespace ::aidl::android::hardware::vibrator; |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 34 | using namespace ::std::chrono_literals; |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 35 | |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 36 | // Fixed number of iterations for benchmarks that trigger a vibration on the loop. |
| 37 | // They require slow cleanup to ensure a stable state on each run and less noisy metrics. |
| 38 | static constexpr auto VIBRATION_ITERATIONS = 500; |
| 39 | |
| 40 | // Timeout to wait for vibration callback completion. |
| 41 | static constexpr auto VIBRATION_CALLBACK_TIMEOUT = 100ms; |
| 42 | |
| 43 | // Max duration the vibrator can be turned on, in milliseconds. |
| 44 | static constexpr uint32_t MAX_ON_DURATION_MS = UINT16_MAX; |
| 45 | |
Lais Andrade | 6677c6f | 2025-03-21 09:48:28 -0700 | [diff] [blame] | 46 | class VibratorBench : public Fixture { |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 47 | public: |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 48 | void SetUp(State& /*state*/) override { |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 49 | ABinderProcess_setThreadPoolMaxThreadCount(1); |
| 50 | ABinderProcess_startThreadPool(); |
Lais Andrade | 6677c6f | 2025-03-21 09:48:28 -0700 | [diff] [blame] | 51 | auto serviceName = std::string(IVibrator::descriptor) + "/default"; |
| 52 | this->mVibrator = IVibrator::fromBinder( |
| 53 | ndk::SpAIBinder(AServiceManager_waitForService(serviceName.c_str()))); |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 56 | void TearDown(State& /*state*/) override { |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 57 | if (mVibrator) { |
| 58 | mVibrator->off(); |
Lais Andrade | 6677c6f | 2025-03-21 09:48:28 -0700 | [diff] [blame] | 59 | mVibrator->setExternalControl(false); |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 60 | } |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | static void DefaultConfig(Benchmark* b) { b->Unit(kMicrosecond); } |
| 64 | |
Lais Andrade | 6677c6f | 2025-03-21 09:48:28 -0700 | [diff] [blame] | 65 | static void DefaultArgs(Benchmark* /*b*/) { /* none */ } |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 66 | |
| 67 | protected: |
Lais Andrade | 6677c6f | 2025-03-21 09:48:28 -0700 | [diff] [blame] | 68 | std::shared_ptr<IVibrator> mVibrator; |
| 69 | |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 70 | auto getOtherArg(const State& state, std::size_t index) const { return state.range(index + 0); } |
| 71 | |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 72 | int32_t hasCapabilities(int32_t capabilities) { |
| 73 | int32_t deviceCapabilities = 0; |
| 74 | this->mVibrator->getCapabilities(&deviceCapabilities); |
| 75 | return (deviceCapabilities & capabilities) == capabilities; |
| 76 | } |
| 77 | |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 78 | bool shouldSkipWithError(State& state, const ndk::ScopedAStatus&& status) { |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 79 | if (!status.isOk()) { |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 80 | state.SkipWithError(status.getMessage()); |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 81 | return true; |
| 82 | } |
| 83 | return false; |
| 84 | } |
| 85 | |
Lais Andrade | a5131c5 | 2024-06-12 16:39:29 +0100 | [diff] [blame] | 86 | void waitForComplete(std::future<void>& callbackFuture) { |
| 87 | // Wait until the HAL has finished processing previous vibration before starting a new one, |
| 88 | // so the HAL state is consistent on each run and metrics are less noisy. Some of the newest |
| 89 | // HAL implementations are waiting on previous vibration cleanup and might be significantly |
| 90 | // slower, so make sure we measure vibrations on a clean slate. |
| 91 | if (callbackFuture.valid()) { |
| 92 | callbackFuture.wait_for(VIBRATION_CALLBACK_TIMEOUT); |
| 93 | } |
| 94 | } |
| 95 | |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 96 | static void SlowBenchConfig(Benchmark* b) { b->Iterations(VIBRATION_ITERATIONS); } |
| 97 | }; |
| 98 | |
Lais Andrade | 6677c6f | 2025-03-21 09:48:28 -0700 | [diff] [blame] | 99 | class SlowVibratorBench : public VibratorBench { |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 100 | public: |
| 101 | static void DefaultConfig(Benchmark* b) { |
Lais Andrade | 6677c6f | 2025-03-21 09:48:28 -0700 | [diff] [blame] | 102 | VibratorBench::DefaultConfig(b); |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 103 | SlowBenchConfig(b); |
| 104 | } |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 105 | }; |
| 106 | |
Lais Andrade | 6677c6f | 2025-03-21 09:48:28 -0700 | [diff] [blame] | 107 | class HalCallback : public BnVibratorCallback { |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 108 | public: |
| 109 | HalCallback() = default; |
| 110 | ~HalCallback() = default; |
| 111 | |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 112 | ndk::ScopedAStatus onComplete() override { |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 113 | mPromise.set_value(); |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 114 | return ndk::ScopedAStatus::ok(); |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Lais Andrade | a5131c5 | 2024-06-12 16:39:29 +0100 | [diff] [blame] | 117 | std::future<void> getFuture() { return mPromise.get_future(); } |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 118 | |
| 119 | private: |
| 120 | std::promise<void> mPromise; |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 121 | }; |
| 122 | |
Lais Andrade | 6677c6f | 2025-03-21 09:48:28 -0700 | [diff] [blame] | 123 | #define BENCHMARK_WRAPPER(fixt, test, code) \ |
| 124 | BENCHMARK_DEFINE_F(fixt, test) \ |
| 125 | /* NOLINTNEXTLINE */ \ |
| 126 | (State & state) { \ |
| 127 | if (!mVibrator) { \ |
| 128 | state.SkipWithMessage("HAL unavailable"); \ |
| 129 | return; \ |
| 130 | } \ |
| 131 | \ |
| 132 | code \ |
| 133 | } \ |
| 134 | BENCHMARK_REGISTER_F(fixt, test)->Apply(fixt::DefaultConfig)->Apply(fixt::DefaultArgs) |
| 135 | |
| 136 | BENCHMARK_WRAPPER(SlowVibratorBench, on, { |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 137 | auto ms = MAX_ON_DURATION_MS; |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 138 | |
| 139 | for (auto _ : state) { |
Lais Andrade | 6677c6f | 2025-03-21 09:48:28 -0700 | [diff] [blame] | 140 | auto cb = hasCapabilities(IVibrator::CAP_ON_CALLBACK) |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 141 | ? ndk::SharedRefBase::make<HalCallback>() |
| 142 | : nullptr; |
Lais Andrade | a5131c5 | 2024-06-12 16:39:29 +0100 | [diff] [blame] | 143 | // Grab the future before callback promise is destroyed by the HAL. |
| 144 | auto cbFuture = cb ? cb->getFuture() : std::future<void>(); |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 145 | |
| 146 | // Test |
| 147 | if (shouldSkipWithError(state, mVibrator->on(ms, cb))) { |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | // Cleanup |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 152 | state.PauseTiming(); |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 153 | if (shouldSkipWithError(state, mVibrator->off())) { |
| 154 | return; |
| 155 | } |
Lais Andrade | a5131c5 | 2024-06-12 16:39:29 +0100 | [diff] [blame] | 156 | waitForComplete(cbFuture); |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 157 | state.ResumeTiming(); |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 158 | } |
| 159 | }); |
| 160 | |
Lais Andrade | 6677c6f | 2025-03-21 09:48:28 -0700 | [diff] [blame] | 161 | BENCHMARK_WRAPPER(SlowVibratorBench, off, { |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 162 | auto ms = MAX_ON_DURATION_MS; |
| 163 | |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 164 | for (auto _ : state) { |
Lais Andrade | 6677c6f | 2025-03-21 09:48:28 -0700 | [diff] [blame] | 165 | auto cb = hasCapabilities(IVibrator::CAP_ON_CALLBACK) |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 166 | ? ndk::SharedRefBase::make<HalCallback>() |
| 167 | : nullptr; |
Lais Andrade | a5131c5 | 2024-06-12 16:39:29 +0100 | [diff] [blame] | 168 | // Grab the future before callback promise is destroyed by the HAL. |
| 169 | auto cbFuture = cb ? cb->getFuture() : std::future<void>(); |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 170 | |
| 171 | // Setup |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 172 | state.PauseTiming(); |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 173 | if (shouldSkipWithError(state, mVibrator->on(ms, cb))) { |
| 174 | return; |
| 175 | } |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 176 | state.ResumeTiming(); |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 177 | |
| 178 | // Test |
| 179 | if (shouldSkipWithError(state, mVibrator->off())) { |
| 180 | return; |
| 181 | } |
| 182 | |
| 183 | // Cleanup |
| 184 | state.PauseTiming(); |
Lais Andrade | a5131c5 | 2024-06-12 16:39:29 +0100 | [diff] [blame] | 185 | waitForComplete(cbFuture); |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 186 | state.ResumeTiming(); |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 187 | } |
| 188 | }); |
| 189 | |
Lais Andrade | 6677c6f | 2025-03-21 09:48:28 -0700 | [diff] [blame] | 190 | BENCHMARK_WRAPPER(VibratorBench, getCapabilities, { |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 191 | int32_t capabilities = 0; |
| 192 | |
| 193 | for (auto _ : state) { |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 194 | if (shouldSkipWithError(state, mVibrator->getCapabilities(&capabilities))) { |
| 195 | return; |
| 196 | } |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 197 | } |
| 198 | }); |
| 199 | |
Lais Andrade | 6677c6f | 2025-03-21 09:48:28 -0700 | [diff] [blame] | 200 | BENCHMARK_WRAPPER(VibratorBench, setAmplitude, { |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 201 | auto ms = MAX_ON_DURATION_MS; |
| 202 | float amplitude = 1.0f; |
| 203 | |
Lais Andrade | 6677c6f | 2025-03-21 09:48:28 -0700 | [diff] [blame] | 204 | if (!hasCapabilities(IVibrator::CAP_AMPLITUDE_CONTROL)) { |
Adrian Roos | 7765942 | 2024-02-09 17:20:54 +0000 | [diff] [blame] | 205 | state.SkipWithMessage("amplitude control unavailable"); |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 206 | return; |
| 207 | } |
| 208 | |
Lais Andrade | 6677c6f | 2025-03-21 09:48:28 -0700 | [diff] [blame] | 209 | auto cb = hasCapabilities(IVibrator::CAP_ON_CALLBACK) |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 210 | ? ndk::SharedRefBase::make<HalCallback>() |
| 211 | : nullptr; |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 212 | if (shouldSkipWithError(state, mVibrator->on(ms, cb))) { |
| 213 | return; |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 216 | for (auto _ : state) { |
| 217 | if (shouldSkipWithError(state, mVibrator->setAmplitude(amplitude))) { |
| 218 | return; |
| 219 | } |
| 220 | } |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 221 | }); |
| 222 | |
Lais Andrade | 6677c6f | 2025-03-21 09:48:28 -0700 | [diff] [blame] | 223 | BENCHMARK_WRAPPER(VibratorBench, setExternalControl, { |
| 224 | if (!hasCapabilities(IVibrator::CAP_EXTERNAL_CONTROL)) { |
Adrian Roos | 7765942 | 2024-02-09 17:20:54 +0000 | [diff] [blame] | 225 | state.SkipWithMessage("external control unavailable"); |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 226 | return; |
| 227 | } |
| 228 | |
| 229 | for (auto _ : state) { |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 230 | // Test |
| 231 | if (shouldSkipWithError(state, mVibrator->setExternalControl(true))) { |
| 232 | return; |
| 233 | } |
| 234 | |
| 235 | // Cleanup |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 236 | state.PauseTiming(); |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 237 | if (shouldSkipWithError(state, mVibrator->setExternalControl(false))) { |
| 238 | return; |
| 239 | } |
| 240 | state.ResumeTiming(); |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 241 | } |
| 242 | }); |
| 243 | |
Lais Andrade | 6677c6f | 2025-03-21 09:48:28 -0700 | [diff] [blame] | 244 | BENCHMARK_WRAPPER(VibratorBench, setExternalAmplitude, { |
| 245 | auto externalControl = static_cast<int32_t>(IVibrator::CAP_EXTERNAL_CONTROL); |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 246 | auto externalAmplitudeControl = |
Lais Andrade | 6677c6f | 2025-03-21 09:48:28 -0700 | [diff] [blame] | 247 | static_cast<int32_t>(IVibrator::CAP_EXTERNAL_AMPLITUDE_CONTROL); |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 248 | if (!hasCapabilities(externalControl | externalAmplitudeControl)) { |
Adrian Roos | 7765942 | 2024-02-09 17:20:54 +0000 | [diff] [blame] | 249 | state.SkipWithMessage("external amplitude control unavailable"); |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 250 | return; |
| 251 | } |
| 252 | |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 253 | if (shouldSkipWithError(state, mVibrator->setExternalControl(true))) { |
| 254 | return; |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 257 | float amplitude = 1.0f; |
| 258 | for (auto _ : state) { |
| 259 | if (shouldSkipWithError(state, mVibrator->setAmplitude(amplitude))) { |
| 260 | return; |
| 261 | } |
| 262 | } |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 263 | }); |
| 264 | |
Lais Andrade | 6677c6f | 2025-03-21 09:48:28 -0700 | [diff] [blame] | 265 | BENCHMARK_WRAPPER(VibratorBench, getSupportedEffects, { |
| 266 | std::vector<Effect> supportedEffects; |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 267 | |
| 268 | for (auto _ : state) { |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 269 | if (shouldSkipWithError(state, mVibrator->getSupportedEffects(&supportedEffects))) { |
| 270 | return; |
| 271 | } |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 272 | } |
| 273 | }); |
| 274 | |
Lais Andrade | 6677c6f | 2025-03-21 09:48:28 -0700 | [diff] [blame] | 275 | BENCHMARK_WRAPPER(VibratorBench, getSupportedAlwaysOnEffects, { |
| 276 | if (!hasCapabilities(IVibrator::CAP_ALWAYS_ON_CONTROL)) { |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 277 | state.SkipWithMessage("always on control unavailable"); |
| 278 | return; |
| 279 | } |
| 280 | |
Lais Andrade | 6677c6f | 2025-03-21 09:48:28 -0700 | [diff] [blame] | 281 | std::vector<Effect> supportedEffects; |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 282 | |
| 283 | for (auto _ : state) { |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 284 | if (shouldSkipWithError(state, mVibrator->getSupportedAlwaysOnEffects(&supportedEffects))) { |
| 285 | return; |
| 286 | } |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 287 | } |
| 288 | }); |
| 289 | |
Lais Andrade | 6677c6f | 2025-03-21 09:48:28 -0700 | [diff] [blame] | 290 | BENCHMARK_WRAPPER(VibratorBench, getSupportedPrimitives, { |
| 291 | std::vector<CompositePrimitive> supportedPrimitives; |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 292 | |
| 293 | for (auto _ : state) { |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 294 | if (shouldSkipWithError(state, mVibrator->getSupportedPrimitives(&supportedPrimitives))) { |
| 295 | return; |
| 296 | } |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 297 | } |
| 298 | }); |
| 299 | |
Lais Andrade | 6677c6f | 2025-03-21 09:48:28 -0700 | [diff] [blame] | 300 | class EffectsVibratorBench : public VibratorBench { |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 301 | public: |
| 302 | static void DefaultArgs(Benchmark* b) { |
| 303 | b->ArgNames({"Effect", "Strength"}); |
Lais Andrade | 6677c6f | 2025-03-21 09:48:28 -0700 | [diff] [blame] | 304 | for (const auto& effect : enum_range<Effect>()) { |
| 305 | for (const auto& strength : enum_range<EffectStrength>()) { |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 306 | b->Args({static_cast<long>(effect), static_cast<long>(strength)}); |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | protected: |
| 312 | auto getEffect(const State& state) const { |
Lais Andrade | 6677c6f | 2025-03-21 09:48:28 -0700 | [diff] [blame] | 313 | return static_cast<Effect>(this->getOtherArg(state, 0)); |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | auto getStrength(const State& state) const { |
Lais Andrade | 6677c6f | 2025-03-21 09:48:28 -0700 | [diff] [blame] | 317 | return static_cast<EffectStrength>(this->getOtherArg(state, 1)); |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 318 | } |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 319 | |
Lais Andrade | 6677c6f | 2025-03-21 09:48:28 -0700 | [diff] [blame] | 320 | bool isEffectSupported(const Effect& effect) { |
| 321 | std::vector<Effect> supported; |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 322 | mVibrator->getSupportedEffects(&supported); |
| 323 | return std::find(supported.begin(), supported.end(), effect) != supported.end(); |
| 324 | } |
| 325 | |
Lais Andrade | 6677c6f | 2025-03-21 09:48:28 -0700 | [diff] [blame] | 326 | bool isAlwaysOnEffectSupported(const Effect& effect) { |
| 327 | std::vector<Effect> supported; |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 328 | mVibrator->getSupportedAlwaysOnEffects(&supported); |
| 329 | return std::find(supported.begin(), supported.end(), effect) != supported.end(); |
| 330 | } |
| 331 | }; |
| 332 | |
Lais Andrade | 6677c6f | 2025-03-21 09:48:28 -0700 | [diff] [blame] | 333 | class SlowEffectsVibratorBench : public EffectsVibratorBench { |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 334 | public: |
| 335 | static void DefaultConfig(Benchmark* b) { |
Lais Andrade | 6677c6f | 2025-03-21 09:48:28 -0700 | [diff] [blame] | 336 | EffectsVibratorBench::DefaultConfig(b); |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 337 | SlowBenchConfig(b); |
| 338 | } |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 339 | }; |
| 340 | |
Lais Andrade | 6677c6f | 2025-03-21 09:48:28 -0700 | [diff] [blame] | 341 | BENCHMARK_WRAPPER(EffectsVibratorBench, alwaysOnEnable, { |
| 342 | if (!hasCapabilities(IVibrator::CAP_ALWAYS_ON_CONTROL)) { |
Adrian Roos | 7765942 | 2024-02-09 17:20:54 +0000 | [diff] [blame] | 343 | state.SkipWithMessage("always on control unavailable"); |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 344 | return; |
| 345 | } |
| 346 | |
| 347 | int32_t id = 1; |
| 348 | auto effect = getEffect(state); |
| 349 | auto strength = getStrength(state); |
| 350 | |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 351 | if (!isAlwaysOnEffectSupported(effect)) { |
| 352 | state.SkipWithMessage("always on effect unsupported"); |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 353 | return; |
| 354 | } |
| 355 | |
| 356 | for (auto _ : state) { |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 357 | // Test |
| 358 | if (shouldSkipWithError(state, mVibrator->alwaysOnEnable(id, effect, strength))) { |
| 359 | return; |
| 360 | } |
| 361 | |
| 362 | // Cleanup |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 363 | state.PauseTiming(); |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 364 | if (shouldSkipWithError(state, mVibrator->alwaysOnDisable(id))) { |
| 365 | return; |
| 366 | } |
| 367 | state.ResumeTiming(); |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 368 | } |
| 369 | }); |
| 370 | |
Lais Andrade | 6677c6f | 2025-03-21 09:48:28 -0700 | [diff] [blame] | 371 | BENCHMARK_WRAPPER(EffectsVibratorBench, alwaysOnDisable, { |
| 372 | if (!hasCapabilities(IVibrator::CAP_ALWAYS_ON_CONTROL)) { |
Adrian Roos | 7765942 | 2024-02-09 17:20:54 +0000 | [diff] [blame] | 373 | state.SkipWithMessage("always on control unavailable"); |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 374 | return; |
| 375 | } |
| 376 | |
| 377 | int32_t id = 1; |
| 378 | auto effect = getEffect(state); |
| 379 | auto strength = getStrength(state); |
| 380 | |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 381 | if (!isAlwaysOnEffectSupported(effect)) { |
| 382 | state.SkipWithMessage("always on effect unsupported"); |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 383 | return; |
| 384 | } |
| 385 | |
| 386 | for (auto _ : state) { |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 387 | // Setup |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 388 | state.PauseTiming(); |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 389 | if (shouldSkipWithError(state, mVibrator->alwaysOnEnable(id, effect, strength))) { |
| 390 | return; |
| 391 | } |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 392 | state.ResumeTiming(); |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 393 | |
| 394 | // Test |
| 395 | if (shouldSkipWithError(state, mVibrator->alwaysOnDisable(id))) { |
| 396 | return; |
| 397 | } |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 398 | } |
| 399 | }); |
| 400 | |
Lais Andrade | 6677c6f | 2025-03-21 09:48:28 -0700 | [diff] [blame] | 401 | BENCHMARK_WRAPPER(SlowEffectsVibratorBench, perform, { |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 402 | auto effect = getEffect(state); |
| 403 | auto strength = getStrength(state); |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 404 | |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 405 | if (!isEffectSupported(effect)) { |
| 406 | state.SkipWithMessage("effect unsupported"); |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 407 | return; |
| 408 | } |
| 409 | |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 410 | int32_t lengthMs = 0; |
| 411 | |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 412 | for (auto _ : state) { |
Lais Andrade | 6677c6f | 2025-03-21 09:48:28 -0700 | [diff] [blame] | 413 | auto cb = hasCapabilities(IVibrator::CAP_PERFORM_CALLBACK) |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 414 | ? ndk::SharedRefBase::make<HalCallback>() |
| 415 | : nullptr; |
Lais Andrade | a5131c5 | 2024-06-12 16:39:29 +0100 | [diff] [blame] | 416 | // Grab the future before callback promise is destroyed by the HAL. |
| 417 | auto cbFuture = cb ? cb->getFuture() : std::future<void>(); |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 418 | |
| 419 | // Test |
| 420 | if (shouldSkipWithError(state, mVibrator->perform(effect, strength, cb, &lengthMs))) { |
| 421 | return; |
| 422 | } |
| 423 | |
| 424 | // Cleanup |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 425 | state.PauseTiming(); |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 426 | if (shouldSkipWithError(state, mVibrator->off())) { |
| 427 | return; |
| 428 | } |
Lais Andrade | a5131c5 | 2024-06-12 16:39:29 +0100 | [diff] [blame] | 429 | waitForComplete(cbFuture); |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 430 | state.ResumeTiming(); |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 431 | } |
| 432 | }); |
| 433 | |
Lais Andrade | 6677c6f | 2025-03-21 09:48:28 -0700 | [diff] [blame] | 434 | class PrimitivesVibratorBench : public VibratorBench { |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 435 | public: |
| 436 | static void DefaultArgs(Benchmark* b) { |
| 437 | b->ArgNames({"Primitive"}); |
Lais Andrade | 6677c6f | 2025-03-21 09:48:28 -0700 | [diff] [blame] | 438 | for (const auto& primitive : enum_range<CompositePrimitive>()) { |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 439 | b->Args({static_cast<long>(primitive)}); |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | protected: |
| 444 | auto getPrimitive(const State& state) const { |
Lais Andrade | 6677c6f | 2025-03-21 09:48:28 -0700 | [diff] [blame] | 445 | return static_cast<CompositePrimitive>(this->getOtherArg(state, 0)); |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 446 | } |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 447 | |
Lais Andrade | 6677c6f | 2025-03-21 09:48:28 -0700 | [diff] [blame] | 448 | bool isPrimitiveSupported(const CompositePrimitive& primitive) { |
| 449 | std::vector<CompositePrimitive> supported; |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 450 | mVibrator->getSupportedPrimitives(&supported); |
| 451 | return std::find(supported.begin(), supported.end(), primitive) != supported.end(); |
| 452 | } |
| 453 | }; |
| 454 | |
Lais Andrade | 6677c6f | 2025-03-21 09:48:28 -0700 | [diff] [blame] | 455 | class SlowPrimitivesVibratorBench : public PrimitivesVibratorBench { |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 456 | public: |
| 457 | static void DefaultConfig(Benchmark* b) { |
Lais Andrade | 6677c6f | 2025-03-21 09:48:28 -0700 | [diff] [blame] | 458 | PrimitivesVibratorBench::DefaultConfig(b); |
| 459 | SlowBenchConfig(b); |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 460 | } |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 461 | }; |
| 462 | |
Lais Andrade | 6677c6f | 2025-03-21 09:48:28 -0700 | [diff] [blame] | 463 | BENCHMARK_WRAPPER(PrimitivesVibratorBench, getCompositionDelayMax, { |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 464 | int32_t ms = 0; |
| 465 | |
| 466 | for (auto _ : state) { |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 467 | if (shouldSkipWithError(state, mVibrator->getCompositionDelayMax(&ms))) { |
| 468 | return; |
| 469 | } |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 470 | } |
| 471 | }); |
| 472 | |
Lais Andrade | 6677c6f | 2025-03-21 09:48:28 -0700 | [diff] [blame] | 473 | BENCHMARK_WRAPPER(PrimitivesVibratorBench, getCompositionSizeMax, { |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 474 | int32_t size = 0; |
| 475 | |
| 476 | for (auto _ : state) { |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 477 | if (shouldSkipWithError(state, mVibrator->getCompositionSizeMax(&size))) { |
| 478 | return; |
| 479 | } |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 480 | } |
| 481 | }); |
| 482 | |
Lais Andrade | 6677c6f | 2025-03-21 09:48:28 -0700 | [diff] [blame] | 483 | BENCHMARK_WRAPPER(PrimitivesVibratorBench, getPrimitiveDuration, { |
| 484 | if (!hasCapabilities(IVibrator::CAP_COMPOSE_EFFECTS)) { |
Adrian Roos | 7765942 | 2024-02-09 17:20:54 +0000 | [diff] [blame] | 485 | state.SkipWithMessage("compose effects unavailable"); |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 486 | return; |
| 487 | } |
| 488 | |
| 489 | auto primitive = getPrimitive(state); |
| 490 | int32_t ms = 0; |
| 491 | |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 492 | if (!isPrimitiveSupported(primitive)) { |
| 493 | state.SkipWithMessage("primitive unsupported"); |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 494 | return; |
| 495 | } |
| 496 | |
| 497 | for (auto _ : state) { |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 498 | if (shouldSkipWithError(state, mVibrator->getPrimitiveDuration(primitive, &ms))) { |
| 499 | return; |
| 500 | } |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 501 | } |
| 502 | }); |
| 503 | |
Lais Andrade | 6677c6f | 2025-03-21 09:48:28 -0700 | [diff] [blame] | 504 | BENCHMARK_WRAPPER(SlowPrimitivesVibratorBench, compose, { |
| 505 | if (!hasCapabilities(IVibrator::CAP_COMPOSE_EFFECTS)) { |
Adrian Roos | 7765942 | 2024-02-09 17:20:54 +0000 | [diff] [blame] | 506 | state.SkipWithMessage("compose effects unavailable"); |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 507 | return; |
| 508 | } |
| 509 | |
Lais Andrade | 6677c6f | 2025-03-21 09:48:28 -0700 | [diff] [blame] | 510 | CompositeEffect effect; |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 511 | effect.primitive = getPrimitive(state); |
| 512 | effect.scale = 1.0f; |
| 513 | effect.delayMs = 0; |
| 514 | |
Lais Andrade | 6677c6f | 2025-03-21 09:48:28 -0700 | [diff] [blame] | 515 | if (effect.primitive == CompositePrimitive::NOOP) { |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 516 | state.SkipWithMessage("skipping primitive NOOP"); |
| 517 | return; |
| 518 | } |
| 519 | if (!isPrimitiveSupported(effect.primitive)) { |
| 520 | state.SkipWithMessage("primitive unsupported"); |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 521 | return; |
| 522 | } |
| 523 | |
Lais Andrade | 6677c6f | 2025-03-21 09:48:28 -0700 | [diff] [blame] | 524 | std::vector<CompositeEffect> effects; |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 525 | effects.push_back(effect); |
| 526 | |
| 527 | for (auto _ : state) { |
Lais Andrade | 28c81f1 | 2024-06-24 14:32:22 +0100 | [diff] [blame] | 528 | auto cb = ndk::SharedRefBase::make<HalCallback>(); |
Lais Andrade | a5131c5 | 2024-06-12 16:39:29 +0100 | [diff] [blame] | 529 | // Grab the future before callback promise is moved and destroyed by the HAL. |
| 530 | auto cbFuture = cb->getFuture(); |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 531 | |
| 532 | // Test |
| 533 | if (shouldSkipWithError(state, mVibrator->compose(effects, cb))) { |
| 534 | return; |
| 535 | } |
| 536 | |
| 537 | // Cleanup |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 538 | state.PauseTiming(); |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 539 | if (shouldSkipWithError(state, mVibrator->off())) { |
| 540 | return; |
| 541 | } |
Lais Andrade | a5131c5 | 2024-06-12 16:39:29 +0100 | [diff] [blame] | 542 | waitForComplete(cbFuture); |
Lais Andrade | 59a5d54 | 2024-03-12 11:53:00 +0000 | [diff] [blame] | 543 | state.ResumeTiming(); |
Lais Andrade | e29acd8 | 2020-08-07 13:21:17 +0000 | [diff] [blame] | 544 | } |
| 545 | }); |
| 546 | |
| 547 | BENCHMARK_MAIN(); |