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