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::ExtensionWebRequestTimeTracker() { | |
31 } | |
32 | |
33 ExtensionWebRequestTimeTracker::~ExtensionWebRequestTimeTracker() { | |
34 } | |
35 | |
36 void ExtensionWebRequestTimeTracker::LogRequestStartTime( | |
37 int64 request_id, const base::Time& start_time, const GURL& url) { | |
38 // Trim old completed request logs. | |
39 while (request_ids_.size() > kMaxRequestsLogged) { | |
40 int64 to_remove = request_ids_.front(); | |
41 request_ids_.pop(); | |
42 std::map<int64, RequestTimeLog>::iterator iter = | |
43 request_time_logs_.find(to_remove); | |
44 if (iter != request_time_logs_.end() && iter->second.completed) { | |
45 request_time_logs_.erase(iter); | |
46 moderate_delays_.erase(to_remove); | |
47 excessive_delays_.erase(to_remove); | |
48 } | |
49 } | |
50 request_ids_.push(request_id); | |
51 | |
52 if (request_time_logs_.find(request_id) != request_time_logs_.end()) { | |
53 RequestTimeLog& log = request_time_logs_[request_id]; | |
54 DCHECK(!log.completed); | |
55 return; | |
56 } | |
57 RequestTimeLog& log = request_time_logs_[request_id]; | |
58 log.request_start_time = start_time; | |
59 log.url = url; | |
60 } | |
61 | |
62 void ExtensionWebRequestTimeTracker::LogRequestEndTime( | |
63 int64 request_id, const base::Time& end_time) { | |
64 if (request_time_logs_.find(request_id) == request_time_logs_.end()) | |
65 return; | |
66 | |
67 RequestTimeLog& log = request_time_logs_[request_id]; | |
68 if (log.completed) | |
69 return; | |
70 | |
71 log.request_duration = end_time - log.request_start_time; | |
72 log.completed = true; | |
73 | |
74 if (log.extension_block_durations.empty()) | |
75 return; | |
76 | |
77 HISTOGRAM_TIMES("Extensions.NetworkDelay", log.block_duration); | |
78 | |
79 Analyze(request_id); | |
80 } | |
81 | |
82 void ExtensionWebRequestTimeTracker::Analyze(int64 request_id) { | |
83 RequestTimeLog& log = request_time_logs_[request_id]; | |
84 | |
85 // Ignore really short requests. Time spent on these is negligible, and any | |
86 // extra delay the extension adds is likely to be noise. | |
87 if (log.request_duration.InMilliseconds() < kMinRequestTimeToCareMs) | |
88 return; | |
89 | |
90 double percentage = | |
91 log.block_duration.InMillisecondsF() / | |
92 log.request_duration.InMillisecondsF(); | |
93 LOG(ERROR) << "WR percent " << request_id << ": " << log.url << ": " << | |
94 log.block_duration.InMilliseconds() << "/" << | |
95 log.request_duration.InMilliseconds() << " = " << percentage; | |
96 | |
97 // TODO(mpcomplete): need actual UI for the warning. | |
98 // TODO(mpcomplete): blame a specific extension. Maybe go through the list | |
99 // of recent requests and find the extension that has caused the most delays. | |
100 if (percentage > kThresholdExcessiveDelay) { | |
101 excessive_delays_.insert(request_id); | |
102 if (excessive_delays_.size() > kNumExcessiveDelaysBeforeWarning) { | |
103 LOG(ERROR) << "WR excessive delays:" << excessive_delays_.size(); | |
104 } | |
105 } else if (percentage > kThresholdModerateDelay) { | |
106 moderate_delays_.insert(request_id); | |
107 if (moderate_delays_.size() > kNumModerateDelaysBeforeWarning) { | |
108 LOG(ERROR) << "WR moderate delays:" << moderate_delays_.size(); | |
109 } | |
110 } | |
111 } | |
112 | |
113 void ExtensionWebRequestTimeTracker::IncrementExtensionBlockTime( | |
114 const std::string& extension_id, | |
115 int64 request_id, | |
116 const base::TimeDelta& block_time) { | |
117 if (request_time_logs_.find(request_id) == request_time_logs_.end()) | |
118 return; | |
119 RequestTimeLog& log = request_time_logs_[request_id]; | |
120 log.extension_block_durations[extension_id] += block_time; | |
121 } | |
122 | |
123 void ExtensionWebRequestTimeTracker::IncrementTotalBlockTime( | |
124 int64 request_id, | |
125 const base::TimeDelta& block_time) { | |
126 if (request_time_logs_.find(request_id) == request_time_logs_.end()) | |
127 return; | |
128 RequestTimeLog& log = request_time_logs_[request_id]; | |
129 log.block_duration += block_time; | |
130 } | |
131 | |
132 void ExtensionWebRequestTimeTracker::SetRequestCanceled(int64 request_id) { | |
133 // Canceled requests won't actually hit the network, so we can't compare | |
134 // their request time to the time spent waiting on the extension. Just ignore | |
135 // them. | |
battre
2011/08/22 16:23:47
We might compare the blocking time to the mean/med
| |
136 // TODO(mpcomplete): may want to count cancels as "bonuses" for an extension. | |
137 // i.e. if it slows down 50% of requests but cancels 25% of the rest, that | |
138 // might average out to only being "25% slow". | |
139 request_time_logs_.erase(request_id); | |
140 } | |
141 | |
142 void ExtensionWebRequestTimeTracker::SetRequestRedirected(int64 request_id) { | |
143 // When a request is redirected, we have no way of knowing how long the | |
144 // request would have taken, so we can't say how much an extension slowed | |
145 // down this request. Just ignore it. | |
146 request_time_logs_.erase(request_id); | |
147 } | |
OLD | NEW |