1 /*
2 * Copyright 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 <chrono>
18 #include <future>
19 #include <unordered_map>
20
21 #include "benchmark/benchmark.h"
22
23 #include "os/alarm.h"
24 #include "os/repeating_alarm.h"
25 #include "os/thread.h"
26
27 using ::benchmark::State;
28 using ::bluetooth::os::Alarm;
29 using ::bluetooth::os::RepeatingAlarm;
30 using ::bluetooth::os::Thread;
31
32 class BM_ReactableAlarm : public ::benchmark::Fixture {
33 protected:
SetUp(State & st)34 void SetUp(State& st) override {
35 ::benchmark::Fixture::SetUp(st);
36 thread_ = std::make_unique<Thread>("timer_benchmark", Thread::Priority::REAL_TIME);
37 alarm_ = std::make_unique<Alarm>(thread_.get());
38 repeating_alarm_ = std::make_unique<RepeatingAlarm>(thread_.get());
39 map_.clear();
40 scheduled_tasks_ = 0;
41 task_length_ = 0;
42 task_interval_ = 0;
43 task_counter_ = 0;
44 promise_ = std::promise<void>();
45 }
46
TearDown(State & st)47 void TearDown(State& st) override {
48 alarm_ = nullptr;
49 repeating_alarm_ = nullptr;
50 thread_->Stop();
51 thread_ = nullptr;
52 ::benchmark::Fixture::TearDown(st);
53 }
54
AlarmSleepAndCountDelayedTime()55 void AlarmSleepAndCountDelayedTime() {
56 auto end_time = std::chrono::steady_clock::now();
57 auto duration_since_start = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time_);
58 task_counter_++;
59 map_[duration_since_start.count() - task_counter_ * task_interval_]++;
60 std::this_thread::sleep_for(std::chrono::milliseconds(task_length_));
61 if (task_counter_ >= scheduled_tasks_) {
62 promise_.set_value();
63 }
64 }
65
TimerFire()66 void TimerFire() {
67 promise_.set_value();
68 }
69
70 int64_t scheduled_tasks_;
71 int64_t task_length_;
72 int64_t task_interval_;
73 int task_counter_;
74 std::unordered_map<int, int> map_;
75 std::promise<void> promise_;
76 std::chrono::time_point<std::chrono::steady_clock> start_time_;
77 std::unique_ptr<Thread> thread_;
78 std::unique_ptr<Alarm> alarm_;
79 std::unique_ptr<RepeatingAlarm> repeating_alarm_;
80 };
81
BENCHMARK_DEFINE_F(BM_ReactableAlarm,timer_performance_ms)82 BENCHMARK_DEFINE_F(BM_ReactableAlarm, timer_performance_ms)(State& state) {
83 auto milliseconds = static_cast<int>(state.range(0));
84 for (auto _ : state) {
85 auto start_time_point = std::chrono::steady_clock::now();
86 alarm_->Schedule([this] { return TimerFire(); }, std::chrono::milliseconds(milliseconds));
87 promise_.get_future().get();
88 auto end_time_point = std::chrono::steady_clock::now();
89 auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(end_time_point - start_time_point);
90 state.SetIterationTime(static_cast<double>(duration.count()) * 1e-6);
91 alarm_->Cancel();
92 }
93 };
94
95 BENCHMARK_REGISTER_F(BM_ReactableAlarm, timer_performance_ms)
96 ->Arg(1)
97 ->Arg(5)
98 ->Arg(10)
99 ->Arg(20)
100 ->Arg(100)
101 ->Arg(1000)
102 ->Arg(2000)
103 ->Iterations(1)
104 ->UseRealTime();
105
BENCHMARK_DEFINE_F(BM_ReactableAlarm,periodic_accuracy)106 BENCHMARK_DEFINE_F(BM_ReactableAlarm, periodic_accuracy)(State& state) {
107 for (auto _ : state) {
108 scheduled_tasks_ = state.range(0);
109 task_length_ = state.range(1);
110 task_interval_ = state.range(2);
111 start_time_ = std::chrono::steady_clock::now();
112 repeating_alarm_->Schedule([this] { AlarmSleepAndCountDelayedTime(); }, std::chrono::milliseconds(task_interval_));
113 promise_.get_future().get();
114 repeating_alarm_->Cancel();
115 }
116 for (const auto& delay : map_) {
117 state.counters[std::to_string(delay.first)] = delay.second;
118 }
119 };
120
121 BENCHMARK_REGISTER_F(BM_ReactableAlarm, periodic_accuracy)
122 ->Args({2000, 1, 5})
123 ->Args({2000, 3, 5})
124 ->Args({2000, 1, 7})
125 ->Args({2000, 3, 7})
126 ->Args({2000, 1, 20})
127 ->Args({2000, 5, 20})
128 ->Args({2000, 10, 20})
129 ->Args({2000, 15, 20})
130 ->Iterations(1)
131 ->UseRealTime();
132