OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/extensions/extension_webrequest_time_tracker.h" |
| 6 |
| 7 #include "base/metrics/histogram.h" |
| 8 |
| 9 // TODO(mpcomplete): tweak all these constants. |
| 10 namespace { |
| 11 // The number of requests we keep track of at a time. |
| 12 const size_t kMaxRequestsLogged = 100u; |
| 13 |
| 14 // If a request completes faster than this amount (in ms), then we ignore it. |
| 15 // Any delays on such a request was negligible. |
| 16 const int kMinRequestTimeToCareMs = 10; |
| 17 |
| 18 // 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 |
| 20 // waiting on the extension. |
| 21 const double kThresholdModerateDelay = 0.20; |
| 22 const double kThresholdExcessiveDelay = 0.50; |
| 23 |
| 24 // If this many requests (of the past kMaxRequestsLogged) have had "too much" |
| 25 // delay, then we will warn the user. |
| 26 const size_t kNumModerateDelaysBeforeWarning = 50u; |
| 27 const size_t kNumExcessiveDelaysBeforeWarning = 10u; |
| 28 } // namespace |
| 29 |
| 30 ExtensionWebRequestTimeTracker::RequestTimeLog::RequestTimeLog() |
| 31 : completed(false) { |
| 32 } |
| 33 |
| 34 ExtensionWebRequestTimeTracker::RequestTimeLog::~RequestTimeLog() { |
| 35 } |
| 36 |
| 37 ExtensionWebRequestTimeTracker::ExtensionWebRequestTimeTracker() { |
| 38 } |
| 39 |
| 40 ExtensionWebRequestTimeTracker::~ExtensionWebRequestTimeTracker() { |
| 41 } |
| 42 |
| 43 void ExtensionWebRequestTimeTracker::LogRequestStartTime( |
| 44 int64 request_id, const base::Time& start_time, const GURL& url) { |
| 45 // Trim old completed request logs. |
| 46 while (request_ids_.size() > kMaxRequestsLogged) { |
| 47 int64 to_remove = request_ids_.front(); |
| 48 request_ids_.pop(); |
| 49 std::map<int64, RequestTimeLog>::iterator iter = |
| 50 request_time_logs_.find(to_remove); |
| 51 if (iter != request_time_logs_.end() && iter->second.completed) { |
| 52 request_time_logs_.erase(iter); |
| 53 moderate_delays_.erase(to_remove); |
| 54 excessive_delays_.erase(to_remove); |
| 55 } |
| 56 } |
| 57 request_ids_.push(request_id); |
| 58 |
| 59 if (request_time_logs_.find(request_id) != request_time_logs_.end()) { |
| 60 RequestTimeLog& log = request_time_logs_[request_id]; |
| 61 DCHECK(!log.completed); |
| 62 return; |
| 63 } |
| 64 RequestTimeLog& log = request_time_logs_[request_id]; |
| 65 log.request_start_time = start_time; |
| 66 log.url = url; |
| 67 } |
| 68 |
| 69 void ExtensionWebRequestTimeTracker::LogRequestEndTime( |
| 70 int64 request_id, const base::Time& end_time) { |
| 71 if (request_time_logs_.find(request_id) == request_time_logs_.end()) |
| 72 return; |
| 73 |
| 74 RequestTimeLog& log = request_time_logs_[request_id]; |
| 75 if (log.completed) |
| 76 return; |
| 77 |
| 78 log.request_duration = end_time - log.request_start_time; |
| 79 log.completed = true; |
| 80 |
| 81 if (log.extension_block_durations.empty()) |
| 82 return; |
| 83 |
| 84 HISTOGRAM_TIMES("Extensions.NetworkDelay", log.block_duration); |
| 85 |
| 86 Analyze(request_id); |
| 87 } |
| 88 |
| 89 void ExtensionWebRequestTimeTracker::Analyze(int64 request_id) { |
| 90 RequestTimeLog& log = request_time_logs_[request_id]; |
| 91 |
| 92 // Ignore really short requests. Time spent on these is negligible, and any |
| 93 // extra delay the extension adds is likely to be noise. |
| 94 if (log.request_duration.InMilliseconds() < kMinRequestTimeToCareMs) |
| 95 return; |
| 96 |
| 97 double percentage = |
| 98 log.block_duration.InMillisecondsF() / |
| 99 log.request_duration.InMillisecondsF(); |
| 100 LOG(ERROR) << "WR percent " << request_id << ": " << log.url << ": " << |
| 101 log.block_duration.InMilliseconds() << "/" << |
| 102 log.request_duration.InMilliseconds() << " = " << percentage; |
| 103 |
| 104 // TODO(mpcomplete): need actual UI for the warning. |
| 105 // 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. |
| 107 if (percentage > kThresholdExcessiveDelay) { |
| 108 excessive_delays_.insert(request_id); |
| 109 if (excessive_delays_.size() > kNumExcessiveDelaysBeforeWarning) { |
| 110 LOG(ERROR) << "WR excessive delays:" << excessive_delays_.size(); |
| 111 } |
| 112 } else if (percentage > kThresholdModerateDelay) { |
| 113 moderate_delays_.insert(request_id); |
| 114 if (moderate_delays_.size() > kNumModerateDelaysBeforeWarning) { |
| 115 LOG(ERROR) << "WR moderate delays:" << moderate_delays_.size(); |
| 116 } |
| 117 } |
| 118 } |
| 119 |
| 120 void ExtensionWebRequestTimeTracker::IncrementExtensionBlockTime( |
| 121 const std::string& extension_id, |
| 122 int64 request_id, |
| 123 const base::TimeDelta& block_time) { |
| 124 if (request_time_logs_.find(request_id) == request_time_logs_.end()) |
| 125 return; |
| 126 RequestTimeLog& log = request_time_logs_[request_id]; |
| 127 log.extension_block_durations[extension_id] += block_time; |
| 128 } |
| 129 |
| 130 void ExtensionWebRequestTimeTracker::IncrementTotalBlockTime( |
| 131 int64 request_id, |
| 132 const base::TimeDelta& block_time) { |
| 133 if (request_time_logs_.find(request_id) == request_time_logs_.end()) |
| 134 return; |
| 135 RequestTimeLog& log = request_time_logs_[request_id]; |
| 136 log.block_duration += block_time; |
| 137 } |
| 138 |
| 139 void ExtensionWebRequestTimeTracker::SetRequestCanceled(int64 request_id) { |
| 140 // Canceled requests won't actually hit the network, so we can't compare |
| 141 // their request time to the time spent waiting on the extension. Just ignore |
| 142 // them. |
| 143 // TODO(mpcomplete): may want to count cancels as "bonuses" for an extension. |
| 144 // i.e. if it slows down 50% of requests but cancels 25% of the rest, that |
| 145 // might average out to only being "25% slow". |
| 146 request_time_logs_.erase(request_id); |
| 147 } |
| 148 |
| 149 void ExtensionWebRequestTimeTracker::SetRequestRedirected(int64 request_id) { |
| 150 // 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 |
| 152 // down this request. Just ignore it. |
| 153 request_time_logs_.erase(request_id); |
| 154 } |
OLD | NEW |