Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors |
[email protected] | bd5a374 | 2013-09-29 18:06:13 | [diff] [blame] | 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_ELAPSED_TIMER_H_ |
| 6 | #define BASE_TIMER_ELAPSED_TIMER_H_ |
| 7 | |
| 8 | #include "base/base_export.h" |
[email protected] | bd5a374 | 2013-09-29 18:06:13 | [diff] [blame] | 9 | #include "base/time/time.h" |
| 10 | |
| 11 | namespace base { |
| 12 | |
| 13 | // A simple wrapper around TimeTicks::Now(). |
| 14 | class BASE_EXPORT ElapsedTimer { |
| 15 | public: |
| 16 | ElapsedTimer(); |
Peter Boström | 75cd3c0 | 2021-09-28 15:23:18 | [diff] [blame] | 17 | |
| 18 | ElapsedTimer(const ElapsedTimer&) = delete; |
| 19 | ElapsedTimer& operator=(const ElapsedTimer&) = delete; |
| 20 | |
watk | badc743 | 2017-04-29 00:36:38 | [diff] [blame] | 21 | ElapsedTimer(ElapsedTimer&& other); |
| 22 | |
| 23 | void operator=(ElapsedTimer&& other); |
[email protected] | bd5a374 | 2013-09-29 18:06:13 | [diff] [blame] | 24 | |
| 25 | // Returns the time elapsed since object construction. |
hashimoto | dc2f102 | 2015-01-07 22:19:31 | [diff] [blame] | 26 | TimeDelta Elapsed() const; |
[email protected] | bd5a374 | 2013-09-29 18:06:13 | [diff] [blame] | 27 | |
Sadrul Habib Chowdhury | 31c0c1c2 | 2018-10-12 02:55:52 | [diff] [blame] | 28 | // Returns the timestamp of the creation of this timer. |
James Lee | db0f34d | 2023-10-09 10:51:22 | [diff] [blame] | 29 | TimeTicks start_time() const { return start_time_; } |
Sadrul Habib Chowdhury | 31c0c1c2 | 2018-10-12 02:55:52 | [diff] [blame] | 30 | |
[email protected] | bd5a374 | 2013-09-29 18:06:13 | [diff] [blame] | 31 | private: |
James Lee | db0f34d | 2023-10-09 10:51:22 | [diff] [blame] | 32 | TimeTicks start_time_; |
[email protected] | bd5a374 | 2013-09-29 18:06:13 | [diff] [blame] | 33 | }; |
| 34 | |
Benoît Lizé | 0af836f | 2019-05-15 09:44:41 | [diff] [blame] | 35 | // A simple wrapper around ThreadTicks::Now(). |
| 36 | class BASE_EXPORT ElapsedThreadTimer { |
| 37 | public: |
| 38 | ElapsedThreadTimer(); |
| 39 | |
Peter Boström | 75cd3c0 | 2021-09-28 15:23:18 | [diff] [blame] | 40 | ElapsedThreadTimer(const ElapsedThreadTimer&) = delete; |
| 41 | ElapsedThreadTimer& operator=(const ElapsedThreadTimer&) = delete; |
| 42 | |
Benoît Lizé | 0af836f | 2019-05-15 09:44:41 | [diff] [blame] | 43 | // Returns the ThreadTicks time elapsed since object construction. |
| 44 | // Only valid if |is_supported()| returns true, otherwise returns TimeDelta(). |
| 45 | TimeDelta Elapsed() const; |
| 46 | |
| 47 | bool is_supported() const { return is_supported_; } |
| 48 | |
| 49 | private: |
| 50 | const bool is_supported_; |
| 51 | const ThreadTicks begin_; |
Benoît Lizé | 0af836f | 2019-05-15 09:44:41 | [diff] [blame] | 52 | }; |
| 53 | |
Gabriel Charette | ef6cbc2 | 2019-08-02 06:24:59 | [diff] [blame] | 54 | // Whenever there's a ScopedMockElapsedTimersForTest in scope, |
| 55 | // Elapsed(Thread)Timers will always return kMockElapsedTime from Elapsed(). |
| 56 | // This is useful, for example, in unit tests that verify that their impl |
| 57 | // records timing histograms. It enables such tests to observe reliable timings. |
| 58 | class BASE_EXPORT ScopedMockElapsedTimersForTest { |
| 59 | public: |
Peter Kasting | 53fd6ee | 2021-10-05 20:40:48 | [diff] [blame] | 60 | static constexpr TimeDelta kMockElapsedTime = Milliseconds(1337); |
Gabriel Charette | ef6cbc2 | 2019-08-02 06:24:59 | [diff] [blame] | 61 | |
| 62 | // ScopedMockElapsedTimersForTest is not thread-safe (it must be instantiated |
| 63 | // in a test before other threads begin using ElapsedTimers; and it must |
| 64 | // conversely outlive any usage of ElapsedTimer in that test). |
| 65 | ScopedMockElapsedTimersForTest(); |
Gabriel Charette | ef6cbc2 | 2019-08-02 06:24:59 | [diff] [blame] | 66 | |
Peter Boström | 7319bbd | 2021-09-15 22:59:38 | [diff] [blame] | 67 | ScopedMockElapsedTimersForTest(const ScopedMockElapsedTimersForTest&) = |
| 68 | delete; |
| 69 | ScopedMockElapsedTimersForTest& operator=( |
| 70 | const ScopedMockElapsedTimersForTest&) = delete; |
| 71 | |
| 72 | ~ScopedMockElapsedTimersForTest(); |
Gabriel Charette | ef6cbc2 | 2019-08-02 06:24:59 | [diff] [blame] | 73 | }; |
| 74 | |
[email protected] | bd5a374 | 2013-09-29 18:06:13 | [diff] [blame] | 75 | } // namespace base |
| 76 | |
| 77 | #endif // BASE_TIMER_ELAPSED_TIMER_H_ |