1919#include < aidl/android/hardware/power/Boost.h>
2020#include < aidl/android/hardware/power/Mode.h>
2121#include < benchmark/benchmark.h>
22+ #include < chrono>
2223#include < powermanager/PowerHalController.h>
2324#include < testUtil.h>
24- #include < chrono>
2525
2626using aidl::android::hardware::power::Boost;
2727using aidl::android::hardware::power::Mode;
@@ -32,22 +32,38 @@ using namespace android;
3232using namespace std ::chrono_literals;
3333
3434// Values from Boost.aidl and Mode.aidl.
35- static constexpr int64_t FIRST_BOOST = static_cast <int64_t >(Boost::INTERACTION );
36- static constexpr int64_t LAST_BOOST = static_cast <int64_t >(Boost::CAMERA_SHOT );
37- static constexpr int64_t FIRST_MODE = static_cast <int64_t >(Mode::DOUBLE_TAP_TO_WAKE );
38- static constexpr int64_t LAST_MODE = static_cast <int64_t >(Mode::CAMERA_STREAMING_HIGH );
35+ static constexpr int64_t FIRST_BOOST = static_cast <int64_t >(*ndk::enum_range<Boost>().begin() );
36+ static constexpr int64_t LAST_BOOST = static_cast <int64_t >(*(ndk::enum_range<Boost>().end()- 1 ) );
37+ static constexpr int64_t FIRST_MODE = static_cast <int64_t >(*ndk::enum_range<Mode>().begin() );
38+ static constexpr int64_t LAST_MODE = static_cast <int64_t >(*(ndk::enum_range<Mode>().end()- 1 ) );
3939
4040// Delay between oneway method calls to avoid overflowing the binder buffers.
4141static constexpr std::chrono::microseconds ONEWAY_API_DELAY = 100us;
4242
4343template <typename T, class ... Args0, class ... Args1>
4444static void runBenchmark (benchmark::State& state, HalResult<T> (PowerHalController::*fn)(Args0...),
4545 Args1&&... args1) {
46- while (state.KeepRunning ()) {
47- PowerHalController controller;
46+ PowerHalController initController;
47+ HalResult<T> result = (initController.*fn)(std::forward<Args1>(args1)...);
48+ if (result.isFailed ()) {
49+ state.SkipWithError (result.errorMessage ());
50+ return ;
51+ } else if (result.isUnsupported ()) {
52+ ALOGV (" Power HAL does not support this operation, skipping test..." );
53+ state.SkipWithMessage (" operation unsupported" );
54+ return ;
55+ }
56+
57+ for (auto _ : state) {
58+ PowerHalController controller; // new controller to avoid caching
4859 HalResult<T> ret = (controller.*fn)(std::forward<Args1>(args1)...);
60+ if (ret.isFailed ()) {
61+ state.SkipWithError (ret.errorMessage ());
62+ break ;
63+ }
4964 state.PauseTiming ();
50- if (ret.isFailed ()) state.SkipWithError (" Power HAL request failed" );
65+ testDelaySpin (
66+ std::chrono::duration_cast<std::chrono::duration<float >>(ONEWAY_API_DELAY).count ());
5167 state.ResumeTiming ();
5268 }
5369}
@@ -57,22 +73,27 @@ static void runCachedBenchmark(benchmark::State& state,
5773 HalResult<T> (PowerHalController::*fn)(Args0...), Args1&&... args1) {
5874 PowerHalController controller;
5975 // First call out of test, to cache HAL service and isSupported result.
60- (controller.*fn)(std::forward<Args1>(args1)...);
76+ HalResult<T> result = (controller.*fn)(std::forward<Args1>(args1)...);
77+ if (result.isFailed ()) {
78+ state.SkipWithError (result.errorMessage ());
79+ return ;
80+ } else if (result.isUnsupported ()) {
81+ ALOGV (" Power HAL does not support this operation, skipping test..." );
82+ state.SkipWithMessage (" operation unsupported" );
83+ return ;
84+ }
6185
62- while ( state. KeepRunning () ) {
86+ for ( auto _ : state) {
6387 HalResult<T> ret = (controller.*fn)(std::forward<Args1>(args1)...);
64- state.PauseTiming ();
6588 if (ret.isFailed ()) {
66- state.SkipWithError (" Power HAL request failed" );
89+ state.SkipWithError (ret.errorMessage ());
90+ break ;
6791 }
68- testDelaySpin (
69- std::chrono::duration_cast<std::chrono::duration<float >>(ONEWAY_API_DELAY).count ());
70- state.ResumeTiming ();
7192 }
7293}
7394
7495static void BM_PowerHalControllerBenchmarks_init (benchmark::State& state) {
75- while ( state. KeepRunning () ) {
96+ for ( auto _ : state) {
7697 PowerHalController controller;
7798 controller.init ();
7899 }
@@ -90,12 +111,12 @@ static void BM_PowerHalControllerBenchmarks_initCached(benchmark::State& state)
90111
91112static void BM_PowerHalControllerBenchmarks_setBoost (benchmark::State& state) {
92113 Boost boost = static_cast <Boost>(state.range (0 ));
93- runBenchmark (state, &PowerHalController::setBoost, boost, 0 );
114+ runBenchmark (state, &PowerHalController::setBoost, boost, 1 );
94115}
95116
96117static void BM_PowerHalControllerBenchmarks_setBoostCached (benchmark::State& state) {
97118 Boost boost = static_cast <Boost>(state.range (0 ));
98- runCachedBenchmark (state, &PowerHalController::setBoost, boost, 0 );
119+ runCachedBenchmark (state, &PowerHalController::setBoost, boost, 1 );
99120}
100121
101122static void BM_PowerHalControllerBenchmarks_setMode (benchmark::State& state) {
0 commit comments