Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #include "chrome/browser/extensions/extension_webrequest_time_tracker.h" | 5 #include "chrome/browser/extensions/extension_webrequest_time_tracker.h" |
| 6 | 6 |
| 7 #include "base/metrics/histogram.h" | 7 #include "base/metrics/histogram.h" |
| 8 | 8 |
| 9 // TODO(mpcomplete): tweak all these constants. | 9 // TODO(mpcomplete): tweak all these constants. |
| 10 namespace { | 10 namespace { |
| 11 // The number of requests we keep track of at a time. | 11 // The number of requests we keep track of at a time. |
| 12 const size_t kMaxRequestsLogged = 100u; | 12 const size_t kMaxRequestsLogged = 100u; |
| 13 | 13 |
| 14 // If a request completes faster than this amount (in ms), then we ignore it. | 14 // If a request completes faster than this amount (in ms), then we ignore it. |
| 15 // Any delays on such a request was negligible. | 15 // Any delays on such a request was negligible. |
| 16 const int kMinRequestTimeToCareMs = 10; | 16 const int kMinRequestTimeToCareMs = 10; |
| 17 | 17 |
| 18 // TODO(battre): remove TRIGGER_WARNINGS_EARLY before committing the CL. | |
| 19 // This is just to simplify manual testing. | |
| 20 // #define TRIGGER_WARNINGS_EARLY | |
| 21 #ifndef TRIGGER_WARNINGS_EARLY | |
| 18 // Thresholds above which we consider a delay caused by an extension to be "too | 22 // Thresholds above which we consider a delay caused by an extension to be "too |
| 19 // much". This is given in percentage of total request time that was spent | 23 // much". This is given in percentage of total request time that was spent |
| 20 // waiting on the extension. | 24 // waiting on the extension. |
| 21 const double kThresholdModerateDelay = 0.20; | 25 const double kThresholdModerateDelay = 0.20; |
| 22 const double kThresholdExcessiveDelay = 0.50; | 26 const double kThresholdExcessiveDelay = 0.50; |
| 23 | 27 |
| 24 // If this many requests (of the past kMaxRequestsLogged) have had "too much" | 28 // If this many requests (of the past kMaxRequestsLogged) have had "too much" |
| 25 // delay, then we will warn the user. | 29 // delay, then we will warn the user. |
| 26 const size_t kNumModerateDelaysBeforeWarning = 50u; | 30 const size_t kNumModerateDelaysBeforeWarning = 50u; |
| 27 const size_t kNumExcessiveDelaysBeforeWarning = 10u; | 31 const size_t kNumExcessiveDelaysBeforeWarning = 10u; |
| 32 #else | |
| 33 const double kThresholdModerateDelay = 0.10; | |
| 34 const double kThresholdExcessiveDelay = 0.20; | |
| 35 const size_t kNumModerateDelaysBeforeWarning = 5u; | |
| 36 const size_t kNumExcessiveDelaysBeforeWarning = 2u; | |
| 37 #endif // TRIGGER_WARNINGS_EARLY | |
| 38 | |
| 28 } // namespace | 39 } // namespace |
| 29 | 40 |
| 30 ExtensionWebRequestTimeTracker::RequestTimeLog::RequestTimeLog() | 41 ExtensionWebRequestTimeTracker::RequestTimeLog::RequestTimeLog() |
| 31 : completed(false) { | 42 : profile(NULL), completed(false) { |
| 32 } | 43 } |
| 33 | 44 |
| 34 ExtensionWebRequestTimeTracker::RequestTimeLog::~RequestTimeLog() { | 45 ExtensionWebRequestTimeTracker::RequestTimeLog::~RequestTimeLog() { |
| 35 } | 46 } |
| 36 | 47 |
| 37 ExtensionWebRequestTimeTracker::ExtensionWebRequestTimeTracker() { | 48 ExtensionWebRequestTimeTracker::ExtensionWebRequestTimeTracker() |
| 49 : delegate_(NULL) { | |
| 38 } | 50 } |
| 39 | 51 |
| 40 ExtensionWebRequestTimeTracker::~ExtensionWebRequestTimeTracker() { | 52 ExtensionWebRequestTimeTracker::~ExtensionWebRequestTimeTracker() { |
| 41 } | 53 } |
| 42 | 54 |
| 43 void ExtensionWebRequestTimeTracker::LogRequestStartTime( | 55 void ExtensionWebRequestTimeTracker::LogRequestStartTime( |
| 44 int64 request_id, const base::Time& start_time, const GURL& url) { | 56 int64 request_id, |
| 57 const base::Time& start_time, | |
| 58 const GURL& url, | |
| 59 void* profile) { | |
| 45 // Trim old completed request logs. | 60 // Trim old completed request logs. |
| 46 while (request_ids_.size() > kMaxRequestsLogged) { | 61 while (request_ids_.size() > kMaxRequestsLogged) { |
| 47 int64 to_remove = request_ids_.front(); | 62 int64 to_remove = request_ids_.front(); |
| 48 request_ids_.pop(); | 63 request_ids_.pop(); |
| 49 std::map<int64, RequestTimeLog>::iterator iter = | 64 std::map<int64, RequestTimeLog>::iterator iter = |
| 50 request_time_logs_.find(to_remove); | 65 request_time_logs_.find(to_remove); |
| 51 if (iter != request_time_logs_.end() && iter->second.completed) { | 66 if (iter != request_time_logs_.end() && iter->second.completed) { |
| 52 request_time_logs_.erase(iter); | 67 request_time_logs_.erase(iter); |
| 53 moderate_delays_.erase(to_remove); | 68 moderate_delays_.erase(to_remove); |
| 54 excessive_delays_.erase(to_remove); | 69 excessive_delays_.erase(to_remove); |
| 55 } | 70 } |
| 56 } | 71 } |
| 57 request_ids_.push(request_id); | 72 request_ids_.push(request_id); |
| 58 | 73 |
| 59 if (request_time_logs_.find(request_id) != request_time_logs_.end()) { | 74 if (request_time_logs_.find(request_id) != request_time_logs_.end()) { |
| 60 RequestTimeLog& log = request_time_logs_[request_id]; | 75 RequestTimeLog& log = request_time_logs_[request_id]; |
| 61 DCHECK(!log.completed); | 76 DCHECK(!log.completed); |
| 62 return; | 77 return; |
| 63 } | 78 } |
| 64 RequestTimeLog& log = request_time_logs_[request_id]; | 79 RequestTimeLog& log = request_time_logs_[request_id]; |
| 65 log.request_start_time = start_time; | 80 log.request_start_time = start_time; |
| 66 log.url = url; | 81 log.url = url; |
| 82 log.profile = profile; | |
| 67 } | 83 } |
| 68 | 84 |
| 69 void ExtensionWebRequestTimeTracker::LogRequestEndTime( | 85 void ExtensionWebRequestTimeTracker::LogRequestEndTime( |
| 70 int64 request_id, const base::Time& end_time) { | 86 int64 request_id, const base::Time& end_time) { |
| 71 if (request_time_logs_.find(request_id) == request_time_logs_.end()) | 87 if (request_time_logs_.find(request_id) == request_time_logs_.end()) |
| 72 return; | 88 return; |
| 73 | 89 |
| 74 RequestTimeLog& log = request_time_logs_[request_id]; | 90 RequestTimeLog& log = request_time_logs_[request_id]; |
| 75 if (log.completed) | 91 if (log.completed) |
| 76 return; | 92 return; |
| 77 | 93 |
| 78 log.request_duration = end_time - log.request_start_time; | 94 log.request_duration = end_time - log.request_start_time; |
| 79 log.completed = true; | 95 log.completed = true; |
| 80 | 96 |
| 81 if (log.extension_block_durations.empty()) | 97 if (log.extension_block_durations.empty()) |
| 82 return; | 98 return; |
| 83 | 99 |
| 84 HISTOGRAM_TIMES("Extensions.NetworkDelay", log.block_duration); | 100 HISTOGRAM_TIMES("Extensions.NetworkDelay", log.block_duration); |
| 85 | 101 |
| 86 Analyze(request_id); | 102 Analyze(request_id); |
| 87 } | 103 } |
| 88 | 104 |
| 105 std::set<std::string> ExtensionWebRequestTimeTracker::GetExtensionIds( | |
|
Matt Perry
2011/10/06 22:55:54
this is probably OK for now, but not really correc
battre
2011/10/07 14:09:24
I agree.
| |
| 106 const RequestTimeLog& log) const { | |
| 107 std::set<std::string> result; | |
| 108 for (std::map<std::string, base::TimeDelta>::const_iterator i = | |
| 109 log.extension_block_durations.begin(); | |
| 110 i != log.extension_block_durations.end(); | |
| 111 ++i) { | |
| 112 result.insert(i->first); | |
| 113 } | |
| 114 return result; | |
| 115 } | |
| 116 | |
| 89 void ExtensionWebRequestTimeTracker::Analyze(int64 request_id) { | 117 void ExtensionWebRequestTimeTracker::Analyze(int64 request_id) { |
| 90 RequestTimeLog& log = request_time_logs_[request_id]; | 118 RequestTimeLog& log = request_time_logs_[request_id]; |
| 91 | 119 |
| 92 // Ignore really short requests. Time spent on these is negligible, and any | 120 // Ignore really short requests. Time spent on these is negligible, and any |
| 93 // extra delay the extension adds is likely to be noise. | 121 // extra delay the extension adds is likely to be noise. |
| 94 if (log.request_duration.InMilliseconds() < kMinRequestTimeToCareMs) | 122 if (log.request_duration.InMilliseconds() < kMinRequestTimeToCareMs) |
| 95 return; | 123 return; |
| 96 | 124 |
| 97 double percentage = | 125 double percentage = |
| 98 log.block_duration.InMillisecondsF() / | 126 log.block_duration.InMillisecondsF() / |
| 99 log.request_duration.InMillisecondsF(); | 127 log.request_duration.InMillisecondsF(); |
| 100 LOG(ERROR) << "WR percent " << request_id << ": " << log.url << ": " << | 128 LOG(ERROR) << "WR percent " << request_id << ": " << log.url << ": " << |
| 101 log.block_duration.InMilliseconds() << "/" << | 129 log.block_duration.InMilliseconds() << "/" << |
| 102 log.request_duration.InMilliseconds() << " = " << percentage; | 130 log.request_duration.InMilliseconds() << " = " << percentage; |
| 103 | 131 |
| 104 // TODO(mpcomplete): need actual UI for the warning. | |
| 105 // TODO(mpcomplete): blame a specific extension. Maybe go through the list | 132 // TODO(mpcomplete): blame a specific extension. Maybe go through the list |
| 106 // of recent requests and find the extension that has caused the most delays. | 133 // of recent requests and find the extension that has caused the most delays. |
| 107 if (percentage > kThresholdExcessiveDelay) { | 134 if (percentage > kThresholdExcessiveDelay) { |
| 108 excessive_delays_.insert(request_id); | 135 excessive_delays_.insert(request_id); |
| 109 if (excessive_delays_.size() > kNumExcessiveDelaysBeforeWarning) { | 136 if (excessive_delays_.size() > kNumExcessiveDelaysBeforeWarning) { |
| 110 LOG(ERROR) << "WR excessive delays:" << excessive_delays_.size(); | 137 LOG(ERROR) << "WR excessive delays:" << excessive_delays_.size(); |
| 138 if (delegate_) { | |
| 139 delegate_->NotifyExcessiveDelays(log.profile, | |
| 140 excessive_delays_.size(), | |
| 141 request_ids_.size(), | |
| 142 GetExtensionIds(log)); | |
| 143 } | |
| 111 } | 144 } |
| 112 } else if (percentage > kThresholdModerateDelay) { | 145 } else if (percentage > kThresholdModerateDelay) { |
| 113 moderate_delays_.insert(request_id); | 146 moderate_delays_.insert(request_id); |
| 114 if (moderate_delays_.size() > kNumModerateDelaysBeforeWarning) { | 147 if (moderate_delays_.size() + excessive_delays_.size() > |
| 148 kNumModerateDelaysBeforeWarning) { | |
| 115 LOG(ERROR) << "WR moderate delays:" << moderate_delays_.size(); | 149 LOG(ERROR) << "WR moderate delays:" << moderate_delays_.size(); |
| 150 if (delegate_) { | |
| 151 delegate_->NotifyModerateDelays( | |
| 152 log.profile, | |
| 153 moderate_delays_.size() + excessive_delays_.size(), | |
| 154 request_ids_.size(), | |
| 155 GetExtensionIds(log)); | |
| 156 } | |
| 116 } | 157 } |
| 117 } | 158 } |
| 118 } | 159 } |
| 119 | 160 |
| 120 void ExtensionWebRequestTimeTracker::IncrementExtensionBlockTime( | 161 void ExtensionWebRequestTimeTracker::IncrementExtensionBlockTime( |
| 121 const std::string& extension_id, | 162 const std::string& extension_id, |
| 122 int64 request_id, | 163 int64 request_id, |
| 123 const base::TimeDelta& block_time) { | 164 const base::TimeDelta& block_time) { |
| 124 if (request_time_logs_.find(request_id) == request_time_logs_.end()) | 165 if (request_time_logs_.find(request_id) == request_time_logs_.end()) |
| 125 return; | 166 return; |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 145 // might average out to only being "25% slow". | 186 // might average out to only being "25% slow". |
| 146 request_time_logs_.erase(request_id); | 187 request_time_logs_.erase(request_id); |
| 147 } | 188 } |
| 148 | 189 |
| 149 void ExtensionWebRequestTimeTracker::SetRequestRedirected(int64 request_id) { | 190 void ExtensionWebRequestTimeTracker::SetRequestRedirected(int64 request_id) { |
| 150 // When a request is redirected, we have no way of knowing how long the | 191 // When a request is redirected, we have no way of knowing how long the |
| 151 // request would have taken, so we can't say how much an extension slowed | 192 // request would have taken, so we can't say how much an extension slowed |
| 152 // down this request. Just ignore it. | 193 // down this request. Just ignore it. |
| 153 request_time_logs_.erase(request_id); | 194 request_time_logs_.erase(request_id); |
| 154 } | 195 } |
| 196 | |
| 197 void ExtensionWebRequestTimeTracker::SetDelegate( | |
| 198 ExtensionWebRequestTimeTrackerDelegate* delegate) { | |
| 199 delegate_ = delegate; | |
| 200 } | |
| OLD | NEW |