Chromium Code Reviews| Index: chrome/browser/metrics/first_web_contents_profiler.cc |
| diff --git a/chrome/browser/metrics/first_web_contents_profiler.cc b/chrome/browser/metrics/first_web_contents_profiler.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..99c02895bda8bd933b52a2974ed133fb2a5a1cf4 |
| --- /dev/null |
| +++ b/chrome/browser/metrics/first_web_contents_profiler.cc |
| @@ -0,0 +1,98 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/metrics/first_web_contents_profiler.h" |
| + |
| +#include "base/metrics/histogram.h" |
| +#include "base/process/process_info.h" |
| +#include "base/time/time.h" |
| +#include "chrome/browser/ui/browser.h" |
| +#include "chrome/browser/ui/browser_iterator.h" |
| +#include "chrome/browser/ui/tabs/tab_strip_model.h" |
| + |
| +namespace { |
|
Ilya Sherman
2014/11/26 04:12:26
nit: Please leave a blank line after this one.
erikchen
2014/11/26 18:24:43
Done.
|
| +const int kHistogramMinTimeMilliseconds = 200; |
| +const int kHistogramMaxTimeSeconds = 45; |
| + |
| +// The buckets need to be sufficiently tight that small performance |
| +// improvements can be measured. We want to be able to differentiate 1% |
| +// improvements, which requires 781 buckets. |
|
Ilya Sherman
2014/11/26 04:12:26
1% improvements to what? Histograms measure a pre
erikchen
2014/11/26 18:24:43
I'm tracking the two modes of a bi-modal distribut
|
| +// log(45 * 1000 / 200) / log (2) / 0.01 = 781. |
|
Ilya Sherman
2014/11/26 04:12:26
nit: I expect we'll arrive at some different compu
Ilya Sherman
2014/11/26 04:12:26
Sorry, but I don't understand the relevance of thi
erikchen
2014/11/26 18:24:43
I've updated the explanation in patch set 3 to bre
erikchen
2014/11/26 18:24:43
Done.
|
| +const int kHistogramBucketSize = 781; |
|
Ilya Sherman
2014/11/26 04:12:26
nit: Please leave a blank line after this one.
erikchen
2014/11/26 18:24:43
Done.
|
| +} // namespace |
| + |
| +scoped_ptr<FirstWebContentsProfiler> |
| +FirstWebContentsProfiler::CreateProfilerForFirstWebContents( |
| + FirstWebContentsProfilerDelegate* delegate) { |
| + DCHECK(delegate); |
| + for (chrome::BrowserIterator iterator; !iterator.done(); iterator.Next()) { |
| + Browser* browser = *iterator; |
| + content::WebContents* web_contents = |
| + browser->tab_strip_model()->GetActiveWebContents(); |
| + if (web_contents) { |
| + return scoped_ptr<FirstWebContentsProfiler>( |
| + new FirstWebContentsProfiler(web_contents, delegate)); |
| + } |
| + } |
| + return nullptr; |
| +} |
| + |
| +FirstWebContentsProfiler::FirstWebContentsProfiler( |
| + content::WebContents* web_contents, |
| + FirstWebContentsProfilerDelegate* delegate) |
| + : content::WebContentsObserver(web_contents), |
| + collected_paint_metric_(false), |
| + collected_load_metric_(false), |
| + delegate_(delegate) { |
| +} |
| + |
| +void FirstWebContentsProfiler::DidFirstVisuallyNonEmptyPaint() { |
| + if (collected_paint_metric_) |
| + return; |
| + collected_paint_metric_ = true; |
| + const base::Time process_creation_time = |
| + base::CurrentProcessInfo::CreationTime(); |
| + if (!process_creation_time.is_null()) { |
| + base::TimeDelta elapsed = base::Time::Now() - process_creation_time; |
| + UMA_HISTOGRAM_CUSTOM_TIMES( |
| + "Startup.FirstWebContents.NonEmptyPaint", elapsed, |
| + base::TimeDelta::FromMilliseconds(kHistogramMinTimeMilliseconds), |
| + base::TimeDelta::FromSeconds(kHistogramMaxTimeSeconds), |
| + kHistogramBucketSize); |
| + } |
| + |
| + if (IsFinishedCollectingMetrics()) |
| + FinishedCollectingMetrics(); |
| +} |
| + |
| +void FirstWebContentsProfiler::DocumentOnLoadCompletedInMainFrame() { |
| + if (collected_load_metric_) |
| + return; |
| + collected_load_metric_ = true; |
| + const base::Time process_creation_time = |
| + base::CurrentProcessInfo::CreationTime(); |
| + if (!process_creation_time.is_null()) { |
| + base::TimeDelta elapsed = base::Time::Now() - process_creation_time; |
| + UMA_HISTOGRAM_CUSTOM_TIMES( |
| + "Startup.FirstWebContents.MainFrameLoad", elapsed, |
| + base::TimeDelta::FromMilliseconds(kHistogramMinTimeMilliseconds), |
| + base::TimeDelta::FromSeconds(kHistogramMaxTimeSeconds), |
| + kHistogramBucketSize); |
| + } |
| + |
| + if (IsFinishedCollectingMetrics()) |
| + FinishedCollectingMetrics(); |
| +} |
| + |
| +void FirstWebContentsProfiler::WebContentsDestroyed() { |
| + FinishedCollectingMetrics(); |
| +} |
| + |
| +bool FirstWebContentsProfiler::IsFinishedCollectingMetrics() { |
| + return collected_paint_metric_ && collected_load_metric_; |
| +} |
| + |
| +void FirstWebContentsProfiler::FinishedCollectingMetrics() { |
| + delegate_->ProfilerFinishedCollectingMetrics(); |
| +} |