1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "cc/debug/lap_timer.h"
7 #include "base/logging.h"
13 // Returns the offset from the origin from the ThreadTicks time source.
14 // TimeTicks is used as a fallback if ThreadTicks is not available on the
16 base::TimeDelta
Now() {
17 return base::ThreadTicks::IsSupported()
18 ? base::ThreadTicks::Now() - base::ThreadTicks()
19 : base::TimeTicks::Now() - base::TimeTicks();
23 static const int kTimeLimitMillis
= 3000;
24 static const int kWarmupRuns
= 5;
25 static const int kTimeCheckInterval
= 10;
29 LapTimer::LapTimer(int warmup_laps
,
30 base::TimeDelta time_limit
,
32 : warmup_laps_(warmup_laps
),
33 remaining_warmups_(0),
34 remaining_no_check_laps_(0),
35 time_limit_(time_limit
),
36 check_interval_(check_interval
) {
37 DCHECK_GT(check_interval
, 0);
42 : LapTimer(kWarmupRuns
,
43 base::TimeDelta::FromMilliseconds(kTimeLimitMillis
),
47 void LapTimer::Reset() {
48 accumulator_
= base::TimeDelta();
50 remaining_warmups_
= warmup_laps_
;
51 remaining_no_check_laps_
= check_interval_
;
55 void LapTimer::Start() {
59 bool LapTimer::IsWarmedUp() { return remaining_warmups_
<= 0; }
61 void LapTimer::NextLap() {
70 --remaining_no_check_laps_
;
71 if (!remaining_no_check_laps_
) {
72 base::TimeDelta now
= Now();
73 accumulator_
+= now
- start_time_
;
75 remaining_no_check_laps_
= check_interval_
;
79 bool LapTimer::HasTimeLimitExpired() { return accumulator_
>= time_limit_
; }
81 bool LapTimer::HasTimedAllLaps() { return !(num_laps_
% check_interval_
); }
83 float LapTimer::MsPerLap() {
84 DCHECK(HasTimedAllLaps());
85 return accumulator_
.InMillisecondsF() / num_laps_
;
88 float LapTimer::LapsPerSecond() {
89 DCHECK(HasTimedAllLaps());
90 return num_laps_
/ accumulator_
.InSecondsF();
93 int LapTimer::NumLaps() { return num_laps_
; }