1 // Copyright 2014 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/timer/lap_timer.h"
6 #include "base/check_op.h"
7
8 namespace base {
9
10 namespace {
11
12 // Default values.
13 constexpr TimeDelta kDefaultTimeLimit = Seconds(3);
14 constexpr int kDefaultWarmupRuns = 5;
15 constexpr int kDefaultTimeCheckInterval = 10;
16
17 } // namespace
18
LapTimer(int warmup_laps,TimeDelta time_limit,int check_interval,LapTimer::TimerMethod method)19 LapTimer::LapTimer(int warmup_laps,
20 TimeDelta time_limit,
21 int check_interval,
22 LapTimer::TimerMethod method)
23 : warmup_laps_(warmup_laps),
24 time_limit_(time_limit),
25 check_interval_(check_interval),
26 method_(method) {
27 DETACH_FROM_SEQUENCE(sequence_checker_);
28 DCHECK_GT(check_interval, 0);
29 Reset();
30 }
31
LapTimer(LapTimer::TimerMethod method)32 LapTimer::LapTimer(LapTimer::TimerMethod method)
33 : LapTimer(kDefaultWarmupRuns,
34 kDefaultTimeLimit,
35 kDefaultTimeCheckInterval,
36 method) {}
37
Reset()38 void LapTimer::Reset() {
39 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
40 if (ThreadTicks::IsSupported() && method_ == TimerMethod::kUseThreadTicks)
41 ThreadTicks::WaitUntilInitialized();
42 num_laps_ = 0;
43 remaining_warmups_ = warmup_laps_;
44 remaining_no_check_laps_ = check_interval_;
45 Start();
46 }
47
Start()48 void LapTimer::Start() {
49 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
50 DCHECK_EQ(num_laps_, 0);
51 // last_timed_ variables are initialized here (instead of in the constructor)
52 // because not all platforms support ThreadTicks.
53 if (method_ == TimerMethod::kUseThreadTicks) {
54 start_thread_ticks_ = ThreadTicks::Now();
55 last_timed_lap_end_thread_ticks_ = ThreadTicks::Now();
56 } else {
57 start_time_ticks_ = TimeTicks::Now();
58 last_timed_lap_end_ticks_ = TimeTicks::Now();
59 }
60 }
61
IsWarmedUp() const62 bool LapTimer::IsWarmedUp() const {
63 return remaining_warmups_ <= 0;
64 }
65
NextLap()66 void LapTimer::NextLap() {
67 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
68 DCHECK(!start_thread_ticks_.is_null() || !start_time_ticks_.is_null());
69 if (!IsWarmedUp()) {
70 --remaining_warmups_;
71 if (IsWarmedUp()) {
72 Start();
73 }
74 return;
75 }
76 ++num_laps_;
77 --remaining_no_check_laps_;
78 if (!remaining_no_check_laps_) {
79 if (method_ == TimerMethod::kUseTimeTicks) {
80 last_timed_lap_end_ticks_ = TimeTicks::Now();
81 } else {
82 last_timed_lap_end_thread_ticks_ = ThreadTicks::Now();
83 }
84 remaining_no_check_laps_ = check_interval_;
85 }
86 }
87
GetAccumulatedTime() const88 TimeDelta LapTimer::GetAccumulatedTime() const {
89 if (method_ == TimerMethod::kUseTimeTicks) {
90 return last_timed_lap_end_ticks_ - start_time_ticks_;
91 }
92 return last_timed_lap_end_thread_ticks_ - start_thread_ticks_;
93 }
94
HasTimeLimitExpired() const95 bool LapTimer::HasTimeLimitExpired() const {
96 return GetAccumulatedTime() >= time_limit_;
97 }
98
HasTimedAllLaps() const99 bool LapTimer::HasTimedAllLaps() const {
100 return num_laps_ && !(num_laps_ % check_interval_);
101 }
102
TimePerLap() const103 TimeDelta LapTimer::TimePerLap() const {
104 DCHECK(HasTimedAllLaps());
105 DCHECK_GT(num_laps_, 0);
106 return GetAccumulatedTime() / num_laps_;
107 }
108
LapsPerSecond() const109 float LapTimer::LapsPerSecond() const {
110 DCHECK(HasTimedAllLaps());
111 DCHECK_GT(num_laps_, 0);
112 return num_laps_ / GetAccumulatedTime().InSecondsF();
113 }
114
NumLaps() const115 int LapTimer::NumLaps() const {
116 return num_laps_;
117 }
118 } // namespace base
119