Updated metric Distribution and Timer utils.

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

Renamed TimerMetric to Timer.  Timer is used to generate durations
included in metrics, but is not a metric itself.  The method of
getting the current time did not require creating an instance of
std::steady_clock.

Updated Distribution and Timer to use default initializers instead of
constructor initialization list.

Bug: 204946540
Test: Metric unit tests
Change-Id: I7ed291b586347dd0b7ab305960883bec04637315
This commit is contained in:
Alex Dale
2021-11-04 18:33:51 -07:00
parent 1b95db51f1
commit f5759c5149
7 changed files with 33 additions and 40 deletions

View File

@@ -266,8 +266,8 @@ class CdmSession {
// instance variables // instance variables
std::shared_ptr<metrics::SessionMetrics> metrics_; std::shared_ptr<metrics::SessionMetrics> metrics_;
metrics::CryptoMetrics* crypto_metrics_; metrics::CryptoMetrics* crypto_metrics_;
metrics::TimerMetric life_span_; metrics::Timer life_span_;
metrics::TimerMetric license_request_latency_; metrics::Timer license_request_latency_;
CdmKeyRequestType key_request_type_; CdmKeyRequestType key_request_type_;
bool initialized_; bool initialized_;

View File

@@ -479,7 +479,7 @@ class CryptoSession {
}; };
metrics::CryptoMetrics* metrics_; metrics::CryptoMetrics* metrics_;
metrics::TimerMetric life_span_; metrics::Timer life_span_;
uint32_t system_id_; uint32_t system_id_;
bool open_; bool open_;

View File

@@ -4,12 +4,12 @@
// //
// This file contains the definition of a Distribution class which computes // This file contains the definition of a Distribution class which computes
// the distribution values of a series of samples. // the distribution values of a series of samples.
#ifndef WVCDM_METRICS_DISTRIBUTION_H_ #ifndef WVCDM_METRICS_DISTRIBUTION_H_
#define WVCDM_METRICS_DISTRIBUTION_H_ #define WVCDM_METRICS_DISTRIBUTION_H_
#include <stdint.h> #include <stdint.h>
#include <limits>
namespace wvcdm { namespace wvcdm {
namespace metrics { namespace metrics {
// The Distribution class holds statistics about a series of values that the // The Distribution class holds statistics about a series of values that the
@@ -25,7 +25,7 @@ namespace metrics {
// dist.Count(); // Returns 2. // dist.Count(); // Returns 2.
class Distribution { class Distribution {
public: public:
Distribution(); Distribution() {}
// Uses the provided sample value to update the computed statistics. // Uses the provided sample value to update the computed statistics.
void Record(float value); void Record(float value);
@@ -41,11 +41,11 @@ class Distribution {
} }
private: private:
uint64_t count_; uint64_t count_ = 0;
float min_; float min_ = std::numeric_limits<float>::max();
float max_; float max_ = std::numeric_limits<float>::lowest();
float mean_; float mean_ = 0.0f;
double sum_squared_deviation_; double sum_squared_deviation_ = 0.0;
}; };
} // namespace metrics } // namespace metrics
} // namespace wvcdm } // namespace wvcdm

View File

@@ -51,7 +51,7 @@
// sts); // sts);
#define M_TIME(CALL, GROUP, METRIC, ...) \ #define M_TIME(CALL, GROUP, METRIC, ...) \
if (GROUP) { \ if (GROUP) { \
wvcdm::metrics::TimerMetric timer; \ wvcdm::metrics::Timer timer; \
timer.Start(); \ timer.Start(); \
CALL; \ CALL; \
(GROUP)->METRIC.Record(timer.AsUs(), ##__VA_ARGS__); \ (GROUP)->METRIC.Record(timer.AsUs(), ##__VA_ARGS__); \
@@ -488,7 +488,7 @@ class EngineMetrics {
std::vector<std::shared_ptr<metrics::SessionMetrics>> std::vector<std::shared_ptr<metrics::SessionMetrics>>
completed_session_metrics_list_; completed_session_metrics_list_;
// This is used to populate the engine lifespan metric // This is used to populate the engine lifespan metric
metrics::TimerMetric life_span_internal_; metrics::Timer life_span_internal_;
CryptoMetrics crypto_metrics_; CryptoMetrics crypto_metrics_;
std::string app_package_name_; std::string app_package_name_;

View File

@@ -7,10 +7,9 @@
namespace wvcdm { namespace wvcdm {
namespace metrics { namespace metrics {
class TimerMetric { class Timer {
public: public:
// Constructs a new TimerMetric. Timer() {}
explicit TimerMetric() : is_started_(false) {}
// Starts the clock running. If the clock was previously set, this resets it. // Starts the clock running. If the clock was previously set, this resets it.
// IsStarted will return true after this call. // IsStarted will return true after this call.
void Start(); void Start();
@@ -25,9 +24,8 @@ class TimerMetric {
double AsUs() const; double AsUs() const;
private: private:
std::chrono::steady_clock clock_;
std::chrono::time_point<std::chrono::steady_clock> start_; std::chrono::time_point<std::chrono::steady_clock> start_;
bool is_started_; bool is_started_ = false;
}; };
} // namespace metrics } // namespace metrics
} // namespace wvcdm } // namespace wvcdm

View File

@@ -3,27 +3,15 @@
// Agreement. // Agreement.
// //
// This file contains the definitions for the Distribution class members. // This file contains the definitions for the Distribution class members.
#include "distribution.h" #include "distribution.h"
#include <float.h>
namespace wvcdm { namespace wvcdm {
namespace metrics { namespace metrics {
Distribution::Distribution()
: count_(0ULL),
min_(FLT_MAX),
max_(-FLT_MAX),
mean_(0.0),
sum_squared_deviation_(0.0) {}
void Distribution::Record(float value) { void Distribution::Record(float value) {
// Using method of provisional means. // Using method of provisional means.
float deviation = value - mean_; const float deviation = value - mean_;
mean_ = mean_ + (deviation / ++count_); mean_ += (deviation / ++count_);
sum_squared_deviation_ = sum_squared_deviation_ += (deviation * (value - mean_));
sum_squared_deviation_ + (deviation * (value - mean_));
min_ = min_ < value ? min_ : value; min_ = min_ < value ? min_ : value;
max_ = max_ > value ? max_ : value; max_ = max_ > value ? max_ : value;
} }

View File

@@ -5,19 +5,26 @@
namespace wvcdm { namespace wvcdm {
namespace metrics { namespace metrics {
void TimerMetric::Start() { using ClockType = std::chrono::steady_clock;
start_ = clock_.now(); using TimePoint = std::chrono::time_point<ClockType>;
void Timer::Start() {
start_ = ClockType::now();
is_started_ = true; is_started_ = true;
} }
void TimerMetric::Clear() { is_started_ = false; } void Timer::Clear() { is_started_ = false; }
double TimerMetric::AsMs() const { double Timer::AsMs() const {
return (clock_.now() - start_) / std::chrono::milliseconds(1); if (!is_started_) return 0.0;
const TimePoint end = ClockType::now();
return (end - start_) / std::chrono::milliseconds(1);
} }
double TimerMetric::AsUs() const { double Timer::AsUs() const {
return (clock_.now() - start_) / std::chrono::microseconds(1); if (!is_started_) return 0.0;
const TimePoint end = ClockType::now();
return (end - start_) / std::chrono::microseconds(1);
} }
} // namespace metrics } // namespace metrics
} // namespace wvcdm } // namespace wvcdm