blob: 1f2ecf6fa9f9d7df0ec9fad6e0e59dbf90d5d51e [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2013 The Chromium Authors
[email protected]bd5a3742013-09-29 18:06:132// 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]bd5a3742013-09-29 18:06:139#include "base/time/time.h"
10
11namespace base {
12
13// A simple wrapper around TimeTicks::Now().
14class BASE_EXPORT ElapsedTimer {
15 public:
16 ElapsedTimer();
Peter Boström75cd3c02021-09-28 15:23:1817
18 ElapsedTimer(const ElapsedTimer&) = delete;
19 ElapsedTimer& operator=(const ElapsedTimer&) = delete;
20
watkbadc7432017-04-29 00:36:3821 ElapsedTimer(ElapsedTimer&& other);
22
23 void operator=(ElapsedTimer&& other);
[email protected]bd5a3742013-09-29 18:06:1324
25 // Returns the time elapsed since object construction.
hashimotodc2f1022015-01-07 22:19:3126 TimeDelta Elapsed() const;
[email protected]bd5a3742013-09-29 18:06:1327
Sadrul Habib Chowdhury31c0c1c22018-10-12 02:55:5228 // Returns the timestamp of the creation of this timer.
James Leedb0f34d2023-10-09 10:51:2229 TimeTicks start_time() const { return start_time_; }
Sadrul Habib Chowdhury31c0c1c22018-10-12 02:55:5230
[email protected]bd5a3742013-09-29 18:06:1331 private:
James Leedb0f34d2023-10-09 10:51:2232 TimeTicks start_time_;
[email protected]bd5a3742013-09-29 18:06:1333};
34
Benoît Lizé0af836f2019-05-15 09:44:4135// A simple wrapper around ThreadTicks::Now().
36class BASE_EXPORT ElapsedThreadTimer {
37 public:
38 ElapsedThreadTimer();
39
Peter Boström75cd3c02021-09-28 15:23:1840 ElapsedThreadTimer(const ElapsedThreadTimer&) = delete;
41 ElapsedThreadTimer& operator=(const ElapsedThreadTimer&) = delete;
42
Benoît Lizé0af836f2019-05-15 09:44:4143 // 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é0af836f2019-05-15 09:44:4152};
53
Gabriel Charetteef6cbc22019-08-02 06:24:5954// 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.
58class BASE_EXPORT ScopedMockElapsedTimersForTest {
59 public:
Peter Kasting53fd6ee2021-10-05 20:40:4860 static constexpr TimeDelta kMockElapsedTime = Milliseconds(1337);
Gabriel Charetteef6cbc22019-08-02 06:24:5961
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 Charetteef6cbc22019-08-02 06:24:5966
Peter Boström7319bbd2021-09-15 22:59:3867 ScopedMockElapsedTimersForTest(const ScopedMockElapsedTimersForTest&) =
68 delete;
69 ScopedMockElapsedTimersForTest& operator=(
70 const ScopedMockElapsedTimersForTest&) = delete;
71
72 ~ScopedMockElapsedTimersForTest();
Gabriel Charetteef6cbc22019-08-02 06:24:5973};
74
[email protected]bd5a3742013-09-29 18:06:1375} // namespace base
76
77#endif // BASE_TIMER_ELAPSED_TIMER_H_