Chromium Code Reviews| Index: net/nqe/observation_buffer.h |
| diff --git a/net/nqe/observation_buffer.h b/net/nqe/observation_buffer.h |
| index 9a4ce3ab238c2bd1921f70daa232adb0c6a509db..398c14b10d705ed1227effe334aad54fc7a3bd77 100644 |
| --- a/net/nqe/observation_buffer.h |
| +++ b/net/nqe/observation_buffer.h |
| @@ -122,6 +122,87 @@ class NET_EXPORT_PRIVATE ObservationBuffer { |
| return true; |
| } |
| + // Returns true iff the weighted average of the observations in this |
|
Not at Google. Contact bengr
2016/10/12 22:09:52
Can you clarify why weighted average might not be
tbansal1
2016/10/12 22:45:01
Done.
|
| + // buffer is available. Sets |result| to the computed weighted average value |
| + // among all observations since |begin_timestamp|. If the value is |
| + // unavailable, false is returned and |result| is not modified. |result| must |
| + // not be null. |
| + bool GetWeightedAverage(const base::TimeTicks begin_timestamp, |
| + const std::vector<NetworkQualityObservationSource>& |
| + disallowed_observation_sources, |
| + ValueType* result) const { |
| + DCHECK(result); |
| + DCHECK_GE(Capacity(), Size()); |
| + |
| + // Stores WeightedObservation in increasing order of value. |
| + std::vector<WeightedObservation<ValueType>> weighted_observations; |
| + |
| + // Total weight of all observations in |weighted_observations|. |
| + double total_weight = 0.0; |
| + |
| + ComputeWeightedObservations(begin_timestamp, weighted_observations, |
| + &total_weight, disallowed_observation_sources); |
| + if (weighted_observations.empty()) |
| + return false; |
| + |
| + DCHECK_LT(0.0, total_weight); |
| + |
| + // |weighted_observations| may have a smaller size than |observations_| |
| + // since the former contains only the observations later than |
| + // |begin_timestamp|. |
| + DCHECK_GE(observations_.size(), weighted_observations.size()); |
| + |
| + // Weighted average is the sum of observations times their respective |
| + // weight, divided by the sum of weights of all observations. |
| + double total_weight_times_value = 0.0; |
| + for (const auto& weighted_observation : weighted_observations) { |
| + total_weight_times_value += |
| + (weighted_observation.weight * |
| + ConvertValueTypeToDouble(weighted_observation.value)); |
| + } |
| + |
| + ConvertDoubleToValueType(total_weight_times_value / total_weight, result); |
| + return true; |
| + } |
| + |
| + // Returns true iff the unweighted average of the observations in this buffer |
| + // is available. Sets |result| to the computed unweighted average value among |
| + // all observations since |begin_timestamp|. If the value is unavailable, |
| + // false is returned and |result| is not modified. |result| must not be null. |
| + bool GetUnweightedAverage(const base::TimeTicks begin_timestamp, |
| + const std::vector<NetworkQualityObservationSource>& |
| + disallowed_observation_sources, |
| + ValueType* result) const { |
| + DCHECK(result); |
| + DCHECK_GE(Capacity(), Size()); |
| + |
| + // Stores WeightedObservation in increasing order of value. |
| + std::vector<WeightedObservation<ValueType>> weighted_observations; |
| + |
| + // Total weight of all observations in |weighted_observations|. |
| + double total_weight = 0.0; |
| + |
| + ComputeWeightedObservations(begin_timestamp, weighted_observations, |
| + &total_weight, disallowed_observation_sources); |
| + if (weighted_observations.empty()) |
| + return false; |
| + |
| + // |weighted_observations| may have a smaller size than |observations_| |
| + // since the former contains only the observations later than |
| + // |begin_timestamp|. |
| + DCHECK_GE(observations_.size(), weighted_observations.size()); |
| + |
| + // Unweighted average is the sum of all observations divided by the number |
| + // of observations. |
| + double total_value = 0.0; |
| + for (const auto& weighted_observation : weighted_observations) |
| + total_value += ConvertValueTypeToDouble(weighted_observation.value); |
| + |
| + ConvertDoubleToValueType(total_value / weighted_observations.size(), |
| + result); |
| + return true; |
| + } |
| + |
| void SetTickClockForTesting(std::unique_ptr<base::TickClock> tick_clock) { |
| tick_clock_ = std::move(tick_clock); |
| } |
| @@ -130,6 +211,21 @@ class NET_EXPORT_PRIVATE ObservationBuffer { |
| // Maximum number of observations that can be held in the ObservationBuffer. |
| static const size_t kMaximumObservationsBufferSize = 300; |
| + // Convert different ValueTypes to double to make it possible to perform |
| + // arithmetic operations on them. |
| + double ConvertValueTypeToDouble(base::TimeDelta input) const { |
| + return input.InMilliseconds(); |
| + } |
| + double ConvertValueTypeToDouble(int32_t input) const { return input; } |
| + |
| + // Convert double to different ValueTypes. |
| + void ConvertDoubleToValueType(double input, base::TimeDelta* output) const { |
| + *output = base::TimeDelta::FromMilliseconds(input); |
| + } |
| + void ConvertDoubleToValueType(double input, int32_t* output) const { |
| + *output = input; |
| + } |
| + |
| // Computes the weighted observations and stores them in |
| // |weighted_observations| sorted by ascending |WeightedObservation.value|. |
| // Only the observations with timestamp later than |begin_timestamp| are |