Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef NET_NQE_OBSERVATION_BUFFER_H_ | 5 #ifndef NET_NQE_OBSERVATION_BUFFER_H_ |
| 6 #define NET_NQE_OBSERVATION_BUFFER_H_ | 6 #define NET_NQE_OBSERVATION_BUFFER_H_ |
| 7 | 7 |
| 8 #include <float.h> | 8 #include <float.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 115 | 115 |
| 116 // Computation may reach here due to floating point errors. This may happen | 116 // Computation may reach here due to floating point errors. This may happen |
| 117 // if |percentile| was 100 (or close to 100), and |desired_weight| was | 117 // if |percentile| was 100 (or close to 100), and |desired_weight| was |
| 118 // slightly larger than |total_weight| (due to floating point errors). | 118 // slightly larger than |total_weight| (due to floating point errors). |
| 119 // In this case, we return the highest |value| among all observations. | 119 // In this case, we return the highest |value| among all observations. |
| 120 // This is same as value of the last observation in the sorted vector. | 120 // This is same as value of the last observation in the sorted vector. |
| 121 *result = weighted_observations.at(weighted_observations.size() - 1).value; | 121 *result = weighted_observations.at(weighted_observations.size() - 1).value; |
| 122 return true; | 122 return true; |
| 123 } | 123 } |
| 124 | 124 |
| 125 // 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.
| |
| 126 // buffer is available. Sets |result| to the computed weighted average value | |
| 127 // among all observations since |begin_timestamp|. If the value is | |
| 128 // unavailable, false is returned and |result| is not modified. |result| must | |
| 129 // not be null. | |
| 130 bool GetWeightedAverage(const base::TimeTicks begin_timestamp, | |
| 131 const std::vector<NetworkQualityObservationSource>& | |
| 132 disallowed_observation_sources, | |
| 133 ValueType* result) const { | |
| 134 DCHECK(result); | |
| 135 DCHECK_GE(Capacity(), Size()); | |
| 136 | |
| 137 // Stores WeightedObservation in increasing order of value. | |
| 138 std::vector<WeightedObservation<ValueType>> weighted_observations; | |
| 139 | |
| 140 // Total weight of all observations in |weighted_observations|. | |
| 141 double total_weight = 0.0; | |
| 142 | |
| 143 ComputeWeightedObservations(begin_timestamp, weighted_observations, | |
| 144 &total_weight, disallowed_observation_sources); | |
| 145 if (weighted_observations.empty()) | |
| 146 return false; | |
| 147 | |
| 148 DCHECK_LT(0.0, total_weight); | |
| 149 | |
| 150 // |weighted_observations| may have a smaller size than |observations_| | |
| 151 // since the former contains only the observations later than | |
| 152 // |begin_timestamp|. | |
| 153 DCHECK_GE(observations_.size(), weighted_observations.size()); | |
| 154 | |
| 155 // Weighted average is the sum of observations times their respective | |
| 156 // weight, divided by the sum of weights of all observations. | |
| 157 double total_weight_times_value = 0.0; | |
| 158 for (const auto& weighted_observation : weighted_observations) { | |
| 159 total_weight_times_value += | |
| 160 (weighted_observation.weight * | |
| 161 ConvertValueTypeToDouble(weighted_observation.value)); | |
| 162 } | |
| 163 | |
| 164 ConvertDoubleToValueType(total_weight_times_value / total_weight, result); | |
| 165 return true; | |
| 166 } | |
| 167 | |
| 168 // Returns true iff the unweighted average of the observations in this buffer | |
| 169 // is available. Sets |result| to the computed unweighted average value among | |
| 170 // all observations since |begin_timestamp|. If the value is unavailable, | |
| 171 // false is returned and |result| is not modified. |result| must not be null. | |
| 172 bool GetUnweightedAverage(const base::TimeTicks begin_timestamp, | |
| 173 const std::vector<NetworkQualityObservationSource>& | |
| 174 disallowed_observation_sources, | |
| 175 ValueType* result) const { | |
| 176 DCHECK(result); | |
| 177 DCHECK_GE(Capacity(), Size()); | |
| 178 | |
| 179 // Stores WeightedObservation in increasing order of value. | |
| 180 std::vector<WeightedObservation<ValueType>> weighted_observations; | |
| 181 | |
| 182 // Total weight of all observations in |weighted_observations|. | |
| 183 double total_weight = 0.0; | |
| 184 | |
| 185 ComputeWeightedObservations(begin_timestamp, weighted_observations, | |
| 186 &total_weight, disallowed_observation_sources); | |
| 187 if (weighted_observations.empty()) | |
| 188 return false; | |
| 189 | |
| 190 // |weighted_observations| may have a smaller size than |observations_| | |
| 191 // since the former contains only the observations later than | |
| 192 // |begin_timestamp|. | |
| 193 DCHECK_GE(observations_.size(), weighted_observations.size()); | |
| 194 | |
| 195 // Unweighted average is the sum of all observations divided by the number | |
| 196 // of observations. | |
| 197 double total_value = 0.0; | |
| 198 for (const auto& weighted_observation : weighted_observations) | |
| 199 total_value += ConvertValueTypeToDouble(weighted_observation.value); | |
| 200 | |
| 201 ConvertDoubleToValueType(total_value / weighted_observations.size(), | |
| 202 result); | |
| 203 return true; | |
| 204 } | |
| 205 | |
| 125 void SetTickClockForTesting(std::unique_ptr<base::TickClock> tick_clock) { | 206 void SetTickClockForTesting(std::unique_ptr<base::TickClock> tick_clock) { |
| 126 tick_clock_ = std::move(tick_clock); | 207 tick_clock_ = std::move(tick_clock); |
| 127 } | 208 } |
| 128 | 209 |
| 129 private: | 210 private: |
| 130 // Maximum number of observations that can be held in the ObservationBuffer. | 211 // Maximum number of observations that can be held in the ObservationBuffer. |
| 131 static const size_t kMaximumObservationsBufferSize = 300; | 212 static const size_t kMaximumObservationsBufferSize = 300; |
| 132 | 213 |
| 214 // Convert different ValueTypes to double to make it possible to perform | |
| 215 // arithmetic operations on them. | |
| 216 double ConvertValueTypeToDouble(base::TimeDelta input) const { | |
| 217 return input.InMilliseconds(); | |
| 218 } | |
| 219 double ConvertValueTypeToDouble(int32_t input) const { return input; } | |
| 220 | |
| 221 // Convert double to different ValueTypes. | |
| 222 void ConvertDoubleToValueType(double input, base::TimeDelta* output) const { | |
| 223 *output = base::TimeDelta::FromMilliseconds(input); | |
| 224 } | |
| 225 void ConvertDoubleToValueType(double input, int32_t* output) const { | |
| 226 *output = input; | |
| 227 } | |
| 228 | |
| 133 // Computes the weighted observations and stores them in | 229 // Computes the weighted observations and stores them in |
| 134 // |weighted_observations| sorted by ascending |WeightedObservation.value|. | 230 // |weighted_observations| sorted by ascending |WeightedObservation.value|. |
| 135 // Only the observations with timestamp later than |begin_timestamp| are | 231 // Only the observations with timestamp later than |begin_timestamp| are |
| 136 // considered. Also, sets |total_weight| to the total weight of all | 232 // considered. Also, sets |total_weight| to the total weight of all |
| 137 // observations. Should be called only when there is at least one | 233 // observations. Should be called only when there is at least one |
| 138 // observation in the buffer. | 234 // observation in the buffer. |
| 139 void ComputeWeightedObservations( | 235 void ComputeWeightedObservations( |
| 140 const base::TimeTicks& begin_timestamp, | 236 const base::TimeTicks& begin_timestamp, |
| 141 std::vector<WeightedObservation<ValueType>>& weighted_observations, | 237 std::vector<WeightedObservation<ValueType>>& weighted_observations, |
| 142 double* total_weight, | 238 double* total_weight, |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 189 DISALLOW_COPY_AND_ASSIGN(ObservationBuffer); | 285 DISALLOW_COPY_AND_ASSIGN(ObservationBuffer); |
| 190 }; | 286 }; |
| 191 | 287 |
| 192 } // namespace internal | 288 } // namespace internal |
| 193 | 289 |
| 194 } // namespace nqe | 290 } // namespace nqe |
| 195 | 291 |
| 196 } // namespace net | 292 } // namespace net |
| 197 | 293 |
| 198 #endif // NET_NQE_OBSERVATION_BUFFER_H_ | 294 #endif // NET_NQE_OBSERVATION_BUFFER_H_ |
| OLD | NEW |