Index: net/nqe/observation_buffer.h |
diff --git a/net/nqe/observation_buffer.h b/net/nqe/observation_buffer.h |
index 3fcb9cf70839bc71ebb29db00c087076e27c7e37..70441a28519096ece1f87e91dd38c373558142ac 100644 |
--- a/net/nqe/observation_buffer.h |
+++ b/net/nqe/observation_buffer.h |
@@ -6,6 +6,7 @@ |
#define NET_NQE_OBSERVATION_BUFFER_H_ |
#include <float.h> |
+#include <stdint.h> |
#include <algorithm> |
#include <deque> |
@@ -14,6 +15,7 @@ |
#include <vector> |
#include "base/macros.h" |
+#include "base/optional.h" |
#include "base/time/default_tick_clock.h" |
#include "base/time/tick_clock.h" |
#include "base/time/time.h" |
@@ -33,16 +35,16 @@ template <typename ValueType> |
class NET_EXPORT_PRIVATE ObservationBuffer { |
public: |
ObservationBuffer(double weight_multiplier_per_second, |
- double weight_multiplier_per_dbm) |
+ double weight_multiplier_per_signal_level) |
: weight_multiplier_per_second_(weight_multiplier_per_second), |
- weight_multiplier_per_dbm_(weight_multiplier_per_dbm), |
+ weight_multiplier_per_signal_level_(weight_multiplier_per_signal_level), |
tick_clock_(new base::DefaultTickClock()) { |
static_assert(kMaximumObservationsBufferSize > 0U, |
"Minimum size of observation buffer must be > 0"); |
DCHECK_LE(0.0, weight_multiplier_per_second_); |
DCHECK_GE(1.0, weight_multiplier_per_second_); |
- DCHECK_LE(0.0, weight_multiplier_per_dbm_); |
- DCHECK_GE(1.0, weight_multiplier_per_dbm_); |
+ DCHECK_LE(0.0, weight_multiplier_per_signal_level_); |
+ DCHECK_GE(1.0, weight_multiplier_per_signal_level_); |
} |
~ObservationBuffer() {} |
@@ -77,12 +79,11 @@ class NET_EXPORT_PRIVATE ObservationBuffer { |
// value of all observations made on or after |begin_timestamp|. If the |
// value is unavailable, false is returned and |result| is not modified. |
// Percentile value is unavailable if all the values in observation buffer are |
- // older than |begin_timestamp|. |current_signal_strength_dbm| is the current |
- // signal strength in dBm. |
- // |result| must not be null. |
+ // older than |begin_timestamp|. |current_signal_strength| is the current |
+ // signal strength. |result| must not be null. |
// TODO(tbansal): Move out param |result| as the last param of the function. |
bool GetPercentile(base::TimeTicks begin_timestamp, |
- int32_t current_signal_strength_dbm, |
+ const base::Optional<int32_t>& current_signal_strength, |
ValueType* result, |
int percentile, |
const std::vector<NetworkQualityObservationSource>& |
@@ -93,7 +94,7 @@ class NET_EXPORT_PRIVATE ObservationBuffer { |
// Total weight of all observations in |weighted_observations|. |
double total_weight = 0.0; |
- ComputeWeightedObservations(begin_timestamp, current_signal_strength_dbm, |
+ ComputeWeightedObservations(begin_timestamp, current_signal_strength, |
&weighted_observations, &total_weight, |
disallowed_observation_sources); |
if (weighted_observations.empty()) |
@@ -125,20 +126,21 @@ class NET_EXPORT_PRIVATE ObservationBuffer { |
// of all observations made on or after |begin_timestamp|. If the value is |
// unavailable, false is returned and |result| is not modified. The unweighted |
// average value is unavailable if all the values in the observation buffer |
- // are older than |begin_timestamp|. |current_signal_strength_dbm| is the |
- // current signal strength in dBm. |result| must not be null. |
- bool GetWeightedAverage(base::TimeTicks begin_timestamp, |
- int32_t current_signal_strength_dbm, |
- const std::vector<NetworkQualityObservationSource>& |
- disallowed_observation_sources, |
- ValueType* result) const { |
+ // are older than |begin_timestamp|. |current_signal_strength| is the |
+ // current signal strength. |result| must not be null. |
+ bool GetWeightedAverage( |
+ base::TimeTicks begin_timestamp, |
+ const base::Optional<int32_t>& current_signal_strength, |
+ const std::vector<NetworkQualityObservationSource>& |
+ disallowed_observation_sources, |
+ ValueType* result) const { |
// Stores weighted observations in increasing order by value. |
std::vector<WeightedObservation<ValueType>> weighted_observations; |
// Total weight of all observations in |weighted_observations|. |
double total_weight = 0.0; |
- ComputeWeightedObservations(begin_timestamp, current_signal_strength_dbm, |
+ ComputeWeightedObservations(begin_timestamp, current_signal_strength, |
&weighted_observations, &total_weight, |
disallowed_observation_sources); |
if (weighted_observations.empty()) |
@@ -162,20 +164,21 @@ class NET_EXPORT_PRIVATE ObservationBuffer { |
// all observations made on or after |begin_timestamp|. If the value is |
// unavailable, false is returned and |result| is not modified. The weighted |
// average value is unavailable if all the values in the observation buffer |
- // are older than |begin_timestamp|. |current_signal_strength_dbm| is the |
- // current signal strength in dBm. |result| must not be null. |
- bool GetUnweightedAverage(base::TimeTicks begin_timestamp, |
- int32_t current_signal_strength_dbm, |
- const std::vector<NetworkQualityObservationSource>& |
- disallowed_observation_sources, |
- ValueType* result) const { |
+ // are older than |begin_timestamp|. |current_signal_strength| is the |
+ // current signal strength. |result| must not be null. |
+ bool GetUnweightedAverage( |
+ base::TimeTicks begin_timestamp, |
+ const base::Optional<int32_t>& current_signal_strength, |
+ const std::vector<NetworkQualityObservationSource>& |
+ disallowed_observation_sources, |
+ ValueType* result) const { |
// Stores weighted observations in increasing order by value. |
std::vector<WeightedObservation<ValueType>> weighted_observations; |
// Total weight of all observations in |weighted_observations|. |
double total_weight = 0.0; |
- ComputeWeightedObservations(begin_timestamp, current_signal_strength_dbm, |
+ ComputeWeightedObservations(begin_timestamp, current_signal_strength, |
&weighted_observations, &total_weight, |
disallowed_observation_sources); |
if (weighted_observations.empty()) |
@@ -218,14 +221,13 @@ class NET_EXPORT_PRIVATE ObservationBuffer { |
// 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 |
- // considered. |current_signal_strength_dbm| is the current signal strength |
- // (in dBm) when the observation was taken, and is set to INT32_MIN if the |
- // signal strength is currently unavailable. This method also sets |
- // |total_weight| to the total weight of all observations. Should be called |
- // only when there is at least one observation in the buffer. |
+ // considered. |current_signal_strength| is the current signal strength |
+ // when the observation was taken. This method also sets |total_weight| to |
+ // the total weight of all observations. Should be called only when there is |
+ // at least one observation in the buffer. |
void ComputeWeightedObservations( |
const base::TimeTicks& begin_timestamp, |
- int32_t current_signal_strength_dbm, |
+ const base::Optional<int32_t>& current_signal_strength, |
std::vector<WeightedObservation<ValueType>>* weighted_observations, |
double* total_weight, |
const std::vector<NetworkQualityObservationSource>& |
@@ -251,14 +253,12 @@ class NET_EXPORT_PRIVATE ObservationBuffer { |
time_since_sample_taken.InSeconds()); |
double signal_strength_weight = 1.0; |
- if (current_signal_strength_dbm != INT32_MIN && |
- observation.signal_strength_dbm != INT32_MIN && |
- current_signal_strength_dbm != INT32_MAX && |
- observation.signal_strength_dbm != INT32_MAX) { |
- int32_t signal_strength_weight_diff = std::abs( |
- current_signal_strength_dbm - observation.signal_strength_dbm); |
- signal_strength_weight = |
- pow(weight_multiplier_per_dbm_, signal_strength_weight_diff); |
+ if (current_signal_strength && observation.signal_strength) { |
+ int32_t signal_strength_weight_diff = |
+ std::abs(current_signal_strength.value() - |
+ observation.signal_strength.value()); |
+ signal_strength_weight = pow(weight_multiplier_per_signal_level_, |
+ signal_strength_weight_diff); |
} |
double weight = time_weight * signal_strength_weight; |
@@ -294,13 +294,13 @@ class NET_EXPORT_PRIVATE ObservationBuffer { |
// weight_multiplier_per_second_ ^ kHalfLifeSeconds = 0.5 |
const double weight_multiplier_per_second_; |
- // The factor by which the weight of an observation reduces for every dbM |
+ // The factor by which the weight of an observation reduces for every unit |
// difference in the current signal strength, and the signal strength at |
// which the observation was taken. |
- // For example, if the observation was taken at 90 dBm, and current signal |
- // strength is 95 dBm, the weight of the observation would be: |
- // |weight_multiplier_per_dbm_| ^ 5. |
- const double weight_multiplier_per_dbm_; |
+ // For example, if the observation was taken at 1 unit, and current signal |
+ // strength is 4 units, the weight of the observation would be: |
+ // |weight_multiplier_per_signal_level_| ^ 3. |
+ const double weight_multiplier_per_signal_level_; |
std::unique_ptr<base::TickClock> tick_clock_; |