blob: 30093043c40401d00838dad65431995885efc3e3 [file] [log] [blame]
[email protected]bd5a3742013-09-29 18:06:131// 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"
hashimotodc2f1022015-01-07 22:19:319#include "base/macros.h"
[email protected]bd5a3742013-09-29 18:06:1310#include "base/time/time.h"
11
12namespace base {
13
14// A simple wrapper around TimeTicks::Now().
15class BASE_EXPORT ElapsedTimer {
16 public:
17 ElapsedTimer();
Peter Boström75cd3c02021-09-28 15:23:1818
19 ElapsedTimer(const ElapsedTimer&) = delete;
20 ElapsedTimer& operator=(const ElapsedTimer&) = delete;
21
watkbadc7432017-04-29 00:36:3822 ElapsedTimer(ElapsedTimer&& other);
23
24 void operator=(ElapsedTimer&& other);
[email protected]bd5a3742013-09-29 18:06:1325
26 // Returns the time elapsed since object construction.
hashimotodc2f1022015-01-07 22:19:3127 TimeDelta Elapsed() const;
[email protected]bd5a3742013-09-29 18:06:1328
Sadrul Habib Chowdhury31c0c1c22018-10-12 02:55:5229 // Returns the timestamp of the creation of this timer.
30 TimeTicks Begin() const { return begin_; }
31
[email protected]bd5a3742013-09-29 18:06:1332 private:
33 TimeTicks begin_;
[email protected]bd5a3742013-09-29 18:06:1334};
35
Benoît Lizé0af836f2019-05-15 09:44:4136// A simple wrapper around ThreadTicks::Now().
37class BASE_EXPORT ElapsedThreadTimer {
38 public:
39 ElapsedThreadTimer();
40
Peter Boström75cd3c02021-09-28 15:23:1841 ElapsedThreadTimer(const ElapsedThreadTimer&) = delete;
42 ElapsedThreadTimer& operator=(const ElapsedThreadTimer&) = delete;
43
Benoît Lizé0af836f2019-05-15 09:44:4144 // 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é0af836f2019-05-15 09:44:4153};
54
Gabriel Charetteef6cbc22019-08-02 06:24:5955// 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.
59class BASE_EXPORT ScopedMockElapsedTimersForTest {
60 public:
Peter Kasting53fd6ee2021-10-05 20:40:4861 static constexpr TimeDelta kMockElapsedTime = Milliseconds(1337);
Gabriel Charetteef6cbc22019-08-02 06:24:5962
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 Charetteef6cbc22019-08-02 06:24:5967
Peter Boström7319bbd2021-09-15 22:59:3868 ScopedMockElapsedTimersForTest(const ScopedMockElapsedTimersForTest&) =
69 delete;
70 ScopedMockElapsedTimersForTest& operator=(
71 const ScopedMockElapsedTimersForTest&) = delete;
72
73 ~ScopedMockElapsedTimersForTest();
Gabriel Charetteef6cbc22019-08-02 06:24:5974};
75
[email protected]bd5a3742013-09-29 18:06:1376} // namespace base
77
78#endif // BASE_TIMER_ELAPSED_TIMER_H_