Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(110)

Side by Side Diff: net/nqe/observation_buffer.h

Issue 2411833003: NQE: Add more statistics to ObservationBuffer class (Closed)
Patch Set: Addressed kundaji's comments Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
126 // buffer is available. Sets |result| to the computed weighted average value
127 // among all observations since |begin_timestamp|. If the value is
bengr 2016/10/14 21:15:58 among -> of? since -> made on or after
tbansal1 2016/10/14 22:25:29 Done.
128 // unavailable, false is returned and |result| is not modified. The unweighted
129 // average value is unavailable if all the values in observation buffer are
130 // older than |begin_timestamp|. |result| must not be null.
131 bool GetWeightedAverage(const base::TimeTicks begin_timestamp,
bengr 2016/10/14 21:15:58 Did you mean for this parameter to be a const refe
tbansal1 2016/10/14 22:25:30 Done.
132 const std::vector<NetworkQualityObservationSource>&
133 disallowed_observation_sources,
134 ValueType* result) const {
bengr 2016/10/14 21:15:58 Why is this not a base::TimeDelta?
tbansal1 2016/10/14 22:25:30 It is a template class. Caller may set it to base:
135 DCHECK(result);
bengr 2016/10/14 21:15:58 No need for this DCHECK.
tbansal1 2016/10/14 22:25:30 Done.
136 DCHECK_GE(Capacity(), Size());
137
138 // Stores WeightedObservation in increasing order of value.
bengr 2016/10/14 21:15:58 I would say: //Stores weighted observations in inc
tbansal1 2016/10/14 22:25:30 Done.
139 std::vector<WeightedObservation<ValueType>> weighted_observations;
140
141 // Total weight of all observations in |weighted_observations|.
142 double total_weight = 0.0;
143
144 ComputeWeightedObservations(begin_timestamp, weighted_observations,
145 &total_weight, disallowed_observation_sources);
146 if (weighted_observations.empty())
147 return false;
148
149 DCHECK_LT(0.0, total_weight);
bengr 2016/10/14 21:15:58 Why can't a weight be 0.0?
tbansal1 2016/10/14 22:25:30 Because there is a divide by |total_weight| later
150
151 // |weighted_observations| may have a smaller size than |observations_|
152 // since the former contains only the observations later than
153 // |begin_timestamp|.
154 DCHECK_GE(observations_.size(), weighted_observations.size());
155
156 // Weighted average is the sum of observations times their respective
157 // weight, divided by the sum of weights of all observations.
bengr 2016/10/14 21:15:58 weight -> weights of weights -> of the weights
tbansal1 2016/10/14 22:25:29 Done.
158 double total_weight_times_value = 0.0;
159 for (const auto& weighted_observation : weighted_observations) {
160 total_weight_times_value +=
161 (weighted_observation.weight *
162 ConvertValueTypeToDouble(weighted_observation.value));
163 }
164
165 ConvertDoubleToValueType(total_weight_times_value / total_weight, result);
166 return true;
167 }
168
169 // Returns true iff the unweighted average of the observations in this buffer
170 // is available. Sets |result| to the computed unweighted average value among
171 // all observations since |begin_timestamp|. If the value is unavailable,
172 // false is returned and |result| is not modified. The weighted average value
173 // is unavailable if all the values in observation buffer are older than
bengr 2016/10/14 21:15:58 all the values in -> all values in the
tbansal1 2016/10/14 22:25:30 Done.
174 // |begin_timestamp|. |result| must not be null.
175 bool GetUnweightedAverage(const base::TimeTicks begin_timestamp,
bengr 2016/10/14 21:15:58 This method seems very similar to the previous one
tbansal1 2016/10/14 22:25:29 I have moved some of the common code to ComputeWei
176 const std::vector<NetworkQualityObservationSource>&
177 disallowed_observation_sources,
178 ValueType* result) const {
179 DCHECK(result);
bengr 2016/10/14 21:15:58 Remove this DCHECK.
tbansal1 2016/10/14 22:25:29 Done.
180 DCHECK_GE(Capacity(), Size());
181
182 // Stores WeightedObservation in increasing order of value.
bengr 2016/10/14 21:15:58 See previous comment on wording.
tbansal1 2016/10/14 22:25:29 Done.
183 std::vector<WeightedObservation<ValueType>> weighted_observations;
184
185 // Total weight of all observations in |weighted_observations|.
186 double total_weight = 0.0;
187
188 ComputeWeightedObservations(begin_timestamp, weighted_observations,
189 &total_weight, disallowed_observation_sources);
190 if (weighted_observations.empty())
191 return false;
192
193 // |weighted_observations| may have a smaller size than |observations_|
194 // since the former contains only the observations later than
195 // |begin_timestamp|.
196 DCHECK_GE(observations_.size(), weighted_observations.size());
197
198 // Unweighted average is the sum of all observations divided by the number
bengr 2016/10/14 21:15:58 Unweighted -> The unweighted
tbansal1 2016/10/14 22:25:30 Done.
199 // of observations.
200 double total_value = 0.0;
201 for (const auto& weighted_observation : weighted_observations)
202 total_value += ConvertValueTypeToDouble(weighted_observation.value);
203
204 ConvertDoubleToValueType(total_value / weighted_observations.size(),
205 result);
206 return true;
207 }
208
125 void SetTickClockForTesting(std::unique_ptr<base::TickClock> tick_clock) { 209 void SetTickClockForTesting(std::unique_ptr<base::TickClock> tick_clock) {
126 tick_clock_ = std::move(tick_clock); 210 tick_clock_ = std::move(tick_clock);
127 } 211 }
128 212
129 private: 213 private:
130 // Maximum number of observations that can be held in the ObservationBuffer. 214 // Maximum number of observations that can be held in the ObservationBuffer.
131 static const size_t kMaximumObservationsBufferSize = 300; 215 static const size_t kMaximumObservationsBufferSize = 300;
132 216
217 // Convert different ValueTypes to double to make it possible to perform
bengr 2016/10/14 21:15:58 It would be cleaner to not have to use any of thes
tbansal1 2016/10/14 22:25:29 They are necessary for doing arithmetic operations
218 // arithmetic operations on them.
219 double ConvertValueTypeToDouble(base::TimeDelta input) const {
220 return input.InMilliseconds();
221 }
222 double ConvertValueTypeToDouble(int32_t input) const { return input; }
223
224 // Convert double to different ValueTypes.
225 void ConvertDoubleToValueType(double input, base::TimeDelta* output) const {
226 *output = base::TimeDelta::FromMilliseconds(input);
227 }
228 void ConvertDoubleToValueType(double input, int32_t* output) const {
229 *output = input;
230 }
231
133 // Computes the weighted observations and stores them in 232 // Computes the weighted observations and stores them in
134 // |weighted_observations| sorted by ascending |WeightedObservation.value|. 233 // |weighted_observations| sorted by ascending |WeightedObservation.value|.
135 // Only the observations with timestamp later than |begin_timestamp| are 234 // Only the observations with timestamp later than |begin_timestamp| are
136 // considered. Also, sets |total_weight| to the total weight of all 235 // considered. Also, sets |total_weight| to the total weight of all
137 // observations. Should be called only when there is at least one 236 // observations. Should be called only when there is at least one
138 // observation in the buffer. 237 // observation in the buffer.
139 void ComputeWeightedObservations( 238 void ComputeWeightedObservations(
140 const base::TimeTicks& begin_timestamp, 239 const base::TimeTicks& begin_timestamp,
141 std::vector<WeightedObservation<ValueType>>& weighted_observations, 240 std::vector<WeightedObservation<ValueType>>& weighted_observations,
142 double* total_weight, 241 double* total_weight,
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
189 DISALLOW_COPY_AND_ASSIGN(ObservationBuffer); 288 DISALLOW_COPY_AND_ASSIGN(ObservationBuffer);
190 }; 289 };
191 290
192 } // namespace internal 291 } // namespace internal
193 292
194 } // namespace nqe 293 } // namespace nqe
195 294
196 } // namespace net 295 } // namespace net
197 296
198 #endif // NET_NQE_OBSERVATION_BUFFER_H_ 297 #endif // NET_NQE_OBSERVATION_BUFFER_H_
OLDNEW
« no previous file with comments | « no previous file | net/nqe/observation_buffer_unittest.cc » ('j') | net/nqe/observation_buffer_unittest.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698