Use the std::chrono to get the time.

[ Merge of http://go/wvgerrit/67985 ]

Now that we can use C++11, we should use the cross-platform types for
clocks instead of the platform-specific versions.

Test: WV unit/integration tests.
Change-Id: I50318e3d1caf9e814f33f497f83c19c9f3c154a1
This commit is contained in:
Rahul Frias
2018-12-13 12:09:04 -08:00
parent 3c350b677f
commit 25d29fd22b
5 changed files with 35 additions and 76 deletions

View File

@@ -1,7 +1,7 @@
#ifndef WVCDM_METRICS_TIMER_METRIC_H_
#define WVCDM_METRICS_TIMER_METRIC_H_
#include <stdint.h>
#include <chrono>
namespace wvcdm {
namespace metrics {
@@ -23,8 +23,8 @@ class TimerMetric {
double AsUs() const;
private:
double sec_;
double usec_;
std::chrono::steady_clock clock_;
std::chrono::time_point<std::chrono::steady_clock> start_;
bool is_started_;
};

View File

@@ -1,39 +1,23 @@
#include "timer_metric.h"
#include <stddef.h>
#include <sys/time.h>
namespace wvcdm {
namespace metrics {
void TimerMetric::Start() {
struct timeval tv;
gettimeofday(&tv, NULL);
sec_ = tv.tv_sec;
usec_ = tv.tv_usec;
start_ = clock_.now();
is_started_ = true;
}
void TimerMetric::Clear() {
is_started_ = false;
sec_ = 0;
usec_ = 0;
}
double TimerMetric::AsMs() const {
struct timeval tv;
gettimeofday(&tv, NULL);
return usec_ > tv.tv_usec ?
(tv.tv_sec - sec_ - 1) * 1000.0 + (tv.tv_usec - usec_ + 1000000.0) / 1000.0 :
(tv.tv_sec - sec_) * 1000.0 + (tv.tv_usec - usec_) / 1000.0;
return (clock_.now() - start_) / std::chrono::milliseconds(1);
}
double TimerMetric::AsUs() const {
struct timeval tv;
gettimeofday(&tv, NULL);
return usec_ > tv.tv_usec ?
(tv.tv_sec - sec_ - 1) * 1000000.0 + (tv.tv_usec - usec_ + 1000000.0) :
(tv.tv_sec - sec_) * 1000000.0 + (tv.tv_usec - usec_);
return (clock_.now() - start_) / std::chrono::microseconds(1);
}
} // namespace metrics