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