Index: components/html_viewer/stats_collection_controller.cc |
diff --git a/components/html_viewer/stats_collection_controller.cc b/components/html_viewer/stats_collection_controller.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..578e5a3dfac046df4e6915f6c7a89f1dc6c1651f |
--- /dev/null |
+++ b/components/html_viewer/stats_collection_controller.cc |
@@ -0,0 +1,220 @@ |
+// Copyright 2015 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 "components/html_viewer/stats_collection_controller.h" |
+ |
+#include "base/command_line.h" |
+#include "base/json/json_writer.h" |
+#include "base/metrics/histogram.h" |
+#include "base/metrics/statistics_recorder.h" |
+#include "base/run_loop.h" |
+#include "base/strings/string_util.h" |
+#include "base/values.h" |
+#include "components/html_viewer/html_document.h" |
+#include "components/startup_metric_utils/startup_metric_utils.h" |
+#include "gin/handle.h" |
+#include "gin/object_template_builder.h" |
+#include "mojo/application/public/cpp/application_impl.h" |
+#include "mojo/runner/switches.h" |
+#include "third_party/WebKit/public/web/WebKit.h" |
+#include "third_party/WebKit/public/web/WebLocalFrame.h" |
+#include "third_party/WebKit/public/web/WebView.h" |
+ |
+namespace html_viewer { |
+ |
+namespace { |
+ |
+// Initialize the histogram data using the given startup performance times. |
+// TODO(msw): Have mojo:tracing record the histograms and serve their JSON? |
+void GetStartupPerformanceTimesCallbackImpl( |
+ const base::Closure& callback, |
+ tracing::StartupPerformanceTimesPtr times) { |
+ base::StatisticsRecorder::Initialize(); |
+ |
+ const base::Time shell_process_creation_time = |
+ base::Time::FromInternalValue(times->shell_process_creation_time); |
+ startup_metric_utils::RecordSavedMainEntryPointTime( |
+ shell_process_creation_time); |
+ // TODO(msw): Use |browser_message_loop_start_time|, instead of letting |
+ // OnBrowserStartupComplete incorrectly call base::Time::Now()... |
+ startup_metric_utils::OnBrowserStartupComplete(false); |
+ |
+ // TODO(msw): Consolidate with chrome's Browser::OnWindowDidShow()... |
+ const base::Time browser_window_display_time = |
+ base::Time::FromInternalValue(times->browser_window_display_time); |
+ base::TimeDelta browser_window_display_delta = |
+ browser_window_display_time - shell_process_creation_time; |
+ UMA_HISTOGRAM_LONG_TIMES("Startup.BrowserWindowDisplay", |
+ browser_window_display_delta); |
+ |
+ // TODO(msw): Consolidate with chrome's PreMainMessageLoopRunImpl()... |
+ // TODO(msw): Need to measure the "browser_open_start" time for this delta... |
+ const base::Time browser_open_tabs_time = |
+ base::Time::FromInternalValue(times->browser_open_tabs_time); |
+ base::TimeDelta browser_open_tabs_delta = |
+ browser_open_tabs_time - shell_process_creation_time; |
+ UMA_HISTOGRAM_LONG_TIMES_100("Startup.BrowserOpenTabs", |
+ browser_open_tabs_delta); |
+ |
+ // TODO(msw): Consolidate with chrome's first_web_contents_profiler.cc... |
+ const base::Time first_web_contents_main_frame_load_time = |
+ base::Time::FromInternalValue( |
+ times->first_web_contents_main_frame_load_time); |
+ base::TimeDelta first_web_contents_main_frame_load_delta = |
+ first_web_contents_main_frame_load_time - shell_process_creation_time; |
+ UMA_HISTOGRAM_LONG_TIMES_100("Startup.FirstWebContents.MainFrameLoad", |
+ first_web_contents_main_frame_load_delta); |
+ |
+ // TODO(msw): Consolidate with chrome's first_web_contents_profiler.cc... |
+ const base::Time first_visually_non_empty_layout_time = |
+ base::Time::FromInternalValue( |
+ times->first_visually_non_empty_layout_time); |
+ base::TimeDelta first_web_contents_non_empty_paint_delta = |
+ first_visually_non_empty_layout_time - shell_process_creation_time; |
+ UMA_HISTOGRAM_LONG_TIMES_100("Startup.FirstWebContents.NonEmptyPaint", |
+ first_web_contents_non_empty_paint_delta); |
+ |
+ callback.Run(); |
+} |
+ |
+// Encodes a WebContentsLoadTime as JSON. |
+// Input: |
+// - |load_start_time| - time at which page load started. |
+// - |load_stop_time| - time at which page load stopped. |
+// - |result| - returned JSON. |
+// Example return value: |
+// {'load_start_ms': 1, 'load_duration_ms': 2.5} |
+// either value may be null if a web contents hasn't fully loaded. |
+// load_start_ms is represented as milliseconds since the unix epoch. |
+// TODO(msw): Consolidate with content/renderer/stats_collection_controller.cc. |
+void ConvertLoadTimeToJSON(const base::Time& load_start_time, |
+ const base::Time& load_stop_time, |
+ std::string* result) { |
+ base::DictionaryValue item; |
+ |
+ if (load_start_time.is_null()) { |
+ item.Set("load_start_ms", base::Value::CreateNullValue()); |
+ } else { |
+ item.SetDouble("load_start_ms", (load_start_time - base::Time::UnixEpoch()) |
+ .InMillisecondsF()); |
+ } |
+ if (load_start_time.is_null() || load_stop_time.is_null()) { |
+ item.Set("load_duration_ms", base::Value::CreateNullValue()); |
+ } else { |
+ item.SetDouble("load_duration_ms", |
+ (load_stop_time - load_start_time).InMillisecondsF()); |
+ } |
+ base::JSONWriter::Write(item, result); |
+} |
+ |
+} // namespace |
+ |
+// static |
+gin::WrapperInfo StatsCollectionController::kWrapperInfo = { |
+ gin::kEmbedderNativeGin}; |
+ |
+// static |
+StatsCollectionController* StatsCollectionController::Install( |
+ HTMLDocument* html_document, |
+ mojo::ApplicationImpl* app) { |
+ // Only make startup tracing available when running in the context of a test. |
+ if (!base::CommandLine::ForCurrentProcess()->HasSwitch( |
+ switches::kStatsCollectionController)) { |
+ return nullptr; |
+ } |
+ |
+ blink::WebFrame* frame = html_document->web_view()->mainFrame(); |
+ v8::Isolate* isolate = blink::mainThreadIsolate(); |
+ v8::HandleScope handle_scope(isolate); |
+ v8::Local<v8::Context> context = frame->mainWorldScriptContext(); |
+ if (context.IsEmpty()) |
+ return nullptr; |
+ |
+ v8::Context::Scope context_scope(context); |
+ |
+ StatsCollectionController* stats_collection_controller = |
+ new StatsCollectionController(html_document, app); |
+ gin::Handle<StatsCollectionController> controller = |
+ gin::CreateHandle(isolate, stats_collection_controller); |
+ if (controller.IsEmpty()) |
+ return nullptr; |
+ v8::Local<v8::Object> global = context->Global(); |
+ global->Set(gin::StringToV8(isolate, "statsCollectionController"), |
+ controller.ToV8()); |
+ return stats_collection_controller; |
+} |
+ |
+void StatsCollectionController::DidHandleOnloadEvents(const base::Time& time) { |
+ startup_performance_controller_->SetFirstWebContentsMainFrameLoadTime( |
+ time.ToInternalValue()); |
+} |
+ |
+void StatsCollectionController::DidFirstVisuallyNonEmptyLayout( |
+ const base::Time& time) { |
+ startup_performance_controller_->SetFirstVisuallyNonEmptyLayoutTime( |
+ time.ToInternalValue()); |
+} |
+ |
+StatsCollectionController::StatsCollectionController( |
+ HTMLDocument* html_document, |
+ mojo::ApplicationImpl* app) |
+ : observer_(html_document) { |
+ mojo::URLRequestPtr request(mojo::URLRequest::New()); |
+ request->url = mojo::String::From("mojo:tracing"); |
+ app->ConnectToService(request.Pass(), &startup_performance_controller_); |
+} |
+ |
+StatsCollectionController::~StatsCollectionController() {} |
+ |
+gin::ObjectTemplateBuilder StatsCollectionController::GetObjectTemplateBuilder( |
+ v8::Isolate* isolate) { |
+ return gin::Wrappable<StatsCollectionController>::GetObjectTemplateBuilder( |
+ isolate) |
+ .SetMethod("getHistogram", &StatsCollectionController::GetHistogram) |
+ .SetMethod("getBrowserHistogram", |
+ &StatsCollectionController::GetBrowserHistogram) |
+ .SetMethod("tabLoadTiming", &StatsCollectionController::GetTabLoadTiming); |
+} |
+ |
+std::string StatsCollectionController::GetHistogram( |
+ const std::string& histogram_name) { |
+ DCHECK(base::CommandLine::ForCurrentProcess()->HasSwitch( |
+ switches::kStatsCollectionController)); |
+ |
+ // TODO(msw): Use a more robust check for startup histogram init. |
+ if (!base::StatisticsRecorder::IsActive()) { |
+ // Get the startup performance times from the tracing service. |
+ // TODO(msw): Have mojo:tracing provide the histogram JSON? |
+ base::RunLoop run_loop; |
+ auto callback = base::Bind(&GetStartupPerformanceTimesCallbackImpl, |
+ run_loop.QuitClosure()); |
+ startup_performance_controller_->GetStartupPerformanceTimes(callback); |
+ run_loop.Run(); |
yzshen1
2015/08/13 15:59:12
Maybe consider using InterfacePtr<>::WaitForIncomi
msw
2015/08/14 23:20:34
Done.
|
+ DCHECK(base::StatisticsRecorder::IsActive()); |
+ } |
+ |
+ std::string histogram_json = "{}"; |
+ base::HistogramBase* histogram = |
+ base::StatisticsRecorder::FindHistogram(histogram_name); |
+ if (histogram) |
+ histogram->WriteJSON(&histogram_json); |
+ // TODO(msw): Remove debug printing... |
+ LOG(ERROR) << "MSW Histogram [" << histogram_name << "]: " << histogram_json; |
+ return histogram_json; |
+} |
+ |
+std::string StatsCollectionController::GetBrowserHistogram( |
+ const std::string& histogram_name) { |
+ // TODO(msw): Should GetHistogram/GetBrowserHistogram return distinct values? |
+ return GetHistogram(histogram_name); |
+} |
+ |
+std::string StatsCollectionController::GetTabLoadTiming() { |
+ std::string tab_timing_json = "{}"; |
+ ConvertLoadTimeToJSON(observer_.load_start_time(), observer_.load_stop_time(), |
+ &tab_timing_json); |
+ return tab_timing_json; |
+} |
+ |
+} // namespace html_viewer |