Introduce the PlatformNotificationContext class.
[chromium-blink-merge.git] / chrome / renderer / page_load_histograms.cc
blob91ca92b112a42fb9710dba230d3a70f8f115acbb
1 // Copyright (c) 2012 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.
5 #include "chrome/renderer/page_load_histograms.h"
7 #include <string>
9 #include "base/command_line.h"
10 #include "base/logging.h"
11 #include "base/metrics/field_trial.h"
12 #include "base/metrics/histogram.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/string_split.h"
15 #include "base/strings/string_util.h"
16 #include "base/strings/stringprintf.h"
17 #include "base/strings/utf_string_conversions.h"
18 #include "base/time/time.h"
19 #include "chrome/common/chrome_switches.h"
20 #include "chrome/renderer/chrome_content_renderer_client.h"
21 #include "components/data_reduction_proxy/content/common/data_reduction_proxy_messages.h"
22 #include "content/public/common/content_constants.h"
23 #include "content/public/renderer/document_state.h"
24 #include "content/public/renderer/render_thread.h"
25 #include "content/public/renderer/render_view.h"
26 #include "extensions/common/url_pattern.h"
27 #include "net/base/url_util.h"
28 #include "net/http/http_response_headers.h"
29 #include "third_party/WebKit/public/platform/WebURLRequest.h"
30 #include "third_party/WebKit/public/platform/WebURLResponse.h"
31 #include "third_party/WebKit/public/web/WebDocument.h"
32 #include "third_party/WebKit/public/web/WebFrame.h"
33 #include "third_party/WebKit/public/web/WebPerformance.h"
34 #include "third_party/WebKit/public/web/WebView.h"
35 #include "url/gurl.h"
37 using blink::WebDataSource;
38 using blink::WebFrame;
39 using blink::WebPerformance;
40 using blink::WebString;
41 using base::Time;
42 using base::TimeDelta;
43 using content::DocumentState;
45 const size_t kPLTCount = 100;
47 namespace {
49 // ID indicating that no GWS-Chrome joint experiment is active.
50 const int kNoExperiment = 0;
52 // Max ID of GWS-Chrome joint experiment. If you change this value, please
53 // update PLT_HISTOGRAM_WITH_GWS_VARIANT accordingly.
54 const int kMaxExperimentID = 20;
56 TimeDelta kPLTMin() {
57 return TimeDelta::FromMilliseconds(10);
59 TimeDelta kPLTMax() {
60 return TimeDelta::FromMinutes(10);
63 // This function corresponds to PLT_HISTOGRAM macro invocation without caching.
64 // Use this for PLT histograms with dynamically generated names, which
65 // otherwise can't use the caching PLT_HISTOGRAM macro without code duplication.
66 void PltHistogramWithNoMacroCaching(const std::string& name,
67 const TimeDelta& sample) {
68 // The parameters should exacly match the parameters in
69 // UMA_HISTOGRAM_CUSTOM_TIMES macro.
70 base::HistogramBase* histogram_pointer = base::Histogram::FactoryTimeGet(
71 name, kPLTMin(), kPLTMax(), kPLTCount,
72 base::HistogramBase::kUmaTargetedHistogramFlag);
73 histogram_pointer->AddTime(sample);
76 // This records UMA corresponding to the PLT_HISTOGRAM macro without caching.
77 void PltHistogramWithGwsPreview(const char* name,
78 const TimeDelta& sample,
79 bool is_preview,
80 int experiment_id) {
81 std::string preview_suffix = is_preview ? "_Preview" : "_NoPreview";
82 PltHistogramWithNoMacroCaching(name + preview_suffix, sample);
84 if (experiment_id != kNoExperiment) {
85 std::string name_with_experiment_id = base::StringPrintf(
86 "%s%s_Experiment%d", name, preview_suffix.c_str(), experiment_id);
87 PltHistogramWithNoMacroCaching(name_with_experiment_id, sample);
91 #define PLT_HISTOGRAM(name, sample) \
92 UMA_HISTOGRAM_CUSTOM_TIMES(name, sample, kPLTMin(), kPLTMax(), kPLTCount);
94 #define PLT_HISTOGRAM_WITH_GWS_VARIANT( \
95 name, sample, came_from_websearch, websearch_chrome_joint_experiment_id, \
96 is_preview) { \
97 PLT_HISTOGRAM(name, sample); \
98 if (came_from_websearch) { \
99 PLT_HISTOGRAM(base::StringPrintf("%s_FromGWS", name), sample) \
100 if (websearch_chrome_joint_experiment_id != kNoExperiment) { \
101 std::string name_with_experiment_id = base::StringPrintf( \
102 "%s_FromGWS_Experiment%d", \
103 name, websearch_chrome_joint_experiment_id); \
104 PltHistogramWithNoMacroCaching(name_with_experiment_id, sample); \
107 PltHistogramWithGwsPreview(name, sample, is_preview, \
108 websearch_chrome_joint_experiment_id); \
111 // In addition to PLT_HISTOGRAM, add the *_DataReductionProxy variant
112 // conditionally. This macro runs only in one thread.
113 #define PLT_HISTOGRAM_DRP( \
114 name, sample, data_reduction_proxy_was_used, scheme_type) \
115 do { \
116 static base::HistogramBase* counter(NULL); \
117 static base::HistogramBase* drp_counter(NULL); \
118 static base::HistogramBase* https_drp_counter(NULL); \
119 if (!counter) { \
120 DCHECK(drp_counter == NULL); \
121 DCHECK(https_drp_counter == NULL); \
122 counter = base::Histogram::FactoryTimeGet( \
123 name, kPLTMin(), kPLTMax(), kPLTCount, \
124 base::Histogram::kUmaTargetedHistogramFlag); \
126 counter->AddTime(sample); \
127 if (!data_reduction_proxy_was_used) break; \
128 if ((scheme_type & URLPattern::SCHEME_HTTPS) == 0) { \
129 if (!https_drp_counter) { \
130 https_drp_counter = base::Histogram::FactoryTimeGet( \
131 std::string(name) + "_DataReductionProxy", \
132 kPLTMin(), kPLTMax(), kPLTCount, \
133 base::Histogram::kUmaTargetedHistogramFlag); \
135 https_drp_counter->AddTime(sample); \
136 } else { \
137 if (!drp_counter) { \
138 drp_counter = base::Histogram::FactoryTimeGet( \
139 std::string(name) + "_HTTPS_DataReductionProxy", \
140 kPLTMin(), kPLTMax(), kPLTCount, \
141 base::Histogram::kUmaTargetedHistogramFlag); \
143 drp_counter->AddTime(sample); \
145 } while (0)
147 // Returns the scheme type of the given URL if its type is one for which we
148 // dump page load histograms. Otherwise returns NULL.
149 URLPattern::SchemeMasks GetSupportedSchemeType(const GURL& url) {
150 if (url.SchemeIs("http"))
151 return URLPattern::SCHEME_HTTP;
152 else if (url.SchemeIs("https"))
153 return URLPattern::SCHEME_HTTPS;
154 return static_cast<URLPattern::SchemeMasks>(0);
157 // Helper function to check for string in 'via' header. Returns true if
158 // |via_value| is one of the values listed in the Via header.
159 bool ViaHeaderContains(WebFrame* frame, const std::string& via_value) {
160 const char kViaHeaderName[] = "Via";
161 std::vector<std::string> values;
162 // Multiple via headers have already been coalesced and hence each value
163 // separated by a comma corresponds to a proxy. The value added by a proxy is
164 // not expected to contain any commas.
165 // Example., Via: 1.0 Compression proxy, 1.1 Google Instant Proxy Preview
166 base::SplitString(
167 frame->dataSource()->response().httpHeaderField(kViaHeaderName).utf8(),
168 ',', &values);
169 return std::find(values.begin(), values.end(), via_value) != values.end();
172 // Returns true if the provided URL is a referrer string that came from
173 // a Google Web Search results page. This is a little non-deterministic
174 // because desktop and mobile websearch differ and sometimes just provide
175 // http://www.google.com/ as the referrer. In the case of /url we can be sure
176 // that it came from websearch but we will be generous and allow for cases
177 // where a non-Google URL was provided a bare Google URL as a referrer.
178 // The domain validation matches the code used by the prerenderer for similar
179 // purposes.
180 // TODO(pmeenan): Remove the fuzzy logic when the referrer is reliable
181 bool IsFromGoogleSearchResult(const GURL& url, const GURL& referrer) {
182 if (!StartsWithASCII(referrer.host(), "www.google.", true))
183 return false;
184 if (StartsWithASCII(referrer.path(), "/url", true))
185 return true;
186 bool is_possible_search_referrer =
187 referrer.path().empty() ||
188 referrer.path() == "/" ||
189 StartsWithASCII(referrer.path(), "/search", true) ||
190 StartsWithASCII(referrer.path(), "/webhp", true);
191 if (is_possible_search_referrer &&
192 !StartsWithASCII(url.host(), "www.google", true))
193 return true;
194 return false;
197 // Extracts a Google Web Search and Chrome joint experiment ID from a referrer
198 // that came from a Google Web Search results page. An experiment ID is embedded
199 // in a query string as a "gcjeid=" parameter value.
200 int GetQueryStringBasedExperiment(const GURL& referrer) {
201 std::string value;
202 if (!net::GetValueForKeyInQuery(referrer, "gcjeid", &value))
203 return kNoExperiment;
205 int experiment_id;
206 if (!base::StringToInt(value, &experiment_id))
207 return kNoExperiment;
208 if (0 < experiment_id && experiment_id <= kMaxExperimentID)
209 return experiment_id;
210 return kNoExperiment;
213 void DumpHistograms(const WebPerformance& performance,
214 DocumentState* document_state,
215 bool data_reduction_proxy_was_used,
216 bool came_from_websearch,
217 int websearch_chrome_joint_experiment_id,
218 bool is_preview,
219 URLPattern::SchemeMasks scheme_type) {
220 // This function records new histograms based on the Navigation Timing
221 // records. As such, the histograms should not depend on the deprecated timing
222 // information collected in DocumentState. However, here for some reason we
223 // check if document_state->request_time() is null. TODO(ppi): find out why
224 // and remove DocumentState from the parameter list.
225 Time request = document_state->request_time();
227 Time navigation_start = Time::FromDoubleT(performance.navigationStart());
228 Time redirect_start = Time::FromDoubleT(performance.redirectStart());
229 Time redirect_end = Time::FromDoubleT(performance.redirectEnd());
230 Time fetch_start = Time::FromDoubleT(performance.fetchStart());
231 Time domain_lookup_start = Time::FromDoubleT(performance.domainLookupStart());
232 Time domain_lookup_end = Time::FromDoubleT(performance.domainLookupEnd());
233 Time connect_start = Time::FromDoubleT(performance.connectStart());
234 Time connect_end = Time::FromDoubleT(performance.connectEnd());
235 Time request_start = Time::FromDoubleT(performance.requestStart());
236 Time response_start = Time::FromDoubleT(performance.responseStart());
237 Time response_end = Time::FromDoubleT(performance.responseEnd());
238 Time dom_loading = Time::FromDoubleT(performance.domLoading());
239 Time dom_interactive = Time::FromDoubleT(performance.domInteractive());
240 Time dom_content_loaded_start =
241 Time::FromDoubleT(performance.domContentLoadedEventStart());
242 Time dom_content_loaded_end =
243 Time::FromDoubleT(performance.domContentLoadedEventEnd());
244 Time load_event_start = Time::FromDoubleT(performance.loadEventStart());
245 Time load_event_end = Time::FromDoubleT(performance.loadEventEnd());
246 Time begin = (request.is_null() ? navigation_start : request_start);
248 DCHECK(!navigation_start.is_null());
250 // It is possible for a document to have navigation_start time, but no
251 // request_start. An example is doing a window.open, which synchronously
252 // loads "about:blank", then using document.write add a meta http-equiv
253 // refresh tag, which causes a navigation. In such case, we will arrive at
254 // this function with no request/response timing data and identical load
255 // start/end values. Avoid logging this case, as it doesn't add any
256 // meaningful information to the histogram.
257 if (request_start.is_null())
258 return;
260 // TODO(dominich): Investigate conditions under which |load_event_start| and
261 // |load_event_end| may be NULL as in the non-PT_ case below. Examples in
262 // http://crbug.com/112006.
263 // DCHECK(!load_event_start.is_null());
264 // DCHECK(!load_event_end.is_null());
266 if (document_state->web_timing_histograms_recorded())
267 return;
268 document_state->set_web_timing_histograms_recorded(true);
270 if (!redirect_start.is_null() && !redirect_end.is_null()) {
271 PLT_HISTOGRAM_DRP("PLT.NT_Redirect",
272 redirect_end - redirect_start,
273 data_reduction_proxy_was_used,
274 scheme_type);
275 PLT_HISTOGRAM_DRP(
276 "PLT.NT_DelayBeforeFetchRedirect",
277 (fetch_start - navigation_start) - (redirect_end - redirect_start),
278 data_reduction_proxy_was_used,
279 scheme_type);
280 } else {
281 PLT_HISTOGRAM_DRP("PLT.NT_DelayBeforeFetch",
282 fetch_start - navigation_start,
283 data_reduction_proxy_was_used,
284 scheme_type);
286 PLT_HISTOGRAM_DRP("PLT.NT_DelayBeforeDomainLookup",
287 domain_lookup_start - fetch_start,
288 data_reduction_proxy_was_used,
289 scheme_type);
290 PLT_HISTOGRAM_DRP("PLT.NT_DomainLookup",
291 domain_lookup_end - domain_lookup_start,
292 data_reduction_proxy_was_used,
293 scheme_type);
294 PLT_HISTOGRAM_DRP("PLT.NT_DelayBeforeConnect",
295 connect_start - domain_lookup_end,
296 data_reduction_proxy_was_used,
297 scheme_type);
298 PLT_HISTOGRAM_DRP("PLT.NT_Connect",
299 connect_end - connect_start,
300 data_reduction_proxy_was_used,
301 scheme_type);
302 PLT_HISTOGRAM_DRP("PLT.NT_DelayBeforeRequest",
303 request_start - connect_end,
304 data_reduction_proxy_was_used,
305 scheme_type);
306 PLT_HISTOGRAM_DRP("PLT.NT_Request",
307 response_start - request_start,
308 data_reduction_proxy_was_used,
309 scheme_type);
310 PLT_HISTOGRAM_DRP("PLT.NT_Response",
311 response_end - response_start,
312 data_reduction_proxy_was_used,
313 scheme_type);
315 if (!dom_loading.is_null()) {
316 PLT_HISTOGRAM_DRP("PLT.NT_DelayBeforeDomLoading",
317 dom_loading - response_start,
318 data_reduction_proxy_was_used,
319 scheme_type);
321 if (!dom_interactive.is_null() && !dom_loading.is_null()) {
322 PLT_HISTOGRAM_DRP("PLT.NT_DomLoading",
323 dom_interactive - dom_loading,
324 data_reduction_proxy_was_used,
325 scheme_type);
327 if (!dom_content_loaded_start.is_null() && !dom_interactive.is_null()) {
328 PLT_HISTOGRAM_DRP("PLT.NT_DomInteractive",
329 dom_content_loaded_start - dom_interactive,
330 data_reduction_proxy_was_used,
331 scheme_type);
333 if (!dom_content_loaded_start.is_null() &&
334 !dom_content_loaded_end.is_null() ) {
335 PLT_HISTOGRAM_DRP("PLT.NT_DomContentLoaded",
336 dom_content_loaded_end - dom_content_loaded_start,
337 data_reduction_proxy_was_used,
338 scheme_type);
340 if (!dom_content_loaded_end.is_null() && !load_event_start.is_null()) {
341 PLT_HISTOGRAM_DRP("PLT.NT_DelayBeforeLoadEvent",
342 load_event_start - dom_content_loaded_end,
343 data_reduction_proxy_was_used,
344 scheme_type);
347 // TODO(simonjam): There is no way to distinguish between abandonment and
348 // intentional Javascript navigation before the load event fires.
349 // TODO(dominich): Load type breakdown
350 if (!load_event_start.is_null()) {
351 PLT_HISTOGRAM_WITH_GWS_VARIANT("PLT.PT_BeginToFinishDoc",
352 load_event_start - begin,
353 came_from_websearch,
354 websearch_chrome_joint_experiment_id,
355 is_preview);
356 PLT_HISTOGRAM_WITH_GWS_VARIANT("PLT.PT_CommitToFinishDoc",
357 load_event_start - response_start,
358 came_from_websearch,
359 websearch_chrome_joint_experiment_id,
360 is_preview);
361 PLT_HISTOGRAM_WITH_GWS_VARIANT("PLT.PT_RequestToFinishDoc",
362 load_event_start - navigation_start,
363 came_from_websearch,
364 websearch_chrome_joint_experiment_id,
365 is_preview);
366 if (data_reduction_proxy_was_used) {
367 if ((scheme_type & URLPattern::SCHEME_HTTPS) == 0) {
368 PLT_HISTOGRAM("PLT.PT_BeginToFinishDoc_DataReductionProxy",
369 load_event_start - begin);
370 PLT_HISTOGRAM("PLT.PT_CommitToFinishDoc_DataReductionProxy",
371 load_event_start - response_start);
372 PLT_HISTOGRAM("PLT.PT_RequestToFinishDoc_DataReductionProxy",
373 load_event_start - navigation_start);
374 } else {
375 PLT_HISTOGRAM("PLT.PT_BeginToFinishDoc_HTTPS_DataReductionProxy",
376 load_event_start - begin);
377 PLT_HISTOGRAM("PLT.PT_CommitToFinishDoc_HTTPS_DataReductionProxy",
378 load_event_start - response_start);
379 PLT_HISTOGRAM("PLT.PT_RequestToFinishDoc_HTTPS_DataReductionProxy",
380 load_event_start - navigation_start);
384 if (!load_event_end.is_null()) {
385 PLT_HISTOGRAM_WITH_GWS_VARIANT("PLT.PT_BeginToFinish",
386 load_event_end - begin,
387 came_from_websearch,
388 websearch_chrome_joint_experiment_id,
389 is_preview);
390 PLT_HISTOGRAM_WITH_GWS_VARIANT("PLT.PT_CommitToFinish",
391 load_event_end - response_start,
392 came_from_websearch,
393 websearch_chrome_joint_experiment_id,
394 is_preview);
395 PLT_HISTOGRAM_WITH_GWS_VARIANT("PLT.PT_RequestToFinish",
396 load_event_end - navigation_start,
397 came_from_websearch,
398 websearch_chrome_joint_experiment_id,
399 is_preview);
400 PLT_HISTOGRAM_WITH_GWS_VARIANT("PLT.PT_StartToFinish",
401 load_event_end - request_start,
402 came_from_websearch,
403 websearch_chrome_joint_experiment_id,
404 is_preview);
405 if (data_reduction_proxy_was_used) {
406 if ((scheme_type & URLPattern::SCHEME_HTTPS) == 0) {
407 PLT_HISTOGRAM("PLT.PT_BeginToFinish_DataReductionProxy",
408 load_event_end - begin);
409 PLT_HISTOGRAM("PLT.PT_CommitToFinish_DataReductionProxy",
410 load_event_end - response_start);
411 PLT_HISTOGRAM("PLT.PT_RequestToFinish_DataReductionProxy",
412 load_event_end - navigation_start);
413 PLT_HISTOGRAM("PLT.PT_StartToFinish_DataReductionProxy",
414 load_event_end - request_start);
415 } else {
416 PLT_HISTOGRAM("PLT.PT_BeginToFinish_HTTPS_DataReductionProxy",
417 load_event_end - begin);
418 PLT_HISTOGRAM("PLT.PT_CommitToFinish_HTTPS_DataReductionProxy",
419 load_event_end - response_start);
420 PLT_HISTOGRAM("PLT.PT_RequestToFinish_HTTPS_DataReductionProxy",
421 load_event_end - navigation_start);
422 PLT_HISTOGRAM("PLT.PT_StartToFinish_HTTPS_DataReductionProxy",
423 load_event_end - request_start);
427 if (!load_event_start.is_null() && !load_event_end.is_null()) {
428 PLT_HISTOGRAM("PLT.PT_FinishDocToFinish",
429 load_event_end - load_event_start);
430 PLT_HISTOGRAM_DRP("PLT.NT_LoadEvent",
431 load_event_end - load_event_start,
432 data_reduction_proxy_was_used,
433 scheme_type);
435 if (data_reduction_proxy_was_used) {
436 if ((scheme_type & URLPattern::SCHEME_HTTPS) == 0) {
437 PLT_HISTOGRAM("PLT.PT_FinishDocToFinish_DataReductionProxy",
438 load_event_end - load_event_start);
439 } else {
440 PLT_HISTOGRAM("PLT.PT_FinishDocToFinish_HTTPS_DataReductionProxy",
441 load_event_end - load_event_start);
445 if (!dom_content_loaded_start.is_null()) {
446 PLT_HISTOGRAM_WITH_GWS_VARIANT("PLT.PT_RequestToDomContentLoaded",
447 dom_content_loaded_start - navigation_start,
448 came_from_websearch,
449 websearch_chrome_joint_experiment_id,
450 is_preview);
451 if (data_reduction_proxy_was_used) {
452 if ((scheme_type & URLPattern::SCHEME_HTTPS) == 0) {
453 PLT_HISTOGRAM("PLT.PT_RequestToDomContentLoaded_DataReductionProxy",
454 dom_content_loaded_start - navigation_start);
455 } else {
456 PLT_HISTOGRAM(
457 "PLT.PT_RequestToDomContentLoaded_HTTPS_DataReductionProxy",
458 dom_content_loaded_start - navigation_start);
462 PLT_HISTOGRAM_WITH_GWS_VARIANT("PLT.PT_BeginToCommit",
463 response_start - begin,
464 came_from_websearch,
465 websearch_chrome_joint_experiment_id,
466 is_preview);
467 PLT_HISTOGRAM_WITH_GWS_VARIANT("PLT.PT_RequestToStart",
468 request_start - navigation_start,
469 came_from_websearch,
470 websearch_chrome_joint_experiment_id,
471 is_preview);
472 PLT_HISTOGRAM_WITH_GWS_VARIANT("PLT.PT_StartToCommit",
473 response_start - request_start,
474 came_from_websearch,
475 websearch_chrome_joint_experiment_id,
476 is_preview);
477 PLT_HISTOGRAM_WITH_GWS_VARIANT("PLT.PT_RequestToCommit",
478 response_start - navigation_start,
479 came_from_websearch,
480 websearch_chrome_joint_experiment_id,
481 is_preview);
482 if (data_reduction_proxy_was_used) {
483 if ((scheme_type & URLPattern::SCHEME_HTTPS) == 0) {
484 PLT_HISTOGRAM("PLT.PT_BeginToCommit_DataReductionProxy",
485 response_start - begin);
486 PLT_HISTOGRAM("PLT.PT_RequestToStart_DataReductionProxy",
487 request_start - navigation_start);
488 PLT_HISTOGRAM("PLT.PT_StartToCommit_DataReductionProxy",
489 response_start - request_start);
490 PLT_HISTOGRAM("PLT.PT_RequestToCommit_DataReductionProxy",
491 response_start - navigation_start);
492 } else {
493 PLT_HISTOGRAM("PLT.PT_BeginToCommit_HTTPS_DataReductionProxy",
494 response_start - begin);
495 PLT_HISTOGRAM("PLT.PT_RequestToStart_HTTPS_DataReductionProxy",
496 request_start - navigation_start);
497 PLT_HISTOGRAM("PLT.PT_StartToCommit_HTTPS_DataReductionProxy",
498 response_start - request_start);
499 PLT_HISTOGRAM("PLT.PT_RequestToCommit_HTTPS_DataReductionProxy",
500 response_start - navigation_start);
505 // These histograms are based on the timing information collected in
506 // DocumentState. They should be transitioned to equivalents based on the
507 // Navigation Timing records (see DumpPerformanceTiming()) or dropped if not
508 // needed. Please do not add new metrics based on DocumentState.
509 void DumpDeprecatedHistograms(const WebPerformance& performance,
510 DocumentState* document_state,
511 bool data_reduction_proxy_was_used,
512 bool came_from_websearch,
513 int websearch_chrome_joint_experiment_id,
514 bool is_preview,
515 URLPattern::SchemeMasks scheme_type) {
516 // If we've already dumped, do nothing.
517 // This simple bool works because we only dump for the main frame.
518 if (document_state->load_histograms_recorded())
519 return;
521 // Abort if any of these is missing.
522 Time start = document_state->start_load_time();
523 Time commit = document_state->commit_load_time();
524 Time navigation_start =
525 Time::FromDoubleT(performance.navigationStart());
526 if (start.is_null() || commit.is_null() || navigation_start.is_null())
527 return;
529 // We properly handle null values for the next 3 variables.
530 Time request = document_state->request_time();
531 Time first_paint = document_state->first_paint_time();
532 Time first_paint_after_load = document_state->first_paint_after_load_time();
533 Time finish_doc = document_state->finish_document_load_time();
534 Time finish_all_loads = document_state->finish_load_time();
536 // Handle case where user hits "stop" or "back" before loading completely.
537 // Note that this makes abandoned page loads be recorded as if they were
538 // completed, polluting the metrics with artifically short completion times.
539 // We are not fixing this as these metrics are being dropped as deprecated.
540 if (finish_doc.is_null()) {
541 finish_doc = Time::Now();
542 document_state->set_finish_document_load_time(finish_doc);
544 if (finish_all_loads.is_null()) {
545 finish_all_loads = Time::Now();
546 document_state->set_finish_load_time(finish_all_loads);
549 document_state->set_load_histograms_recorded(true);
551 // Note: Client side redirects will have no request time.
552 Time begin = request.is_null() ? start : request;
553 TimeDelta begin_to_finish_doc = finish_doc - begin;
554 TimeDelta begin_to_finish_all_loads = finish_all_loads - begin;
555 TimeDelta start_to_finish_all_loads = finish_all_loads - start;
556 TimeDelta start_to_commit = commit - start;
558 DocumentState::LoadType load_type = document_state->load_type();
560 // The above code sanitized all values of times, in preparation for creating
561 // actual histograms. The remainder of this code could be run at destructor
562 // time for the document_state, since all data is intact.
564 // Aggregate PLT data across all link types.
565 UMA_HISTOGRAM_ENUMERATION("PLT.LoadType", load_type,
566 DocumentState::kLoadTypeMax);
567 PLT_HISTOGRAM("PLT.StartToCommit", start_to_commit);
568 PLT_HISTOGRAM("PLT.CommitToFinishDoc", finish_doc - commit);
569 PLT_HISTOGRAM("PLT.FinishDocToFinish", finish_all_loads - finish_doc);
570 PLT_HISTOGRAM("PLT.BeginToCommit", commit - begin);
571 PLT_HISTOGRAM("PLT.StartToFinish", start_to_finish_all_loads);
572 if (!request.is_null()) {
573 PLT_HISTOGRAM("PLT.RequestToStart", start - request);
574 PLT_HISTOGRAM("PLT.RequestToFinish", finish_all_loads - request);
576 PLT_HISTOGRAM("PLT.CommitToFinish", finish_all_loads - commit);
578 scoped_ptr<TimeDelta> begin_to_first_paint;
579 scoped_ptr<TimeDelta> commit_to_first_paint;
580 if (!first_paint.is_null()) {
581 // 'first_paint' can be before 'begin' for an unknown reason.
582 // See bug http://crbug.com/125273 for details.
583 if (begin <= first_paint) {
584 begin_to_first_paint.reset(new TimeDelta(first_paint - begin));
585 PLT_HISTOGRAM_WITH_GWS_VARIANT("PLT.BeginToFirstPaint",
586 *begin_to_first_paint,
587 came_from_websearch,
588 websearch_chrome_joint_experiment_id,
589 is_preview);
592 // Conditional was previously a DCHECK. Changed due to multiple bot
593 // failures, listed in crbug.com/383963
594 if (commit <= first_paint) {
595 commit_to_first_paint.reset(new TimeDelta(first_paint - commit));
596 PLT_HISTOGRAM_WITH_GWS_VARIANT("PLT.CommitToFirstPaint",
597 *commit_to_first_paint,
598 came_from_websearch,
599 websearch_chrome_joint_experiment_id,
600 is_preview);
603 if (!first_paint_after_load.is_null()) {
604 // 'first_paint_after_load' can be before 'begin' for an unknown reason.
605 // See bug http://crbug.com/125273 for details.
606 if (begin <= first_paint_after_load) {
607 PLT_HISTOGRAM("PLT.BeginToFirstPaintAfterLoad",
608 first_paint_after_load - begin);
610 // Both following conditionals were previously DCHECKs. Changed due to
611 // multiple bot failures, listed in crbug.com/383963
612 if (commit <= first_paint_after_load) {
613 PLT_HISTOGRAM("PLT.CommitToFirstPaintAfterLoad",
614 first_paint_after_load - commit);
616 if (finish_all_loads <= first_paint_after_load) {
617 PLT_HISTOGRAM("PLT.FinishToFirstPaintAfterLoad",
618 first_paint_after_load - finish_all_loads);
621 PLT_HISTOGRAM_WITH_GWS_VARIANT("PLT.BeginToFinishDoc", begin_to_finish_doc,
622 came_from_websearch,
623 websearch_chrome_joint_experiment_id,
624 is_preview);
625 PLT_HISTOGRAM_WITH_GWS_VARIANT("PLT.BeginToFinish", begin_to_finish_all_loads,
626 came_from_websearch,
627 websearch_chrome_joint_experiment_id,
628 is_preview);
630 // Load type related histograms.
631 switch (load_type) {
632 case DocumentState::UNDEFINED_LOAD:
633 PLT_HISTOGRAM("PLT.BeginToFinishDoc_UndefLoad", begin_to_finish_doc);
634 PLT_HISTOGRAM("PLT.BeginToFinish_UndefLoad", begin_to_finish_all_loads);
635 break;
636 case DocumentState::RELOAD:
637 PLT_HISTOGRAM("PLT.BeginToFinishDoc_Reload", begin_to_finish_doc);
638 PLT_HISTOGRAM("PLT.BeginToFinish_Reload", begin_to_finish_all_loads);
639 break;
640 case DocumentState::HISTORY_LOAD:
641 PLT_HISTOGRAM("PLT.BeginToFinishDoc_HistoryLoad", begin_to_finish_doc);
642 PLT_HISTOGRAM("PLT.BeginToFinish_HistoryLoad", begin_to_finish_all_loads);
643 break;
644 case DocumentState::NORMAL_LOAD:
645 PLT_HISTOGRAM("PLT.BeginToFinishDoc_NormalLoad", begin_to_finish_doc);
646 PLT_HISTOGRAM("PLT.BeginToFinish_NormalLoad", begin_to_finish_all_loads);
647 break;
648 case DocumentState::LINK_LOAD_NORMAL:
649 PLT_HISTOGRAM("PLT.BeginToFinishDoc_LinkLoadNormal",
650 begin_to_finish_doc);
651 PLT_HISTOGRAM("PLT.BeginToFinish_LinkLoadNormal",
652 begin_to_finish_all_loads);
653 break;
654 case DocumentState::LINK_LOAD_RELOAD:
655 PLT_HISTOGRAM("PLT.BeginToFinishDoc_LinkLoadReload",
656 begin_to_finish_doc);
657 PLT_HISTOGRAM("PLT.BeginToFinish_LinkLoadReload",
658 begin_to_finish_all_loads);
659 break;
660 case DocumentState::LINK_LOAD_CACHE_STALE_OK:
661 PLT_HISTOGRAM("PLT.BeginToFinishDoc_LinkLoadStaleOk",
662 begin_to_finish_doc);
663 PLT_HISTOGRAM("PLT.BeginToFinish_LinkLoadStaleOk",
664 begin_to_finish_all_loads);
665 break;
666 case DocumentState::LINK_LOAD_CACHE_ONLY:
667 PLT_HISTOGRAM("PLT.BeginToFinishDoc_LinkLoadCacheOnly",
668 begin_to_finish_doc);
669 PLT_HISTOGRAM("PLT.BeginToFinish_LinkLoadCacheOnly",
670 begin_to_finish_all_loads);
671 break;
672 default:
673 break;
676 if (data_reduction_proxy_was_used) {
677 PLT_HISTOGRAM("PLT.BeginToFinishDoc_SpdyProxy", begin_to_finish_doc);
678 PLT_HISTOGRAM("PLT.BeginToFinish_SpdyProxy", begin_to_finish_all_loads);
681 if (document_state->was_prefetcher()) {
682 PLT_HISTOGRAM("PLT.BeginToFinishDoc_ContentPrefetcher",
683 begin_to_finish_doc);
684 PLT_HISTOGRAM("PLT.BeginToFinish_ContentPrefetcher",
685 begin_to_finish_all_loads);
687 if (document_state->was_referred_by_prefetcher()) {
688 PLT_HISTOGRAM("PLT.BeginToFinishDoc_ContentPrefetcherReferrer",
689 begin_to_finish_doc);
690 PLT_HISTOGRAM("PLT.BeginToFinish_ContentPrefetcherReferrer",
691 begin_to_finish_all_loads);
694 const bool use_webrequest_histogram =
695 ChromeContentRendererClient::WasWebRequestUsedBySomeExtensions();
696 if (use_webrequest_histogram) {
697 switch (load_type) {
698 case DocumentState::NORMAL_LOAD:
699 PLT_HISTOGRAM(
700 "PLT.BeginToFinish_NormalLoad_ExtensionWebRequest",
701 begin_to_finish_all_loads);
702 break;
703 case DocumentState::LINK_LOAD_NORMAL:
704 PLT_HISTOGRAM(
705 "PLT.BeginToFinish_LinkLoadNormal_ExtensionWebRequest",
706 begin_to_finish_all_loads);
707 break;
708 case DocumentState::LINK_LOAD_RELOAD:
709 PLT_HISTOGRAM(
710 "PLT.BeginToFinish_LinkLoadReload_ExtensionWebRequest",
711 begin_to_finish_all_loads);
712 break;
713 case DocumentState::LINK_LOAD_CACHE_STALE_OK:
714 PLT_HISTOGRAM(
715 "PLT.BeginToFinish_LinkLoadStaleOk_ExtensionWebRequest",
716 begin_to_finish_all_loads);
717 break;
718 default:
719 break;
724 } // namespace
726 PageLoadHistograms::PageLoadHistograms(content::RenderView* render_view)
727 : content::RenderViewObserver(render_view) {
730 void PageLoadHistograms::Dump(WebFrame* frame) {
731 // We only dump histograms for main frames.
732 // In the future, it may be interesting to tag subframes and dump them too.
733 if (!frame || frame->parent())
734 return;
736 // Only dump for supported schemes.
737 URLPattern::SchemeMasks scheme_type =
738 GetSupportedSchemeType(frame->document().url());
739 if (scheme_type == 0)
740 return;
742 // Ignore multipart requests.
743 if (frame->dataSource()->response().isMultipartPayload())
744 return;
746 DocumentState* document_state =
747 DocumentState::FromDataSource(frame->dataSource());
749 bool data_reduction_proxy_was_used = false;
750 if (!document_state->proxy_server().IsEmpty()) {
751 Send(new DataReductionProxyViewHostMsg_IsDataReductionProxy(
752 document_state->proxy_server(), &data_reduction_proxy_was_used));
755 bool came_from_websearch =
756 IsFromGoogleSearchResult(frame->document().url(),
757 GURL(frame->document().referrer()));
758 int websearch_chrome_joint_experiment_id = kNoExperiment;
759 bool is_preview = false;
760 if (came_from_websearch) {
761 websearch_chrome_joint_experiment_id =
762 GetQueryStringBasedExperiment(GURL(frame->document().referrer()));
763 is_preview = ViaHeaderContains(frame, "1.1 Google Instant Proxy Preview");
766 // Metrics based on the timing information recorded for the Navigation Timing
767 // API - http://www.w3.org/TR/navigation-timing/.
768 DumpHistograms(frame->performance(), document_state,
769 data_reduction_proxy_was_used,
770 came_from_websearch,
771 websearch_chrome_joint_experiment_id,
772 is_preview,
773 scheme_type);
775 // Old metrics based on the timing information stored in DocumentState. These
776 // are deprecated and should go away.
777 DumpDeprecatedHistograms(frame->performance(), document_state,
778 data_reduction_proxy_was_used,
779 came_from_websearch,
780 websearch_chrome_joint_experiment_id,
781 is_preview,
782 scheme_type);
784 // Log the PLT to the info log.
785 LogPageLoadTime(document_state, frame->dataSource());
787 // Since there are currently no guarantees that renderer histograms will be
788 // sent to the browser, we initiate a PostTask here to be sure that we send
789 // the histograms we generated. Without this call, pages that don't have an
790 // on-close-handler might generate data that is lost when the renderer is
791 // shutdown abruptly (perchance because the user closed the tab).
792 // TODO(jar) BUG=33233: This needs to be moved to a PostDelayedTask, and it
793 // should post when the onload is complete, so that it doesn't interfere with
794 // the next load.
795 content::RenderThread::Get()->UpdateHistograms(
796 content::kHistogramSynchronizerReservedSequenceNumber);
799 void PageLoadHistograms::FrameWillClose(WebFrame* frame) {
800 Dump(frame);
803 void PageLoadHistograms::ClosePage() {
804 // TODO(davemoore) This code should be removed once willClose() gets
805 // called when a page is destroyed. page_load_histograms_.Dump() is safe
806 // to call multiple times for the same frame, but it will simplify things.
807 Dump(render_view()->GetWebView()->mainFrame());
810 void PageLoadHistograms::LogPageLoadTime(const DocumentState* document_state,
811 const WebDataSource* ds) const {
812 // Because this function gets called on every page load,
813 // take extra care to optimize it away if logging is turned off.
814 if (logging::LOG_INFO < logging::GetMinLogLevel())
815 return;
817 DCHECK(document_state);
818 DCHECK(ds);
819 GURL url(ds->request().url());
820 Time start = document_state->start_load_time();
821 Time finish = document_state->finish_load_time();
822 // TODO(mbelshe): should we log more stats?
823 VLOG(1) << "PLT: " << (finish - start).InMilliseconds() << "ms "
824 << url.spec();