OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 "components/html_viewer/stats_collection_controller.h" |
| 6 |
| 7 #include "base/command_line.h" |
| 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/metrics/histogram.h" |
| 10 #include "base/metrics/statistics_recorder.h" |
| 11 #include "base/time/time.h" |
| 12 #include "components/startup_metric_utils/startup_metric_utils.h" |
| 13 #include "gin/handle.h" |
| 14 #include "gin/object_template_builder.h" |
| 15 #include "mojo/application/public/cpp/application_impl.h" |
| 16 #include "mojo/services/tracing/public/cpp/switches.h" |
| 17 #include "third_party/WebKit/public/web/WebKit.h" |
| 18 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
| 19 |
| 20 namespace html_viewer { |
| 21 |
| 22 namespace { |
| 23 |
| 24 // Initialize the histogram data using the given startup performance times. |
| 25 // TODO(msw): Use TimeTicks to avoid system clock changes: crbug.com/521164 |
| 26 void GetStartupPerformanceTimesCallbackImpl( |
| 27 tracing::StartupPerformanceTimesPtr times) { |
| 28 base::StatisticsRecorder::Initialize(); |
| 29 |
| 30 const base::Time shell_process_creation_time = |
| 31 base::Time::FromInternalValue(times->shell_process_creation_time); |
| 32 startup_metric_utils::RecordSavedMainEntryPointTime( |
| 33 shell_process_creation_time); |
| 34 // TODO(msw): Use |browser_message_loop_start_time|, instead of letting |
| 35 // OnBrowserStartupComplete incorrectly call base::Time::Now()... |
| 36 startup_metric_utils::OnBrowserStartupComplete(false); |
| 37 |
| 38 // TODO(msw): Consolidate with chrome's Browser::OnWindowDidShow()... |
| 39 const base::Time browser_window_display_time = |
| 40 base::Time::FromInternalValue(times->browser_window_display_time); |
| 41 base::TimeDelta browser_window_display_delta = |
| 42 browser_window_display_time - shell_process_creation_time; |
| 43 UMA_HISTOGRAM_LONG_TIMES("Startup.BrowserWindowDisplay", |
| 44 browser_window_display_delta); |
| 45 |
| 46 // TODO(msw): Consolidate with chrome's PreMainMessageLoopRunImpl()... |
| 47 // TODO(msw): Need to measure the "browser_open_start" time for this delta... |
| 48 const base::Time browser_open_tabs_time = |
| 49 base::Time::FromInternalValue(times->browser_open_tabs_time); |
| 50 base::TimeDelta browser_open_tabs_delta = |
| 51 browser_open_tabs_time - shell_process_creation_time; |
| 52 UMA_HISTOGRAM_LONG_TIMES_100("Startup.BrowserOpenTabs", |
| 53 browser_open_tabs_delta); |
| 54 |
| 55 // TODO(msw): Consolidate with chrome's first_web_contents_profiler.cc... |
| 56 const base::Time first_web_contents_main_frame_load_time = |
| 57 base::Time::FromInternalValue( |
| 58 times->first_web_contents_main_frame_load_time); |
| 59 base::TimeDelta first_web_contents_main_frame_load_delta = |
| 60 first_web_contents_main_frame_load_time - shell_process_creation_time; |
| 61 UMA_HISTOGRAM_LONG_TIMES_100("Startup.FirstWebContents.MainFrameLoad", |
| 62 first_web_contents_main_frame_load_delta); |
| 63 |
| 64 // TODO(msw): Consolidate with chrome's first_web_contents_profiler.cc... |
| 65 const base::Time first_visually_non_empty_layout_time = |
| 66 base::Time::FromInternalValue( |
| 67 times->first_visually_non_empty_layout_time); |
| 68 base::TimeDelta first_web_contents_non_empty_paint_delta = |
| 69 first_visually_non_empty_layout_time - shell_process_creation_time; |
| 70 UMA_HISTOGRAM_LONG_TIMES_100("Startup.FirstWebContents.NonEmptyPaint", |
| 71 first_web_contents_non_empty_paint_delta); |
| 72 } |
| 73 |
| 74 } // namespace |
| 75 |
| 76 // static |
| 77 gin::WrapperInfo StatsCollectionController::kWrapperInfo = { |
| 78 gin::kEmbedderNativeGin}; |
| 79 |
| 80 // static |
| 81 tracing::StartupPerformanceDataCollectorPtr StatsCollectionController::Install( |
| 82 blink::WebFrame* frame, |
| 83 mojo::ApplicationImpl* app) { |
| 84 // Only make startup tracing available when running in the context of a test. |
| 85 if (!app || |
| 86 !base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 87 tracing::kEnableStatsCollectionBindings)) { |
| 88 return nullptr; |
| 89 } |
| 90 |
| 91 v8::Isolate* isolate = blink::mainThreadIsolate(); |
| 92 v8::HandleScope handle_scope(isolate); |
| 93 v8::Local<v8::Context> context = frame->mainWorldScriptContext(); |
| 94 if (context.IsEmpty()) |
| 95 return nullptr; |
| 96 |
| 97 v8::Context::Scope context_scope(context); |
| 98 |
| 99 mojo::URLRequestPtr request(mojo::URLRequest::New()); |
| 100 request->url = mojo::String::From("mojo:tracing"); |
| 101 scoped_ptr<mojo::ApplicationConnection> connection = |
| 102 app->ConnectToApplication(request.Pass()); |
| 103 if (!connection) |
| 104 return nullptr; |
| 105 tracing::StartupPerformanceDataCollectorPtr collector_for_controller; |
| 106 tracing::StartupPerformanceDataCollectorPtr collector_for_caller; |
| 107 connection->ConnectToService(&collector_for_controller); |
| 108 connection->ConnectToService(&collector_for_caller); |
| 109 |
| 110 gin::Handle<StatsCollectionController> controller = gin::CreateHandle( |
| 111 isolate, new StatsCollectionController(collector_for_controller.Pass())); |
| 112 DCHECK(!controller.IsEmpty()); |
| 113 v8::Local<v8::Object> global = context->Global(); |
| 114 global->Set(gin::StringToV8(isolate, "statsCollectionController"), |
| 115 controller.ToV8()); |
| 116 return collector_for_caller.Pass(); |
| 117 } |
| 118 |
| 119 StatsCollectionController::StatsCollectionController( |
| 120 tracing::StartupPerformanceDataCollectorPtr collector) |
| 121 : startup_performance_data_collector_(collector.Pass()) {} |
| 122 |
| 123 StatsCollectionController::~StatsCollectionController() {} |
| 124 |
| 125 gin::ObjectTemplateBuilder StatsCollectionController::GetObjectTemplateBuilder( |
| 126 v8::Isolate* isolate) { |
| 127 return gin::Wrappable<StatsCollectionController>::GetObjectTemplateBuilder( |
| 128 isolate) |
| 129 .SetMethod("getHistogram", &StatsCollectionController::GetHistogram) |
| 130 .SetMethod("getBrowserHistogram", |
| 131 &StatsCollectionController::GetBrowserHistogram); |
| 132 } |
| 133 |
| 134 std::string StatsCollectionController::GetHistogram( |
| 135 const std::string& histogram_name) { |
| 136 DCHECK(base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 137 tracing::kEnableStatsCollectionBindings)); |
| 138 |
| 139 static bool startup_histogram_initialized = false; |
| 140 if (!startup_histogram_initialized) { |
| 141 // Get the startup performance times from the tracing service. |
| 142 auto callback = base::Bind(&GetStartupPerformanceTimesCallbackImpl); |
| 143 startup_performance_data_collector_->GetStartupPerformanceTimes(callback); |
| 144 startup_performance_data_collector_.WaitForIncomingResponse(); |
| 145 DCHECK(base::StatisticsRecorder::IsActive()); |
| 146 startup_histogram_initialized = true; |
| 147 } |
| 148 |
| 149 std::string histogram_json = "{}"; |
| 150 base::HistogramBase* histogram = |
| 151 base::StatisticsRecorder::FindHistogram(histogram_name); |
| 152 if (histogram) |
| 153 histogram->WriteJSON(&histogram_json); |
| 154 return histogram_json; |
| 155 } |
| 156 |
| 157 std::string StatsCollectionController::GetBrowserHistogram( |
| 158 const std::string& histogram_name) { |
| 159 return GetHistogram(histogram_name); |
| 160 } |
| 161 |
| 162 } // namespace html_viewer |
OLD | NEW |