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