Source release v3.5.0
This commit is contained in:
205
metrics/include/counter_metric.h
Normal file
205
metrics/include/counter_metric.h
Normal file
@@ -0,0 +1,205 @@
|
||||
// Copyright 2017 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// This file contains the declarations for the Metric class and related
|
||||
// types.
|
||||
#ifndef WVCDM_METRICS_COUNTER_METRIC_H_
|
||||
#define WVCDM_METRICS_COUNTER_METRIC_H_
|
||||
|
||||
#include <cstdarg>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "field_tuples.h"
|
||||
#include "lock.h"
|
||||
#include "metric_serialization.h"
|
||||
|
||||
namespace wvcdm {
|
||||
namespace metrics {
|
||||
|
||||
class CounterMetricTest;
|
||||
|
||||
// This base class provides the common defintion used by all templated
|
||||
// instances of CounterMetric.
|
||||
class BaseCounterMetric : public MetricSerializable {
|
||||
public:
|
||||
// Send metric values to the MetricSerializer. |serializer| must
|
||||
// not be null and is owned by the caller.
|
||||
virtual void Serialize(MetricSerializer* serializer);
|
||||
|
||||
protected:
|
||||
// Instantiates a BaseCounterMetric.
|
||||
BaseCounterMetric(const std::string& metric_name)
|
||||
: metric_name_(metric_name) {}
|
||||
|
||||
virtual ~BaseCounterMetric() {}
|
||||
|
||||
// Increment will look for an existing instance of the field names and
|
||||
// add the new value to the existing value. If the instance does not exist,
|
||||
// this method will create it.
|
||||
void Increment(const std::string& field_names_values, int64_t value);
|
||||
|
||||
private:
|
||||
friend class CounterMetricTest;
|
||||
const std::string metric_name_;
|
||||
// value_map_ contains a mapping from the field name/value pairs to the
|
||||
// counter(int64_t) instance.
|
||||
std::map<std::string, int64_t> value_map_;
|
||||
|
||||
/*
|
||||
* This locks the internal state of the counter metric to ensure safety across
|
||||
* multiple threads preventing the caller from worrying about locking.
|
||||
*/
|
||||
Lock internal_lock_;
|
||||
};
|
||||
|
||||
// The CounterMetric class is used to track a counter such as the number of
|
||||
// times a method is called. It can also track a delta that could be positive
|
||||
// or negative.
|
||||
//
|
||||
// The CounterMetric class supports the ability to keep track of multiple
|
||||
// variations of the count based on certain "field" values. E.g. keep track of
|
||||
// the counts of success and failure counts for a method call. Or keep track of
|
||||
// number of times the method was called with a particular parameter.
|
||||
// Fields define what variations to track. Each Field is a separate dimension.
|
||||
// The count for each combination of field values is tracked independently.
|
||||
//
|
||||
// Example usage:
|
||||
//
|
||||
// CounterMetric<int, int> my_metric("multiple/fields/metric",
|
||||
// "request_type", // Field name.
|
||||
// "error_code"); // Field name.
|
||||
//
|
||||
// my_metric.Increment(1, 7, 23); // (counter value, request type, error code).
|
||||
//
|
||||
// The CounterMetric supports serialization. A call to Serialize will serialize
|
||||
// all values to the provided MetricsSerializer instance.
|
||||
//
|
||||
// example:
|
||||
//
|
||||
// class MyMetricSerializer : public MetricSerializer {
|
||||
// // Add implementation here.
|
||||
// }
|
||||
//
|
||||
// MyMetricSerializer serializer;
|
||||
// my_metric.Serialize(&serializer);
|
||||
template<typename F1=util::Unused,
|
||||
typename F2=util::Unused,
|
||||
typename F3=util::Unused,
|
||||
typename F4=util::Unused>
|
||||
class CounterMetric : public BaseCounterMetric {
|
||||
public:
|
||||
// Create a CounterMetric instance with the name |metric_name|.
|
||||
CounterMetric(const std::string& metric_name);
|
||||
|
||||
// Overloaded constructors with variable field name arguments. The number
|
||||
// of |field_name| arguments must match the number of used Field type
|
||||
// arguments.
|
||||
CounterMetric(const std::string& metric_name,
|
||||
const char* field_name);
|
||||
CounterMetric(const std::string& metric_name,
|
||||
const char* field_name1,
|
||||
const char* field_name2);
|
||||
CounterMetric(const std::string& metric_name,
|
||||
const char* field_name1,
|
||||
const char* field_name2,
|
||||
const char* field_name3);
|
||||
CounterMetric(const std::string& metric_name,
|
||||
const char* field_name1,
|
||||
const char* field_name2,
|
||||
const char* field_name3,
|
||||
const char* field_name4);
|
||||
|
||||
// Increment will update the counter value associated with the provided
|
||||
// field values.
|
||||
void Increment(F1 field1 = util::Unused(), F2 field2 = util::Unused(),
|
||||
F3 field3 = util::Unused(), F4 field4 = util::Unused());
|
||||
|
||||
// Increment will add the value to the counter associated with the provided
|
||||
// field values.
|
||||
void Increment(int64_t value,
|
||||
F1 field1 = util::Unused(), F2 field2 = util::Unused(),
|
||||
F3 field3 = util::Unused(), F4 field4 = util::Unused());
|
||||
|
||||
private:
|
||||
friend class CounterMetricTest;
|
||||
std::vector<std::string> field_names_;
|
||||
};
|
||||
|
||||
// Overloaded template constructor implementations for CounterMetric.
|
||||
template<typename F1, typename F2, typename F3, typename F4>
|
||||
CounterMetric<F1, F2, F3, F4>::CounterMetric(
|
||||
const std::string& metric_name)
|
||||
: BaseCounterMetric(metric_name) {
|
||||
ASSERT_METRIC_UNUSED_START_FROM(1);
|
||||
}
|
||||
|
||||
template<typename F1, typename F2, typename F3, typename F4>
|
||||
CounterMetric<F1, F2, F3, F4>::CounterMetric(
|
||||
const std::string& metric_name,
|
||||
const char* field_name)
|
||||
: BaseCounterMetric(metric_name) {
|
||||
ASSERT_METRIC_UNUSED_START_FROM(2);
|
||||
util::AppendFieldNames(&field_names_,
|
||||
util::FirstUnusedType<F1, F2, F3, F4>::value - 1,
|
||||
field_name);
|
||||
}
|
||||
template<typename F1, typename F2, typename F3, typename F4>
|
||||
CounterMetric<F1, F2, F3, F4>::CounterMetric(
|
||||
const std::string& metric_name,
|
||||
const char* field_name1,
|
||||
const char* field_name2)
|
||||
: BaseCounterMetric(metric_name) {
|
||||
ASSERT_METRIC_UNUSED_START_FROM(3);
|
||||
util::AppendFieldNames(&field_names_,
|
||||
util::FirstUnusedType<F1, F2, F3, F4>::value - 1,
|
||||
field_name1, field_name2);
|
||||
}
|
||||
template<typename F1, typename F2, typename F3, typename F4>
|
||||
CounterMetric<F1, F2, F3, F4>::CounterMetric(
|
||||
const std::string& metric_name,
|
||||
const char* field_name1,
|
||||
const char* field_name2,
|
||||
const char* field_name3)
|
||||
: BaseCounterMetric(metric_name) {
|
||||
ASSERT_METRIC_UNUSED_START_FROM(4);
|
||||
util::AppendFieldNames(&field_names_,
|
||||
util::FirstUnusedType<F1, F2, F3, F4>::value - 1,
|
||||
field_name1, field_name2, field_name3);
|
||||
}
|
||||
template<typename F1, typename F2, typename F3, typename F4>
|
||||
CounterMetric<F1, F2, F3, F4>::CounterMetric(
|
||||
const std::string& metric_name,
|
||||
const char* field_name1,
|
||||
const char* field_name2,
|
||||
const char* field_name3,
|
||||
const char* field_name4)
|
||||
: BaseCounterMetric(metric_name) {
|
||||
ASSERT_METRIC_UNUSED_START_FROM(5);
|
||||
util::AppendFieldNames(&field_names_,
|
||||
util::FirstUnusedType<F1, F2, F3, F4>::value - 1,
|
||||
field_name1, field_name2,
|
||||
field_name3, field_name4);
|
||||
}
|
||||
|
||||
template<typename F1, typename F2, typename F3, typename F4>
|
||||
void CounterMetric<F1, F2, F3, F4>::Increment(
|
||||
F1 field1, F2 field2, F3 field3, F4 field4) {
|
||||
std::string field_name_values =
|
||||
util::MakeFieldNameString(field_names_, field1, field2, field3, field4);
|
||||
BaseCounterMetric::Increment(field_name_values, 1);
|
||||
}
|
||||
|
||||
template<typename F1, typename F2, typename F3, typename F4>
|
||||
void CounterMetric<F1, F2, F3, F4>::Increment(
|
||||
int64_t value, F1 field1, F2 field2, F3 field3, F4 field4) {
|
||||
std::string field_name_values =
|
||||
util::MakeFieldNameString(field_names_, field1, field2, field3, field4);
|
||||
BaseCounterMetric::Increment(field_name_values, value);
|
||||
}
|
||||
|
||||
} // namespace metrics
|
||||
} // namespace wvcdm
|
||||
|
||||
#endif // WVCDM_METRICS_COUNTER_METRIC_H_
|
||||
53
metrics/include/distribution.h
Normal file
53
metrics/include/distribution.h
Normal file
@@ -0,0 +1,53 @@
|
||||
// Copyright 2017 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// This file contains the definition of a Distribution class which computes
|
||||
// the distribution values of a series of samples.
|
||||
|
||||
#ifndef WVCDM_METRICS_DISTRIBUTION_H_
|
||||
#define WVCDM_METRICS_DISTRIBUTION_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
namespace wvcdm {
|
||||
namespace metrics {
|
||||
|
||||
// The Distribution class holds statistics about a series of values that the
|
||||
// client provides via the Record method. A caller will call Record once for
|
||||
// each of the values in a series. The Distribution instance will calculate the
|
||||
// mean, count, min, max and variance of the distribution.
|
||||
//
|
||||
// Example usage:
|
||||
// Distribution dist;
|
||||
// dist.Record(1);
|
||||
// dist.Record(2);
|
||||
// dist.Mean(); // Returns 1.5.
|
||||
// dist.Count(); // Returns 2.
|
||||
class Distribution {
|
||||
public:
|
||||
Distribution();
|
||||
|
||||
// Uses the provided sample value to update the computed statistics.
|
||||
void Record(double value);
|
||||
|
||||
// Return the value for each of the stats computed about the series of
|
||||
// values (min, max, count, etc.).
|
||||
double Min() const { return min_; }
|
||||
double Max() const { return max_; }
|
||||
double Mean() const { return mean_; }
|
||||
int64_t Count() const { return count_; }
|
||||
double Variance() const {
|
||||
return count_ == 0 ? 0.0 : sum_squared_deviation_ / count_;
|
||||
}
|
||||
|
||||
private:
|
||||
int64_t count_;
|
||||
double min_;
|
||||
double max_;
|
||||
double mean_;
|
||||
double sum_squared_deviation_;
|
||||
};
|
||||
|
||||
} // namespace metrics
|
||||
} // namespace wvcdm
|
||||
|
||||
#endif // WVCDM_METRICS_DISTRIBUTION_H_
|
||||
232
metrics/include/event_metric.h
Normal file
232
metrics/include/event_metric.h
Normal file
@@ -0,0 +1,232 @@
|
||||
// Copyright 2017 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// This file contains the declarations for the EventMetric class and related
|
||||
// types.
|
||||
#ifndef WVCDM_METRICS_EVENT_METRIC_H_
|
||||
#define WVCDM_METRICS_EVENT_METRIC_H_
|
||||
|
||||
#include <cstdarg>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "distribution.h"
|
||||
#include "field_tuples.h"
|
||||
#include "lock.h"
|
||||
#include "metric_serialization.h"
|
||||
|
||||
namespace wvcdm {
|
||||
namespace metrics {
|
||||
|
||||
class EventMetricTest;
|
||||
|
||||
// This base class provides the common defintion used by all templated
|
||||
// instances of EventMetric.
|
||||
class BaseEventMetric : public MetricSerializable {
|
||||
public:
|
||||
// Send metric values to the MetricSerializer. |serializer| must
|
||||
// not be null and is owned by the caller.
|
||||
virtual void Serialize(MetricSerializer* serializer);
|
||||
|
||||
protected:
|
||||
// Instantiates a BaseEventMetric.
|
||||
BaseEventMetric(const std::string& metric_name)
|
||||
: metric_name_(metric_name) {}
|
||||
|
||||
virtual ~BaseEventMetric();
|
||||
|
||||
// Record will look for an existing instance of the Distribution identified
|
||||
// by the field_names_values string and update it. If the instance does
|
||||
// not exist, this will create it.
|
||||
void Record(const std::string& field_names_values, double value);
|
||||
|
||||
private:
|
||||
friend class EventMetricTest;
|
||||
const std::string metric_name_;
|
||||
// value_map_ contains a mapping from the field name/value pairs to the
|
||||
// distribution instance which holds the metric information (min, max, sum,
|
||||
// etc.).
|
||||
std::map<std::string, Distribution*> value_map_;
|
||||
|
||||
/*
|
||||
* This locks the internal state of the event metric to ensure safety across
|
||||
* multiple threads preventing the caller from worrying about locking.
|
||||
*/
|
||||
Lock internal_lock_;
|
||||
};
|
||||
|
||||
// This class converts the size_t value into the highest power of two
|
||||
// below the value. E.g. for 7, the value is 4. For 11, the value is 8.
|
||||
// This class is intended to simplify the use of EventMetric Fields that may
|
||||
// have many possible values, but we want to bucket them into a small set of
|
||||
// numbers (32 or 64).
|
||||
class Pow2Bucket {
|
||||
public:
|
||||
explicit Pow2Bucket(size_t value) : value_(GetLowerBucket(value)) {}
|
||||
|
||||
Pow2Bucket(const Pow2Bucket& value) : value_(value.value_) {}
|
||||
|
||||
// Support for converting to string.
|
||||
friend std::ostream& operator<<(std::ostream& os, const Pow2Bucket& log)
|
||||
{ return os << log.value_; }
|
||||
|
||||
private:
|
||||
inline size_t GetLowerBucket(size_t value) {
|
||||
if (!value) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t log = 0;
|
||||
while (value) {
|
||||
log++;
|
||||
value >>= 1;
|
||||
}
|
||||
|
||||
return 1u << (log - 1);
|
||||
}
|
||||
|
||||
size_t value_;
|
||||
};
|
||||
|
||||
// The EventMetric class is used to capture statistics about an event such as
|
||||
// a method call. EventMetric keeps track of the count, mean, min, max and
|
||||
// variance for the value being measured. For example, we may want to track the
|
||||
// latency of a particular operation. Each time the operation is run, the time
|
||||
// is calculated and provided to the EventMetric by using the
|
||||
// EventMetric::Record method to capture and maintain statistics about the
|
||||
// latency (max, min, count, mean, variance).
|
||||
//
|
||||
// The EventMetric class supports the ability to breakdown the statistics based
|
||||
// on certain "field" values. For example, if a particular operation can run in
|
||||
// one of two modes, it's useful to track the latency of the operation in each
|
||||
// mode separately. You can use Fields to define how to breakdown the
|
||||
// statistics. Each Field is a separate dimension. The statistics for each
|
||||
// combination of field values are tracked independently.
|
||||
//
|
||||
// Example usage:
|
||||
//
|
||||
// EventMetric<int, int> my_metric("multiple/fields/metric",
|
||||
// "request_type", "error_code", // Field names);
|
||||
//
|
||||
// my_metric.Record(1, 7, 23); // (latency value, request type, error code).
|
||||
//
|
||||
// The EventMetric supports serialization. A call to Serialize will
|
||||
// serialize all values to the provided MetricsSerializer instance.
|
||||
//
|
||||
// example:
|
||||
//
|
||||
// class MyMetricSerializer : public MetricSerializer {
|
||||
// // Add implementation here.
|
||||
// }
|
||||
//
|
||||
// MyMetricSerializer serializer;
|
||||
// my_metric.Serialize(&serializer);
|
||||
//
|
||||
template<typename F1=util::Unused,
|
||||
typename F2=util::Unused,
|
||||
typename F3=util::Unused,
|
||||
typename F4=util::Unused>
|
||||
class EventMetric : public BaseEventMetric {
|
||||
public:
|
||||
// Create an EventMetric instance with the name |metric_name|.
|
||||
EventMetric(const std::string& metric_name);
|
||||
|
||||
// Overloaded constructors with variable field name arguments. The number
|
||||
// of |field_name| arguments must match the number of used Field type
|
||||
// arguments.
|
||||
EventMetric(const std::string& metric_name,
|
||||
const char* field_name);
|
||||
EventMetric(const std::string& metric_name,
|
||||
const char* field_name1,
|
||||
const char* field_name2);
|
||||
EventMetric(const std::string& metric_name,
|
||||
const char* field_name1,
|
||||
const char* field_name2,
|
||||
const char* field_name3);
|
||||
EventMetric(const std::string& metric_name,
|
||||
const char* field_name1,
|
||||
const char* field_name2,
|
||||
const char* field_name3,
|
||||
const char* field_name4);
|
||||
|
||||
// Record will update the statistics of the EventMetric broken down by the
|
||||
// given field values.
|
||||
void Record(double value,
|
||||
F1 field1 = util::Unused(),
|
||||
F2 field2 = util::Unused(),
|
||||
F3 field3 = util::Unused(),
|
||||
F4 field4 = util::Unused());
|
||||
|
||||
private:
|
||||
friend class EventMetricTest;
|
||||
std::vector<std::string> field_names_;
|
||||
};
|
||||
|
||||
// Overloaded template constructor implementations for EventMetric.
|
||||
template<typename F1, typename F2, typename F3, typename F4>
|
||||
EventMetric<F1, F2, F3, F4>::EventMetric(
|
||||
const std::string& metric_name)
|
||||
: BaseEventMetric(metric_name) {
|
||||
ASSERT_METRIC_UNUSED_START_FROM(1);
|
||||
}
|
||||
|
||||
template<typename F1, typename F2, typename F3, typename F4>
|
||||
EventMetric<F1, F2, F3, F4>::EventMetric(
|
||||
const std::string& metric_name,
|
||||
const char* field_name)
|
||||
: BaseEventMetric(metric_name) {
|
||||
ASSERT_METRIC_UNUSED_START_FROM(2);
|
||||
util::AppendFieldNames(&field_names_,
|
||||
util::FirstUnusedType<F1, F2, F3, F4>::value - 1,
|
||||
field_name);
|
||||
}
|
||||
template<typename F1, typename F2, typename F3, typename F4>
|
||||
EventMetric<F1, F2, F3, F4>::EventMetric(
|
||||
const std::string& metric_name,
|
||||
const char* field_name1,
|
||||
const char* field_name2)
|
||||
: BaseEventMetric(metric_name) {
|
||||
ASSERT_METRIC_UNUSED_START_FROM(3);
|
||||
util::AppendFieldNames(&field_names_,
|
||||
util::FirstUnusedType<F1, F2, F3, F4>::value - 1,
|
||||
field_name1, field_name2);
|
||||
}
|
||||
template<typename F1, typename F2, typename F3, typename F4>
|
||||
EventMetric<F1, F2, F3, F4>::EventMetric(
|
||||
const std::string& metric_name,
|
||||
const char* field_name1,
|
||||
const char* field_name2,
|
||||
const char* field_name3)
|
||||
: BaseEventMetric(metric_name) {
|
||||
ASSERT_METRIC_UNUSED_START_FROM(4);
|
||||
util::AppendFieldNames(&field_names_,
|
||||
util::FirstUnusedType<F1, F2, F3, F4>::value - 1,
|
||||
field_name1, field_name2, field_name3);
|
||||
}
|
||||
template<typename F1, typename F2, typename F3, typename F4>
|
||||
EventMetric<F1, F2, F3, F4>::EventMetric(
|
||||
const std::string& metric_name,
|
||||
const char* field_name1,
|
||||
const char* field_name2,
|
||||
const char* field_name3,
|
||||
const char* field_name4)
|
||||
: BaseEventMetric(metric_name) {
|
||||
ASSERT_METRIC_UNUSED_START_FROM(5);
|
||||
util::AppendFieldNames(&field_names_,
|
||||
util::FirstUnusedType<F1, F2, F3, F4>::value - 1,
|
||||
field_name1, field_name2,
|
||||
field_name3, field_name4);
|
||||
}
|
||||
|
||||
template<typename F1, typename F2, typename F3, typename F4>
|
||||
void EventMetric<F1, F2, F3, F4>::Record(
|
||||
double value, F1 field1, F2 field2, F3 field3, F4 field4) {
|
||||
std::string field_name_values =
|
||||
util::MakeFieldNameString(field_names_, field1, field2, field3, field4);
|
||||
BaseEventMetric::Record(field_name_values, value);
|
||||
}
|
||||
|
||||
} // namespace metrics
|
||||
} // namespace wvcdm
|
||||
|
||||
#endif // WVCDM_METRICS_EVENT_METRIC_H_
|
||||
128
metrics/include/field_tuples.h
Normal file
128
metrics/include/field_tuples.h
Normal file
@@ -0,0 +1,128 @@
|
||||
// Copyright 2017 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// This file contains the helper classes and methods for using field tuples
|
||||
// used by metrics classes to record variations of a single metric.
|
||||
#ifndef WVCDM_METRICS_FIELD_TUPLES_H_
|
||||
#define WVCDM_METRICS_FIELD_TUPLES_H_
|
||||
|
||||
#include <cstdarg>
|
||||
#include <memory>
|
||||
#include <ostream>
|
||||
#include <sstream>
|
||||
#include <stdint.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace wvcdm {
|
||||
namespace metrics {
|
||||
namespace util {
|
||||
|
||||
// TODO(blueeyes): Change to use C++ 11 support for variadic template args.
|
||||
// The C++ 03 pattern is no longer needed since we require C++11. b/68766426.
|
||||
|
||||
// This is a placeholder type for unused type parameters. It aids in supporting
|
||||
// templated classes with "variable" type arguments.
|
||||
struct Unused {
|
||||
// Required for compilation. Should never be used.
|
||||
inline friend std::ostream& operator<< (std::ostream& out, const Unused&)
|
||||
{ return out; }
|
||||
};
|
||||
|
||||
// This utility method formats the collection of field name/value pairs.
|
||||
// The format of the string is:
|
||||
//
|
||||
// [{field:value[&field:value]*}]
|
||||
//
|
||||
// If there are no pairs, returns a blank string.
|
||||
template<typename F1, typename F2, typename F3, typename F4>
|
||||
std::string MakeFieldNameString(const std::vector<std::string>& field_names,
|
||||
const F1 field1, const F2 field2,
|
||||
const F3 field3, const F4 field4) {
|
||||
std::stringstream field_name_and_values;
|
||||
std::vector<std::string>::const_iterator field_name_iterator =
|
||||
field_names.begin();
|
||||
if (field_name_iterator == field_names.end()) {
|
||||
return field_name_and_values.str();
|
||||
}
|
||||
// There is at least one name/value pair. Prepend open brace.
|
||||
field_name_and_values << "{";
|
||||
field_name_and_values << *field_name_iterator << ':' << field1;
|
||||
if (++field_name_iterator == field_names.end()) {
|
||||
field_name_and_values << "}";
|
||||
return field_name_and_values.str();
|
||||
}
|
||||
field_name_and_values << '&' << *field_name_iterator << ':' << field2;
|
||||
if (++field_name_iterator == field_names.end()) {
|
||||
field_name_and_values << "}";
|
||||
return field_name_and_values.str();
|
||||
}
|
||||
field_name_and_values << '&' << *field_name_iterator << ':' << field3;
|
||||
if (++field_name_iterator == field_names.end()) {
|
||||
field_name_and_values << "}";
|
||||
return field_name_and_values.str();
|
||||
}
|
||||
field_name_and_values << '&' << *field_name_iterator << ':' << field4;
|
||||
field_name_and_values << "}";
|
||||
return field_name_and_values.str();
|
||||
}
|
||||
|
||||
// This specialization of the helper method is a shortcut for class
|
||||
// instances with no fields.
|
||||
template<>
|
||||
inline std::string MakeFieldNameString<Unused, Unused, Unused, Unused>(
|
||||
const std::vector<std::string>& /* field_names */,
|
||||
const Unused /* unused1 */, const Unused /* unused2 */,
|
||||
const Unused /* unused3 */, const Unused /* unused4 */) {
|
||||
return "";
|
||||
}
|
||||
|
||||
// This helper function appends the field names to a vector of strings.
|
||||
inline void AppendFieldNames(std::vector<std::string>* field_name_vector,
|
||||
int field_count, ...) {
|
||||
va_list field_names;
|
||||
|
||||
va_start(field_names, field_count);
|
||||
for (int x = 0; x < field_count; x++) {
|
||||
field_name_vector->push_back(va_arg(field_names, const char*));
|
||||
}
|
||||
va_end(field_names);
|
||||
}
|
||||
|
||||
// These helper methods and FirstUnusedType assure that there is no mismatch
|
||||
// between the specified types for metrics type parameters and the constructors
|
||||
// and methods used for the specializations.
|
||||
template <bool>
|
||||
struct CompileAssert {};
|
||||
#define COMPILE_ASSERT(expr, msg) \
|
||||
typedef util::CompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1]
|
||||
|
||||
template <typename T> struct is_unused { static const bool value = false; };
|
||||
template <> struct is_unused<Unused> { static const bool value = true; };
|
||||
|
||||
template <typename F1, typename F2, typename F3, typename F4>
|
||||
class FirstUnusedType {
|
||||
static const bool a = is_unused<F1>::value;
|
||||
static const bool b = is_unused<F2>::value;
|
||||
static const bool c = is_unused<F3>::value;
|
||||
static const bool d = is_unused<F4>::value;
|
||||
// Check that all types after the first Unused are also Unused.
|
||||
COMPILE_ASSERT(a <= b, Invalid_Unused_At_Position_2);
|
||||
COMPILE_ASSERT(b <= c, Invalid_Unused_At_Position_3);
|
||||
COMPILE_ASSERT(c <= d, Invalid_Unused_At_Position_4);
|
||||
|
||||
public:
|
||||
static const int value = 5 - (a + b + c + d);
|
||||
};
|
||||
|
||||
// Asserts that no Unused types exist before N; after N, are all Unused types.
|
||||
#define ASSERT_METRIC_UNUSED_START_FROM(N) \
|
||||
COMPILE_ASSERT((\
|
||||
util::FirstUnusedType<F1, F2, F3, F4>::value) == N, \
|
||||
Unused_Start_From_##N)
|
||||
|
||||
} // namespace util
|
||||
} // namespace metrics
|
||||
} // namespace wvcdm
|
||||
|
||||
#endif // WVCDM_METRICS_FIELD_TUPLES_H_
|
||||
|
||||
46
metrics/include/metric_serialization.h
Normal file
46
metrics/include/metric_serialization.h
Normal file
@@ -0,0 +1,46 @@
|
||||
// Copyright 2017 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// This file contains the declarations for the Metric class and related
|
||||
// types.
|
||||
#ifndef WVCDM_METRICS_METRIC_SERIALIZATION_H_
|
||||
#define WVCDM_METRICS_METRIC_SERIALIZATION_H_
|
||||
|
||||
namespace wvcdm {
|
||||
namespace metrics {
|
||||
|
||||
// The MetricSerializer is implemented by the code that instantiates
|
||||
// MetricSerializable instances. The caller provides this MetricSerializer
|
||||
// instance to MetricSerializable::Serialize. In turn, Serialize will make a
|
||||
// call to Set* for each value to be Serialized. The MetricSerialiable
|
||||
// implementation may set zero or more values on the MetricSerializer.
|
||||
class MetricSerializer {
|
||||
public:
|
||||
virtual ~MetricSerializer() {};
|
||||
|
||||
// The metric_id is the metric name, plus the metric part name, and the
|
||||
// metric field name/value string. E.g.
|
||||
// name: "drm/cdm/decrypt/duration"
|
||||
// part: "mean"
|
||||
// field name/value string: "{error_code:0&buffer_size:1024}"
|
||||
// becomes: "drm/cdm/decrypt/duration/mean/{error_code:0&buffer_size:1024}".
|
||||
virtual void SetString(const std::string& metric_id,
|
||||
const std::string& value) = 0;
|
||||
virtual void SetInt32(const std::string& metric_id, int32_t value) = 0;
|
||||
virtual void SetInt64(const std::string& metric_id, int64_t value) = 0;
|
||||
virtual void SetDouble(const std::string& metric_id, double value) = 0;
|
||||
};
|
||||
|
||||
// This abstract class merely provides the definition for serializing the value
|
||||
// of the metric.
|
||||
class MetricSerializable {
|
||||
public:
|
||||
virtual ~MetricSerializable() { }
|
||||
// Serialize metric values to the MetricSerializer. |serializer| must
|
||||
// not be null and is owned by the caller.
|
||||
virtual void Serialize(MetricSerializer* serializer) = 0;
|
||||
};
|
||||
|
||||
} // namespace metrics
|
||||
} // namespace wvcdm
|
||||
|
||||
#endif // WVCDM_METRICS_METRIC_SERIALIZATION_H_
|
||||
301
metrics/include/metrics_collections.h
Normal file
301
metrics/include/metrics_collections.h
Normal file
@@ -0,0 +1,301 @@
|
||||
// Copyright 2016 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// This file contains definitions for metrics being collected throughout the
|
||||
// CDM.
|
||||
|
||||
#ifndef WVCDM_METRICS_METRICS_GROUP_H_
|
||||
#define WVCDM_METRICS_METRICS_GROUP_H_
|
||||
|
||||
#include <ostream>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "counter_metric.h"
|
||||
#include "event_metric.h"
|
||||
#include "metrics.pb.h"
|
||||
#include "OEMCryptoCENC.h"
|
||||
#include "value_metric.h"
|
||||
#include "wv_cdm_types.h"
|
||||
|
||||
// This definition indicates that a given metric does not need timing
|
||||
// stats. Example:
|
||||
//
|
||||
// M_RECORD(my_metrics, my_metric_name, NO_TIME);
|
||||
#define NO_TIME 0
|
||||
|
||||
// Used to record metric timing and additional information about a specific
|
||||
// event. Assumes that a microsecond timing has been provided. Example:
|
||||
//
|
||||
// long total_time = 0;
|
||||
// long error_code = TimeSomeOperation(&total_time);
|
||||
// M_RECORD(my_metrics, my_metric_name, total_time, error_code);
|
||||
#define M_RECORD(GROUP, METRIC, TIME, ...) \
|
||||
if ( GROUP ) { \
|
||||
( GROUP ) -> METRIC . Record( TIME, ##__VA_ARGS__ ); \
|
||||
}
|
||||
|
||||
// This definition automatically times an operation and records the time and
|
||||
// additional information such as error code to the provided metric.
|
||||
// Example:
|
||||
//
|
||||
// OEMCryptoResult sts;
|
||||
// M_TIME(
|
||||
// sts = OEMCrypto_Initialize(),
|
||||
// my_metrics_collection,
|
||||
// oemcrypto_initialize_,
|
||||
// sts);
|
||||
#define M_TIME(CALL, GROUP, METRIC, ...) \
|
||||
if ( GROUP ) { \
|
||||
wvcdm::metrics::TimerMetric timer; \
|
||||
timer.Start(); \
|
||||
CALL; \
|
||||
( GROUP ) -> METRIC . Record(timer.AsUs(), ##__VA_ARGS__ ); \
|
||||
} else { \
|
||||
CALL; \
|
||||
}
|
||||
|
||||
namespace wvcdm {
|
||||
namespace metrics {
|
||||
|
||||
// This enum defines the conditions encountered during OEMCrypto Initialization
|
||||
// in oemcrypto_adapter_dynamic.
|
||||
typedef enum OEMCryptoInitializationMode {
|
||||
OEMCrypto_INITIALIZED_USING_IN_APP = 0,
|
||||
OEMCrypto_INITIALIZED_FORCING_L3 = 1,
|
||||
OEMCrypto_INITIALIZED_USING_L3_NO_L1_LIBRARY_PATH = 2,
|
||||
OEMCrypto_INITIALIZED_USING_L3_L1_OPEN_FAILED = 3,
|
||||
OEMCrypto_INITIALIZED_USING_L3_L1_LOAD_FAILED = 4,
|
||||
OEMCrypto_INITIALIZED_USING_L3_COULD_NOT_INITIALIZE_L1 = 5,
|
||||
OEMCrypto_INITIALIZED_USING_L3_WRONG_L1_VERSION = 6,
|
||||
OEMCrypto_INITIALIZED_USING_L1_WITH_KEYBOX = 7,
|
||||
OEMCrypto_INITIALIZED_USING_L1_WITH_CERTIFICATE = 8,
|
||||
OEMCrypto_INITIALIZED_USING_L1_CERTIFICATE_MIX = 9,
|
||||
OEMCrypto_INITIALIZED_USING_L3_BAD_KEYBOX = 10,
|
||||
OEMCrypto_INITIALIZED_USING_L3_COULD_NOT_OPEN_FACTORY_KEYBOX = 11,
|
||||
OEMCrypto_INITIALIZED_USING_L3_COULD_NOT_INSTALL_KEYBOX = 12,
|
||||
OEMCrypto_INITIALIZED_USING_L1_INSTALLED_KEYBOX = 13,
|
||||
OEMCrypto_INITIALIZED_USING_L3_INVALID_L1 = 14,
|
||||
OEMCrypto_INITIALIZED_USING_L1_WITH_PROVISIONING_3_0 = 15,
|
||||
OEMCrypto_INITIALIZED_L3_INITIALIZATION_FAILED = 16
|
||||
} OEMCryptoInitializationMode;
|
||||
|
||||
|
||||
// This class contains metrics for Crypto Session and OEM Crypto.
|
||||
class CryptoMetrics {
|
||||
public:
|
||||
CryptoMetrics();
|
||||
|
||||
void Serialize(drm_metrics::MetricsGroup* metrics);
|
||||
|
||||
/* CRYPTO SESSION */
|
||||
// TODO(blueeyes): Convert this to crypto_session_default_security_level_.
|
||||
ValueMetric<CdmSecurityLevel> crypto_session_security_level_;
|
||||
CounterMetric<CdmResponseType> crypto_session_delete_all_usage_reports_;
|
||||
CounterMetric<CdmResponseType> crypto_session_delete_multiple_usage_information_;
|
||||
EventMetric<CdmResponseType, Pow2Bucket, CdmEncryptionAlgorithm> crypto_session_generic_decrypt_;
|
||||
EventMetric<CdmResponseType, Pow2Bucket, CdmEncryptionAlgorithm> crypto_session_generic_encrypt_;
|
||||
EventMetric<CdmResponseType, Pow2Bucket, CdmSigningAlgorithm> crypto_session_generic_sign_;
|
||||
EventMetric<CdmResponseType, Pow2Bucket, CdmSigningAlgorithm> crypto_session_generic_verify_;
|
||||
CounterMetric<bool> crypto_session_get_device_unique_id_;
|
||||
CounterMetric<bool> crypto_session_get_token_;
|
||||
ValueMetric<double> crypto_session_life_span_;
|
||||
EventMetric<bool> crypto_session_load_certificate_private_key_;
|
||||
EventMetric<CdmResponseType, SecurityLevel> crypto_session_open_; // This is the requested security level.
|
||||
ValueMetric<uint32_t> crypto_session_system_id_;
|
||||
EventMetric<CdmResponseType> crypto_session_update_usage_information_;
|
||||
ValueMetric<bool> crypto_session_usage_information_support_;
|
||||
/* OEMCRYPTO */
|
||||
ValueMetric<uint32_t> oemcrypto_api_version_;
|
||||
CounterMetric<OEMCryptoResult> oemcrypto_close_session_;
|
||||
EventMetric<OEMCryptoResult, Pow2Bucket> oemcrypto_copy_buffer_;
|
||||
ValueMetric<OEMCrypto_HDCP_Capability> oemcrypto_current_hdcp_capability_;
|
||||
CounterMetric<OEMCryptoResult> oemcrypto_deactivate_usage_entry_;
|
||||
EventMetric<OEMCryptoResult, Pow2Bucket> oemcrypto_decrypt_cenc_;
|
||||
CounterMetric<OEMCryptoResult> oemcrypto_delete_usage_entry_;
|
||||
CounterMetric<OEMCryptoResult> oemcrypto_delete_usage_table_;
|
||||
EventMetric<OEMCryptoResult> oemcrypto_derive_keys_from_session_key_;
|
||||
CounterMetric<OEMCryptoResult> oemcrypto_force_delete_usage_entry_;
|
||||
EventMetric<OEMCryptoResult> oemcrypto_generate_derived_keys_;
|
||||
CounterMetric<OEMCryptoResult> oemcrypto_generate_nonce_;
|
||||
EventMetric<OEMCryptoResult, Pow2Bucket> oemcrypto_generate_rsa_signature_;
|
||||
EventMetric<OEMCryptoResult, Pow2Bucket> oemcrypto_generate_signature_;
|
||||
EventMetric<OEMCryptoResult, Pow2Bucket> oemcrypto_generic_decrypt_;
|
||||
EventMetric<OEMCryptoResult, Pow2Bucket> oemcrypto_generic_encrypt_;
|
||||
EventMetric<OEMCryptoResult, Pow2Bucket> oemcrypto_generic_sign_;
|
||||
EventMetric<OEMCryptoResult, Pow2Bucket> oemcrypto_generic_verify_;
|
||||
CounterMetric<OEMCryptoResult> oemcrypto_get_device_id_;
|
||||
EventMetric<OEMCryptoResult, Pow2Bucket> oemcrypto_get_key_data_;
|
||||
CounterMetric<OEMCryptoResult> oemcrypto_get_oem_public_certificate_;
|
||||
CounterMetric<OEMCryptoResult> oemcrypto_get_random_;
|
||||
EventMetric<OEMCryptoResult> oemcrypto_initialize_;
|
||||
EventMetric<OEMCryptoResult> oemcrypto_install_keybox_;
|
||||
ValueMetric<bool> oemcrypto_is_anti_rollback_hw_present_;
|
||||
ValueMetric<bool> oemcrypto_is_keybox_valid_;
|
||||
EventMetric<OEMCryptoResult> oemcrypto_load_device_rsa_key_;
|
||||
EventMetric<OEMCryptoResult> oemcrypto_load_keys_;
|
||||
ValueMetric<OEMCrypto_HDCP_Capability> oemcrypto_max_hdcp_capability_;
|
||||
ValueMetric<size_t> oemcrypto_max_number_of_sessions_;
|
||||
ValueMetric<size_t> oemcrypto_number_of_open_sessions_;
|
||||
ValueMetric<OEMCrypto_ProvisioningMethod> oemcrypto_provisioning_method_;
|
||||
EventMetric<OEMCryptoResult> oemcrypto_refresh_keys_;
|
||||
CounterMetric<OEMCryptoResult> oemcrypto_report_usage_;
|
||||
EventMetric<OEMCryptoResult> oemcrypto_rewrap_device_rsa_key_;
|
||||
EventMetric<OEMCryptoResult> oemcrypto_rewrap_device_rsa_key_30_;
|
||||
ValueMetric<uint16_t> oemcrypto_security_patch_level_;
|
||||
EventMetric<OEMCryptoResult> oemcrypto_select_key_;
|
||||
ValueMetric<bool> oemcrypto_supports_usage_table_;
|
||||
CounterMetric<OEMCryptoResult> oemcrypto_update_usage_table_;
|
||||
EventMetric<OEMCryptoResult> oemcrypto_wrap_keybox_;
|
||||
};
|
||||
|
||||
// This class contains session-scoped metrics. All properties and
|
||||
// statistics related to operations within a single session are
|
||||
// recorded here.
|
||||
class SessionMetrics {
|
||||
public:
|
||||
SessionMetrics();
|
||||
|
||||
// Sets the session id of the metrics group. This allows the session
|
||||
// id to be captured and reported as part of the collection of metrics.
|
||||
void SetSessionId(const CdmSessionId& session_id) {
|
||||
session_id_ = session_id; }
|
||||
|
||||
// Returns the session id or an empty session id if it has not been set.
|
||||
const CdmSessionId& GetSessionId() const { return session_id_; }
|
||||
|
||||
// Marks the metrics object as completed and ready for serialization.
|
||||
void SetCompleted() { completed_ = true; }
|
||||
|
||||
// Returns true if the object is completed. This is used to determine
|
||||
// when the stats are ready to be published.
|
||||
bool IsCompleted() const { return completed_; }
|
||||
|
||||
// Returns a pointer to the crypto metrics belonging to the engine instance.
|
||||
// This instance retains ownership of the object.
|
||||
CryptoMetrics* GetCryptoMetrics() { return &crypto_metrics_; }
|
||||
|
||||
// Metrics collected at the session level.
|
||||
ValueMetric<double> cdm_session_life_span_; // Milliseconds.
|
||||
EventMetric<CdmResponseType> cdm_session_renew_key_;
|
||||
CounterMetric<CdmResponseType> cdm_session_restore_offline_session_;
|
||||
CounterMetric<CdmResponseType> cdm_session_restore_usage_session_;
|
||||
|
||||
// Serialize the session metrics to the provided |metric_group|.
|
||||
// |metric_group| is owned by the caller and must not be null.
|
||||
void Serialize(drm_metrics::MetricsGroup* metric_group);
|
||||
|
||||
private:
|
||||
void SerializeSessionMetrics(drm_metrics::MetricsGroup* metric_group);
|
||||
CdmSessionId session_id_;
|
||||
bool completed_;
|
||||
CryptoMetrics crypto_metrics_;
|
||||
};
|
||||
|
||||
// This class contains metrics for the OEMCrypto Dynamic Adapter. They are
|
||||
// separated from other metrics because they need to be encapsulated in a
|
||||
// singleton object. This is because the dynamic adapter uses the OEMCrypto
|
||||
// function signatures and contract and cannot be extended to inject
|
||||
// dependencies.
|
||||
//
|
||||
// Operations for this metrics class are serialized since these particular
|
||||
// metrics may be accessed by a separate thread during intialize even as
|
||||
// the metric may be serialized.
|
||||
class OemCryptoDynamicAdapterMetrics {
|
||||
public:
|
||||
explicit OemCryptoDynamicAdapterMetrics();
|
||||
|
||||
// Set methods for OEMCrypto metrics.
|
||||
void SetInitializationMode(OEMCryptoInitializationMode mode);
|
||||
void SetL1ApiVersion(uint32_t version);
|
||||
void SetL1MinApiVersion(uint32_t version);
|
||||
|
||||
// Serialize the session metrics to the provided |metric_group|.
|
||||
// |metric_group| is owned by the caller and must not be null.
|
||||
void Serialize(drm_metrics::MetricsGroup* metric_group);
|
||||
|
||||
// Clears the existing metric values.
|
||||
void Clear();
|
||||
|
||||
private:
|
||||
Lock adapter_lock_;
|
||||
ValueMetric<OEMCryptoInitializationMode> oemcrypto_initialization_mode_;
|
||||
ValueMetric<uint32_t> oemcrypto_l1_api_version_;
|
||||
ValueMetric<uint32_t> oemcrypto_l1_min_api_version_;
|
||||
};
|
||||
|
||||
// This will fetch the singleton instance for dynamic adapter metrics.
|
||||
// This method is safe only if we use C++ 11. In C++ 11, static function-local
|
||||
// initialization is guaranteed to be threadsafe. We return the reference to
|
||||
// avoid non-guaranteed destructor order problems. Effectively, the destructor
|
||||
// is never run for the created instance.
|
||||
OemCryptoDynamicAdapterMetrics& GetDynamicAdapterMetricsInstance();
|
||||
|
||||
// This class contains engine-scoped metrics. All properties and
|
||||
// statistics related to operations within the engine, but outside
|
||||
// the scope of a session are recorded here.
|
||||
class EngineMetrics {
|
||||
public:
|
||||
EngineMetrics();
|
||||
~EngineMetrics();
|
||||
|
||||
// Add a new SessionMetrics instance and return a pointer to the caller.
|
||||
// The new SessionMetrics instance is owned by this EngineMetrics instance
|
||||
// and will exist until RemoveSession is called or this object is deleted.
|
||||
SessionMetrics* AddSession();
|
||||
|
||||
// Removes the metrics object for the given session id. This should only
|
||||
// be called when the SessionMetrics instance is no longer in use.
|
||||
void RemoveSession(CdmSessionId session_id);
|
||||
|
||||
// Returns a pointer to the crypto metrics belonging to the engine instance.
|
||||
// The CryptoMetrics instance is still owned by this object and will exist
|
||||
// until this object is deleted.
|
||||
CryptoMetrics* GetCryptoMetrics() { return &crypto_metrics_; }
|
||||
|
||||
// Serialize engine and session metrics into a serialized MetricsGroup
|
||||
// instance and output that instance to the provided |metric_group|.
|
||||
// |metric_group| is owned by the caller and must NOT be null.
|
||||
// |completed_only| indicates that this call should only publish
|
||||
// SessionMetrics instances that are marked as completed.
|
||||
// |clear_sessions| indicates that this call should clear sessions metrics
|
||||
// for those sessions that were serialized. This allows atomic
|
||||
// serialization and closing of session-level metrics.
|
||||
void Serialize(drm_metrics::MetricsGroup* metric_group, bool completed_only,
|
||||
bool clear_serialized_sessions);
|
||||
|
||||
void SetAppPackageName(const std::string& app_package_name);
|
||||
|
||||
// Metrics recorded at the engine level.
|
||||
EventMetric<CdmResponseType> cdm_engine_add_key_;
|
||||
ValueMetric<std::string> cdm_engine_cdm_version_;
|
||||
CounterMetric<CdmResponseType> cdm_engine_close_session_;
|
||||
ValueMetric<int64_t> cdm_engine_creation_time_millis_;
|
||||
EventMetric<CdmResponseType, Pow2Bucket> cdm_engine_decrypt_;
|
||||
CounterMetric<bool> cdm_engine_find_session_for_key_;
|
||||
EventMetric<CdmResponseType> cdm_engine_generate_key_request_;
|
||||
EventMetric<CdmResponseType> cdm_engine_get_provisioning_request_;
|
||||
EventMetric<CdmResponseType> cdm_engine_get_usage_info_;
|
||||
EventMetric<CdmResponseType> cdm_engine_handle_provisioning_response_;
|
||||
ValueMetric<double> cdm_engine_life_span_; // Milliseconds
|
||||
CounterMetric<CdmResponseType> cdm_engine_open_key_set_session_;
|
||||
CounterMetric<CdmResponseType> cdm_engine_open_session_;
|
||||
EventMetric<CdmResponseType> cdm_engine_query_key_status_;
|
||||
CounterMetric<CdmResponseType> cdm_engine_release_all_usage_info_;
|
||||
CounterMetric<CdmResponseType> cdm_engine_release_usage_info_;
|
||||
CounterMetric<CdmResponseType> cdm_engine_remove_keys_;
|
||||
EventMetric<CdmResponseType> cdm_engine_restore_key_;
|
||||
CounterMetric<CdmResponseType, CdmSecurityLevel> cdm_engine_unprovision_;
|
||||
|
||||
private:
|
||||
Lock session_metrics_lock_;
|
||||
std::vector<metrics::SessionMetrics*> session_metrics_list_;
|
||||
CryptoMetrics crypto_metrics_;
|
||||
std::string app_package_name_;
|
||||
|
||||
void SerializeEngineMetrics(drm_metrics::MetricsGroup* out);
|
||||
};
|
||||
|
||||
} // namespace metrics
|
||||
} // namespace wvcdm
|
||||
|
||||
#endif // WVCDM_METRICS_METRICS_GROUP_H_
|
||||
24
metrics/include/timer_metric.h
Normal file
24
metrics/include/timer_metric.h
Normal file
@@ -0,0 +1,24 @@
|
||||
#ifndef WVCDM_METRICS_TIMER_METRIC_H_
|
||||
#define WVCDM_METRICS_TIMER_METRIC_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
namespace wvcdm {
|
||||
namespace metrics {
|
||||
|
||||
class TimerMetric {
|
||||
|
||||
public:
|
||||
void Start();
|
||||
double AsMs() const;
|
||||
double AsUs() const;
|
||||
|
||||
private:
|
||||
double sec_;
|
||||
double usec_;
|
||||
|
||||
};
|
||||
|
||||
} // namespace metrics
|
||||
} // namespace wvcdm
|
||||
#endif
|
||||
106
metrics/include/value_metric.h
Normal file
106
metrics/include/value_metric.h
Normal file
@@ -0,0 +1,106 @@
|
||||
// Copyright 2017 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// This file contains the declarations for the Metric class and related
|
||||
// types.
|
||||
#ifndef WVCDM_METRICS_VALUE_METRIC_H_
|
||||
#define WVCDM_METRICS_VALUE_METRIC_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string>
|
||||
|
||||
#include "metric_serialization.h"
|
||||
|
||||
namespace wvcdm {
|
||||
namespace metrics {
|
||||
|
||||
// Private namespace for some helper implementation functions.
|
||||
namespace impl {
|
||||
|
||||
// These helper functions map the templated ValueMetric class
|
||||
// Serialize call to the MetricSerializer explicit calls.
|
||||
template<typename T>
|
||||
void Serialize(MetricSerializer* serializer,
|
||||
const std::string& metric_name, const T& t);
|
||||
|
||||
inline void SerializeError(MetricSerializer* serializer,
|
||||
const std::string& metric_name,
|
||||
const int& error_code) {
|
||||
serializer->SetInt32(metric_name + "/error", error_code);
|
||||
}
|
||||
|
||||
} // namespace impl
|
||||
|
||||
// The Metric class supports storing a single value which can be overwritten.
|
||||
// the Metric class also supports the MetricSerializer interface through
|
||||
// which the value can be serialized. If the value was never given a value
|
||||
// or an error code, then the metric will not serialize anything.
|
||||
//
|
||||
// Example Usage:
|
||||
// Metric<string> cdm_version("drm/cdm/version")
|
||||
// .Record("a.b.c.d");
|
||||
//
|
||||
// MyMetricSerializerImpl serialzer;
|
||||
// cdm_version.Serialize(&serializer);
|
||||
//
|
||||
// Example Error Usage:
|
||||
//
|
||||
// Metric<string> cdm_version("drm/cdm/version")
|
||||
// .SetError(error_code);
|
||||
//
|
||||
// Note that serialization is the same. But the ValueMetric will serialize
|
||||
// the error code to <metric_name>/error instead of just <metric_name>.
|
||||
template<typename T>
|
||||
class ValueMetric : public MetricSerializable {
|
||||
public:
|
||||
// Constructs a metric with the given metric name.
|
||||
explicit ValueMetric(const std::string& metric_name)
|
||||
: metric_name_(metric_name), error_code_(0),
|
||||
has_error_(false), has_value_(false) {}
|
||||
|
||||
// Serialize the metric name and value using the given serializer.
|
||||
// Caller owns |serializer| which cannot be null.
|
||||
virtual void Serialize(MetricSerializer* serializer) {
|
||||
if (has_value_) {
|
||||
impl::Serialize(serializer, metric_name_, value_);
|
||||
} else if (has_error_) {
|
||||
impl::SerializeError(serializer, metric_name_, error_code_);
|
||||
} else {
|
||||
// Do nothing if there is no value and no error.
|
||||
}
|
||||
}
|
||||
|
||||
// Record the value of the metric.
|
||||
void Record(const T& value) {
|
||||
value_ = value;
|
||||
has_value_ = true;
|
||||
has_error_ = false;
|
||||
}
|
||||
|
||||
// Set the error code if an error was encountered.
|
||||
void SetError(int error_code) {
|
||||
error_code_ = error_code;
|
||||
has_value_ = false;
|
||||
has_error_ = true;
|
||||
}
|
||||
|
||||
// Get the current value of the metric.
|
||||
const T& GetValue() { return value_; }
|
||||
|
||||
// Clears the indicators that the metric or error was set.
|
||||
void Clear() {
|
||||
has_value_ = false;
|
||||
has_error_ = false;
|
||||
}
|
||||
|
||||
private:
|
||||
std::string metric_name_;
|
||||
T value_;
|
||||
int error_code_;
|
||||
bool has_error_;
|
||||
bool has_value_;
|
||||
};
|
||||
|
||||
} // namespace metrics
|
||||
} // namespace wvcdm
|
||||
|
||||
#endif // WVCDM_METRICS_VALUE_METRIC_H_
|
||||
33
metrics/src/counter_metric.cpp
Normal file
33
metrics/src/counter_metric.cpp
Normal file
@@ -0,0 +1,33 @@
|
||||
// Copyright 2017 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// This file contains implementations for the BaseCounterMetric, the base class
|
||||
// for CounterMetric.
|
||||
|
||||
#include "counter_metric.h"
|
||||
|
||||
namespace wvcdm {
|
||||
namespace metrics {
|
||||
|
||||
void BaseCounterMetric::Increment(const std::string& field_names_values,
|
||||
int64_t value) {
|
||||
AutoLock lock(internal_lock_);
|
||||
|
||||
if (value_map_.find(field_names_values) == value_map_.end()) {
|
||||
value_map_[field_names_values] = value;
|
||||
} else {
|
||||
value_map_[field_names_values] = value_map_[field_names_values] + value;
|
||||
}
|
||||
}
|
||||
|
||||
void BaseCounterMetric::Serialize(MetricSerializer* serializer) {
|
||||
AutoLock lock(internal_lock_);
|
||||
|
||||
for (std::map<std::string, int64_t>::iterator it
|
||||
= value_map_.begin(); it != value_map_.end(); it++) {
|
||||
serializer->SetInt64(metric_name_ + "/count" + it->first, it->second);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace metrics
|
||||
} // namespace wvcdm
|
||||
|
||||
33
metrics/src/distribution.cpp
Normal file
33
metrics/src/distribution.cpp
Normal file
@@ -0,0 +1,33 @@
|
||||
// Copyright 2017 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// This file contains the definitions for the Distribution class members.
|
||||
|
||||
#include "distribution.h"
|
||||
|
||||
#include <float.h>
|
||||
|
||||
namespace wvcdm {
|
||||
namespace metrics {
|
||||
|
||||
Distribution::Distribution() :
|
||||
count_(0LL),
|
||||
min_(DBL_MAX),
|
||||
max_(-DBL_MAX),
|
||||
mean_(0.0),
|
||||
sum_squared_deviation_(0.0) {
|
||||
}
|
||||
|
||||
void Distribution::Record(double value) {
|
||||
// Using method of provisional means.
|
||||
double deviation = value - mean_;
|
||||
mean_ = mean_ + (deviation / ++count_);
|
||||
sum_squared_deviation_ =
|
||||
sum_squared_deviation_ + (deviation * (value - mean_));
|
||||
|
||||
min_ = min_ < value ? min_ : value;
|
||||
max_ = max_ > value ? max_ : value;
|
||||
}
|
||||
|
||||
} // namespace metrics
|
||||
} // namespace wvcdm
|
||||
|
||||
63
metrics/src/event_metric.cpp
Normal file
63
metrics/src/event_metric.cpp
Normal file
@@ -0,0 +1,63 @@
|
||||
// Copyright 2017 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// This file contains implementations for the BaseEventMetric.
|
||||
|
||||
#include "event_metric.h"
|
||||
|
||||
namespace wvcdm {
|
||||
namespace metrics {
|
||||
|
||||
BaseEventMetric::~BaseEventMetric() {
|
||||
AutoLock lock(internal_lock_);
|
||||
|
||||
for (std::map<std::string, Distribution*>::iterator it
|
||||
= value_map_.begin(); it != value_map_.end(); it++) {
|
||||
delete it->second;
|
||||
}
|
||||
}
|
||||
|
||||
void BaseEventMetric::Record(const std::string& field_names_values,
|
||||
double value) {
|
||||
AutoLock lock(internal_lock_);
|
||||
|
||||
Distribution* distribution;
|
||||
|
||||
if (value_map_.find(field_names_values) == value_map_.end()) {
|
||||
distribution = new Distribution();
|
||||
value_map_[field_names_values] = distribution;
|
||||
} else {
|
||||
distribution = value_map_[field_names_values];
|
||||
}
|
||||
|
||||
distribution->Record(value);
|
||||
}
|
||||
|
||||
void BaseEventMetric::Serialize(MetricSerializer* serializer) {
|
||||
AutoLock lock(internal_lock_);
|
||||
|
||||
for (std::map<std::string, Distribution*>::iterator it
|
||||
= value_map_.begin(); it != value_map_.end(); it++) {
|
||||
serializer->SetInt64(
|
||||
metric_name_ + "/count" + it->first,
|
||||
it->second->Count());
|
||||
serializer->SetDouble(
|
||||
metric_name_ + "/mean" + it->first,
|
||||
it->second->Mean());
|
||||
// Only publish additional information if there was more than one sample.
|
||||
if (it->second->Count() > 1) {
|
||||
serializer->SetDouble(
|
||||
metric_name_ + "/variance" + it->first,
|
||||
it->second->Variance());
|
||||
serializer->SetDouble(
|
||||
metric_name_ + "/min" + it->first,
|
||||
it->second->Min());
|
||||
serializer->SetDouble(
|
||||
metric_name_ + "/max" + it->first,
|
||||
it->second->Max());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace metrics
|
||||
} // namespace wvcdm
|
||||
|
||||
39
metrics/src/metrics.proto
Normal file
39
metrics/src/metrics.proto
Normal file
@@ -0,0 +1,39 @@
|
||||
// Copyright 2017 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// This file contains a proto definition for serialization of metrics data.
|
||||
//
|
||||
syntax = "proto2";
|
||||
|
||||
package drm_metrics;
|
||||
|
||||
// need this if we are using libprotobuf-cpp-2.3.0-lite
|
||||
option optimize_for = LITE_RUNTIME;
|
||||
|
||||
// The MetricsGroup is a collection of metric name/value pair instances
|
||||
// that can be serialized and provided to a caller.
|
||||
message MetricsGroup {
|
||||
message Metric {
|
||||
message MetricValue {
|
||||
// Only one of the following values must be set. Note that the oneof
|
||||
// keyword is not supported in the protobuf version checked into the CDM.
|
||||
optional int64 int_value = 1;
|
||||
optional double double_value = 2;
|
||||
optional string string_value = 3;
|
||||
}
|
||||
|
||||
// The name of the metric. Must be valid UTF-8. Required.
|
||||
optional string name = 1;
|
||||
|
||||
// The value of the metric. Required.
|
||||
optional MetricValue value = 2;
|
||||
}
|
||||
|
||||
// The list of name/value pairs of metrics.
|
||||
repeated Metric metric = 1;
|
||||
|
||||
// Allow multiple sub groups of metrics.
|
||||
repeated MetricsGroup metric_sub_group = 2;
|
||||
|
||||
// Name of the application package associated with the metrics.
|
||||
optional string app_package_name = 3;
|
||||
}
|
||||
541
metrics/src/metrics_collections.cpp
Normal file
541
metrics/src/metrics_collections.cpp
Normal file
@@ -0,0 +1,541 @@
|
||||
// Copyright 2016 Google Inc. All Rights Reserved.
|
||||
|
||||
#include "metrics_collections.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include "log.h"
|
||||
#include "metrics.pb.h"
|
||||
|
||||
using drm_metrics::MetricsGroup;
|
||||
using wvcdm::metrics::MetricSerializer;
|
||||
|
||||
namespace {
|
||||
// Helper struct for comparing session ids.
|
||||
struct CompareSessionIds {
|
||||
const std::string& target_;
|
||||
|
||||
CompareSessionIds(const wvcdm::CdmSessionId& target) : target_(target) {};
|
||||
|
||||
bool operator()(const wvcdm::metrics::SessionMetrics* metrics) const {
|
||||
return metrics->GetSessionId() == target_;
|
||||
}
|
||||
};
|
||||
|
||||
// Local class used to serialize to the MetricsGroup proto message.
|
||||
class ProtoMetricSerializer : public wvcdm::metrics::MetricSerializer {
|
||||
public:
|
||||
ProtoMetricSerializer(MetricsGroup* metric_group)
|
||||
: metric_group_(metric_group) {}
|
||||
|
||||
virtual void SetString(const std::string& metric_id,
|
||||
const std::string& value) {
|
||||
MetricsGroup::Metric* metric = metric_group_->add_metric();
|
||||
metric->set_name(metric_id);
|
||||
metric->mutable_value()->set_string_value(value);
|
||||
}
|
||||
|
||||
virtual void SetInt32(const std::string& metric_id, int32_t value) {
|
||||
MetricsGroup::Metric* metric = metric_group_->add_metric();
|
||||
metric->set_name(metric_id);
|
||||
metric->mutable_value()->set_int_value(value);
|
||||
}
|
||||
|
||||
virtual void SetInt64(const std::string& metric_id, int64_t value) {
|
||||
MetricsGroup::Metric* metric = metric_group_->add_metric();
|
||||
metric->set_name(metric_id);
|
||||
metric->mutable_value()->set_int_value(value);
|
||||
}
|
||||
|
||||
virtual void SetDouble(const std::string& metric_id, double value) {
|
||||
MetricsGroup::Metric* metric = metric_group_->add_metric();
|
||||
metric->set_name(metric_id);
|
||||
metric->mutable_value()->set_double_value(value);
|
||||
}
|
||||
|
||||
private:
|
||||
MetricsGroup* metric_group_;
|
||||
};
|
||||
|
||||
} // anonymous namespace
|
||||
|
||||
namespace wvcdm {
|
||||
namespace metrics {
|
||||
|
||||
CryptoMetrics::CryptoMetrics() :
|
||||
crypto_session_security_level_(
|
||||
"/drm/widevine/crypto_session/security_level"),
|
||||
crypto_session_delete_all_usage_reports_(
|
||||
"/drm/widevine/crypto_session/delete_all_usage_reports",
|
||||
"error"),
|
||||
crypto_session_delete_multiple_usage_information_(
|
||||
"/drm/widevine/crypto_session/delete_multiple_usage_information",
|
||||
"error"),
|
||||
crypto_session_generic_decrypt_(
|
||||
"/drm/widevine/crypto_session/generic_decrypt/time",
|
||||
"error",
|
||||
"length",
|
||||
"encryption_algorithm"),
|
||||
crypto_session_generic_encrypt_(
|
||||
"/drm/widevine/crypto_session/generic_encrypt/time",
|
||||
"error",
|
||||
"length",
|
||||
"encryption_algorithm"),
|
||||
crypto_session_generic_sign_(
|
||||
"/drm/widevine/crypto_session/generic_sign/time",
|
||||
"error",
|
||||
"length",
|
||||
"signing_algorithm"),
|
||||
crypto_session_generic_verify_(
|
||||
"/drm/widevine/crypto_session/generic_verify/time",
|
||||
"error",
|
||||
"length",
|
||||
"signing_algorithm"),
|
||||
crypto_session_get_device_unique_id_(
|
||||
"/drm/widevine/crypto_session/get_device_unique_id",
|
||||
"success"),
|
||||
crypto_session_get_token_(
|
||||
"/drm/widevine/crypto_session/get_token",
|
||||
"success"),
|
||||
crypto_session_life_span_(
|
||||
"/drm/widevine/crypto_session/life_span"),
|
||||
crypto_session_load_certificate_private_key_(
|
||||
"/drm/widevine/crypto_session/load_certificate_private_key/time",
|
||||
"success"),
|
||||
crypto_session_open_(
|
||||
"/drm/widevine/crypto_session/open/time",
|
||||
"error",
|
||||
"requested_security_level"),
|
||||
crypto_session_system_id_(
|
||||
"/drm/widevine/crypto_session/system_id"),
|
||||
crypto_session_update_usage_information_(
|
||||
"/drm/widevine/crypto_session/update_usage_information/time",
|
||||
"error"),
|
||||
crypto_session_usage_information_support_(
|
||||
"/drm/widevine/crypto_session/usage_information_support"),
|
||||
oemcrypto_api_version_(
|
||||
"/drm/widevine/oemcrypto/api_version"),
|
||||
oemcrypto_close_session_(
|
||||
"/drm/widevine/oemcrypto/close_session",
|
||||
"oemcrypto_error"),
|
||||
oemcrypto_copy_buffer_(
|
||||
"/drm/widevine/oemcrypto/copy_buffer/time",
|
||||
"oemcrypto_error",
|
||||
"length"),
|
||||
oemcrypto_current_hdcp_capability_(
|
||||
"/drm/widevine/oemcrypto/current_hdcp_capability"),
|
||||
oemcrypto_deactivate_usage_entry_(
|
||||
"/drm/widevine/oemcrypto/deactivate_usage_entry",
|
||||
"oemcrypto_error"),
|
||||
oemcrypto_decrypt_cenc_(
|
||||
"/drm/widevine/oemcrypto/decrypt_cenc/time",
|
||||
"oemcrypto_error",
|
||||
"length"),
|
||||
oemcrypto_delete_usage_entry_(
|
||||
"/drm/widevine/oemcrypto/delete_usage_entry",
|
||||
"oemcrypto_error"),
|
||||
oemcrypto_delete_usage_table_(
|
||||
"/drm/widevine/oemcrypto/delete_usage_table",
|
||||
"oemcrypto_error"),
|
||||
oemcrypto_derive_keys_from_session_key_(
|
||||
"/drm/widevine/oemcrypto/derive_keys_from_session_key/time",
|
||||
"oemcrypto_error"),
|
||||
oemcrypto_force_delete_usage_entry_(
|
||||
"/drm/widevine/oemcrypto/force_delete_usage_entry",
|
||||
"oemcrypto_error"),
|
||||
oemcrypto_generate_derived_keys_(
|
||||
"/drm/widevine/oemcrypto/generate_derived_keys/time",
|
||||
"oemcrypto_error"),
|
||||
oemcrypto_generate_nonce_(
|
||||
"/drm/widevine/oemcrypto/generate_nonce",
|
||||
"oemcrypto_error"),
|
||||
oemcrypto_generate_rsa_signature_(
|
||||
"/drm/widevine/oemcrypto/generate_rsa_signature/time",
|
||||
"oemcrypto_error",
|
||||
"length"),
|
||||
oemcrypto_generate_signature_(
|
||||
"/drm/widevine/oemcrypto/generate_signature/time",
|
||||
"oemcrypto_error",
|
||||
"length"),
|
||||
oemcrypto_generic_decrypt_(
|
||||
"/drm/widevine/oemcrypto/generic_decrypt/time",
|
||||
"oemcrypto_error",
|
||||
"length"),
|
||||
oemcrypto_generic_encrypt_(
|
||||
"/drm/widevine/oemcrypto/generic_encrypt/time",
|
||||
"oemcrypto_error",
|
||||
"length"),
|
||||
oemcrypto_generic_sign_(
|
||||
"/drm/widevine/oemcrypto/generic_sign/time",
|
||||
"oemcrypto_error",
|
||||
"length"),
|
||||
oemcrypto_generic_verify_(
|
||||
"/drm/widevine/oemcrypto/generic_verify/time",
|
||||
"oemcrypto_error",
|
||||
"length"),
|
||||
oemcrypto_get_device_id_(
|
||||
"/drm/widevine/oemcrypto/get_device_id",
|
||||
"oemcrypto_error"),
|
||||
oemcrypto_get_key_data_(
|
||||
"/drm/widevine/oemcrypto/get_key_data/time",
|
||||
"oemcrypto_error",
|
||||
"length"),
|
||||
oemcrypto_get_oem_public_certificate_(
|
||||
"/drm/widevine/oemcrypto/get_oem_public_certificate",
|
||||
"oemcrypto_error"),
|
||||
oemcrypto_get_random_(
|
||||
"/drm/widevine/oemcrypto/get_random",
|
||||
"oemcrypto_error"),
|
||||
oemcrypto_initialize_(
|
||||
"/drm/widevine/oemcrypto/initialize/time",
|
||||
"oemcrypto_error"),
|
||||
oemcrypto_install_keybox_(
|
||||
"/drm/widevine/oemcrypto/install_keybox/time",
|
||||
"oemcrypto_error"),
|
||||
oemcrypto_is_anti_rollback_hw_present_(
|
||||
"/drm/widevine/oemcrypto/is_anti_rollback_hw_present"),
|
||||
oemcrypto_is_keybox_valid_(
|
||||
"/drm/widevine/oemcrypto/is_keybox_valid"),
|
||||
oemcrypto_load_device_rsa_key_(
|
||||
"/drm/widevine/oemcrypto/load_device_rsa_key/time",
|
||||
"oemcrypto_error"),
|
||||
oemcrypto_load_keys_(
|
||||
"/drm/widevine/oemcrypto/load_keys/time",
|
||||
"oemcrypto_error"),
|
||||
oemcrypto_max_hdcp_capability_(
|
||||
"/drm/widevine/oemcrypto/max_hdcp_capability"),
|
||||
oemcrypto_max_number_of_sessions_(
|
||||
"/drm/widevine/oemcrypto/max_number_of_sessions"),
|
||||
oemcrypto_number_of_open_sessions_(
|
||||
"/drm/widevine/oemcrypto/number_of_open_sessions"),
|
||||
oemcrypto_provisioning_method_(
|
||||
"/drm/widevine/oemcrypto/provisioning_method"),
|
||||
oemcrypto_refresh_keys_(
|
||||
"/drm/widevine/oemcrypto/refresh_keys/time",
|
||||
"oemcrypto_error"),
|
||||
oemcrypto_report_usage_(
|
||||
"/drm/widevine/oemcrypto/report_usage",
|
||||
"oemcrypto_error"),
|
||||
oemcrypto_rewrap_device_rsa_key_(
|
||||
"/drm/widevine/oemcrypto/rewrap_device_rsa_key/time",
|
||||
"oemcrypto_error"),
|
||||
oemcrypto_rewrap_device_rsa_key_30_(
|
||||
"/drm/widevine/oemcrypto/rewrap_device_rsa_key_30/time",
|
||||
"oemcrypto_error"),
|
||||
oemcrypto_security_patch_level_(
|
||||
"/drm/widevine/oemcrypto/security_patch_level"),
|
||||
oemcrypto_select_key_(
|
||||
"/drm/widevine/oemcrypto/select_key/time",
|
||||
"oemcrypto_error"),
|
||||
oemcrypto_supports_usage_table_(
|
||||
"/drm/widevine/oemcrypto/supports_usage_table"),
|
||||
oemcrypto_update_usage_table_(
|
||||
"/drm/widevine/oemcrypto/update_usage_table",
|
||||
"oemcrypto_error"),
|
||||
oemcrypto_wrap_keybox_(
|
||||
"/drm/widevine/oemcrypto/wrap_keybox/time",
|
||||
"oemcrypto_error") {
|
||||
}
|
||||
|
||||
void CryptoMetrics::Serialize(MetricsGroup* metrics) {
|
||||
ProtoMetricSerializer serializer(metrics);
|
||||
/* CRYPTO SESSION */
|
||||
crypto_session_security_level_.Serialize(&serializer);
|
||||
crypto_session_delete_all_usage_reports_.Serialize(&serializer);
|
||||
crypto_session_delete_multiple_usage_information_.Serialize(&serializer);
|
||||
crypto_session_generic_decrypt_.Serialize(&serializer);
|
||||
crypto_session_generic_encrypt_.Serialize(&serializer);
|
||||
crypto_session_generic_sign_.Serialize(&serializer);
|
||||
crypto_session_generic_verify_.Serialize(&serializer);
|
||||
crypto_session_get_device_unique_id_.Serialize(&serializer);
|
||||
crypto_session_get_token_.Serialize(&serializer);
|
||||
crypto_session_life_span_.Serialize(&serializer);
|
||||
crypto_session_load_certificate_private_key_.Serialize(&serializer);
|
||||
crypto_session_open_.Serialize(&serializer);
|
||||
crypto_session_system_id_.Serialize(&serializer);
|
||||
crypto_session_update_usage_information_.Serialize(&serializer);
|
||||
crypto_session_usage_information_support_.Serialize(&serializer);
|
||||
|
||||
/* OEMCRYPTO */
|
||||
oemcrypto_api_version_.Serialize(&serializer);
|
||||
oemcrypto_close_session_.Serialize(&serializer);
|
||||
oemcrypto_copy_buffer_.Serialize(&serializer);
|
||||
oemcrypto_current_hdcp_capability_.Serialize(&serializer);
|
||||
oemcrypto_deactivate_usage_entry_.Serialize(&serializer);
|
||||
oemcrypto_decrypt_cenc_.Serialize(&serializer);
|
||||
oemcrypto_delete_usage_entry_.Serialize(&serializer);
|
||||
oemcrypto_delete_usage_table_.Serialize(&serializer);
|
||||
oemcrypto_derive_keys_from_session_key_.Serialize(&serializer);
|
||||
oemcrypto_force_delete_usage_entry_.Serialize(&serializer);
|
||||
oemcrypto_generate_derived_keys_.Serialize(&serializer);
|
||||
oemcrypto_generate_nonce_.Serialize(&serializer);
|
||||
oemcrypto_generate_rsa_signature_.Serialize(&serializer);
|
||||
oemcrypto_generate_signature_.Serialize(&serializer);
|
||||
oemcrypto_generic_decrypt_.Serialize(&serializer);
|
||||
oemcrypto_generic_encrypt_.Serialize(&serializer);
|
||||
oemcrypto_generic_sign_.Serialize(&serializer);
|
||||
oemcrypto_generic_verify_.Serialize(&serializer);
|
||||
oemcrypto_get_device_id_.Serialize(&serializer);
|
||||
oemcrypto_get_key_data_.Serialize(&serializer);
|
||||
oemcrypto_get_oem_public_certificate_.Serialize(&serializer);
|
||||
oemcrypto_get_random_.Serialize(&serializer);
|
||||
oemcrypto_initialize_.Serialize(&serializer);
|
||||
oemcrypto_install_keybox_.Serialize(&serializer);
|
||||
oemcrypto_is_anti_rollback_hw_present_.Serialize(&serializer);
|
||||
oemcrypto_is_keybox_valid_.Serialize(&serializer);
|
||||
oemcrypto_load_device_rsa_key_.Serialize(&serializer);
|
||||
oemcrypto_load_keys_.Serialize(&serializer);
|
||||
oemcrypto_max_hdcp_capability_.Serialize(&serializer);
|
||||
oemcrypto_max_number_of_sessions_.Serialize(&serializer);
|
||||
oemcrypto_number_of_open_sessions_.Serialize(&serializer);
|
||||
oemcrypto_provisioning_method_.Serialize(&serializer);
|
||||
oemcrypto_refresh_keys_.Serialize(&serializer);
|
||||
oemcrypto_report_usage_.Serialize(&serializer);
|
||||
oemcrypto_rewrap_device_rsa_key_.Serialize(&serializer);
|
||||
oemcrypto_rewrap_device_rsa_key_30_.Serialize(&serializer);
|
||||
oemcrypto_security_patch_level_.Serialize(&serializer);
|
||||
oemcrypto_select_key_.Serialize(&serializer);
|
||||
oemcrypto_supports_usage_table_.Serialize(&serializer);
|
||||
oemcrypto_update_usage_table_.Serialize(&serializer);
|
||||
oemcrypto_wrap_keybox_.Serialize(&serializer);
|
||||
}
|
||||
|
||||
SessionMetrics::SessionMetrics() :
|
||||
cdm_session_life_span_(
|
||||
"/drm/widevine/cdm_session/life_span"),
|
||||
cdm_session_renew_key_(
|
||||
"/drm/widevine/cdm_session/renew_key/time",
|
||||
"error"),
|
||||
cdm_session_restore_offline_session_(
|
||||
"/drm/widevine/cdm_session/restore_offline_session",
|
||||
"error"),
|
||||
cdm_session_restore_usage_session_(
|
||||
"/drm/widevine/cdm_session/restore_usage_session",
|
||||
"error"),
|
||||
completed_(false) {
|
||||
}
|
||||
|
||||
void SessionMetrics::Serialize(MetricsGroup* metric_group) {
|
||||
SerializeSessionMetrics(metric_group);
|
||||
crypto_metrics_.Serialize(metric_group);
|
||||
}
|
||||
|
||||
void SessionMetrics::SerializeSessionMetrics(MetricsGroup* metric_group) {
|
||||
ProtoMetricSerializer serializer(metric_group);
|
||||
// Add the session id as a single-valued metric.
|
||||
serializer.SetString("/drm/widevine/cdm_session/session_id", session_id_);
|
||||
cdm_session_life_span_.Serialize(&serializer);
|
||||
cdm_session_renew_key_.Serialize(&serializer);
|
||||
cdm_session_restore_offline_session_.Serialize(&serializer);
|
||||
cdm_session_restore_usage_session_.Serialize(&serializer);
|
||||
}
|
||||
|
||||
OemCryptoDynamicAdapterMetrics::OemCryptoDynamicAdapterMetrics() :
|
||||
oemcrypto_initialization_mode_(
|
||||
"/drm/widevine/oemcrypto/initialization_mode"),
|
||||
oemcrypto_l1_api_version_(
|
||||
"/drm/widevine/oemcrypto/l1_api_version"),
|
||||
oemcrypto_l1_min_api_version_(
|
||||
"/drm/widevine/oemcrypto/l1_min_api_version") {
|
||||
}
|
||||
|
||||
void OemCryptoDynamicAdapterMetrics::SetInitializationMode(
|
||||
OEMCryptoInitializationMode mode) {
|
||||
AutoLock lock(adapter_lock_);
|
||||
oemcrypto_initialization_mode_.Record(mode);
|
||||
}
|
||||
|
||||
void OemCryptoDynamicAdapterMetrics::SetL1ApiVersion(uint32_t version) {
|
||||
AutoLock lock(adapter_lock_);
|
||||
oemcrypto_l1_api_version_.Record(version);
|
||||
}
|
||||
|
||||
void OemCryptoDynamicAdapterMetrics::SetL1MinApiVersion(uint32_t version) {
|
||||
AutoLock lock(adapter_lock_);
|
||||
oemcrypto_l1_min_api_version_.Record(version);
|
||||
}
|
||||
|
||||
void OemCryptoDynamicAdapterMetrics::Serialize(
|
||||
drm_metrics::MetricsGroup* metric_group) {
|
||||
AutoLock lock(adapter_lock_);
|
||||
ProtoMetricSerializer serializer(metric_group);
|
||||
|
||||
oemcrypto_initialization_mode_.Serialize(&serializer);
|
||||
oemcrypto_l1_api_version_.Serialize(&serializer);
|
||||
oemcrypto_l1_min_api_version_.Serialize(&serializer);
|
||||
}
|
||||
|
||||
void OemCryptoDynamicAdapterMetrics::Clear() {
|
||||
AutoLock lock(adapter_lock_);
|
||||
|
||||
oemcrypto_initialization_mode_.Clear();
|
||||
oemcrypto_l1_api_version_.Clear();
|
||||
oemcrypto_l1_min_api_version_.Clear();
|
||||
}
|
||||
|
||||
// This method returns a reference. This means that the destructor is never
|
||||
// executed for the returned object.
|
||||
OemCryptoDynamicAdapterMetrics& GetDynamicAdapterMetricsInstance() {
|
||||
// This is safe in C++ 11 since the initialization is guaranteed to run
|
||||
// only once regardless of multi-threaded access.
|
||||
static OemCryptoDynamicAdapterMetrics* adapter_metrics =
|
||||
new OemCryptoDynamicAdapterMetrics();
|
||||
return *adapter_metrics;
|
||||
}
|
||||
|
||||
EngineMetrics::EngineMetrics() :
|
||||
cdm_engine_add_key_(
|
||||
"/drm/widevine/cdm_engine/add_key/time",
|
||||
"error"),
|
||||
cdm_engine_cdm_version_(
|
||||
"/drm/widevine/cdm_engine/version"),
|
||||
cdm_engine_close_session_(
|
||||
"/drm/widevine/cdm_engine/close_session",
|
||||
"error"),
|
||||
cdm_engine_creation_time_millis_(
|
||||
"/drm/widevine/cdm_engine/creation_time_millis"),
|
||||
cdm_engine_decrypt_(
|
||||
"/drm/widevine/cdm_engine/decrypt/time",
|
||||
"error",
|
||||
"length"),
|
||||
cdm_engine_find_session_for_key_(
|
||||
"/drm/widevine/cdm_engine/find_session_for_key",
|
||||
"success"),
|
||||
cdm_engine_generate_key_request_(
|
||||
"/drm/widevine/cdm_engine/generate_key_request/time",
|
||||
"error"),
|
||||
cdm_engine_get_provisioning_request_(
|
||||
"/drm/widevine/cdm_engine/get_provisioning_request/time",
|
||||
"error"),
|
||||
cdm_engine_get_usage_info_(
|
||||
"/drm/widevine/cdm_engine/get_usage_info/time",
|
||||
"error"),
|
||||
cdm_engine_handle_provisioning_response_(
|
||||
"/drm/widevine/cdm_engine/handle_provisioning_response/time",
|
||||
"error"),
|
||||
cdm_engine_life_span_(
|
||||
"/drm/widevine/cdm_engine/life_span"),
|
||||
cdm_engine_open_key_set_session_(
|
||||
"/drm/widevine/cdm_engine/open_key_set_session",
|
||||
"error"),
|
||||
cdm_engine_open_session_(
|
||||
"/drm/widevine/cdm_engine/open_session",
|
||||
"error"),
|
||||
cdm_engine_query_key_status_(
|
||||
"/drm/widevine/cdm_engine/query_key_status/time",
|
||||
"error"),
|
||||
cdm_engine_release_all_usage_info_(
|
||||
"/drm/widevine/cdm_engine/release_all_usage_info",
|
||||
"error"),
|
||||
cdm_engine_release_usage_info_(
|
||||
"/drm/widevine/cdm_engine/release_usage_info",
|
||||
"error"),
|
||||
cdm_engine_remove_keys_(
|
||||
"/drm/widevine/cdm_engine/remove_keys",
|
||||
"error"),
|
||||
cdm_engine_restore_key_(
|
||||
"/drm/widevine/cdm_engine/restore_key/time",
|
||||
"error"),
|
||||
cdm_engine_unprovision_(
|
||||
"/drm/widevine/cdm_engine/unprovision",
|
||||
"error",
|
||||
"security_level"),
|
||||
app_package_name_("") {
|
||||
}
|
||||
|
||||
EngineMetrics::~EngineMetrics() {
|
||||
AutoLock lock(session_metrics_lock_);
|
||||
std::vector<SessionMetrics*>::iterator i;
|
||||
if (!session_metrics_list_.empty()) {
|
||||
LOGV("EngineMetrics::~EngineMetrics. Session count: %d",
|
||||
session_metrics_list_.size());
|
||||
}
|
||||
for (i = session_metrics_list_.begin(); i != session_metrics_list_.end();
|
||||
i++) {
|
||||
delete *i;
|
||||
}
|
||||
session_metrics_list_.clear();
|
||||
}
|
||||
|
||||
SessionMetrics* EngineMetrics::AddSession() {
|
||||
AutoLock lock(session_metrics_lock_);
|
||||
SessionMetrics* metrics = new SessionMetrics();
|
||||
session_metrics_list_.push_back(metrics);
|
||||
return metrics;
|
||||
}
|
||||
|
||||
void EngineMetrics::RemoveSession(wvcdm::CdmSessionId session_id) {
|
||||
AutoLock lock(session_metrics_lock_);
|
||||
session_metrics_list_.erase(
|
||||
std::remove_if(session_metrics_list_.begin(),
|
||||
session_metrics_list_.end(),
|
||||
CompareSessionIds(session_id)),
|
||||
session_metrics_list_.end());
|
||||
}
|
||||
|
||||
void EngineMetrics::Serialize(drm_metrics::MetricsGroup* metric_group,
|
||||
bool completed_only,
|
||||
bool clear_serialized_sessions) {
|
||||
AutoLock lock(session_metrics_lock_);
|
||||
|
||||
// Serialize the most recent metrics from the OemCyrpto dynamic adapter.
|
||||
OemCryptoDynamicAdapterMetrics& adapter_metrics =
|
||||
GetDynamicAdapterMetricsInstance();
|
||||
adapter_metrics.Serialize(metric_group);
|
||||
if (!app_package_name_.empty()) {
|
||||
metric_group->set_app_package_name(app_package_name_);
|
||||
}
|
||||
SerializeEngineMetrics(metric_group);
|
||||
std::vector<SessionMetrics*>::iterator i;
|
||||
for (i = session_metrics_list_.begin(); i != session_metrics_list_.end();
|
||||
/* no increment */) {
|
||||
bool serialized = false;
|
||||
if (!completed_only || (*i)->IsCompleted()) {
|
||||
MetricsGroup* metric_sub_group = metric_group->add_metric_sub_group();
|
||||
if (!app_package_name_.empty()) {
|
||||
metric_sub_group->set_app_package_name(app_package_name_);
|
||||
}
|
||||
(*i)->Serialize(metric_sub_group);
|
||||
serialized = true;
|
||||
}
|
||||
|
||||
// Clear the serialized session metrics if requested.
|
||||
if (serialized && clear_serialized_sessions) {
|
||||
session_metrics_list_.erase(i);
|
||||
} else {
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void EngineMetrics::SetAppPackageName(const std::string& app_package_name) {
|
||||
app_package_name_ = app_package_name;
|
||||
}
|
||||
|
||||
void EngineMetrics::SerializeEngineMetrics(MetricsGroup* metric_group) {
|
||||
ProtoMetricSerializer serializer(metric_group);
|
||||
cdm_engine_add_key_.Serialize(&serializer);
|
||||
cdm_engine_cdm_version_.Serialize(&serializer);
|
||||
cdm_engine_close_session_.Serialize(&serializer);
|
||||
cdm_engine_creation_time_millis_.Serialize(&serializer);
|
||||
cdm_engine_decrypt_.Serialize(&serializer);
|
||||
cdm_engine_find_session_for_key_.Serialize(&serializer);
|
||||
cdm_engine_generate_key_request_.Serialize(&serializer);
|
||||
cdm_engine_get_provisioning_request_.Serialize(&serializer);
|
||||
cdm_engine_get_usage_info_.Serialize(&serializer);
|
||||
cdm_engine_handle_provisioning_response_.Serialize(&serializer);
|
||||
cdm_engine_life_span_.Serialize(&serializer);
|
||||
cdm_engine_open_key_set_session_.Serialize(&serializer);
|
||||
cdm_engine_open_session_.Serialize(&serializer);
|
||||
cdm_engine_query_key_status_.Serialize(&serializer);
|
||||
cdm_engine_release_all_usage_info_.Serialize(&serializer);
|
||||
cdm_engine_release_usage_info_.Serialize(&serializer);
|
||||
cdm_engine_remove_keys_.Serialize(&serializer);
|
||||
cdm_engine_restore_key_.Serialize(&serializer);
|
||||
cdm_engine_unprovision_.Serialize(&serializer);
|
||||
|
||||
crypto_metrics_.Serialize(metric_group);
|
||||
}
|
||||
|
||||
} // metrics
|
||||
} // wvcdm
|
||||
33
metrics/src/timer_metric.cpp
Normal file
33
metrics/src/timer_metric.cpp
Normal file
@@ -0,0 +1,33 @@
|
||||
#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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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_);
|
||||
}
|
||||
|
||||
} // namespace metrics
|
||||
} // namespace wvcdm
|
||||
115
metrics/src/value_metric.cpp
Normal file
115
metrics/src/value_metric.cpp
Normal file
@@ -0,0 +1,115 @@
|
||||
// Copyright 2017 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// This file contains the specializations for helper methods for the
|
||||
// ValueMetric class.
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string>
|
||||
|
||||
#include "value_metric.h"
|
||||
|
||||
#include "metrics_collections.h"
|
||||
#include "OEMCryptoCENC.h"
|
||||
#include "wv_cdm_types.h"
|
||||
|
||||
namespace wvcdm {
|
||||
namespace metrics {
|
||||
|
||||
// Private namespace for some helper implementation functions.
|
||||
namespace impl {
|
||||
|
||||
template<>
|
||||
void Serialize<int32_t>(MetricSerializer* serializer,
|
||||
const std::string& metric_name,
|
||||
const int32_t& value) {
|
||||
serializer->SetInt32(metric_name, value);
|
||||
}
|
||||
|
||||
template<>
|
||||
void Serialize<int64_t>(MetricSerializer* serializer,
|
||||
const std::string& metric_name,
|
||||
const int64_t& value) {
|
||||
serializer->SetInt64(metric_name, value);
|
||||
}
|
||||
|
||||
// This specialization forces the uint32_t to an int32_t.
|
||||
template<>
|
||||
void Serialize<uint32_t>(MetricSerializer* serializer,
|
||||
const std::string& metric_name,
|
||||
const uint32_t& value) {
|
||||
serializer->SetInt32(metric_name, value);
|
||||
}
|
||||
|
||||
// This specialization forces the uint32_t to an int64_t.
|
||||
template<>
|
||||
void Serialize<uint64_t>(MetricSerializer* serializer,
|
||||
const std::string& metric_name,
|
||||
const uint64_t& value) {
|
||||
serializer->SetInt64(metric_name, value);
|
||||
}
|
||||
|
||||
// This specialization forces a bool to an int32_t.
|
||||
template<>
|
||||
void Serialize<bool>(MetricSerializer* serializer,
|
||||
const std::string& metric_name,
|
||||
const bool& value) {
|
||||
serializer->SetInt32(metric_name, value);
|
||||
}
|
||||
|
||||
// This specialization forces an unsigned short to an int32_t.
|
||||
template<>
|
||||
void Serialize<unsigned short>(MetricSerializer* serializer,
|
||||
const std::string& metric_name,
|
||||
const unsigned short& value) {
|
||||
serializer->SetInt32(metric_name, value);
|
||||
}
|
||||
|
||||
template<>
|
||||
void Serialize<std::string>(MetricSerializer* serializer,
|
||||
const std::string& metric_name,
|
||||
const std::string& value) {
|
||||
serializer->SetString(metric_name, value);
|
||||
}
|
||||
|
||||
template<>
|
||||
void Serialize<double>(MetricSerializer* serializer,
|
||||
const std::string& metric_name,
|
||||
const double& value) {
|
||||
serializer->SetDouble(metric_name, value);
|
||||
}
|
||||
|
||||
// These specializations force CDM-specific types to int32_t
|
||||
template<>
|
||||
void Serialize<CdmSecurityLevel>(MetricSerializer* serializer,
|
||||
const std::string& metric_name,
|
||||
const CdmSecurityLevel& value) {
|
||||
serializer->SetInt32(metric_name, value);
|
||||
}
|
||||
|
||||
template<>
|
||||
void Serialize<OEMCrypto_HDCP_Capability>(
|
||||
MetricSerializer* serializer,
|
||||
const std::string& metric_name,
|
||||
const OEMCrypto_HDCP_Capability& value) {
|
||||
serializer->SetInt32(metric_name, value);
|
||||
}
|
||||
|
||||
template<>
|
||||
void Serialize<OEMCrypto_ProvisioningMethod>(
|
||||
MetricSerializer* serializer,
|
||||
const std::string& metric_name,
|
||||
const OEMCrypto_ProvisioningMethod& value) {
|
||||
serializer->SetInt32(metric_name, value);
|
||||
}
|
||||
|
||||
template<>
|
||||
void Serialize<OEMCryptoInitializationMode>(
|
||||
MetricSerializer* serializer,
|
||||
const std::string& metric_name,
|
||||
const OEMCryptoInitializationMode& value) {
|
||||
serializer->SetInt32(metric_name, value);
|
||||
}
|
||||
|
||||
} // namespace impl
|
||||
} // namespace metrics
|
||||
} // namespace wvcdm
|
||||
177
metrics/test/counter_metric_unittest.cpp
Normal file
177
metrics/test/counter_metric_unittest.cpp
Normal file
@@ -0,0 +1,177 @@
|
||||
// Copyright 2017 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Unit tests for CounterMetric
|
||||
|
||||
#include "counter_metric.h"
|
||||
|
||||
#include "gmock/gmock.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "metric_serialization.h"
|
||||
#include "scoped_ptr.h"
|
||||
|
||||
using testing::IsNull;
|
||||
using testing::NotNull;
|
||||
|
||||
namespace wvcdm {
|
||||
namespace metrics {
|
||||
|
||||
class MockCounterMetricSerializer : public MetricSerializer {
|
||||
public:
|
||||
MOCK_METHOD2(SetString, void(const std::string& metric_id,
|
||||
const std::string& value));
|
||||
MOCK_METHOD2(SetInt32, void(const std::string& metric_id,
|
||||
int32_t value));
|
||||
MOCK_METHOD2(SetInt64, void(const std::string& metric_id,
|
||||
int64_t value));
|
||||
MOCK_METHOD2(SetDouble, void(const std::string& metric_id,
|
||||
double value));
|
||||
};
|
||||
|
||||
class CounterMetricTest : public ::testing::Test {
|
||||
public:
|
||||
void SetUp() {
|
||||
mock_serializer_.reset(new MockCounterMetricSerializer());
|
||||
}
|
||||
protected:
|
||||
template<typename F1,
|
||||
typename F2,
|
||||
typename F3,
|
||||
typename F4>
|
||||
const std::map<std::string, int64_t>
|
||||
GetValueMap(
|
||||
const wvcdm::metrics::CounterMetric<F1, F2, F3, F4>&
|
||||
metric) {
|
||||
return metric.value_map_;
|
||||
}
|
||||
|
||||
scoped_ptr<MockCounterMetricSerializer> mock_serializer_;
|
||||
};
|
||||
|
||||
TEST_F(CounterMetricTest, NoFieldsSuccessNullCallback) {
|
||||
wvcdm::metrics::CounterMetric<> metric("no/fields/metric");
|
||||
metric.Increment();
|
||||
metric.Increment(10);
|
||||
|
||||
std::map<std::string, int64_t> value_map = GetValueMap(metric);
|
||||
ASSERT_EQ(1u, GetValueMap(metric).size());
|
||||
EXPECT_EQ(11, value_map.begin()->second);
|
||||
EXPECT_EQ("", value_map.begin()->first);
|
||||
}
|
||||
|
||||
TEST_F(CounterMetricTest, NoFieldsSuccessWithCallback) {
|
||||
wvcdm::metrics::CounterMetric<> metric("no/fields/metric");
|
||||
EXPECT_CALL(*mock_serializer_,
|
||||
SetInt64("no/fields/metric/count", 11));
|
||||
|
||||
metric.Increment();
|
||||
metric.Increment(10);
|
||||
metric.Serialize(mock_serializer_.get());
|
||||
|
||||
std::map<std::string, int64_t> value_map = GetValueMap(metric);
|
||||
ASSERT_EQ(1u, GetValueMap(metric).size());
|
||||
EXPECT_EQ(11, value_map.begin()->second);
|
||||
EXPECT_EQ("", value_map.begin()->first);
|
||||
}
|
||||
|
||||
TEST_F(CounterMetricTest, NoFieldsSuccessSingleIncrementWithCallback) {
|
||||
wvcdm::metrics::CounterMetric<> metric("no/fields/metric");
|
||||
EXPECT_CALL(*mock_serializer_,
|
||||
SetInt64("no/fields/metric/count", 1));
|
||||
|
||||
metric.Increment();
|
||||
metric.Serialize(mock_serializer_.get());
|
||||
|
||||
std::map<std::string, int64_t> value_map = GetValueMap(metric);
|
||||
ASSERT_EQ(1u, GetValueMap(metric).size());
|
||||
EXPECT_EQ(1, value_map.begin()->second);
|
||||
EXPECT_EQ("", value_map.begin()->first);
|
||||
}
|
||||
|
||||
TEST_F(CounterMetricTest, OneFieldSuccessNoCallback) {
|
||||
wvcdm::metrics::CounterMetric<int> metric(
|
||||
"single/fields/metric",
|
||||
"error_code");
|
||||
metric.Increment(7);
|
||||
metric.Increment(10, 7);
|
||||
metric.Increment(13);
|
||||
metric.Increment(20, 13);
|
||||
std::map<std::string, int64_t> value_map = GetValueMap(metric);
|
||||
ASSERT_EQ(2u, GetValueMap(metric).size());
|
||||
|
||||
// Verify both instances.
|
||||
EXPECT_EQ(11, value_map["{error_code:7}"]);
|
||||
EXPECT_EQ(21, value_map["{error_code:13}"]);
|
||||
}
|
||||
|
||||
TEST_F(CounterMetricTest, TwoFieldsSuccess) {
|
||||
wvcdm::metrics::CounterMetric<int, int> metric(
|
||||
"two/fields/metric",
|
||||
"error_code",
|
||||
"size");
|
||||
metric.Increment(7, 23);
|
||||
metric.Increment(2, 7, 29);
|
||||
metric.Increment(3, 11, 23);
|
||||
metric.Increment(4, 11, 29);
|
||||
metric.Increment(5, 7, 23);
|
||||
metric.Increment(-5, 7, 29);
|
||||
|
||||
std::map<std::string, int64_t> value_map = GetValueMap(metric);
|
||||
ASSERT_EQ(4u, GetValueMap(metric).size());
|
||||
|
||||
// Verify all instances.
|
||||
EXPECT_EQ(6, value_map["{error_code:7&size:23}"]);
|
||||
EXPECT_EQ(-3, value_map["{error_code:7&size:29}"]);
|
||||
EXPECT_EQ(3, value_map["{error_code:11&size:23}"]);
|
||||
EXPECT_EQ(4, value_map["{error_code:11&size:29}"]);
|
||||
|
||||
// Verify that a non-existent distribution returns default 0
|
||||
EXPECT_EQ(0, value_map["error_code:1,size:1"]);
|
||||
}
|
||||
|
||||
TEST_F(CounterMetricTest, TwoFieldsSuccessWithCallback) {
|
||||
wvcdm::metrics::CounterMetric<int, std::string> metric("two/fields/metric",
|
||||
"error_code",
|
||||
"stringval");
|
||||
|
||||
EXPECT_CALL(
|
||||
*mock_serializer_,
|
||||
SetInt64("two/fields/metric/count{error_code:11&stringval:foo}", 5));
|
||||
metric.Increment(11, "foo");
|
||||
metric.Increment(4, 11, "foo");
|
||||
metric.Serialize(mock_serializer_.get());
|
||||
}
|
||||
|
||||
TEST_F(CounterMetricTest, ThreeFieldsSuccess) {
|
||||
wvcdm::metrics::CounterMetric<int, int, bool> metric(
|
||||
"three/fields/metric",
|
||||
"error_code",
|
||||
"size",
|
||||
"woke up happy");
|
||||
metric.Increment(7, 13, false);
|
||||
|
||||
std::map<std::string, int64_t> value_map = GetValueMap(metric);
|
||||
ASSERT_EQ(1u, GetValueMap(metric).size());
|
||||
EXPECT_EQ("{error_code:7&size:13&woke up happy:0}",
|
||||
value_map.begin()->first);
|
||||
EXPECT_EQ(1, value_map.begin()->second);
|
||||
}
|
||||
|
||||
TEST_F(CounterMetricTest, FourFieldsSuccess) {
|
||||
wvcdm::metrics::CounterMetric<int, int, bool, std::string> metric(
|
||||
"Four/fields/metric",
|
||||
"error_code",
|
||||
"size",
|
||||
"woke up happy",
|
||||
"horoscope");
|
||||
metric.Increment(10LL, 7, 13, true, "find your true love");
|
||||
|
||||
std::map<std::string, int64_t> value_map = GetValueMap(metric);
|
||||
ASSERT_EQ(1u, GetValueMap(metric).size());
|
||||
EXPECT_EQ(
|
||||
"{error_code:7&size:13&woke up happy:1&horoscope:find your true love}",
|
||||
value_map.begin()->first);
|
||||
EXPECT_EQ(10, value_map.begin()->second);
|
||||
}
|
||||
|
||||
} // namespace metrics
|
||||
} // namespace wvcdm
|
||||
47
metrics/test/distribution_unittest.cpp
Normal file
47
metrics/test/distribution_unittest.cpp
Normal file
@@ -0,0 +1,47 @@
|
||||
// Copyright 2017 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Unit tests for Distribution.
|
||||
|
||||
#include <float.h>
|
||||
|
||||
#include "distribution.h"
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
namespace wvcdm {
|
||||
namespace metrics {
|
||||
|
||||
TEST(DistributionTest, NoValuesRecorded) {
|
||||
Distribution distribution;
|
||||
EXPECT_EQ(DBL_MAX, distribution.Min());
|
||||
EXPECT_EQ(-DBL_MAX, distribution.Max());
|
||||
EXPECT_EQ(0, distribution.Mean());
|
||||
EXPECT_EQ(0, distribution.Count());
|
||||
EXPECT_EQ(0, distribution.Variance());
|
||||
}
|
||||
|
||||
TEST(DistributionTest, OneValueRecorded) {
|
||||
Distribution distribution;
|
||||
distribution.Record(5.0);
|
||||
EXPECT_EQ(5, distribution.Min());
|
||||
EXPECT_EQ(5, distribution.Max());
|
||||
EXPECT_EQ(5, distribution.Mean());
|
||||
EXPECT_EQ(1, distribution.Count());
|
||||
EXPECT_EQ(0, distribution.Variance());
|
||||
}
|
||||
|
||||
TEST(DistributionTest, MultipleValuesRecorded) {
|
||||
Distribution distribution;
|
||||
distribution.Record(5.0);
|
||||
distribution.Record(10.0);
|
||||
distribution.Record(15.0);
|
||||
EXPECT_EQ(5, distribution.Min());
|
||||
EXPECT_EQ(15, distribution.Max());
|
||||
EXPECT_EQ(10, distribution.Mean());
|
||||
EXPECT_EQ(3, distribution.Count());
|
||||
EXPECT_NEAR(16.6667, distribution.Variance(), 0.0001);
|
||||
}
|
||||
|
||||
} // namespace metrics
|
||||
} // namespace wvcdm
|
||||
|
||||
242
metrics/test/event_metric_unittest.cpp
Normal file
242
metrics/test/event_metric_unittest.cpp
Normal file
@@ -0,0 +1,242 @@
|
||||
// Copyright 2017 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Unit tests for EventMetric
|
||||
|
||||
#include "event_metric.h"
|
||||
#include "metric_serialization.h"
|
||||
|
||||
#include "gmock/gmock.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "scoped_ptr.h"
|
||||
|
||||
using testing::IsNull;
|
||||
using testing::NotNull;
|
||||
|
||||
namespace wvcdm {
|
||||
namespace metrics {
|
||||
|
||||
class MockEventMetricSerializer : public MetricSerializer {
|
||||
public:
|
||||
MOCK_METHOD2(SetString, void(const std::string& metric_id,
|
||||
const std::string& value));
|
||||
MOCK_METHOD2(SetInt32, void(const std::string& metric_id,
|
||||
int32_t value));
|
||||
MOCK_METHOD2(SetInt64, void(const std::string& metric_id,
|
||||
int64_t value));
|
||||
MOCK_METHOD2(SetDouble, void(const std::string& metric_id,
|
||||
double value));
|
||||
};
|
||||
|
||||
class EventMetricTest : public ::testing::Test {
|
||||
public:
|
||||
void SetUp() {
|
||||
mock_serializer_.reset(new MockEventMetricSerializer());
|
||||
}
|
||||
protected:
|
||||
template<typename F1,
|
||||
typename F2,
|
||||
typename F3,
|
||||
typename F4>
|
||||
const std::map<std::string, Distribution*>
|
||||
GetValueMap(
|
||||
const wvcdm::metrics::EventMetric<F1, F2, F3, F4>&
|
||||
metric) {
|
||||
return metric.value_map_;
|
||||
}
|
||||
|
||||
scoped_ptr<MockEventMetricSerializer> mock_serializer_;
|
||||
};
|
||||
|
||||
TEST_F(EventMetricTest, NoFieldsSuccessNullCallback) {
|
||||
wvcdm::metrics::EventMetric<> metric("no/fields/metric");
|
||||
metric.Record(10LL);
|
||||
metric.Record(10LL);
|
||||
|
||||
std::map<std::string, Distribution*> value_map = GetValueMap(metric);
|
||||
ASSERT_EQ(1u, GetValueMap(metric).size());
|
||||
EXPECT_EQ(2, value_map.begin()->second->Count());
|
||||
EXPECT_EQ("", value_map.begin()->first);
|
||||
}
|
||||
|
||||
TEST_F(EventMetricTest, NoFieldsSuccessWithCallback) {
|
||||
wvcdm::metrics::EventMetric<> metric("no/fields/metric");
|
||||
EXPECT_CALL(*mock_serializer_,
|
||||
SetInt64("no/fields/metric/count", 2));
|
||||
EXPECT_CALL(*mock_serializer_,
|
||||
SetDouble("no/fields/metric/mean", 10.0));
|
||||
EXPECT_CALL(*mock_serializer_,
|
||||
SetDouble("no/fields/metric/variance", 0.0));
|
||||
EXPECT_CALL(*mock_serializer_,
|
||||
SetDouble("no/fields/metric/min", 10.0));
|
||||
EXPECT_CALL(*mock_serializer_,
|
||||
SetDouble("no/fields/metric/max", 10.0));
|
||||
|
||||
metric.Record(10);
|
||||
metric.Record(10);
|
||||
metric.Serialize(mock_serializer_.get());
|
||||
|
||||
std::map<std::string, Distribution*> value_map = GetValueMap(metric);
|
||||
ASSERT_EQ(1u, GetValueMap(metric).size());
|
||||
EXPECT_EQ(2, value_map.begin()->second->Count());
|
||||
EXPECT_EQ("", value_map.begin()->first);
|
||||
}
|
||||
|
||||
TEST_F(EventMetricTest, NoFieldsSuccessSingleRecordWithCallback) {
|
||||
wvcdm::metrics::EventMetric<> metric("no/fields/metric");
|
||||
EXPECT_CALL(*mock_serializer_,
|
||||
SetInt64("no/fields/metric/count", 1.0));
|
||||
EXPECT_CALL(*mock_serializer_,
|
||||
SetDouble("no/fields/metric/mean", 10.0));
|
||||
|
||||
metric.Record(10);
|
||||
metric.Serialize(mock_serializer_.get());
|
||||
|
||||
std::map<std::string, Distribution*> value_map = GetValueMap(metric);
|
||||
ASSERT_EQ(1u, GetValueMap(metric).size());
|
||||
EXPECT_EQ(1, value_map.begin()->second->Count());
|
||||
EXPECT_EQ("", value_map.begin()->first);
|
||||
}
|
||||
|
||||
TEST_F(EventMetricTest, OneFieldSuccessNoCallback) {
|
||||
wvcdm::metrics::EventMetric<int> metric(
|
||||
"single/fields/metric",
|
||||
"error_code");
|
||||
metric.Record(10LL, 7);
|
||||
metric.Record(11LL, 13);
|
||||
metric.Record(12LL, 13);
|
||||
std::map<std::string, Distribution*> value_map = GetValueMap(metric);
|
||||
ASSERT_EQ(2u, GetValueMap(metric).size());
|
||||
|
||||
// Verify both instances.
|
||||
// TODO(blueeyes): Export MakeFieldNameString so it can be used here.
|
||||
Distribution* distribution_error_7 = value_map["{error_code:7}"];
|
||||
Distribution* distribution_error_13 = value_map["{error_code:13}"];
|
||||
ASSERT_THAT(distribution_error_7, NotNull());
|
||||
ASSERT_THAT(distribution_error_13, NotNull());
|
||||
|
||||
EXPECT_EQ(1, distribution_error_7->Count());
|
||||
EXPECT_EQ(2, distribution_error_13->Count());
|
||||
}
|
||||
|
||||
TEST_F(EventMetricTest, TwoFieldsSuccess) {
|
||||
wvcdm::metrics::EventMetric<int, int> metric(
|
||||
"two/fields/metric",
|
||||
"error_code",
|
||||
"size");
|
||||
metric.Record(1, 7, 23);
|
||||
metric.Record(2, 7, 29);
|
||||
metric.Record(3, 11, 23);
|
||||
metric.Record(4, 11, 29);
|
||||
metric.Record(5, 7, 23);
|
||||
|
||||
std::map<std::string, Distribution*> value_map = GetValueMap(metric);
|
||||
ASSERT_EQ(4u, GetValueMap(metric).size());
|
||||
|
||||
// Verify all instances.
|
||||
Distribution* distribution_7_23 = value_map["{error_code:7&size:23}"];
|
||||
Distribution* distribution_7_29 = value_map["{error_code:7&size:29}"];
|
||||
Distribution* distribution_11_23 = value_map["{error_code:11&size:23}"];
|
||||
Distribution* distribution_11_29 = value_map["{error_code:11&size:29}"];
|
||||
ASSERT_THAT(distribution_7_23, NotNull());
|
||||
ASSERT_THAT(distribution_7_29, NotNull());
|
||||
ASSERT_THAT(distribution_11_23, NotNull());
|
||||
ASSERT_THAT(distribution_11_29, NotNull());
|
||||
|
||||
EXPECT_EQ(2, distribution_7_23->Count());
|
||||
EXPECT_EQ(1, distribution_7_29->Count());
|
||||
EXPECT_EQ(1, distribution_11_23->Count());
|
||||
EXPECT_EQ(1, distribution_11_29->Count());
|
||||
|
||||
// Verify that a non-existent distribution returns nullptr.
|
||||
Distribution* null_distribution = value_map["error_code:1,size:1"];
|
||||
ASSERT_THAT(null_distribution, IsNull());
|
||||
}
|
||||
|
||||
TEST_F(EventMetricTest, TwoFieldsSuccessWithCallback) {
|
||||
wvcdm::metrics::EventMetric<int, Pow2Bucket> metric("two/fields/metric",
|
||||
"error_code",
|
||||
"pow2_size");
|
||||
|
||||
// Callbacks from second record operation.
|
||||
EXPECT_CALL(
|
||||
*mock_serializer_,
|
||||
SetInt64("two/fields/metric/count{error_code:11&pow2_size:16}", 2.0));
|
||||
EXPECT_CALL(
|
||||
*mock_serializer_,
|
||||
SetDouble("two/fields/metric/mean{error_code:11&pow2_size:16}", 3.5));
|
||||
EXPECT_CALL(
|
||||
*mock_serializer_,
|
||||
SetDouble("two/fields/metric/variance{error_code:11&pow2_size:16}",
|
||||
0.25));
|
||||
EXPECT_CALL(
|
||||
*mock_serializer_,
|
||||
SetDouble("two/fields/metric/min{error_code:11&pow2_size:16}", 3.0));
|
||||
EXPECT_CALL(
|
||||
*mock_serializer_,
|
||||
SetDouble("two/fields/metric/max{error_code:11&pow2_size:16}", 4.0));
|
||||
metric.Record(3, 11, Pow2Bucket(29));
|
||||
metric.Record(4, 11, Pow2Bucket(29));
|
||||
metric.Serialize(mock_serializer_.get());
|
||||
}
|
||||
|
||||
TEST_F(EventMetricTest, ThreeFieldsSuccess) {
|
||||
wvcdm::metrics::EventMetric<int, int, bool> metric(
|
||||
"three/fields/metric",
|
||||
"error_code",
|
||||
"size",
|
||||
"woke up happy");
|
||||
metric.Record(10LL, 7, 13, false);
|
||||
|
||||
|
||||
std::map<std::string, Distribution*> value_map = GetValueMap(metric);
|
||||
ASSERT_EQ(1u, GetValueMap(metric).size());
|
||||
EXPECT_EQ("{error_code:7&size:13&woke up happy:0}",
|
||||
value_map.begin()->first);
|
||||
EXPECT_EQ(1, value_map.begin()->second->Count());
|
||||
}
|
||||
|
||||
TEST_F(EventMetricTest, FourFieldsSuccess) {
|
||||
wvcdm::metrics::EventMetric<int, int, bool, std::string> metric(
|
||||
"Four/fields/metric",
|
||||
"error_code",
|
||||
"size",
|
||||
"woke up happy",
|
||||
"horoscope");
|
||||
metric.Record(10LL, 7, 13, true, "find your true love");
|
||||
|
||||
std::map<std::string, Distribution*> value_map = GetValueMap(metric);
|
||||
ASSERT_EQ(1u, GetValueMap(metric).size());
|
||||
EXPECT_EQ(
|
||||
"{error_code:7&size:13&woke up happy:1&horoscope:find your true love}",
|
||||
value_map.begin()->first);
|
||||
EXPECT_EQ(1, value_map.begin()->second->Count());
|
||||
}
|
||||
|
||||
TEST_F(EventMetricTest, Pow2BucketTest) {
|
||||
std::stringstream value;
|
||||
value << Pow2Bucket(1);
|
||||
EXPECT_EQ("1", value.str());
|
||||
|
||||
value.str("");
|
||||
value << Pow2Bucket(0);
|
||||
EXPECT_EQ("0", value.str());
|
||||
|
||||
value.str("");
|
||||
value << Pow2Bucket(2);
|
||||
EXPECT_EQ("2", value.str());
|
||||
|
||||
value.str("");
|
||||
value << Pow2Bucket(0xFFFFFFFFu);
|
||||
EXPECT_EQ("2147483648", value.str());
|
||||
|
||||
value.str("");
|
||||
value << Pow2Bucket(0x80000000u);
|
||||
EXPECT_EQ("2147483648", value.str());
|
||||
|
||||
value.str("");
|
||||
value << Pow2Bucket(0x7FFFFFFF);
|
||||
EXPECT_EQ("1073741824", value.str());
|
||||
}
|
||||
|
||||
} // namespace metrics
|
||||
} // namespace wvcdm
|
||||
445
metrics/test/metrics_collections_test.cpp
Normal file
445
metrics/test/metrics_collections_test.cpp
Normal file
@@ -0,0 +1,445 @@
|
||||
// Copyright 2017 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Unit tests for the metrics collections,
|
||||
// EngineMetrics, SessionMetrics and CrytpoMetrics.
|
||||
|
||||
#include "metrics_collections.h"
|
||||
|
||||
#include <sstream>
|
||||
|
||||
#include "gmock/gmock.h"
|
||||
#include "google/protobuf/text_format.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "log.h"
|
||||
#include "metrics.pb.h"
|
||||
#include "wv_cdm_types.h"
|
||||
|
||||
using drm_metrics::MetricsGroup;
|
||||
using google::protobuf::TextFormat;
|
||||
|
||||
namespace wvcdm {
|
||||
namespace metrics {
|
||||
|
||||
// TODO(blueeyes): Improve this implementation by supporting full message
|
||||
// API In CDM. That allows us to use MessageDifferencer.
|
||||
class EngineMetricsTest : public ::testing::Test {
|
||||
};
|
||||
|
||||
TEST_F(EngineMetricsTest, AllEngineMetrics) {
|
||||
EngineMetrics engine_metrics;
|
||||
|
||||
// Set some values in all of the engine metrics.
|
||||
engine_metrics.cdm_engine_add_key_.Record(1.0, KEY_ADDED);
|
||||
engine_metrics.cdm_engine_close_session_.Record(1.0, NO_ERROR);
|
||||
engine_metrics.cdm_engine_decrypt_.Record(1.0, NO_ERROR);
|
||||
engine_metrics.cdm_engine_find_session_for_key_.Record(1.0, false);
|
||||
engine_metrics.cdm_engine_generate_key_request_.Record(1.0, NO_ERROR);
|
||||
engine_metrics.cdm_engine_get_provisioning_request_.Record(1.0, NO_ERROR);
|
||||
engine_metrics.cdm_engine_get_usage_info_.Record(1.0, NO_ERROR);
|
||||
engine_metrics.cdm_engine_handle_provisioning_response_.Record(1.0, NO_ERROR);
|
||||
engine_metrics.cdm_engine_life_span_.Record(1.0);
|
||||
engine_metrics.cdm_engine_open_key_set_session_.Record(1.0, NO_ERROR);
|
||||
engine_metrics.cdm_engine_open_session_.Record(1.0, NO_ERROR);
|
||||
engine_metrics.cdm_engine_query_key_status_.Record(1.0, NO_ERROR);
|
||||
engine_metrics.cdm_engine_release_all_usage_info_.Record(1.0, NO_ERROR);
|
||||
engine_metrics.cdm_engine_release_usage_info_.Record(1.0, NO_ERROR);
|
||||
engine_metrics.cdm_engine_remove_keys_.Record(1.0, NO_ERROR);
|
||||
engine_metrics.cdm_engine_restore_key_.Record(1.0, NO_ERROR);
|
||||
engine_metrics.cdm_engine_unprovision_.Record(1.0, NO_ERROR, kSecurityLevelL1);
|
||||
|
||||
drm_metrics::MetricsGroup actual_metrics;
|
||||
engine_metrics.Serialize(&actual_metrics, true, false);
|
||||
|
||||
// For each EventMetric, 2 metrics get serialized since only one sample was
|
||||
// taken. So, the total number of serialized metrics are 2*17.
|
||||
ASSERT_EQ(2 * 17, actual_metrics.metric_size());
|
||||
EXPECT_EQ(0, actual_metrics.metric_sub_group_size());
|
||||
|
||||
// Spot check some metrics.
|
||||
EXPECT_EQ("/drm/widevine/cdm_engine/add_key/time/count{error:2}",
|
||||
actual_metrics.metric(0).name());
|
||||
EXPECT_EQ("/drm/widevine/cdm_engine/close_session/time/mean{error:0}",
|
||||
actual_metrics.metric(3).name());
|
||||
EXPECT_EQ("/drm/widevine/cdm_engine/decrypt/time/mean{error:0}",
|
||||
actual_metrics.metric(5).name());
|
||||
EXPECT_EQ(1.0, actual_metrics.metric(5).value().double_value());
|
||||
}
|
||||
|
||||
TEST_F(EngineMetricsTest, EngineAndCryptoMetrics) {
|
||||
EngineMetrics engine_metrics;
|
||||
|
||||
// Set some values in some of the engine metrics and some crypto metrics.
|
||||
engine_metrics.cdm_engine_add_key_.Record(1.0, KEY_ADDED);
|
||||
engine_metrics.cdm_engine_close_session_.Record(1.0, NO_ERROR);
|
||||
CryptoMetrics* crypto_metrics = engine_metrics.GetCryptoMetrics();
|
||||
|
||||
crypto_metrics->crypto_session_generic_decrypt_
|
||||
.Record(2.0, NO_ERROR, Pow2Bucket(1025), kEncryptionAlgorithmAesCbc128);
|
||||
crypto_metrics->crypto_session_get_device_unique_id_
|
||||
.Record(4.0, false);
|
||||
|
||||
drm_metrics::MetricsGroup actual_metrics;
|
||||
engine_metrics.Serialize(&actual_metrics, true, false);
|
||||
|
||||
// For each EventMetric, 2 metrics get serialized since only one sample was
|
||||
// taken. So, the total number of serialized metrics are 2*4 since we
|
||||
// touched 4 metrics.
|
||||
ASSERT_EQ(2 * 4, actual_metrics.metric_size());
|
||||
EXPECT_EQ(0, actual_metrics.metric_sub_group_size());
|
||||
|
||||
// Spot check some metrics.
|
||||
EXPECT_EQ("/drm/widevine/cdm_engine/add_key/time/count{error:2}",
|
||||
actual_metrics.metric(0).name());
|
||||
EXPECT_EQ(
|
||||
"/drm/widevine/crypto_session/generic_decrypt/time/count"
|
||||
"{error:0&length:1024&encryption_algorithm:1}",
|
||||
actual_metrics.metric(4).name());
|
||||
EXPECT_EQ(
|
||||
"/drm/widevine/crypto_session/get_device_unique_id/time/mean{success:0}",
|
||||
actual_metrics.metric(7).name());
|
||||
EXPECT_EQ(4.0, actual_metrics.metric(7).value().double_value());
|
||||
}
|
||||
|
||||
TEST_F(EngineMetricsTest, EmptyEngineMetrics) {
|
||||
EngineMetrics engine_metrics;
|
||||
|
||||
drm_metrics::MetricsGroup actual_metrics;
|
||||
engine_metrics.Serialize(&actual_metrics, true, false);
|
||||
|
||||
EXPECT_EQ(0, actual_metrics.metric_size());
|
||||
EXPECT_EQ(0, actual_metrics.metric_sub_group_size());
|
||||
}
|
||||
|
||||
TEST_F(EngineMetricsTest, EngineMetricsWithCompletedSessions) {
|
||||
EngineMetrics engine_metrics;
|
||||
|
||||
// Set a values in an engine metric and in a crypto metric.
|
||||
engine_metrics.cdm_engine_add_key_.Record(1.0, KEY_ADDED);
|
||||
engine_metrics.GetCryptoMetrics()
|
||||
->crypto_session_load_certificate_private_key_.Record(2.0, true);
|
||||
|
||||
// Create two sessions and record some metrics.
|
||||
SessionMetrics* session_metrics_1 = engine_metrics.AddSession();
|
||||
session_metrics_1->SetSessionId("session_id_1");
|
||||
SessionMetrics* session_metrics_2 = engine_metrics.AddSession();
|
||||
session_metrics_2->SetSessionId("session_id_2");
|
||||
// Record a CryptoMetrics metric in the session.
|
||||
session_metrics_2->GetCryptoMetrics()->crypto_session_generic_decrypt_
|
||||
.Record(2.0, NO_ERROR, Pow2Bucket(1025), kEncryptionAlgorithmAesCbc128);
|
||||
session_metrics_2->SetSessionId("session_id_2");
|
||||
// Mark only session 2 as completed.
|
||||
session_metrics_2->SetCompleted();
|
||||
|
||||
drm_metrics::MetricsGroup actual_metrics;
|
||||
engine_metrics.Serialize(&actual_metrics, true, false);
|
||||
|
||||
// Validate metric counts.
|
||||
// For each EventMetric, 2 metrics get serialized since only one sample was
|
||||
// taken. So, the total number of serialized metrics are 2*2 since we
|
||||
// touched 2 metrics.
|
||||
ASSERT_EQ(2 * 2, actual_metrics.metric_size());
|
||||
ASSERT_EQ(1, actual_metrics.metric_sub_group_size());
|
||||
ASSERT_EQ(3, actual_metrics.metric_sub_group(0).metric_size());
|
||||
|
||||
// Spot check some metrics.
|
||||
EXPECT_EQ("/drm/widevine/cdm_engine/add_key/time/count{error:2}",
|
||||
actual_metrics.metric(0).name());
|
||||
EXPECT_EQ("/drm/widevine/crypto_session/load_certificate_private_key"
|
||||
"/time/count{success:1}",
|
||||
actual_metrics.metric(2).name());
|
||||
EXPECT_EQ("/drm/widevine/cdm_session/session_id",
|
||||
actual_metrics.metric_sub_group(0).metric(0).name());
|
||||
EXPECT_EQ(
|
||||
"session_id_2",
|
||||
actual_metrics.metric_sub_group(0).metric(0).value().string_value());
|
||||
EXPECT_EQ(
|
||||
"/drm/widevine/crypto_session/generic_decrypt/time/count"
|
||||
"{error:0&length:1024&encryption_algorithm:1}",
|
||||
actual_metrics.metric_sub_group(0).metric(1).name());
|
||||
}
|
||||
|
||||
TEST_F(EngineMetricsTest, EngineMetricsSerializeAllSessions) {
|
||||
EngineMetrics engine_metrics;
|
||||
|
||||
// Create two sessions and record some metrics.
|
||||
SessionMetrics* session_metrics_1 = engine_metrics.AddSession();
|
||||
session_metrics_1->SetSessionId("session_id_1");
|
||||
SessionMetrics* session_metrics_2 = engine_metrics.AddSession();
|
||||
session_metrics_2->SetSessionId("session_id_2");
|
||||
// Mark only session 2 as completed.
|
||||
session_metrics_2->SetCompleted();
|
||||
|
||||
drm_metrics::MetricsGroup actual_metrics;
|
||||
engine_metrics.Serialize(&actual_metrics, false, false);
|
||||
|
||||
// Validate metric counts.
|
||||
// No Engine-level metrics were recorded.
|
||||
ASSERT_EQ(0, actual_metrics.metric_size());
|
||||
// Two sub groups, 1 per session.
|
||||
ASSERT_EQ(2, actual_metrics.metric_sub_group_size());
|
||||
ASSERT_EQ(1, actual_metrics.metric_sub_group(0).metric_size());
|
||||
|
||||
// Spot check some metrics.
|
||||
EXPECT_EQ("/drm/widevine/cdm_session/session_id",
|
||||
actual_metrics.metric_sub_group(0).metric(0).name());
|
||||
EXPECT_EQ(
|
||||
"session_id_1",
|
||||
actual_metrics.metric_sub_group(0).metric(0).value().string_value());
|
||||
EXPECT_EQ("/drm/widevine/cdm_session/session_id",
|
||||
actual_metrics.metric_sub_group(1).metric(0).name());
|
||||
EXPECT_EQ(
|
||||
"session_id_2",
|
||||
actual_metrics.metric_sub_group(1).metric(0).value().string_value());
|
||||
}
|
||||
|
||||
TEST_F(EngineMetricsTest, EngineMetricsRemoveSessions) {
|
||||
EngineMetrics engine_metrics;
|
||||
|
||||
// Create two sessions and record some metrics.
|
||||
SessionMetrics* session_metrics_1 = engine_metrics.AddSession();
|
||||
session_metrics_1->SetSessionId("session_id_1");
|
||||
SessionMetrics* session_metrics_2 = engine_metrics.AddSession();
|
||||
session_metrics_2->SetSessionId("session_id_2");
|
||||
// Mark only session 2 as completed.
|
||||
session_metrics_2->SetCompleted();
|
||||
|
||||
// Serialize all metrics, don't remove any.
|
||||
drm_metrics::MetricsGroup actual_metrics;
|
||||
engine_metrics.Serialize(&actual_metrics, false, false);
|
||||
|
||||
// Validate metric counts.
|
||||
// Two sub groups, 1 per session.
|
||||
ASSERT_EQ(2, actual_metrics.metric_sub_group_size());
|
||||
|
||||
// Serialize completed metrics, remove them.
|
||||
actual_metrics.Clear();
|
||||
engine_metrics.Serialize(&actual_metrics, true, true);
|
||||
// Validate metric counts.
|
||||
// Only one, completed session should exist.
|
||||
ASSERT_EQ(1, actual_metrics.metric_sub_group_size());
|
||||
ASSERT_EQ(1, actual_metrics.metric_sub_group(0).metric_size());
|
||||
EXPECT_EQ(
|
||||
"session_id_2",
|
||||
actual_metrics.metric_sub_group(0).metric(0).value().string_value());
|
||||
|
||||
// Serialize all metrics, remove them.
|
||||
actual_metrics.Clear();
|
||||
engine_metrics.Serialize(&actual_metrics, false, true);
|
||||
// Validate metric counts.
|
||||
// Only one, non-complete session should exist.
|
||||
ASSERT_EQ(1, actual_metrics.metric_sub_group_size());
|
||||
ASSERT_EQ(1, actual_metrics.metric_sub_group(0).metric_size());
|
||||
EXPECT_EQ(
|
||||
"session_id_1",
|
||||
actual_metrics.metric_sub_group(0).metric(0).value().string_value());
|
||||
|
||||
// Serialize all metrics, don't remove any.
|
||||
actual_metrics.Clear();
|
||||
engine_metrics.Serialize(&actual_metrics, false, false);
|
||||
// Validate metric counts.
|
||||
// There should be no more metric subgroups left.
|
||||
ASSERT_EQ(0, actual_metrics.metric_sub_group_size());
|
||||
}
|
||||
|
||||
class SessionMetricsTest : public ::testing::Test {
|
||||
};
|
||||
|
||||
TEST_F(SessionMetricsTest, AllSessionMetrics) {
|
||||
SessionMetrics session_metrics;
|
||||
session_metrics.SetSessionId("session_id 1");
|
||||
|
||||
session_metrics.cdm_session_life_span_.Record(1.0);
|
||||
session_metrics.cdm_session_renew_key_.Record(1.0, NO_ERROR);
|
||||
session_metrics.cdm_session_restore_offline_session_.Record(1.0, NO_ERROR);
|
||||
session_metrics.cdm_session_restore_usage_session_.Record(1.0, NO_ERROR);
|
||||
|
||||
// Record a CryptoMetrics metric in the session.
|
||||
session_metrics.GetCryptoMetrics()->crypto_session_generic_decrypt_
|
||||
.Record(2.0, NO_ERROR, Pow2Bucket(1025), kEncryptionAlgorithmAesCbc128);
|
||||
|
||||
MetricsGroup actual_metrics;
|
||||
session_metrics.Serialize(&actual_metrics);
|
||||
|
||||
ASSERT_EQ(11, actual_metrics.metric_size());
|
||||
EXPECT_EQ(0, actual_metrics.metric_sub_group_size());
|
||||
|
||||
// Spot check some metrics.
|
||||
EXPECT_EQ("/drm/widevine/cdm_session/session_id",
|
||||
actual_metrics.metric(0).name());
|
||||
EXPECT_EQ("/drm/widevine/cdm_session/life_span/time/count",
|
||||
actual_metrics.metric(1).name());
|
||||
EXPECT_EQ("/drm/widevine/cdm_session/renew_key/time/mean{error:0}",
|
||||
actual_metrics.metric(4).name());
|
||||
EXPECT_EQ(1.0, actual_metrics.metric(4).value().double_value());
|
||||
EXPECT_EQ("/drm/widevine/crypto_session/generic_decrypt/time/count"
|
||||
"{error:0&length:1024&encryption_algorithm:1}",
|
||||
actual_metrics.metric(9).name());
|
||||
}
|
||||
|
||||
TEST_F(SessionMetricsTest, EmptySessionMetrics) {
|
||||
SessionMetrics session_metrics;
|
||||
|
||||
MetricsGroup actual_metrics;
|
||||
session_metrics.Serialize(&actual_metrics);
|
||||
|
||||
// Session metric always has a session id.
|
||||
ASSERT_EQ(1, actual_metrics.metric_size());
|
||||
EXPECT_EQ("/drm/widevine/cdm_session/session_id",
|
||||
actual_metrics.metric(0).name());
|
||||
EXPECT_EQ("", actual_metrics.metric(0).value().string_value());
|
||||
EXPECT_EQ(0, actual_metrics.metric_sub_group_size());
|
||||
}
|
||||
|
||||
class CryptoMetricsTest : public ::testing::Test {
|
||||
};
|
||||
|
||||
TEST_F(CryptoMetricsTest, AllCryptoMetrics) {
|
||||
CryptoMetrics crypto_metrics;
|
||||
|
||||
// Crypto session metrics.
|
||||
crypto_metrics.crypto_session_delete_all_usage_reports_
|
||||
.Record(1.0, NO_ERROR);
|
||||
crypto_metrics.crypto_session_delete_multiple_usage_information_
|
||||
.Record(1.0, NO_ERROR);
|
||||
crypto_metrics.crypto_session_generic_decrypt_
|
||||
.Record(2.0, NO_ERROR, Pow2Bucket(1025), kEncryptionAlgorithmAesCbc128);
|
||||
crypto_metrics.crypto_session_generic_encrypt_
|
||||
.Record(2.0, NO_ERROR, Pow2Bucket(1025), kEncryptionAlgorithmAesCbc128);
|
||||
crypto_metrics.crypto_session_generic_sign_
|
||||
.Record(2.0, NO_ERROR, Pow2Bucket(1025), kSigningAlgorithmHmacSha256);
|
||||
crypto_metrics.crypto_session_generic_verify_
|
||||
.Record(2.0, NO_ERROR, Pow2Bucket(1025), kSigningAlgorithmHmacSha256);
|
||||
crypto_metrics.crypto_session_get_device_unique_id_.Record(1.0, true);
|
||||
crypto_metrics.crypto_session_get_security_level_
|
||||
.Record(1.0, kSecurityLevelL1);
|
||||
crypto_metrics.crypto_session_get_system_id_.Record(1.0, true, 1234);
|
||||
crypto_metrics.crypto_session_get_token_.Record(1.0, true);
|
||||
crypto_metrics.crypto_session_life_span_.Record(1.0);
|
||||
crypto_metrics.crypto_session_load_certificate_private_key_
|
||||
.Record(1.0, true);
|
||||
crypto_metrics.crypto_session_open_.Record(1.0, NO_ERROR, kLevelDefault);
|
||||
crypto_metrics.crypto_session_update_usage_information_
|
||||
.Record(1.0, NO_ERROR);
|
||||
crypto_metrics.crypto_session_usage_information_support_.Record(1.0, true);
|
||||
|
||||
// Oem crypto metrics.
|
||||
crypto_metrics.oemcrypto_api_version_.Record(1.0, 123, kLevelDefault);
|
||||
crypto_metrics.oemcrypto_close_session_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED);
|
||||
crypto_metrics.oemcrypto_copy_buffer_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED,
|
||||
kLevelDefault, Pow2Bucket(1025));
|
||||
crypto_metrics.oemcrypto_deactivate_usage_entry_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED);
|
||||
crypto_metrics.oemcrypto_decrypt_cenc_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED, Pow2Bucket(1025));
|
||||
crypto_metrics.oemcrypto_delete_usage_entry_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED);
|
||||
crypto_metrics.oemcrypto_delete_usage_table_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED);
|
||||
crypto_metrics.oemcrypto_derive_keys_from_session_key_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED);
|
||||
crypto_metrics.oemcrypto_force_delete_usage_entry_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED);
|
||||
crypto_metrics.oemcrypto_generate_derived_keys_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED);
|
||||
crypto_metrics.oemcrypto_generate_nonce_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED);
|
||||
crypto_metrics.oemcrypto_generate_rsa_signature_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED, Pow2Bucket(1025));
|
||||
crypto_metrics.oemcrypto_generate_signature_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED, Pow2Bucket(1025));
|
||||
crypto_metrics.oemcrypto_generic_decrypt_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED, Pow2Bucket(1025));
|
||||
crypto_metrics.oemcrypto_generic_encrypt_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED, Pow2Bucket(1025));
|
||||
crypto_metrics.oemcrypto_generic_sign_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED, Pow2Bucket(1025));
|
||||
crypto_metrics.oemcrypto_generic_verify_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED, Pow2Bucket(1025));
|
||||
crypto_metrics.oemcrypto_get_device_id_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED, kLevelDefault);
|
||||
crypto_metrics.oemcrypto_get_hdcp_capability_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED, kLevelDefault);
|
||||
crypto_metrics.oemcrypto_get_key_data_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED,
|
||||
Pow2Bucket(1025), kLevelDefault);
|
||||
crypto_metrics.oemcrypto_get_max_number_of_sessions_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED, kLevelDefault);
|
||||
crypto_metrics.oemcrypto_get_number_of_open_sessions_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED, kLevelDefault);
|
||||
crypto_metrics.oemcrypto_get_oem_public_certificate_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED);
|
||||
|
||||
crypto_metrics.oemcrypto_get_provisioning_method_
|
||||
.Record(1.0, OEMCrypto_Keybox, kLevelDefault);
|
||||
|
||||
crypto_metrics.oemcrypto_get_random_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED, Pow2Bucket(1025));
|
||||
crypto_metrics.oemcrypto_initialize_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED);
|
||||
crypto_metrics.oemcrypto_install_keybox_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED, kLevelDefault);
|
||||
crypto_metrics.oemcrypto_is_anti_rollback_hw_present_
|
||||
.Record(1.0, true, kLevelDefault);
|
||||
|
||||
crypto_metrics.oemcrypto_is_keybox_valid_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED, kLevelDefault);
|
||||
crypto_metrics.oemcrypto_load_device_rsa_key_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED);
|
||||
crypto_metrics.oemcrypto_load_keys_.Record(1.0, OEMCrypto_ERROR_INIT_FAILED);
|
||||
crypto_metrics.oemcrypto_load_test_keybox_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED);
|
||||
crypto_metrics.oemcrypto_load_test_rsa_key_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED);
|
||||
crypto_metrics.oemcrypto_open_session_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED, kLevelDefault);
|
||||
crypto_metrics.oemcrypto_refresh_keys_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED);
|
||||
crypto_metrics.oemcrypto_report_usage_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED);
|
||||
crypto_metrics.oemcrypto_rewrap_device_rsa_key_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED);
|
||||
crypto_metrics.oemcrypto_rewrap_device_rsa_key_30_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED);
|
||||
crypto_metrics.oemcrypto_security_level_
|
||||
.Record(1.0, kSecurityLevelL2, kLevelDefault);
|
||||
crypto_metrics.oemcrypto_security_patch_level_
|
||||
.Record(1.0, 123, kLevelDefault);
|
||||
crypto_metrics.oemcrypto_select_key_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED);
|
||||
crypto_metrics.oemcrypto_supports_usage_table_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED, kLevelDefault);
|
||||
crypto_metrics.oemcrypto_update_usage_table_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED);
|
||||
crypto_metrics.oemcrypto_wrap_keybox_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED);
|
||||
|
||||
// Internal OEMCrypto Metrics
|
||||
crypto_metrics.oemcrypto_initialization_mode_
|
||||
.Record(1.0, OEMCrypto_INITIALIZED_FORCING_L3);
|
||||
crypto_metrics.oemcrypto_l1_api_version_.Record(1.0, 12, 123);
|
||||
|
||||
MetricsGroup actual_metrics;
|
||||
crypto_metrics.Serialize(&actual_metrics);
|
||||
|
||||
// 61 EventMetric instances, 2 values each.
|
||||
ASSERT_EQ(122, actual_metrics.metric_size());
|
||||
|
||||
// Spot check some metrics.
|
||||
EXPECT_EQ(
|
||||
"/drm/widevine/crypto_session/delete_all_usage_reports/time/count"
|
||||
"{error:0}",
|
||||
actual_metrics.metric(0).name());
|
||||
EXPECT_EQ(1, actual_metrics.metric(0).value().int_value());
|
||||
EXPECT_EQ(
|
||||
"/drm/widevine/oemcrypto/l1_api_version/mean{version:12&min_version:123}",
|
||||
actual_metrics.metric(121).name());
|
||||
EXPECT_EQ(1.0, actual_metrics.metric(121).value().double_value());
|
||||
|
||||
// No subgroups should exist.
|
||||
EXPECT_EQ(0, actual_metrics.metric_sub_group_size());
|
||||
}
|
||||
|
||||
} // namespace metrics
|
||||
} // namespace wvcdm
|
||||
445
metrics/test/metrics_collections_unittest.cpp
Normal file
445
metrics/test/metrics_collections_unittest.cpp
Normal file
@@ -0,0 +1,445 @@
|
||||
// Copyright 2017 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Unit tests for the metrics collections,
|
||||
// EngineMetrics, SessionMetrics and CryptoMetrics.
|
||||
|
||||
#include "metrics_collections.h"
|
||||
|
||||
#include <sstream>
|
||||
|
||||
#include "gmock/gmock.h"
|
||||
#include "google/protobuf/text_format.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "log.h"
|
||||
#include "metrics.pb.h"
|
||||
#include "wv_cdm_types.h"
|
||||
|
||||
using drm_metrics::MetricsGroup;
|
||||
using google::protobuf::TextFormat;
|
||||
|
||||
namespace wvcdm {
|
||||
namespace metrics {
|
||||
|
||||
// TODO(blueeyes): Improve this implementation by supporting full message
|
||||
// API In CDM. That allows us to use MessageDifferencer.
|
||||
class EngineMetricsTest : public ::testing::Test {
|
||||
};
|
||||
|
||||
TEST_F(EngineMetricsTest, AllEngineMetrics) {
|
||||
EngineMetrics engine_metrics;
|
||||
|
||||
// Set some values in all of the engine metrics.
|
||||
engine_metrics.cdm_engine_add_key_.Record(1.0, KEY_ADDED);
|
||||
engine_metrics.cdm_engine_close_session_.Record(1.0, NO_ERROR);
|
||||
engine_metrics.cdm_engine_decrypt_.Record(1.0, NO_ERROR);
|
||||
engine_metrics.cdm_engine_find_session_for_key_.Record(1.0, false);
|
||||
engine_metrics.cdm_engine_generate_key_request_.Record(1.0, NO_ERROR);
|
||||
engine_metrics.cdm_engine_get_provisioning_request_.Record(1.0, NO_ERROR);
|
||||
engine_metrics.cdm_engine_get_usage_info_.Record(1.0, NO_ERROR);
|
||||
engine_metrics.cdm_engine_handle_provisioning_response_.Record(1.0, NO_ERROR);
|
||||
engine_metrics.cdm_engine_life_span_.Record(1.0);
|
||||
engine_metrics.cdm_engine_open_key_set_session_.Record(1.0, NO_ERROR);
|
||||
engine_metrics.cdm_engine_open_session_.Record(1.0, NO_ERROR);
|
||||
engine_metrics.cdm_engine_query_key_status_.Record(1.0, NO_ERROR);
|
||||
engine_metrics.cdm_engine_release_all_usage_info_.Record(1.0, NO_ERROR);
|
||||
engine_metrics.cdm_engine_release_usage_info_.Record(1.0, NO_ERROR);
|
||||
engine_metrics.cdm_engine_remove_keys_.Record(1.0, NO_ERROR);
|
||||
engine_metrics.cdm_engine_restore_key_.Record(1.0, NO_ERROR);
|
||||
engine_metrics.cdm_engine_unprovision_.Record(1.0, NO_ERROR, kSecurityLevelL1);
|
||||
|
||||
drm_metrics::MetricsGroup actual_metrics;
|
||||
engine_metrics.Serialize(&actual_metrics, true, false);
|
||||
|
||||
// For each EventMetric, 2 metrics get serialized since only one sample was
|
||||
// taken. So, the total number of serialized metrics are 2*17.
|
||||
ASSERT_EQ(2 * 17, actual_metrics.metric_size());
|
||||
EXPECT_EQ(0, actual_metrics.metric_sub_group_size());
|
||||
|
||||
// Spot check some metrics.
|
||||
EXPECT_EQ("/drm/widevine/cdm_engine/add_key/time/count{error:2}",
|
||||
actual_metrics.metric(0).name());
|
||||
EXPECT_EQ("/drm/widevine/cdm_engine/close_session/count{error:0}",
|
||||
actual_metrics.metric(3).name());
|
||||
EXPECT_EQ("/drm/widevine/cdm_engine/decrypt/time/mean{error:0}",
|
||||
actual_metrics.metric(5).name());
|
||||
EXPECT_EQ(1.0, actual_metrics.metric(5).value().double_value());
|
||||
}
|
||||
|
||||
TEST_F(EngineMetricsTest, EngineAndCryptoMetrics) {
|
||||
EngineMetrics engine_metrics;
|
||||
|
||||
// Set some values in some of the engine metrics and some crypto metrics.
|
||||
engine_metrics.cdm_engine_add_key_.Record(1.0, KEY_ADDED);
|
||||
engine_metrics.cdm_engine_close_session_.Record(1.0, NO_ERROR);
|
||||
CryptoMetrics* crypto_metrics = engine_metrics.GetCryptoMetrics();
|
||||
|
||||
crypto_metrics->crypto_session_generic_decrypt_
|
||||
.Record(2.0, NO_ERROR, Pow2Bucket(1025), kEncryptionAlgorithmAesCbc128);
|
||||
crypto_metrics->crypto_session_get_device_unique_id_
|
||||
.Record(4.0, false);
|
||||
|
||||
drm_metrics::MetricsGroup actual_metrics;
|
||||
engine_metrics.Serialize(&actual_metrics, true, false);
|
||||
|
||||
// For each EventMetric, 2 metrics get serialized since only one sample was
|
||||
// taken. So, the total number of serialized metrics are 2*4 since we
|
||||
// touched 4 metrics.
|
||||
ASSERT_EQ(2 * 4, actual_metrics.metric_size());
|
||||
EXPECT_EQ(0, actual_metrics.metric_sub_group_size());
|
||||
|
||||
// Spot check some metrics.
|
||||
EXPECT_EQ("/drm/widevine/cdm_engine/add_key/time/count{error:2}",
|
||||
actual_metrics.metric(0).name());
|
||||
EXPECT_EQ(
|
||||
"/drm/widevine/crypto_session/generic_decrypt/time/count"
|
||||
"{error:0&length:1024&encryption_algorithm:1}",
|
||||
actual_metrics.metric(4).name());
|
||||
EXPECT_EQ(
|
||||
"/drm/widevine/crypto_session/get_device_unique_id/count{success:0}",
|
||||
actual_metrics.metric(7).name());
|
||||
EXPECT_EQ(4.0, actual_metrics.metric(7).value().double_value());
|
||||
}
|
||||
|
||||
TEST_F(EngineMetricsTest, EmptyEngineMetrics) {
|
||||
EngineMetrics engine_metrics;
|
||||
|
||||
drm_metrics::MetricsGroup actual_metrics;
|
||||
engine_metrics.Serialize(&actual_metrics, true, false);
|
||||
|
||||
EXPECT_EQ(0, actual_metrics.metric_size());
|
||||
EXPECT_EQ(0, actual_metrics.metric_sub_group_size());
|
||||
}
|
||||
|
||||
TEST_F(EngineMetricsTest, EngineMetricsWithCompletedSessions) {
|
||||
EngineMetrics engine_metrics;
|
||||
|
||||
// Set a values in an engine metric and in a crypto metric.
|
||||
engine_metrics.cdm_engine_add_key_.Record(1.0, KEY_ADDED);
|
||||
engine_metrics.GetCryptoMetrics()
|
||||
->crypto_session_load_certificate_private_key_.Record(2.0, true);
|
||||
|
||||
// Create two sessions and record some metrics.
|
||||
SessionMetrics* session_metrics_1 = engine_metrics.AddSession();
|
||||
session_metrics_1->SetSessionId("session_id_1");
|
||||
SessionMetrics* session_metrics_2 = engine_metrics.AddSession();
|
||||
session_metrics_2->SetSessionId("session_id_2");
|
||||
// Record a CryptoMetrics metric in the session.
|
||||
session_metrics_2->GetCryptoMetrics()->crypto_session_generic_decrypt_
|
||||
.Record(2.0, NO_ERROR, Pow2Bucket(1025), kEncryptionAlgorithmAesCbc128);
|
||||
session_metrics_2->SetSessionId("session_id_2");
|
||||
// Mark only session 2 as completed.
|
||||
session_metrics_2->SetCompleted();
|
||||
|
||||
drm_metrics::MetricsGroup actual_metrics;
|
||||
engine_metrics.Serialize(&actual_metrics, true, false);
|
||||
|
||||
// Validate metric counts.
|
||||
// For each EventMetric, 2 metrics get serialized since only one sample was
|
||||
// taken. So, the total number of serialized metrics are 2*2 since we
|
||||
// touched 2 metrics.
|
||||
ASSERT_EQ(2 * 2, actual_metrics.metric_size());
|
||||
ASSERT_EQ(1, actual_metrics.metric_sub_group_size());
|
||||
ASSERT_EQ(3, actual_metrics.metric_sub_group(0).metric_size());
|
||||
|
||||
// Spot check some metrics.
|
||||
EXPECT_EQ("/drm/widevine/cdm_engine/add_key/time/count{error:2}",
|
||||
actual_metrics.metric(0).name());
|
||||
EXPECT_EQ("/drm/widevine/crypto_session/load_certificate_private_key"
|
||||
"/time/count{success:1}",
|
||||
actual_metrics.metric(2).name());
|
||||
EXPECT_EQ("/drm/widevine/cdm_session/session_id",
|
||||
actual_metrics.metric_sub_group(0).metric(0).name());
|
||||
EXPECT_EQ(
|
||||
"session_id_2",
|
||||
actual_metrics.metric_sub_group(0).metric(0).value().string_value());
|
||||
EXPECT_EQ(
|
||||
"/drm/widevine/crypto_session/generic_decrypt/time/count"
|
||||
"{error:0&length:1024&encryption_algorithm:1}",
|
||||
actual_metrics.metric_sub_group(0).metric(1).name());
|
||||
}
|
||||
|
||||
TEST_F(EngineMetricsTest, EngineMetricsSerializeAllSessions) {
|
||||
EngineMetrics engine_metrics;
|
||||
|
||||
// Create two sessions and record some metrics.
|
||||
SessionMetrics* session_metrics_1 = engine_metrics.AddSession();
|
||||
session_metrics_1->SetSessionId("session_id_1");
|
||||
SessionMetrics* session_metrics_2 = engine_metrics.AddSession();
|
||||
session_metrics_2->SetSessionId("session_id_2");
|
||||
// Mark only session 2 as completed.
|
||||
session_metrics_2->SetCompleted();
|
||||
|
||||
drm_metrics::MetricsGroup actual_metrics;
|
||||
engine_metrics.Serialize(&actual_metrics, false, false);
|
||||
|
||||
// Validate metric counts.
|
||||
// No Engine-level metrics were recorded.
|
||||
ASSERT_EQ(0, actual_metrics.metric_size());
|
||||
// Two sub groups, 1 per session.
|
||||
ASSERT_EQ(2, actual_metrics.metric_sub_group_size());
|
||||
ASSERT_EQ(1, actual_metrics.metric_sub_group(0).metric_size());
|
||||
|
||||
// Spot check some metrics.
|
||||
EXPECT_EQ("/drm/widevine/cdm_session/session_id",
|
||||
actual_metrics.metric_sub_group(0).metric(0).name());
|
||||
EXPECT_EQ(
|
||||
"session_id_1",
|
||||
actual_metrics.metric_sub_group(0).metric(0).value().string_value());
|
||||
EXPECT_EQ("/drm/widevine/cdm_session/session_id",
|
||||
actual_metrics.metric_sub_group(1).metric(0).name());
|
||||
EXPECT_EQ(
|
||||
"session_id_2",
|
||||
actual_metrics.metric_sub_group(1).metric(0).value().string_value());
|
||||
}
|
||||
|
||||
TEST_F(EngineMetricsTest, EngineMetricsRemoveSessions) {
|
||||
EngineMetrics engine_metrics;
|
||||
|
||||
// Create two sessions and record some metrics.
|
||||
SessionMetrics* session_metrics_1 = engine_metrics.AddSession();
|
||||
session_metrics_1->SetSessionId("session_id_1");
|
||||
SessionMetrics* session_metrics_2 = engine_metrics.AddSession();
|
||||
session_metrics_2->SetSessionId("session_id_2");
|
||||
// Mark only session 2 as completed.
|
||||
session_metrics_2->SetCompleted();
|
||||
|
||||
// Serialize all metrics, don't remove any.
|
||||
drm_metrics::MetricsGroup actual_metrics;
|
||||
engine_metrics.Serialize(&actual_metrics, false, false);
|
||||
|
||||
// Validate metric counts.
|
||||
// Two sub groups, 1 per session.
|
||||
ASSERT_EQ(2, actual_metrics.metric_sub_group_size());
|
||||
|
||||
// Serialize completed metrics, remove them.
|
||||
actual_metrics.Clear();
|
||||
engine_metrics.Serialize(&actual_metrics, true, true);
|
||||
// Validate metric counts.
|
||||
// Only one, completed session should exist.
|
||||
ASSERT_EQ(1, actual_metrics.metric_sub_group_size());
|
||||
ASSERT_EQ(1, actual_metrics.metric_sub_group(0).metric_size());
|
||||
EXPECT_EQ(
|
||||
"session_id_2",
|
||||
actual_metrics.metric_sub_group(0).metric(0).value().string_value());
|
||||
|
||||
// Serialize all metrics, remove them.
|
||||
actual_metrics.Clear();
|
||||
engine_metrics.Serialize(&actual_metrics, false, true);
|
||||
// Validate metric counts.
|
||||
// Only one, non-complete session should exist.
|
||||
ASSERT_EQ(1, actual_metrics.metric_sub_group_size());
|
||||
ASSERT_EQ(1, actual_metrics.metric_sub_group(0).metric_size());
|
||||
EXPECT_EQ(
|
||||
"session_id_1",
|
||||
actual_metrics.metric_sub_group(0).metric(0).value().string_value());
|
||||
|
||||
// Serialize all metrics, don't remove any.
|
||||
actual_metrics.Clear();
|
||||
engine_metrics.Serialize(&actual_metrics, false, false);
|
||||
// Validate metric counts.
|
||||
// There should be no more metric subgroups left.
|
||||
ASSERT_EQ(0, actual_metrics.metric_sub_group_size());
|
||||
}
|
||||
|
||||
class SessionMetricsTest : public ::testing::Test {
|
||||
};
|
||||
|
||||
TEST_F(SessionMetricsTest, AllSessionMetrics) {
|
||||
SessionMetrics session_metrics;
|
||||
session_metrics.SetSessionId("session_id 1");
|
||||
|
||||
session_metrics.cdm_session_life_span_.Record(1.0);
|
||||
session_metrics.cdm_session_renew_key_.Record(1.0, NO_ERROR);
|
||||
session_metrics.cdm_session_restore_offline_session_.Record(1.0, NO_ERROR);
|
||||
session_metrics.cdm_session_restore_usage_session_.Record(1.0, NO_ERROR);
|
||||
|
||||
// Record a CryptoMetrics metric in the session.
|
||||
session_metrics.GetCryptoMetrics()->crypto_session_generic_decrypt_
|
||||
.Record(2.0, NO_ERROR, Pow2Bucket(1025), kEncryptionAlgorithmAesCbc128);
|
||||
|
||||
MetricsGroup actual_metrics;
|
||||
session_metrics.Serialize(&actual_metrics);
|
||||
|
||||
ASSERT_EQ(11, actual_metrics.metric_size());
|
||||
EXPECT_EQ(0, actual_metrics.metric_sub_group_size());
|
||||
|
||||
// Spot check some metrics.
|
||||
EXPECT_EQ("/drm/widevine/cdm_session/session_id",
|
||||
actual_metrics.metric(0).name());
|
||||
EXPECT_EQ("/drm/widevine/cdm_session/life_span",
|
||||
actual_metrics.metric(1).name());
|
||||
EXPECT_EQ("/drm/widevine/cdm_session/renew_key/time/mean{error:0}",
|
||||
actual_metrics.metric(4).name());
|
||||
EXPECT_EQ(1.0, actual_metrics.metric(4).value().double_value());
|
||||
EXPECT_EQ("/drm/widevine/crypto_session/generic_decrypt/time/count"
|
||||
"{error:0&length:1024&encryption_algorithm:1}",
|
||||
actual_metrics.metric(9).name());
|
||||
}
|
||||
|
||||
TEST_F(SessionMetricsTest, EmptySessionMetrics) {
|
||||
SessionMetrics session_metrics;
|
||||
|
||||
MetricsGroup actual_metrics;
|
||||
session_metrics.Serialize(&actual_metrics);
|
||||
|
||||
// Session metric always has a session id.
|
||||
ASSERT_EQ(1, actual_metrics.metric_size());
|
||||
EXPECT_EQ("/drm/widevine/cdm_session/session_id",
|
||||
actual_metrics.metric(0).name());
|
||||
EXPECT_EQ("", actual_metrics.metric(0).value().string_value());
|
||||
EXPECT_EQ(0, actual_metrics.metric_sub_group_size());
|
||||
}
|
||||
|
||||
class CryptoMetricsTest : public ::testing::Test {
|
||||
};
|
||||
|
||||
TEST_F(CryptoMetricsTest, AllCryptoMetrics) {
|
||||
CryptoMetrics crypto_metrics;
|
||||
|
||||
// Crypto session metrics.
|
||||
crypto_metrics.crypto_session_delete_all_usage_reports_
|
||||
.Record(1.0, NO_ERROR);
|
||||
crypto_metrics.crypto_session_delete_multiple_usage_information_
|
||||
.Record(1.0, NO_ERROR);
|
||||
crypto_metrics.crypto_session_generic_decrypt_
|
||||
.Record(2.0, NO_ERROR, Pow2Bucket(1025), kEncryptionAlgorithmAesCbc128);
|
||||
crypto_metrics.crypto_session_generic_encrypt_
|
||||
.Record(2.0, NO_ERROR, Pow2Bucket(1025), kEncryptionAlgorithmAesCbc128);
|
||||
crypto_metrics.crypto_session_generic_sign_
|
||||
.Record(2.0, NO_ERROR, Pow2Bucket(1025), kSigningAlgorithmHmacSha256);
|
||||
crypto_metrics.crypto_session_generic_verify_
|
||||
.Record(2.0, NO_ERROR, Pow2Bucket(1025), kSigningAlgorithmHmacSha256);
|
||||
crypto_metrics.crypto_session_get_device_unique_id_.Record(1.0, true);
|
||||
crypto_metrics.crypto_session_get_security_level_
|
||||
.Record(1.0, kSecurityLevelL1);
|
||||
crypto_metrics.crypto_session_get_system_id_.Record(1.0, true, 1234);
|
||||
crypto_metrics.crypto_session_get_token_.Record(1.0, true);
|
||||
crypto_metrics.crypto_session_life_span_.Record(1.0);
|
||||
crypto_metrics.crypto_session_load_certificate_private_key_
|
||||
.Record(1.0, true);
|
||||
crypto_metrics.crypto_session_open_.Record(1.0, NO_ERROR, kLevelDefault);
|
||||
crypto_metrics.crypto_session_update_usage_information_
|
||||
.Record(1.0, NO_ERROR);
|
||||
crypto_metrics.crypto_session_usage_information_support_.Record(1.0, true);
|
||||
|
||||
// Oem crypto metrics.
|
||||
crypto_metrics.oemcrypto_api_version_.Record(1.0, 123, kLevelDefault);
|
||||
crypto_metrics.oemcrypto_close_session_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED);
|
||||
crypto_metrics.oemcrypto_copy_buffer_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED,
|
||||
kLevelDefault, Pow2Bucket(1025));
|
||||
crypto_metrics.oemcrypto_deactivate_usage_entry_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED);
|
||||
crypto_metrics.oemcrypto_decrypt_cenc_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED, Pow2Bucket(1025));
|
||||
crypto_metrics.oemcrypto_delete_usage_entry_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED);
|
||||
crypto_metrics.oemcrypto_delete_usage_table_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED);
|
||||
crypto_metrics.oemcrypto_derive_keys_from_session_key_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED);
|
||||
crypto_metrics.oemcrypto_force_delete_usage_entry_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED);
|
||||
crypto_metrics.oemcrypto_generate_derived_keys_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED);
|
||||
crypto_metrics.oemcrypto_generate_nonce_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED);
|
||||
crypto_metrics.oemcrypto_generate_rsa_signature_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED, Pow2Bucket(1025));
|
||||
crypto_metrics.oemcrypto_generate_signature_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED, Pow2Bucket(1025));
|
||||
crypto_metrics.oemcrypto_generic_decrypt_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED, Pow2Bucket(1025));
|
||||
crypto_metrics.oemcrypto_generic_encrypt_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED, Pow2Bucket(1025));
|
||||
crypto_metrics.oemcrypto_generic_sign_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED, Pow2Bucket(1025));
|
||||
crypto_metrics.oemcrypto_generic_verify_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED, Pow2Bucket(1025));
|
||||
crypto_metrics.oemcrypto_get_device_id_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED, kLevelDefault);
|
||||
crypto_metrics.oemcrypto_get_hdcp_capability_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED, kLevelDefault);
|
||||
crypto_metrics.oemcrypto_get_key_data_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED,
|
||||
Pow2Bucket(1025), kLevelDefault);
|
||||
crypto_metrics.oemcrypto_get_max_number_of_sessions_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED, kLevelDefault);
|
||||
crypto_metrics.oemcrypto_get_number_of_open_sessions_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED, kLevelDefault);
|
||||
crypto_metrics.oemcrypto_get_oem_public_certificate_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED);
|
||||
|
||||
crypto_metrics.oemcrypto_get_provisioning_method_
|
||||
.Record(1.0, OEMCrypto_Keybox, kLevelDefault);
|
||||
|
||||
crypto_metrics.oemcrypto_get_random_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED, Pow2Bucket(1025));
|
||||
crypto_metrics.oemcrypto_initialize_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED);
|
||||
crypto_metrics.oemcrypto_install_keybox_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED, kLevelDefault);
|
||||
crypto_metrics.oemcrypto_is_anti_rollback_hw_present_
|
||||
.Record(1.0, true, kLevelDefault);
|
||||
|
||||
crypto_metrics.oemcrypto_is_keybox_valid_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED, kLevelDefault);
|
||||
crypto_metrics.oemcrypto_load_device_rsa_key_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED);
|
||||
crypto_metrics.oemcrypto_load_keys_.Record(1.0, OEMCrypto_ERROR_INIT_FAILED);
|
||||
crypto_metrics.oemcrypto_load_test_keybox_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED);
|
||||
crypto_metrics.oemcrypto_load_test_rsa_key_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED);
|
||||
crypto_metrics.oemcrypto_open_session_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED, kLevelDefault);
|
||||
crypto_metrics.oemcrypto_refresh_keys_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED);
|
||||
crypto_metrics.oemcrypto_report_usage_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED);
|
||||
crypto_metrics.oemcrypto_rewrap_device_rsa_key_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED);
|
||||
crypto_metrics.oemcrypto_rewrap_device_rsa_key_30_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED);
|
||||
crypto_metrics.oemcrypto_security_level_
|
||||
.Record(1.0, kSecurityLevelL2, kLevelDefault);
|
||||
crypto_metrics.oemcrypto_security_patch_level_
|
||||
.Record(1.0, 123, kLevelDefault);
|
||||
crypto_metrics.oemcrypto_select_key_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED);
|
||||
crypto_metrics.oemcrypto_supports_usage_table_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED, kLevelDefault);
|
||||
crypto_metrics.oemcrypto_update_usage_table_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED);
|
||||
crypto_metrics.oemcrypto_wrap_keybox_
|
||||
.Record(1.0, OEMCrypto_ERROR_INIT_FAILED);
|
||||
|
||||
// Internal OEMCrypto Metrics
|
||||
crypto_metrics.oemcrypto_initialization_mode_
|
||||
.Record(1.0, OEMCrypto_INITIALIZED_FORCING_L3);
|
||||
crypto_metrics.oemcrypto_l1_api_version_.Record(1.0, 12, 123);
|
||||
|
||||
MetricsGroup actual_metrics;
|
||||
crypto_metrics.Serialize(&actual_metrics);
|
||||
|
||||
// 61 EventMetric instances, 2 values each.
|
||||
ASSERT_EQ(122, actual_metrics.metric_size());
|
||||
|
||||
// Spot check some metrics.
|
||||
EXPECT_EQ(
|
||||
"/drm/widevine/crypto_session/delete_all_usage_reports/count"
|
||||
"{error:0}",
|
||||
actual_metrics.metric(0).name());
|
||||
EXPECT_EQ(1, actual_metrics.metric(0).value().int_value());
|
||||
EXPECT_EQ(
|
||||
"/drm/widevine/oemcrypto/l1_api_version/mean{version:12&min_version:123}",
|
||||
actual_metrics.metric(121).name());
|
||||
EXPECT_EQ(1.0, actual_metrics.metric(121).value().double_value());
|
||||
|
||||
// No subgroups should exist.
|
||||
EXPECT_EQ(0, actual_metrics.metric_sub_group_size());
|
||||
}
|
||||
|
||||
} // namespace metrics
|
||||
} // namespace wvcdm
|
||||
77
metrics/test/value_metric_unittest.cpp
Normal file
77
metrics/test/value_metric_unittest.cpp
Normal file
@@ -0,0 +1,77 @@
|
||||
// Copyright 2017 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Unit tests for ValueMetric.
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "value_metric.h"
|
||||
|
||||
#include "gmock/gmock.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "metric_serialization.h"
|
||||
#include "scoped_ptr.h"
|
||||
|
||||
namespace wvcdm {
|
||||
namespace metrics {
|
||||
|
||||
class MockMetricSerializer : public MetricSerializer {
|
||||
public:
|
||||
MOCK_METHOD2(SetString, void(const std::string& metric_id,
|
||||
const std::string& value));
|
||||
MOCK_METHOD2(SetInt32, void(const std::string& metric_id,
|
||||
int32_t value));
|
||||
MOCK_METHOD2(SetInt64, void(const std::string& metric_id,
|
||||
int64_t value));
|
||||
MOCK_METHOD2(SetDouble, void(const std::string& metric_id,
|
||||
double value));
|
||||
};
|
||||
|
||||
class ValueMetricTest : public ::testing::Test {
|
||||
public:
|
||||
void SetUp() {
|
||||
mock_serializer_.reset(new MockMetricSerializer());
|
||||
}
|
||||
|
||||
protected:
|
||||
scoped_ptr<MockMetricSerializer> mock_serializer_;
|
||||
};
|
||||
|
||||
TEST_F(ValueMetricTest, StringValue) {
|
||||
ValueMetric<std::string> value_metric("string/metric");
|
||||
value_metric.Record("foo");
|
||||
|
||||
EXPECT_CALL(*mock_serializer_,
|
||||
SetString("string/metric", "foo"));
|
||||
value_metric.Serialize(mock_serializer_.get());
|
||||
}
|
||||
|
||||
TEST_F(ValueMetricTest, DoubleValue) {
|
||||
ValueMetric<double> value_metric("double/metric");
|
||||
value_metric.Record(42.0);
|
||||
|
||||
EXPECT_CALL(*mock_serializer_,
|
||||
SetDouble("double/metric", 42.0));
|
||||
value_metric.Serialize(mock_serializer_.get());
|
||||
}
|
||||
|
||||
TEST_F(ValueMetricTest, Int32Value) {
|
||||
ValueMetric<int32_t> value_metric("int32/metric");
|
||||
value_metric.Record(42);
|
||||
|
||||
EXPECT_CALL(*mock_serializer_,
|
||||
SetInt32("int32/metric", 42));
|
||||
value_metric.Serialize(mock_serializer_.get());
|
||||
}
|
||||
|
||||
TEST_F(ValueMetricTest, Int64Value) {
|
||||
ValueMetric<int64_t> value_metric("int64/metric");
|
||||
value_metric.Record(42);
|
||||
|
||||
EXPECT_CALL(*mock_serializer_,
|
||||
SetInt64("int64/metric", 42));
|
||||
value_metric.Serialize(mock_serializer_.get());
|
||||
}
|
||||
|
||||
} // namespace metrics
|
||||
} // namespace wvcdm
|
||||
|
||||
Reference in New Issue
Block a user