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