Roll src/third_party/WebKit 2f4214f:d3a4736 (svn 202056:202057)
[chromium-blink-merge.git] / cc / debug / lap_timer.cc
blobeb6900c460a7403c7abdaaf882c8a3b25ba89e3a
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 // 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
15 // current platform.
16 base::TimeDelta Now() {
17 return base::ThreadTicks::IsSupported()
18 ? base::ThreadTicks::Now() - base::ThreadTicks()
19 : base::TimeTicks::Now() - base::TimeTicks();
22 // Default values.
23 static const int kTimeLimitMillis = 3000;
24 static const int kWarmupRuns = 5;
25 static const int kTimeCheckInterval = 10;
27 } // namespace
29 LapTimer::LapTimer(int warmup_laps,
30 base::TimeDelta time_limit,
31 int check_interval)
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);
38 Reset();
41 LapTimer::LapTimer()
42 : LapTimer(kWarmupRuns,
43 base::TimeDelta::FromMilliseconds(kTimeLimitMillis),
44 kTimeCheckInterval) {
47 void LapTimer::Reset() {
48 accumulator_ = base::TimeDelta();
49 num_laps_ = 0;
50 remaining_warmups_ = warmup_laps_;
51 remaining_no_check_laps_ = check_interval_;
52 Start();
55 void LapTimer::Start() {
56 start_time_ = Now();
59 bool LapTimer::IsWarmedUp() { return remaining_warmups_ <= 0; }
61 void LapTimer::NextLap() {
62 if (!IsWarmedUp()) {
63 --remaining_warmups_;
64 if (IsWarmedUp()) {
65 Start();
67 return;
69 ++num_laps_;
70 --remaining_no_check_laps_;
71 if (!remaining_no_check_laps_) {
72 base::TimeDelta now = Now();
73 accumulator_ += now - start_time_;
74 start_time_ = now;
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_; }
95 } // namespace cc