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

Unified Diff: chrome/browser/devtools/devtools_network_controller.cc

Issue 324953002: DevToolsNetworkController: support "limit throughput" network condition. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Resolved TODO Created 6 years, 6 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/devtools/devtools_network_controller.cc
diff --git a/chrome/browser/devtools/devtools_network_controller.cc b/chrome/browser/devtools/devtools_network_controller.cc
index 362dc467fe645c4b010b55f2cdf8779671135647..9734a0136f5c080aec174565c0904801647a97ca 100644
--- a/chrome/browser/devtools/devtools_network_controller.cc
+++ b/chrome/browser/devtools/devtools_network_controller.cc
@@ -4,6 +4,7 @@
#include "chrome/browser/devtools/devtools_network_controller.h"
+#include "base/time/time.h"
#include "chrome/browser/devtools/devtools_network_conditions.h"
#include "chrome/browser/devtools/devtools_network_transaction.h"
#include "chrome/browser/profiles/profile.h"
@@ -17,6 +18,13 @@ namespace {
const char kDevToolsRequestInitiator[] = "X-DevTools-Request-Initiator";
+struct Throttle {
+ DevToolsNetworkTransaction* transaction;
+ int64_t penalty;
+};
+
+int64_t kPacketSize = 1500;
+
} // namespace
DevToolsNetworkController::DevToolsNetworkController()
@@ -37,6 +45,15 @@ void DevToolsNetworkController::RemoveTransaction(
DCHECK(thread_checker_.CalledOnValidThread());
DCHECK(transactions_.find(transaction) != transactions_.end());
transactions_.erase(transaction);
+
+ size_t length = throttles_.size();
+ if (!length)
vsevik 2014/06/10 17:24:19 redundant
eustas 2014/06/11 10:54:25 Done.
+ return;
+
+ for (size_t i = 0; i < length; ++i) {
+ if (throttles_[i].transaction == transaction)
+ throttles_[i].transaction = NULL;
+ }
}
void DevToolsNetworkController::SetNetworkState(
@@ -57,27 +74,52 @@ void DevToolsNetworkController::SetNetworkStateOnIO(
const std::string& client_id,
const scoped_refptr<DevToolsNetworkConditions> conditions) {
DCHECK(thread_checker_.CalledOnValidThread());
+ bool was_throttling = conditions_ && conditions_->IsThrottling();
vsevik 2014/06/10 17:24:19 Can we put the line below to the beginning of this
eustas 2014/06/11 10:54:26 Done.
+
if (!conditions) {
if (client_id == active_client_id_) {
conditions_ = NULL;
active_client_id_ = std::string();
+ if (was_throttling)
+ CancelThrottles();
}
return;
}
+
+ // Prepare to update throughput.
+ if (was_throttling)
vsevik 2014/06/10 17:24:19 I think this calls should be noops in case we were
eustas 2014/06/11 10:54:25 Done.
+ UpdateThrottles();
+
conditions_ = conditions;
active_client_id_ = client_id;
- // Iterate over a copy of set, because failing of transaction could result in
- // creating a new one, or (theoretically) destroying one.
- Transactions old_transactions(transactions_);
- for (Transactions::iterator it = old_transactions.begin();
- it != old_transactions.end(); ++it) {
- if (transactions_.find(*it) == transactions_.end())
- continue;
- if (!(*it)->request() || (*it)->failed())
- continue;
- if (ShouldFail((*it)->request()))
- (*it)->Fail();
+ if (was_throttling)
+ FireThrottledCallbacks();
+
+ if (conditions_->IsOffline()) {
+ // Iterate over a copy of set, because failing of transaction could
+ // result in creating a new one, or (theoretically) destroying one.
+ Transactions old_transactions(transactions_);
+ for (Transactions::iterator it = old_transactions.begin();
+ it != old_transactions.end(); ++it) {
+ if (transactions_.find(*it) == transactions_.end())
+ continue;
+ if (!(*it)->request() || (*it)->failed())
+ continue;
+ if (ShouldFail((*it)->request()))
+ (*it)->Fail();
+ }
+ }
+
+ if (conditions_->IsThrottling()) {
+ DCHECK(conditions_->maximal_throughput() != 0);
+ offset_ = base::TimeTicks::Now();
+ last_tick_ = 0;
+ tick_length_ = base::TimeDelta::FromMicroseconds(
+ 1000000L * kPacketSize / conditions_->maximal_throughput());
+ ArmTimer();
+ } else if (was_throttling) {
+ CancelThrottles();
vsevik 2014/06/10 17:24:19 Can we join two Cancel... calls?
eustas 2014/06/11 10:54:26 Done.
}
}
@@ -93,3 +135,111 @@ bool DevToolsNetworkController::ShouldFail(
return !request->extra_headers.HasHeader(kDevToolsRequestInitiator);
}
+
+bool DevToolsNetworkController::ShouldThrottle(
+ const net::HttpRequestInfo* request) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ DCHECK(request);
+ if (!conditions_ || !conditions_->IsThrottling())
+ return false;
+
+ if (!conditions_->HasMatchingDomain(request->url))
+ return false;
+
+ return !request->extra_headers.HasHeader(kDevToolsRequestInitiator);
+}
+
+void DevToolsNetworkController::UpdateThrottles() {
+ int64_t last_tick = (base::TimeTicks::Now() - offset_) / tick_length_;
+ int64_t ticks = last_tick - last_tick_;
+ last_tick_ = last_tick;
+
+ int64_t length = throttles_.size();
+ if (!length)
+ return;
+
+ int64_t full_rounds = ticks / length;
+ int64_t reminder = ticks % length;
vsevik 2014/06/10 17:24:19 remainder :)
vsevik 2014/06/11 08:39:32 last_throttle_to_get_packet
eustas 2014/06/11 10:54:25 Done.
eustas 2014/06/11 10:54:26 Thanks =)
+ Throttles throttles;
+ for (int64_t i = reminder; i < length; ++i) {
+ throttles.push_back(throttles_[i]);
vsevik 2014/06/11 08:39:32 std::rotate FTW!
eustas 2014/06/11 10:54:25 Done.
+ throttles.back().penalty -= (full_rounds * kPacketSize);
+ }
+ for (int64_t i = 0; i < reminder; ++i) {
+ throttles.push_back(throttles_[i]);
+ throttles.back().penalty -= ((full_rounds + 1) * kPacketSize);
+ }
+ throttles_.swap(throttles);
+}
+
+void DevToolsNetworkController::CancelThrottles() {
+ int64_t length = throttles_.size();
+ if (!length)
+ return;
+
+ Throttles throttles;
+ throttles_.swap(throttles);
+ for (int64_t i = 0; i < length; ++i) {
+ DevToolsNetworkTransaction* transaction = throttles[i].transaction;
+ if (transaction)
+ transaction->FireThrottledCallback();
+ }
+}
+
+void DevToolsNetworkController::FireThrottledCallbacks() {
+ size_t length = throttles_.size();
+ if (!length)
+ return;
+
+ Throttles active;
+ Throttles finished;
+ for (size_t i = 0; i < length; ++i) {
+ if (throttles_[i].penalty <= 0)
+ finished.push_back(throttles_[i]);
+ else
+ active.push_back(throttles_[i]);
+ }
+ throttles_.swap(active);
+
+ length = finished.size();
+ for (size_t i = 0; i < length; ++i) {
+ DevToolsNetworkTransaction* transaction = finished[i].transaction;
+ if (transaction)
+ transaction->FireThrottledCallback();
+ }
+}
+
+void DevToolsNetworkController::OnTimer() {
+ UpdateThrottles();
+ FireThrottledCallbacks();
+ ArmTimer();
+}
+
+void DevToolsNetworkController::ArmTimer() {
+ size_t length = throttles_.size();
+ if (!length)
+ return;
+ int64_t best_n;
vsevik 2014/06/11 08:39:32 min_ticks_left
eustas 2014/06/11 10:54:25 Done.
+ for (size_t i = 0; i < length; ++i) {
+ int64_t n1 = (throttles_[i].penalty + kPacketSize - 1) / kPacketSize;
vsevik 2014/06/11 08:39:32 penalty_packets_left
eustas 2014/06/11 10:54:25 Done.
+ int64_t n = (i + 1) + length * (n1 - 1);
vsevik 2014/06/11 08:39:32 ticksLeft
eustas 2014/06/11 10:54:26 Done.
+ if (i == 0 || n < best_n)
+ best_n = n;
+ }
+ base::TimeDelta desired_time = offset_ + tick_length_ * (last_tick_ + best_n);
+ timer_.Start(
+ FROM_HERE,
+ desired_time - base::TimeTicks::Now(),
+ base::Bind(
+ &DevToolsNetworkController::OnTimer,
+ weak_ptr_factory_.GetWeakPtr()));
+}
+
+void DevToolsNetworkController::ThrottleTransaction(
+ DevToolsNetworkTransaction* transaction,
+ int64_t penalty) {
+ UpdateThrottles();
+ throttles_.push_back({transaction, penalty});
+ FireThrottledCallbacks();
+ ArmTimer();
+}

Powered by Google App Engine
This is Rietveld 408576698