Skip to content

Commit

Permalink
Change NQE API to provide Getter for each metric.
Browse files Browse the repository at this point in the history
Change Network Quality Estimator API to provide a separate getter for
each metric.

BUG=502415

Review URL: https://codereview.chromium.org/1264033002

Cr-Commit-Position: refs/heads/master@{#341854}
  • Loading branch information
tarunban authored and Commit bot committed Aug 5, 2015
1 parent 52956e0 commit 91fb588
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 26 deletions.
52 changes: 46 additions & 6 deletions net/base/network_quality_estimator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ void NetworkQualityEstimator::OnConnectionTypeChanged(
// Add the remaining percentile values.
static const int kPercentiles[] = {0, 10, 90, 100};
for (size_t i = 0; i < arraysize(kPercentiles); ++i) {
network_quality = GetEstimate(kPercentiles[i]);
network_quality = GetEstimateInternal(kPercentiles[i]);

rtt_percentile = GetHistogram(
"RTT.Percentile" + base::IntToString(kPercentiles[i]) + ".",
Expand Down Expand Up @@ -481,7 +481,26 @@ bool NetworkQualityEstimator::GetEstimate(NetworkQuality* median) const {
*median = NetworkQuality();
return false;
}
*median = GetEstimate(50);
*median = GetEstimateInternal(50);
return true;
}

bool NetworkQualityEstimator::GetRTTEstimate(base::TimeDelta* rtt) const {
if (rtt_msec_observations_.Size() == 0) {
*rtt = NetworkQuality::InvalidRTT();
return false;
}
*rtt = GetRTTEstimateInternal(50);
return true;
}

bool NetworkQualityEstimator::GetDownlinkThroughputKbpsEstimate(
int32_t* kbps) const {
if (kbps_observations_.Size() == 0) {
*kbps = NetworkQuality::kInvalidThroughput;
return false;
}
*kbps = GetDownlinkThroughputKbpsEstimateInternal(50);
return true;
}

Expand Down Expand Up @@ -529,20 +548,41 @@ void NetworkQualityEstimator::ObservationBuffer::Clear() {
DCHECK(observations_.empty());
}

NetworkQuality NetworkQualityEstimator::GetEstimate(int percentile) const {
NetworkQuality NetworkQualityEstimator::GetEstimateInternal(
int percentile) const {
DCHECK(thread_checker_.CalledOnValidThread());
DCHECK_GE(percentile, 0);
DCHECK_LE(percentile, 100);
DCHECK_GT(kbps_observations_.Size(), 0U);
DCHECK_GT(rtt_msec_observations_.Size(), 0U);

return NetworkQuality(GetRTTEstimateInternal(percentile),
GetDownlinkThroughputKbpsEstimateInternal(percentile));
}

base::TimeDelta NetworkQualityEstimator::GetRTTEstimateInternal(
int percentile) const {
DCHECK(thread_checker_.CalledOnValidThread());
DCHECK_GE(percentile, 0);
DCHECK_LE(percentile, 100);
DCHECK_GT(rtt_msec_observations_.Size(), 0U);

// RTT observations are sorted by duration from shortest to longest, thus
// a higher percentile RTT will have a longer RTT than a lower percentile.
return base::TimeDelta::FromMilliseconds(
rtt_msec_observations_.GetPercentile(percentile));
}

int32_t NetworkQualityEstimator::GetDownlinkThroughputKbpsEstimateInternal(
int percentile) const {
DCHECK(thread_checker_.CalledOnValidThread());
DCHECK_GE(percentile, 0);
DCHECK_LE(percentile, 100);
DCHECK_GT(kbps_observations_.Size(), 0U);

// Throughput observations are sorted by kbps from slowest to fastest,
// thus a higher percentile throughput will be faster than a lower one.
return NetworkQuality(base::TimeDelta::FromMilliseconds(
rtt_msec_observations_.GetPercentile(percentile)),
kbps_observations_.GetPercentile(100 - percentile));
return kbps_observations_.GetPercentile(100 - percentile);
}

void NetworkQualityEstimator::ObservationBuffer::ComputeWeightedObservations(
Expand Down
11 changes: 10 additions & 1 deletion net/base/network_quality_estimator.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ class NET_EXPORT_PRIVATE NetworkQualityEstimator
// value.
virtual bool GetEstimate(NetworkQuality* median) const;

// Returns true if RTT is available and sets |rtt| to estimated RTT.
bool GetRTTEstimate(base::TimeDelta* rtt) const;

// Returns true if downlink throughput is available and sets |kbps| to
// estimated downlink throughput (in Kilobits per second).
bool GetDownlinkThroughputKbpsEstimate(int32_t* kbps) const;

// Notifies NetworkQualityEstimator that a response has been received.
// |cumulative_prefilter_bytes_read| is the count of the bytes received prior
// to applying filters (e.g. decompression, SDCH) from request creation time
Expand Down Expand Up @@ -296,7 +303,9 @@ class NET_EXPORT_PRIVATE NetworkQualityEstimator
// |percentile| is 90, then the network is expected to be faster than the
// returned estimate with 0.9 probability. Similarly, network is expected to
// be slower than the returned estimate with 0.1 probability.
NetworkQuality GetEstimate(int percentile) const;
NetworkQuality GetEstimateInternal(int percentile) const;
base::TimeDelta GetRTTEstimateInternal(int percentile) const;
int32_t GetDownlinkThroughputKbpsEstimateInternal(int percentile) const;

// Returns the current network ID checking by calling the platform APIs.
// Virtualized for testing.
Expand Down
53 changes: 34 additions & 19 deletions net/base/network_quality_estimator_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,19 @@ TEST(NetworkQualityEstimatorTest, TestKbpsRTTUpdates) {
// Both RTT and downstream throughput should be updated.
estimator.NotifyDataReceived(*request, 1000, 1000);
NetworkQuality network_quality = estimator.GetPeakEstimate();
EXPECT_GE(network_quality.rtt(), base::TimeDelta());
EXPECT_LT(network_quality.rtt(), base::TimeDelta::Max());
EXPECT_GE(network_quality.downstream_throughput_kbps(), 1);
EXPECT_NE(NetworkQuality::InvalidRTT(), network_quality.rtt());
EXPECT_NE(NetworkQuality::kInvalidThroughput,
network_quality.downstream_throughput_kbps());

base::TimeDelta rtt = NetworkQuality::InvalidRTT();
int32_t kbps = NetworkQuality::kInvalidThroughput;
EXPECT_TRUE(estimator.GetRTTEstimate(&rtt));
EXPECT_TRUE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps));
EXPECT_NE(NetworkQuality::InvalidRTT(), rtt);
EXPECT_NE(NetworkQuality::kInvalidThroughput, kbps);

EXPECT_NEAR(network_quality.rtt().InMilliseconds(),
estimator.GetEstimate(100).rtt().InMilliseconds(), 1);
estimator.GetEstimateInternal(100).rtt().InMilliseconds(), 1);

// Check UMA histograms.
histogram_tester.ExpectTotalCount("NQE.PeakKbps.Unknown", 0);
Expand All @@ -126,6 +133,10 @@ TEST(NetworkQualityEstimatorTest, TestKbpsRTTUpdates) {
EXPECT_EQ(NetworkQuality::InvalidRTT(), network_quality.rtt());
EXPECT_EQ(NetworkQuality::kInvalidThroughput,
network_quality.downstream_throughput_kbps());
EXPECT_FALSE(estimator.GetRTTEstimate(&rtt));
EXPECT_FALSE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps));
EXPECT_EQ(NetworkQuality::InvalidRTT(), rtt);
EXPECT_EQ(NetworkQuality::kInvalidThroughput, kbps);

estimator.SimulateNetworkChangeTo(
NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI, std::string());
Expand All @@ -135,6 +146,8 @@ TEST(NetworkQualityEstimatorTest, TestKbpsRTTUpdates) {
EXPECT_EQ(NetworkQuality::InvalidRTT(), network_quality.rtt());
EXPECT_EQ(NetworkQuality::kInvalidThroughput,
network_quality.downstream_throughput_kbps());
EXPECT_FALSE(estimator.GetRTTEstimate(&rtt));
EXPECT_FALSE(estimator.GetDownlinkThroughputKbpsEstimate(&kbps));
}

TEST(NetworkQualityEstimatorTest, StoreObservations) {
Expand Down Expand Up @@ -223,17 +236,18 @@ TEST(NetworkQualityEstimatorTest, PercentileSameTimestamps) {
// required because computed percentiles may be slightly different from
// what is expected due to floating point computation errors and integer
// rounding off errors.
EXPECT_NEAR(estimator.GetEstimate(i).downstream_throughput_kbps(), 100 - i,
1);
EXPECT_NEAR(estimator.GetEstimate(i).rtt().InMilliseconds(), i, 1);
EXPECT_NEAR(estimator.GetEstimateInternal(i).downstream_throughput_kbps(),
100 - i, 1);
EXPECT_NEAR(estimator.GetEstimateInternal(i).rtt().InMilliseconds(), i, 1);
}

EXPECT_TRUE(estimator.GetEstimate(&network_quality));
// |network_quality| should be equal to the 50 percentile value.
EXPECT_EQ(network_quality.downstream_throughput_kbps() > 0,
estimator.GetEstimate(50).downstream_throughput_kbps() > 0);
EXPECT_EQ(network_quality.rtt() != NetworkQuality::InvalidRTT(),
estimator.GetEstimate(50).rtt() != NetworkQuality::InvalidRTT());
estimator.GetEstimateInternal(50).downstream_throughput_kbps() > 0);
EXPECT_EQ(
network_quality.rtt() != NetworkQuality::InvalidRTT(),
estimator.GetEstimateInternal(50).rtt() != NetworkQuality::InvalidRTT());
}

// Verifies that the percentiles are correctly computed. Observations have
Expand Down Expand Up @@ -270,10 +284,10 @@ TEST(NetworkQualityEstimatorTest, PercentileDifferentTimestamps) {
// required because computed percentiles may be slightly different from
// what is expected due to floating point computation errors and integer
// rounding off errors.
EXPECT_NEAR(estimator.GetEstimate(i).downstream_throughput_kbps(),
EXPECT_NEAR(estimator.GetEstimateInternal(i).downstream_throughput_kbps(),
51 + 0.49 * (100 - i), 1);
EXPECT_NEAR(estimator.GetEstimate(i).rtt().InMilliseconds(), 51 + 0.49 * i,
1);
EXPECT_NEAR(estimator.GetEstimateInternal(i).rtt().InMilliseconds(),
51 + 0.49 * i, 1);
}
}

Expand Down Expand Up @@ -314,17 +328,18 @@ TEST(NetworkQualityEstimatorTest, ComputedPercentiles) {

// Verify the percentiles through simple tests.
for (int i = 0; i <= 100; ++i) {
EXPECT_GT(estimator.GetEstimate(i).downstream_throughput_kbps(), 0);
EXPECT_LT(estimator.GetEstimate(i).rtt(), base::TimeDelta::Max());
EXPECT_GT(estimator.GetEstimateInternal(i).downstream_throughput_kbps(), 0);
EXPECT_LT(estimator.GetEstimateInternal(i).rtt(), base::TimeDelta::Max());

if (i != 0) {
// Throughput percentiles are in decreasing order.
EXPECT_LE(estimator.GetEstimate(i).downstream_throughput_kbps(),
estimator.GetEstimate(i - 1).downstream_throughput_kbps());
EXPECT_LE(
estimator.GetEstimateInternal(i).downstream_throughput_kbps(),
estimator.GetEstimateInternal(i - 1).downstream_throughput_kbps());

// RTT percentiles are in increasing order.
EXPECT_GE(estimator.GetEstimate(i).rtt(),
estimator.GetEstimate(i - 1).rtt());
EXPECT_GE(estimator.GetEstimateInternal(i).rtt(),
estimator.GetEstimateInternal(i - 1).rtt());
}
}
}
Expand Down

0 comments on commit 91fb588

Please sign in to comment.