unittest: Remove dependency on absl::GetCurrentTimeNanos()

Signed-off-by: Stefan Weil <sw@weilnetz.de>
This commit is contained in:
Stefan Weil 2021-08-05 10:30:57 +02:00
parent 6b8b1f0007
commit fe5ca9dad9
2 changed files with 10 additions and 13 deletions

View File

@ -1173,13 +1173,11 @@ libgtest_main_la_CPPFLAGS = $(libgtest_la_CPPFLAGS)
# Build Abseil (needed for some unit tests).
check_LTLIBRARIES += libabseil.la
libabseil_la_SOURCES =
libabseil_la_SOURCES += abseil/absl/base/internal/cycleclock.cc
libabseil_la_SOURCES += abseil/absl/base/internal/raw_logging.cc
libabseil_la_SOURCES += abseil/absl/base/internal/spinlock.cc
libabseil_la_SOURCES += abseil/absl/base/internal/spinlock_wait.cc
libabseil_la_SOURCES += abseil/absl/base/internal/sysinfo.cc
libabseil_la_SOURCES += abseil/absl/base/internal/throw_delegate.cc
libabseil_la_SOURCES += abseil/absl/base/internal/unscaledcycleclock.cc
libabseil_la_SOURCES += abseil/absl/numeric/int128.cc
libabseil_la_SOURCES += abseil/absl/strings/ascii.cc
libabseil_la_SOURCES += abseil/absl/strings/charconv.cc
@ -1197,8 +1195,6 @@ libabseil_la_SOURCES += abseil/absl/strings/numbers.cc
libabseil_la_SOURCES += abseil/absl/strings/str_cat.cc
libabseil_la_SOURCES += abseil/absl/strings/str_split.cc
libabseil_la_SOURCES += abseil/absl/strings/string_view.cc
libabseil_la_SOURCES += abseil/absl/time/clock.cc
libabseil_la_SOURCES += abseil/absl/time/duration.cc
libabseil_la_CPPFLAGS = -I$(top_srcdir)/abseil
GMOCK_INCLUDES = -I$(top_srcdir)/googletest/googlemock/include \

View File

@ -13,10 +13,16 @@
#ifndef TESSERACT_UNITTEST_CYCLETIMER_H
#define TESSERACT_UNITTEST_CYCLETIMER_H
#include "absl/time/clock.h" // for GetCurrentTimeNanos
#include <chrono> // for std::chrono
// See https://github.com/google/or-tools/blob/master/ortools/base/timer.h
class CycleTimer {
private:
static int64_t now() {
return std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now().time_since_epoch()).count();
}
public:
CycleTimer() {
Reset();
@ -31,7 +37,7 @@ public:
// When Start() is called multiple times, only the most recent is used.
void Start() {
running_ = true;
start_ = absl::GetCurrentTimeNanos();
start_ = now();
}
void Restart() {
@ -41,17 +47,12 @@ public:
void Stop() {
if (running_) {
sum_ += absl::GetCurrentTimeNanos() - start_;
sum_ += now() - start_;
running_ = false;
}
}
int64_t GetInMs() const {
return GetNanos() / 1000000;
}
protected:
int64_t GetNanos() const {
return running_ ? absl::GetCurrentTimeNanos() - start_ + sum_ : sum_;
return running_ ? now() - start_ + sum_ : sum_;
}
private: