Roll src/third_party/WebKit 0dfcee8:b5303f0 (svn 194095:194099)
[chromium-blink-merge.git] / cc / debug / lap_timer.cc
blob17bca3d313f11869563d90728bbb46741b345d9e
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"
9 namespace cc {
11 namespace {
13 base::TimeTicks Now() {
14 return base::TimeTicks::IsThreadNowSupported() ? base::TimeTicks::ThreadNow()
15 : base::TimeTicks::Now();
18 // Default values.
19 static const int kTimeLimitMillis = 3000;
20 static const int kWarmupRuns = 5;
21 static const int kTimeCheckInterval = 10;
23 } // namespace
25 LapTimer::LapTimer(int warmup_laps,
26 base::TimeDelta time_limit,
27 int check_interval)
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);
34 Reset();
37 LapTimer::LapTimer()
38 : LapTimer(kWarmupRuns,
39 base::TimeDelta::FromMilliseconds(kTimeLimitMillis),
40 kTimeCheckInterval) {
43 void LapTimer::Reset() {
44 accumulator_ = base::TimeDelta();
45 num_laps_ = 0;
46 remaining_warmups_ = warmup_laps_;
47 remaining_no_check_laps_ = check_interval_;
48 Start();
51 void LapTimer::Start() {
52 start_time_ = Now();
55 bool LapTimer::IsWarmedUp() { return remaining_warmups_ <= 0; }
57 void LapTimer::NextLap() {
58 if (!IsWarmedUp()) {
59 --remaining_warmups_;
60 if (IsWarmedUp()) {
61 Start();
63 return;
65 ++num_laps_;
66 --remaining_no_check_laps_;
67 if (!remaining_no_check_laps_) {
68 base::TimeTicks now = Now();
69 accumulator_ += now - start_time_;
70 start_time_ = now;
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_; }
91 } // namespace cc