• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 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.
4 
5 #include "cc/test/lap_timer.h"
6 
7 #include "base/logging.h"
8 
9 namespace cc {
10 
LapTimer(int warmup_laps,base::TimeDelta time_limit,int check_interval)11 LapTimer::LapTimer(int warmup_laps,
12                    base::TimeDelta time_limit,
13                    int check_interval)
14     : warmup_laps_(warmup_laps),
15       time_limit_(time_limit),
16       check_interval_(check_interval) {
17   DCHECK_GT(check_interval, 0);
18   Reset();
19 }
20 
Reset()21 void LapTimer::Reset() {
22   accumulator_ = base::TimeDelta();
23   num_laps_ = 0;
24   accumulated_ = true;
25   remaining_warmups_ = warmup_laps_;
26   Start();
27 }
28 
Start()29 void LapTimer::Start() { start_time_ = base::TimeTicks::HighResNow(); }
30 
IsWarmedUp()31 bool LapTimer::IsWarmedUp() { return remaining_warmups_ <= 0; }
32 
NextLap()33 void LapTimer::NextLap() {
34   if (!IsWarmedUp()) {
35     --remaining_warmups_;
36     if (IsWarmedUp()) {
37       Start();
38     }
39     return;
40   }
41   ++num_laps_;
42   accumulated_ = (num_laps_ % check_interval_) == 0;
43   if (accumulated_) {
44     base::TimeTicks now = base::TimeTicks::HighResNow();
45     accumulator_ += now - start_time_;
46     start_time_ = now;
47   }
48 }
49 
HasTimeLimitExpired()50 bool LapTimer::HasTimeLimitExpired() { return accumulator_ >= time_limit_; }
51 
MsPerLap()52 float LapTimer::MsPerLap() {
53   DCHECK(accumulated_);
54   return accumulator_.InMillisecondsF() / num_laps_;
55 }
56 
LapsPerSecond()57 float LapTimer::LapsPerSecond() {
58   DCHECK(accumulated_);
59   return num_laps_ / accumulator_.InSecondsF();
60 }
61 
NumLaps()62 int LapTimer::NumLaps() { return num_laps_; }
63 
64 }  // namespace cc
65