| 6 | #define BASE_TIMER_ELAPSED_TIMER_H_ |
| 7 |
| 8 | #include "base/base_export.h" |
| 9 | #include "base/macros.h" |
| 10 | #include "base/time/time.h" |
| 11 |
| 12 | namespace base { |
| 13 |
| 14 | // A simple wrapper around TimeTicks::Now(). |
| 15 | class BASE_EXPORT ElapsedTimer { |
| 16 | public: |
| 17 | ElapsedTimer(); |
| 18 | ElapsedTimer(ElapsedTimer&& other); |
| 19 |
| 20 | void operator=(ElapsedTimer&& other); |
| 21 |
| 22 | // Returns the time elapsed since object construction. |
| 23 | TimeDelta Elapsed() const; |
| 24 |
| 25 | // Returns the timestamp of the creation of this timer. |
| 26 | TimeTicks Begin() const { return begin_; } |
| 27 |
| 28 | private: |
| 29 | TimeTicks begin_; |
| 30 |
| 31 | DISALLOW_COPY_AND_ASSIGN(ElapsedTimer); |
| 32 | }; |
| 33 |
| 34 | // A simple wrapper around ThreadTicks::Now(). |
| 35 | class BASE_EXPORT ElapsedThreadTimer { |
| 36 | public: |
| 37 | ElapsedThreadTimer(); |
| 38 |
| 39 | // Returns the ThreadTicks time elapsed since object construction. |
| 40 | // Only valid if |is_supported()| returns true, otherwise returns TimeDelta(). |
| 41 | TimeDelta Elapsed() const; |
| 42 |
| 43 | bool is_supported() const { return is_supported_; } |
| 44 |
| 45 | private: |
| 46 | const bool is_supported_; |
| 47 | const ThreadTicks begin_; |
| 48 |
| 49 | DISALLOW_COPY_AND_ASSIGN(ElapsedThreadTimer); |
| 50 | }; |
| 51 |
| 52 | // Whenever there's a ScopedMockElapsedTimersForTest in scope, |
| 53 | // Elapsed(Thread)Timers will always return kMockElapsedTime from Elapsed(). |
| 54 | // This is useful, for example, in unit tests that verify that their impl |
| 55 | // records timing histograms. It enables such tests to observe reliable timings. |
| 56 | class BASE_EXPORT ScopedMockElapsedTimersForTest { |
| 57 | public: |
| 58 | static constexpr TimeDelta kMockElapsedTime = |
| 59 | TimeDelta::FromMilliseconds(1337); |
| 60 |
| 61 | // ScopedMockElapsedTimersForTest is not thread-safe (it must be instantiated |
| 62 | // in a test before other threads begin using ElapsedTimers; and it must |
| 63 | // conversely outlive any usage of ElapsedTimer in that test). |
| 64 | ScopedMockElapsedTimersForTest(); |
| 65 | ~ScopedMockElapsedTimersForTest(); |
| 66 |
| 67 | private: |
| 68 | DISALLOW_COPY_AND_ASSIGN(ScopedMockElapsedTimersForTest); |
| 69 | }; |
| 70 |
| 71 | } // namespace base |
| 72 |
| 73 | #endif // BASE_TIMER_ELAPSED_TIMER_H_ |
| 74 |