aboutsummaryrefslogtreecommitdiff
path: root/icing/util/clock.h
diff options
context:
space:
mode:
authorAlexander Dorokhine <adorokhine@google.com>2020-11-18 16:39:25 -0800
committerAlexander Dorokhine <adorokhine@google.com>2020-11-18 16:39:25 -0800
commit1ae91ee03a08a4f0391f0f90e2797fb3c2e02960 (patch)
treeab1344e2dc53bdbd4c9c5eb970def0d49a9ebdc3 /icing/util/clock.h
parente111bb917a63282b8e11012acb8f97cba882f342 (diff)
downloadicing-1ae91ee03a08a4f0391f0f90e2797fb3c2e02960.tar.gz
Update Icing from upstream.
Change-Id: Ic022a44e876a6060a47e0db991e63b2b73807769
Diffstat (limited to 'icing/util/clock.h')
-rw-r--r--icing/util/clock.h44
1 files changed, 33 insertions, 11 deletions
diff --git a/icing/util/clock.h b/icing/util/clock.h
index a37fe58..06f1c9d 100644
--- a/icing/util/clock.h
+++ b/icing/util/clock.h
@@ -16,21 +16,11 @@
#define ICING_UTIL_CLOCK_H_
#include <cstdint>
+#include <memory>
namespace icing {
namespace lib {
-// Wrapper around real-time clock functions. This is separated primarily so
-// tests can override this clock and inject it into the class under test.
-class Clock {
- public:
- virtual ~Clock() = default;
-
- // Returns the current time in milliseconds, it's guaranteed that the return
- // value is non-negative.
- virtual int64_t GetSystemTimeMilliseconds() const;
-};
-
// Returns the current steady time in nanoseconds. The steady clock is different
// from the system clock. It's monotonic and never returns a lower value than a
// previous call, while a system clock can be occasionally adjusted.
@@ -42,6 +32,38 @@ int64_t GetSteadyTimeNanoseconds();
// adjusted.
int64_t GetSteadyTimeMilliseconds();
+// Used to calculate the elapsed time.
+class Timer {
+ public:
+ // Creates and starts the timer.
+ Timer() : start_timestamp_milliseconds_(GetSteadyTimeMilliseconds()) {}
+
+ virtual ~Timer() = default;
+
+ // Returns the elapsed time from when timer started.
+ virtual int64_t GetElapsedMilliseconds() {
+ return GetSteadyTimeMilliseconds() - start_timestamp_milliseconds_;
+ }
+
+ private:
+ int64_t start_timestamp_milliseconds_;
+};
+
+// Wrapper around real-time clock functions. This is separated primarily so
+// tests can override this clock and inject it into the class under test.
+class Clock {
+ public:
+ virtual ~Clock() = default;
+
+ // Returns the current time in milliseconds, it's guaranteed that the return
+ // value is non-negative.
+ virtual int64_t GetSystemTimeMilliseconds() const;
+
+ // Returns a timer used to calculate the elapsed time. The timer starts when
+ // the method returns.
+ virtual std::unique_ptr<Timer> GetNewTimer() const;
+};
+
} // namespace lib
} // namespace icing