lap_timer.h 2.9 KB

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