lap_timer.h 2.8 KB

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