OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
| 5 #include "chrome/test/chromedriver/performance_logger.h" |
| 6 |
5 #include <string> | 7 #include <string> |
6 #include <vector> | 8 #include <vector> |
7 | 9 |
8 #include "chrome/test/chromedriver/performance_logger.h" | 10 #include "base/bind.h" |
9 | |
10 #include "base/json/json_writer.h" | 11 #include "base/json/json_writer.h" |
11 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
12 #include "base/values.h" | 13 #include "base/values.h" |
| 14 #include "chrome/test/chromedriver/chrome/chrome.h" |
13 #include "chrome/test/chromedriver/chrome/devtools_client.h" | 15 #include "chrome/test/chromedriver/chrome/devtools_client.h" |
14 #include "chrome/test/chromedriver/chrome/devtools_client_impl.h" | 16 #include "chrome/test/chromedriver/chrome/devtools_client_impl.h" |
15 #include "chrome/test/chromedriver/chrome/log.h" | 17 #include "chrome/test/chromedriver/chrome/log.h" |
16 #include "chrome/test/chromedriver/chrome/status.h" | 18 #include "chrome/test/chromedriver/chrome/status.h" |
| 19 #include "chrome/test/chromedriver/chrome/version.h" |
| 20 #include "chrome/test/chromedriver/session.h" |
17 | 21 |
18 namespace { | 22 namespace { |
19 | 23 |
20 // DevTools event domain prefixes to intercept. | 24 // DevTools event domain prefixes to intercept. |
21 const char* const kDomains[] = {"Network.", "Page.", "Timeline."}; | 25 const char* const kDomains[] = {"Network.", "Page.", "Timeline."}; |
22 | 26 |
| 27 // Whitelist of WebDriver commands on which to request buffered trace events. |
| 28 const char* const kRequestTraceCommands[] = {"GetLog" /* required */, |
| 29 "Navigate"}; |
| 30 |
| 31 bool IsBrowserwideClient(DevToolsClient* client) { |
| 32 return (client->GetId() == DevToolsClientImpl::kBrowserwideDevToolsClientId); |
| 33 } |
| 34 |
| 35 bool IsEnabled(const PerfLoggingPrefs::InspectorDomainStatus& domain_status) { |
| 36 return (domain_status == |
| 37 PerfLoggingPrefs::InspectorDomainStatus::kDefaultEnabled || |
| 38 domain_status == |
| 39 PerfLoggingPrefs::InspectorDomainStatus::kExplicitlyEnabled); |
| 40 } |
| 41 |
| 42 // Returns whether |command| is in kRequestTraceCommands[] (case-insensitive). |
| 43 bool ShouldRequestTraceEvents(const std::string& command) { |
| 44 for (size_t i_domain = 0; i_domain < arraysize(kRequestTraceCommands); |
| 45 ++i_domain) { |
| 46 if (base::strcasecmp(command.c_str(), kRequestTraceCommands[i_domain]) == 0) |
| 47 return true; |
| 48 } |
| 49 return false; |
| 50 } |
| 51 |
23 // Returns whether the event belongs to one of kDomains. | 52 // Returns whether the event belongs to one of kDomains. |
24 bool ShouldLogEvent(const std::string& method) { | 53 bool ShouldLogEvent(const std::string& method) { |
25 for (size_t i_domain = 0; i_domain < arraysize(kDomains); ++i_domain) { | 54 for (size_t i_domain = 0; i_domain < arraysize(kDomains); ++i_domain) { |
26 if (StartsWithASCII(method, kDomains[i_domain], true /* case_sensitive */)) | 55 if (StartsWithASCII(method, kDomains[i_domain], true /* case_sensitive */)) |
27 return true; | 56 return true; |
28 } | 57 } |
29 return false; | 58 return false; |
30 } | 59 } |
31 | 60 |
32 bool IsEnabled(const PerfLoggingPrefs::InspectorDomainStatus& domain_status) { | |
33 return (domain_status == | |
34 PerfLoggingPrefs::InspectorDomainStatus::kDefaultEnabled || | |
35 domain_status == | |
36 PerfLoggingPrefs::InspectorDomainStatus::kExplicitlyEnabled); | |
37 } | |
38 | |
39 } // namespace | 61 } // namespace |
40 | 62 |
41 PerformanceLogger::PerformanceLogger(Log* log) | 63 PerformanceLogger::PerformanceLogger(Log* log, const Session* session) |
42 : log_(log) {} | 64 : log_(log), |
| 65 session_(session), |
| 66 browser_client_(NULL), |
| 67 trace_buffering_(false) {} |
43 | 68 |
44 PerformanceLogger::PerformanceLogger(Log* log, const PerfLoggingPrefs& prefs) | 69 PerformanceLogger::PerformanceLogger(Log* log, |
| 70 const Session* session, |
| 71 const PerfLoggingPrefs& prefs) |
45 : log_(log), | 72 : log_(log), |
46 prefs_(prefs) { | 73 session_(session), |
47 if (!prefs_.trace_categories.empty()) { | 74 prefs_(prefs), |
48 LOG(WARNING) << "Ignoring trace categories because tracing support not yet " | 75 browser_client_(NULL), |
49 << "implemented: " << prefs_.trace_categories; | 76 trace_buffering_(false) {} |
50 prefs_.trace_categories = ""; | |
51 } | |
52 } | |
53 | 77 |
54 bool PerformanceLogger::subscribes_to_browser() { | 78 bool PerformanceLogger::subscribes_to_browser() { |
55 return true; | 79 return true; |
56 } | 80 } |
57 | 81 |
58 Status PerformanceLogger::OnConnected(DevToolsClient* client) { | 82 Status PerformanceLogger::OnConnected(DevToolsClient* client) { |
59 if (client->GetId() == DevToolsClientImpl::kBrowserwideDevToolsClientId) { | 83 if (IsBrowserwideClient(client)) { |
60 // TODO(johnmoore): Implement tracing log. | 84 browser_client_ = client; |
| 85 if (!prefs_.trace_categories.empty()) { |
| 86 Status status = StartTrace(); |
| 87 if (status.IsError()) |
| 88 return status; |
| 89 } |
61 return Status(kOk); | 90 return Status(kOk); |
62 } | 91 } |
| 92 return EnableInspectorDomains(client); |
| 93 } |
| 94 |
| 95 Status PerformanceLogger::OnEvent( |
| 96 DevToolsClient* client, |
| 97 const std::string& method, |
| 98 const base::DictionaryValue& params) { |
| 99 if (IsBrowserwideClient(client)) { |
| 100 return HandleTraceEvents(client, method, params); |
| 101 } else { |
| 102 return HandleInspectorEvents(client, method, params); |
| 103 } |
| 104 } |
| 105 |
| 106 Status PerformanceLogger::BeforeCommand(const std::string& command_name) { |
| 107 // Only dump trace buffer after tracing has been started. |
| 108 if (trace_buffering_ && ShouldRequestTraceEvents(command_name)) { |
| 109 Status status = CollectTraceEvents(); |
| 110 if (status.IsError()) |
| 111 return status; |
| 112 } |
| 113 return Status(kOk); |
| 114 } |
| 115 |
| 116 void PerformanceLogger::AddLogEntry( |
| 117 Log::Level level, |
| 118 const std::string& webview, |
| 119 const std::string& method, |
| 120 const base::DictionaryValue& params) { |
| 121 base::DictionaryValue log_message_dict; |
| 122 log_message_dict.SetString("webview", webview); |
| 123 log_message_dict.SetString("message.method", method); |
| 124 log_message_dict.Set("message.params", params.DeepCopy()); |
| 125 std::string log_message_json; |
| 126 base::JSONWriter::Write(&log_message_dict, &log_message_json); |
| 127 |
| 128 // TODO(klm): extract timestamp from params? |
| 129 // Look at where it is for Page, Network, Timeline, and trace events. |
| 130 log_->AddEntry(level, log_message_json); |
| 131 } |
| 132 |
| 133 void PerformanceLogger::AddLogEntry( |
| 134 const std::string& webview, |
| 135 const std::string& method, |
| 136 const base::DictionaryValue& params) { |
| 137 AddLogEntry(Log::kInfo, webview, method, params); |
| 138 } |
| 139 |
| 140 Status PerformanceLogger::EnableInspectorDomains(DevToolsClient* client) { |
63 std::vector<std::string> enable_commands; | 141 std::vector<std::string> enable_commands; |
64 if (IsEnabled(prefs_.network)) | 142 if (IsEnabled(prefs_.network)) |
65 enable_commands.push_back("Network.enable"); | 143 enable_commands.push_back("Network.enable"); |
66 if (IsEnabled(prefs_.page)) | 144 if (IsEnabled(prefs_.page)) |
67 enable_commands.push_back("Page.enable"); | 145 enable_commands.push_back("Page.enable"); |
68 if (IsEnabled(prefs_.timeline)) { | 146 if (IsEnabled(prefs_.timeline)) { |
69 // Timeline feed implicitly disabled when trace categories are specified. | 147 // Timeline feed implicitly disabled when trace categories are specified. |
70 // So even if kDefaultEnabled, don't enable unless empty |trace_categories|. | 148 // So even if kDefaultEnabled, don't enable unless empty |trace_categories|. |
71 if (prefs_.trace_categories.empty() || prefs_.timeline == | 149 if (prefs_.trace_categories.empty() || prefs_.timeline == |
72 PerfLoggingPrefs::InspectorDomainStatus::kExplicitlyEnabled) | 150 PerfLoggingPrefs::InspectorDomainStatus::kExplicitlyEnabled) |
73 enable_commands.push_back("Timeline.start"); | 151 enable_commands.push_back("Timeline.start"); |
74 } | 152 } |
75 for (std::vector<std::string>::const_iterator it = enable_commands.begin(); | 153 for (std::vector<std::string>::const_iterator it = enable_commands.begin(); |
76 it != enable_commands.end(); ++it) { | 154 it != enable_commands.end(); ++it) { |
77 base::DictionaryValue params; // All the enable commands have empty params. | 155 base::DictionaryValue params; // All the enable commands have empty params. |
78 Status status = client->SendCommand(*it, params); | 156 Status status = client->SendCommand(*it, params); |
79 if (status.IsError()) | 157 if (status.IsError()) |
80 return status; | 158 return status; |
81 } | 159 } |
82 return Status(kOk); | 160 return Status(kOk); |
83 } | 161 } |
84 | 162 |
85 Status PerformanceLogger::OnEvent( | 163 Status PerformanceLogger::HandleInspectorEvents( |
86 DevToolsClient* client, | 164 DevToolsClient* client, |
87 const std::string& method, | 165 const std::string& method, |
88 const base::DictionaryValue& params) { | 166 const base::DictionaryValue& params) { |
89 if (!ShouldLogEvent(method)) | 167 if (!ShouldLogEvent(method)) |
90 return Status(kOk); | 168 return Status(kOk); |
91 | 169 |
92 base::DictionaryValue log_message_dict; | 170 AddLogEntry(client->GetId(), method, params); |
93 log_message_dict.SetString("webview", client->GetId()); | |
94 log_message_dict.SetString("message.method", method); | |
95 log_message_dict.Set("message.params", params.DeepCopy()); | |
96 std::string log_message_json; | |
97 // TODO(klm): extract timestamp from params? | |
98 // Look at where it is for Page, Network, Timeline events. | |
99 base::JSONWriter::Write(&log_message_dict, &log_message_json); | |
100 | |
101 log_->AddEntry(Log::kInfo, log_message_json); | |
102 return Status(kOk); | 171 return Status(kOk); |
103 } | 172 } |
104 | 173 |
105 // TODO(johnmoore): Use BeforeCommand to implement tracing log. | 174 Status PerformanceLogger::HandleTraceEvents( |
106 Status PerformanceLogger::BeforeCommand(const std::string& command_name) { | 175 DevToolsClient* client, |
| 176 const std::string& method, |
| 177 const base::DictionaryValue& params) { |
| 178 if (method == "Tracing.tracingComplete") { |
| 179 trace_buffering_ = false; |
| 180 } else if (method == "Tracing.dataCollected") { |
| 181 // The Tracing.dataCollected event contains a list of trace events. |
| 182 // Add each one as an individual log entry of method Tracing.dataCollected. |
| 183 const base::ListValue* traces; |
| 184 if (!params.GetList("value", &traces)) { |
| 185 return Status(kUnknownError, |
| 186 "received DevTools trace data in unexpected format"); |
| 187 } |
| 188 for (base::ListValue::const_iterator it = traces->begin(); |
| 189 it != traces->end(); |
| 190 ++it) { |
| 191 base::DictionaryValue* event_dict; |
| 192 if (!(*it)->GetAsDictionary(&event_dict)) |
| 193 return Status(kUnknownError, "trace event must be a dictionary"); |
| 194 AddLogEntry(client->GetId(), "Tracing.dataCollected", *event_dict); |
| 195 } |
| 196 } else if (method == "Tracing.bufferUsage") { |
| 197 // 'value' will be between 0-1 and represents how full the DevTools trace |
| 198 // buffer is. If the buffer is full, warn the user. |
| 199 double buffer_usage = 0; |
| 200 if (!params.GetDouble("value", &buffer_usage)) { |
| 201 // Tracing.bufferUsage event will occur once per second, and it really |
| 202 // only serves as a warning, so if we can't reliably tell whether the |
| 203 // buffer is full, just fail silently instead of spamming the logs. |
| 204 return Status(kOk); |
| 205 } |
| 206 if (buffer_usage >= 0.99999) { |
| 207 base::DictionaryValue params; |
| 208 std::string err("Chrome's trace buffer filled while collecting events, " |
| 209 "so some trace events may have been lost"); |
| 210 params.SetString("error", err); |
| 211 // Expose error to client via perf log using same format as other entries. |
| 212 AddLogEntry(Log::kWarning, |
| 213 DevToolsClientImpl::kBrowserwideDevToolsClientId, |
| 214 "Tracing.bufferUsage", params); |
| 215 LOG(WARNING) << err; |
| 216 } |
| 217 } |
107 return Status(kOk); | 218 return Status(kOk); |
108 } | 219 } |
| 220 |
| 221 bool PerformanceLogger::ShouldReportTracingError() { |
| 222 // Chromium builds 1967-2000, which correspond to Blink revisions 172887- |
| 223 // 174227, contain a regression where Tracing.start and Tracing.end commands |
| 224 // erroneously return error -32601 "no such method". The commands still work. |
| 225 if (session_->chrome) { |
| 226 const BrowserInfo* browser_info = session_->chrome->GetBrowserInfo(); |
| 227 |
| 228 bool should_report_error = true; |
| 229 if (browser_info->browser_name == "chrome") { |
| 230 should_report_error = !(browser_info->build_no >= 1967 && |
| 231 browser_info->build_no <= 2000); |
| 232 } else { |
| 233 should_report_error = !(browser_info->blink_revision >= 172887 && |
| 234 browser_info->blink_revision <= 174227); |
| 235 } |
| 236 return should_report_error; |
| 237 } |
| 238 |
| 239 // We're not yet able to tell the Chrome version, so don't report this error. |
| 240 return false; |
| 241 } |
| 242 |
| 243 Status PerformanceLogger::StartTrace() { |
| 244 if (!browser_client_) { |
| 245 return Status(kUnknownError, "tried to start tracing, but connection to " |
| 246 "browser was not yet established"); |
| 247 } |
| 248 if (trace_buffering_) { |
| 249 LOG(WARNING) << "tried to start tracing, but a trace was already started"; |
| 250 return Status(kOk); |
| 251 } |
| 252 base::DictionaryValue params; |
| 253 params.SetString("categories", prefs_.trace_categories); |
| 254 // Ask DevTools to report buffer usage. |
| 255 params.SetInteger("bufferUsageReportingInterval", |
| 256 prefs_.buffer_usage_reporting_interval); |
| 257 Status status = browser_client_->SendCommand("Tracing.start", params); |
| 258 if (status.IsError() && ShouldReportTracingError()) { |
| 259 LOG(ERROR) << "error when starting trace: " << status.message(); |
| 260 return status; |
| 261 } |
| 262 trace_buffering_ = true; |
| 263 return Status(kOk); |
| 264 } |
| 265 |
| 266 Status PerformanceLogger::CollectTraceEvents() { |
| 267 if (!browser_client_) { |
| 268 return Status(kUnknownError, "tried to collect trace events, but " |
| 269 "connection to browser was not yet established"); |
| 270 } |
| 271 if (!trace_buffering_) { |
| 272 return Status(kUnknownError, "tried to collect trace events, but tracing " |
| 273 "was not started"); |
| 274 } |
| 275 |
| 276 Status status = browser_client_->SendCommand("Tracing.end", |
| 277 base::DictionaryValue()); |
| 278 if (status.IsError() && ShouldReportTracingError()) { |
| 279 LOG(ERROR) << "error when stopping trace: " << status.message(); |
| 280 return status; |
| 281 } |
| 282 |
| 283 // Block up to 30 seconds until Tracing.tracingComplete event is received. |
| 284 status = browser_client_->HandleEventsUntil( |
| 285 base::Bind(&PerformanceLogger::IsTraceDone, base::Unretained(this)), |
| 286 base::TimeDelta::FromSeconds(30)); |
| 287 if (status.IsError()) |
| 288 return status; |
| 289 |
| 290 return StartTrace(); |
| 291 } |
| 292 |
| 293 Status PerformanceLogger::IsTraceDone(bool* trace_done) const { |
| 294 *trace_done = !trace_buffering_; |
| 295 return Status(kOk); |
| 296 } |
OLD | NEW |