blob: 05f2defc50f4766642adf22cf7ffbc15e6df4809 [file] [log] [blame]
Richard Townsend5d0b4772019-03-06 13:33:301// 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.
4
5#ifndef BASE_TIMER_LAP_TIMER_H_
6#define BASE_TIMER_LAP_TIMER_H_
7
8#include "base/base_export.h"
9#include "base/macros.h"
10#include "base/sequence_checker.h"
11#include "base/time/time.h"
12
13namespace base {
14
15// LapTimer is used to calculate average times per "Lap" in perf tests.
16// NextLap increments the lap counter, used in counting the per lap averages.
17// If you initialize the LapTimer with a non zero |warmup_laps|, it will ignore
18// the times for that many laps at the start.
19// If you set the |time_limit| then you can use HasTimeLimitExpired() to see if
20// the current accumulated time has crossed that threshold, with an optimization
21// that it only tests this every |check_interval| laps.
22//
23// See base/timer/lap_timer_unittest.cc for a usage example.
24//
25class BASE_EXPORT LapTimer {
26 public:
27 enum class TimerMethod {
28 // Measures CPU time consumed by the thread running the LapTimer.
29 kUseThreadTicks,
30 // Measures elapsed wall time (default).
31 kUseTimeTicks
32 };
33
34 LapTimer(int warmup_laps,
35 TimeDelta time_limit,
36 int check_interval,
37 TimerMethod timing_method = TimerMethod::kUseTimeTicks);
38 // Create LapTimer with sensible default values.
39 LapTimer(TimerMethod timing_method = TimerMethod::kUseTimeTicks);
Peter Boström75cd3c02021-09-28 15:23:1840
41 LapTimer(const LapTimer&) = delete;
42 LapTimer& operator=(const LapTimer&) = delete;
43
Richard Townsend5d0b4772019-03-06 13:33:3044 // Sets the timer back to its starting state.
45 void Reset();
46 // Sets the start point to now.
47 void Start();
48 // Returns true if there are no more warmup laps to do.
49 bool IsWarmedUp() const;
50 // Advance the lap counter and update the accumulated time.
51 // The accumulated time is only updated every check_interval laps.
52 // If accumulating then the start point will also be updated.
53 void NextLap();
54 // Returns true if the stored time has exceeded the time limit specified.
55 // May cause a call to Store().
56 bool HasTimeLimitExpired() const;
57 // The average time taken per lap.
58 TimeDelta TimePerLap() const;
59 // The number of laps per second.
60 float LapsPerSecond() const;
61 // The number of laps recorded.
62 int NumLaps() const;
63
64 private:
65 // Returns true if all lap times have been timed. Only true every n'th
66 // lap, where n = check_interval.
67 bool HasTimedAllLaps() const;
68 // Returns the current accumulated time.
69 TimeDelta GetAccumulatedTime() const;
70
71 const int warmup_laps_;
72 const TimeDelta time_limit_;
73 const int check_interval_;
74 const TimerMethod method_;
75
76 ThreadTicks start_thread_ticks_;
77 TimeTicks start_time_ticks_;
78
79 ThreadTicks last_timed_lap_end_thread_ticks_;
80 TimeTicks last_timed_lap_end_ticks_;
81
82 int num_laps_;
83 int remaining_warmups_ = 0;
84 int remaining_no_check_laps_ = 0;
85
86 SEQUENCE_CHECKER(sequence_checker_);
Richard Townsend5d0b4772019-03-06 13:33:3087};
88} // namespace base
89
90#endif // BASE_TIMER_LAP_TIMER_H_