-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added QNetworkAccessManagerTracer - auxiliary class for performance l…
…ogging support.
- Loading branch information
1 parent
815297c
commit 6e37fa3
Showing
5 changed files
with
227 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/**************************************************************************** | ||
** | ||
** Copyright © 1992-2014 Cisco and/or its affiliates. All rights reserved. | ||
** All rights reserved. | ||
** | ||
** $CISCO_BEGIN_LICENSE:LGPL$ | ||
** | ||
** GNU Lesser General Public License Usage | ||
** Alternatively, this file may be used under the terms of the GNU Lesser | ||
** General Public License version 2.1 as published by the Free Software | ||
** Foundation and appearing in the file LICENSE.LGPL included in the | ||
** packaging of this file. Please review the following information to | ||
** ensure the GNU Lesser General Public License version 2.1 requirements | ||
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | ||
** | ||
** $CISCO_END_LICENSE$ | ||
** | ||
****************************************************************************/ | ||
|
||
#ifndef NETWORK_ACCESS_MANAGER_H | ||
#define NETWORK_ACCESS_MANAGER_H | ||
|
||
#include <string> | ||
#include <QtNetwork/QNetworkAccessManager> | ||
|
||
namespace webdriver { | ||
class Session; | ||
} | ||
|
||
/// Auxiliary class for performance logging support.<br> | ||
/// Limitation: It is currently not supported to change the network access manager after the QWebPage has used it. | ||
class QNetworkAccessManagerTracer: public QNetworkAccessManager { | ||
Q_OBJECT | ||
public: | ||
/// Create custom network access manager. | ||
/// @param session pointer to Session object. | ||
/// @param parent set as the parent object. | ||
QNetworkAccessManagerTracer(webdriver::Session* session, QObject* parent = 0); | ||
|
||
virtual ~QNetworkAccessManagerTracer(); | ||
|
||
protected: | ||
/// Overrided, additional write a JSON entry for every received reply. | ||
virtual QNetworkReply* createRequest(Operation op, const QNetworkRequest& req, QIODevice* outgoingData = 0); | ||
|
||
protected slots: | ||
/// Create single log entry and add it to session's performance log. | ||
/// <br>Currently WebDriver supports only Performance Log of Network. | ||
/// <p>Each entry is a JSON string of the following structure: | ||
/// @code | ||
/// { | ||
/// "webview": "class_name", | ||
/// "message": { | ||
/// "args": { | ||
/// "file": "file_path/name", | ||
/// "method": "HTTP_method", | ||
/// "status": "HTTP_status_code", | ||
/// }, | ||
/// "tid": "thread_id", | ||
/// "ts": "timestamp", | ||
/// "tts": "thread-specific_CPU-time" | ||
/// } | ||
/// } | ||
/// @endcode | ||
/// @param reply pointer to reply, that contains the data which will be written to PerfLog. | ||
void writeReply(QNetworkReply* reply); | ||
|
||
private: | ||
std::string getMethod(Operation op); | ||
webdriver::Session* session_; | ||
double timeStamp_; | ||
}; | ||
|
||
#endif //NETWORK_ACCESS_MANAGER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
src/webdriver/extension_qt/qnetwork_access_manager_tracer.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/**************************************************************************** | ||
** | ||
** Copyright © 1992-2014 Cisco and/or its affiliates. All rights reserved. | ||
** All rights reserved. | ||
** | ||
** $CISCO_BEGIN_LICENSE:LGPL$ | ||
** | ||
** GNU Lesser General Public License Usage | ||
** Alternatively, this file may be used under the terms of the GNU Lesser | ||
** General Public License version 2.1 as published by the Free Software | ||
** Foundation and appearing in the file LICENSE.LGPL included in the | ||
** packaging of this file. Please review the following information to | ||
** ensure the GNU Lesser General Public License version 2.1 requirements | ||
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | ||
** | ||
** $CISCO_END_LICENSE$ | ||
** | ||
****************************************************************************/ | ||
|
||
#include "extension_qt/qnetwork_access_manager_tracer.h" | ||
#include "webdriver_session.h" | ||
#include "webdriver_util.h" | ||
#include "web_view_util.h" | ||
|
||
#include "base/values.h" | ||
#include "base/time.h" | ||
#include "base/threading/platform_thread.h" | ||
#include "base/string_number_conversions.h" | ||
|
||
#include <QtCore/QThread> | ||
#include <QtNetwork/QNetworkReply> | ||
|
||
QNetworkAccessManagerTracer::QNetworkAccessManagerTracer(webdriver::Session* session, QObject* parent) | ||
: QNetworkAccessManager(parent), session_(session) { | ||
} | ||
|
||
QNetworkAccessManagerTracer::~QNetworkAccessManagerTracer() { } | ||
|
||
QNetworkReply* QNetworkAccessManagerTracer::createRequest(QNetworkAccessManager::Operation op, const QNetworkRequest &req, QIODevice *outgoingData) { | ||
|
||
timeStamp_ = static_cast<double>(base::TimeTicks::NowFromSystemTraceTime().ToInternalValue()); | ||
QNetworkReply* reply = QNetworkAccessManager::createRequest(op, req, outgoingData); | ||
connect(this, SIGNAL(finished(QNetworkReply*)), this, SLOT(writeReply(QNetworkReply*))); | ||
|
||
return reply; | ||
} | ||
|
||
void QNetworkAccessManagerTracer::writeReply(QNetworkReply *reply) { | ||
webdriver::LogLevel level = session_->GetMinPerfLogLevel(); | ||
|
||
double thread_timestamp = static_cast<double>((base::TimeTicks::IsThreadNowSupported() ? | ||
base::TimeTicks::ThreadNow() : base::TimeTicks()).ToInternalValue()); | ||
|
||
std::string file = reply->url().path().toStdString(); | ||
// delete '/' in the beginning: | ||
file.erase(0, 1); | ||
|
||
//HTTP status code | ||
QVariant statusCode = reply->attribute( QNetworkRequest::HttpStatusCodeAttribute ); | ||
QString reason; | ||
if ( !statusCode.isValid() ) { | ||
reason = "INVALID"; | ||
level = webdriver::LogLevelFromString("SEVERE"); | ||
} else { | ||
int status = statusCode.toInt(); | ||
if ( status != 200 ) { | ||
level = webdriver::LogLevelFromString("WARNING"); | ||
} else { | ||
level = webdriver::LogLevelFromString("INFO"); | ||
} | ||
reason = reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString(); | ||
} | ||
base::DictionaryValue* args_entry = new base::DictionaryValue(); | ||
args_entry->SetString("method", getMethod(reply->operation())); | ||
args_entry->SetString("status", reason.toStdString()); | ||
args_entry->SetString("file", file); | ||
|
||
base::DictionaryValue* message_entry = new base::DictionaryValue(); | ||
message_entry->Set("args", args_entry); | ||
message_entry->SetDouble("ts", timeStamp_); | ||
message_entry->SetInteger("tid", static_cast<int>(base::PlatformThread::CurrentId())); | ||
message_entry->SetDouble("tts", thread_timestamp); | ||
|
||
base::DictionaryValue* entry = new base::DictionaryValue; | ||
std::string webview = webdriver::QWebViewUtil::getWebView(session_,session_->current_view())->metaObject()->className(); | ||
entry->SetString("webview", webview); | ||
entry->Set("message", message_entry); | ||
std::string log_entry = webdriver::JsonStringifyForDisplay(entry); | ||
|
||
session_->AddPerfLogEntry(level, log_entry); | ||
delete entry; | ||
|
||
} | ||
|
||
std::string QNetworkAccessManagerTracer::getMethod(QNetworkAccessManager::Operation op) { | ||
std::string method; | ||
switch (op) { | ||
case QNetworkAccessManager::HeadOperation: | ||
method = "HEAD"; | ||
break; | ||
case QNetworkAccessManager::GetOperation: | ||
method = "GET"; | ||
break; | ||
case QNetworkAccessManager::PutOperation: | ||
method= "PUT"; | ||
break; | ||
case QNetworkAccessManager::PostOperation: | ||
method = "POST"; | ||
break; | ||
case QNetworkAccessManager::DeleteOperation: | ||
method = "DELETE"; | ||
break; | ||
case QNetworkAccessManager::CustomOperation: | ||
method = "custom"; | ||
break; | ||
default: | ||
method = "unsupported method"; | ||
break; | ||
} | ||
return method; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters