Skip to content

Commit

Permalink
Added performance log handler.
Browse files Browse the repository at this point in the history
  • Loading branch information
nataliakoval committed Feb 12, 2015
1 parent 23989c2 commit 815297c
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 1 deletion.
38 changes: 38 additions & 0 deletions inc/webdriver_logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class LogType {
kInvalid = -1, /// invalid type
kDriver, /// driver
kBrowser, /// browser (console.log)
kPerformance, /// performance
kNum /// number of sources
};

Expand Down Expand Up @@ -162,6 +163,43 @@ class InMemoryLog : public LogHandler {
DISALLOW_COPY_AND_ASSIGN(InMemoryLog);
};

/// Performance log handler.<br>
/// <b>Limitation:</b> currently we can collect Performance Logs only for QWebViewExt.<br>
/// If you need receive perf logs for other QWebViews or their subclasses,
/// you should change the network access manager before the QWebPage has used it
/// (in other cases the results of doing this are undefined) :
/// @code
/// QWebView* view = new QWebView;;
/// QNetworkAccessManager* manager = new QNetworkAccessManagerTracer(session, view->page());
/// view->page()->setNetworkAccessManager(manager);
/// @endcode
/// before the first call to the page. After that you can connect to the window.
/// <p>In other case you can change manager in constructor custom webviews like QWebViewExt.
/// Then you can register and use it as usually :
/// @code
/// webdriver::ViewCreator* webCreator = new webdriver::QWebViewCreator();
/// webCreator->RegisterViewClass<CustomWebView>("CustomWebView");
/// webdriver::ViewFactory::GetInstance()->AddViewCreator(webCreator);
/// @endcode
/// <p>If your CustomWebView class is overriding createWindow method of QWebView,
/// you should ensure that new windows will use needed network access manager with logs support.

class PerfLog : public InMemoryLog {
public:
PerfLog();
virtual ~PerfLog();
virtual void Log(LogLevel level, const base::Time& time,
const std::string& message) OVERRIDE;

void set_min_log_level(LogLevel level);
LogLevel min_log_level();

private:
LogLevel min_log_level_;

DISALLOW_COPY_AND_ASSIGN(PerfLog);
};

/// Forwards logging messages to added logs.
class Logger {
public:
Expand Down
9 changes: 9 additions & 0 deletions inc/webdriver_session.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ class Session {
/// returned value.
base::ListValue* GetLog() const;

void AddPerfLogEntry(LogLevel level, const std::string& message);

/// Return current minimum LogLevel for Performance Logging, by default kOffLogLevel.
LogLevel GetMinPerfLogLevel() const;

/// Returns a copy of the current performance log entries.
base::ListValue* GetPerfLog() const;

/// Runs task in session's context
/// @param task task to run
void RunSessionTask(const base::Closure& task);
Expand Down Expand Up @@ -233,6 +241,7 @@ class Session {
base::WaitableEvent* done_event);

scoped_ptr<InMemoryLog> session_log_;
scoped_ptr<PerfLog> session_perf_log_;
Logger logger_;

// session id
Expand Down
6 changes: 6 additions & 0 deletions src/webdriver/commands/log_command.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ void LogCommand::ExecutePost(Response* const response) {
if (log_type.type() == LogType::kDriver) {
response->SetValue(session_->GetLog());
}
else if (log_type.type() == LogType::kPerformance) {
response->SetValue(session_->GetPerfLog());
}
else if (log_type.type() == LogType::kBrowser) {
Error* error = NULL;

Expand Down Expand Up @@ -85,6 +88,9 @@ void LogTypesCommand::ExecuteGet(Response* const response) {
base::ListValue* logTypes_list = new base::ListValue();
logTypes_list->Append(Value::CreateStringValue(LogType(LogType::kDriver).ToString()));
logTypes_list->Append(Value::CreateStringValue(LogType(LogType::kBrowser).ToString()));
if (session_->GetMinPerfLogLevel() != kOffLogLevel) {
logTypes_list->Append(Value::CreateStringValue(LogType(LogType::kPerformance).ToString()));
}
response->SetValue(logTypes_list);
}

Expand Down
2 changes: 2 additions & 0 deletions src/webdriver/webdriver_capabilities_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ Capabilities::Capabilities()
caps(new DictionaryValue()) {
log_levels[LogType::kDriver] = kAllLogLevel;
log_levels[LogType::kBrowser] = kAllLogLevel;
// disabled by default
log_levels[LogType::kPerformance] = kOffLogLevel;
}

Capabilities::~Capabilities() { }
Expand Down
25 changes: 24 additions & 1 deletion src/webdriver/webdriver_logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ bool LogType::FromString(const std::string& name, LogType* log_type) {
*log_type = LogType(kDriver);
} else if (name == "browser") {
*log_type = LogType(kBrowser);
} else{
} else if (name == "performance") {
*log_type = LogType(kPerformance);
} else {
return false;
}
return true;
Expand All @@ -89,6 +91,8 @@ std::string LogType::ToString() const {
return "driver";
case kBrowser:
return "browser";
case kPerformance:
return "performance";
default:
return "unknown";
};
Expand Down Expand Up @@ -280,6 +284,25 @@ void InMemoryLog::clear_entries_list() {
entries_list_.Clear();
}

PerfLog::PerfLog(): min_log_level_(kOffLogLevel) { }

PerfLog::~PerfLog() { }

void PerfLog::set_min_log_level(LogLevel level) {
min_log_level_= level;
}

LogLevel PerfLog::min_log_level() {
return min_log_level_;
}

void PerfLog::Log(LogLevel level, const base::Time &time, const std::string &message) {
if (level < min_log_level_) {
return;
}
InMemoryLog::Log(level, time, message);
}

Logger::Logger() : min_log_level_(kAllLogLevel) { }

Logger::Logger(LogLevel level) : min_log_level_(level) { }
Expand Down
22 changes: 22 additions & 0 deletions src/webdriver/webdriver_session.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ namespace webdriver {

Session::Session()
: session_log_(new InMemoryLog()),
session_perf_log_(new PerfLog()),
logger_(kAllLogLevel),
id_(GenerateRandomID()),
current_view_id_(ViewId()),
Expand Down Expand Up @@ -295,6 +296,9 @@ Error* Session::Init(const base::DictionaryValue* desired_capabilities_dict,
return error;
}
logger_.set_min_log_level(capabilities_.log_levels[LogType::kDriver]);
if (capabilities_.log_levels[LogType::kPerformance] != kOffLogLevel) {
session_perf_log_->set_min_log_level(capabilities_.log_levels[LogType::kPerformance]);
}

error = life_cycle_actions_->PostInit(desired_capabilities_dict, required_capabilities_dict);

Expand Down Expand Up @@ -412,6 +416,24 @@ base::ListValue* Session::GetLog() const {
return ret_val;
}

void Session::AddPerfLogEntry(LogLevel level, const std::string& message) {
if (!session_perf_log_.get()) {
return;
}
base::Time time = base::Time::Now();
session_perf_log_->Log(level, time, message);
}

LogLevel Session::GetMinPerfLogLevel() const {
return session_perf_log_->min_log_level();
}

base::ListValue* Session::GetPerfLog() const {
base::ListValue* ret_val = session_perf_log_->entries_list()->DeepCopy();
session_perf_log_->clear_entries_list();
return ret_val;
}

void Session::RunSessionTask(const base::Closure& task) {
base::WaitableEvent done_event(false, false);
thread_.message_loop_proxy()->PostTask(FROM_HERE, base::Bind(
Expand Down

0 comments on commit 815297c

Please sign in to comment.