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 base::TimeTicks
Now() {
14 return base::TimeTicks::IsThreadNowSupported() ? base::TimeTicks::ThreadNow()
15 : base::TimeTicks::Now();
19 static const int kTimeLimitMillis
= 3000;
20 static const int kWarmupRuns
= 5;
21 static const int kTimeCheckInterval
= 10;
25 LapTimer::LapTimer(int warmup_laps
,
26 base::TimeDelta time_limit
,
28 : warmup_laps_(warmup_laps
),
29 remaining_warmups_(0),
30 remaining_no_check_laps_(0),
31 time_limit_(time_limit
),
32 check_interval_(check_interval
) {
33 DCHECK_GT(check_interval
, 0);
38 : LapTimer(kWarmupRuns
,
39 base::TimeDelta::FromMilliseconds(kTimeLimitMillis
),
43 void LapTimer::Reset() {
44 accumulator_
= base::TimeDelta();
46 remaining_warmups_
= warmup_laps_
;
47 remaining_no_check_laps_
= check_interval_
;
51 void LapTimer::Start() {
55 bool LapTimer::IsWarmedUp() { return remaining_warmups_
<= 0; }
57 void LapTimer::NextLap() {
66 --remaining_no_check_laps_
;
67 if (!remaining_no_check_laps_
) {
68 base::TimeTicks now
= Now();
69 accumulator_
+= now
- start_time_
;
71 remaining_no_check_laps_
= check_interval_
;
75 bool LapTimer::HasTimeLimitExpired() { return accumulator_
>= time_limit_
; }
77 bool LapTimer::HasTimedAllLaps() { return !(num_laps_
% check_interval_
); }
79 float LapTimer::MsPerLap() {
80 DCHECK(HasTimedAllLaps());
81 return accumulator_
.InMillisecondsF() / num_laps_
;
84 float LapTimer::LapsPerSecond() {
85 DCHECK(HasTimedAllLaps());
86 return num_laps_
/ accumulator_
.InSecondsF();
89 int LapTimer::NumLaps() { return num_laps_
; }