1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the test suite of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
43 #include <QLibraryInfo>
45 #include "debugutil_p.h"
46 #include "qqmldebugclient.h"
47 #include "../../../shared/util.h"
50 #define STR_PORT "13774"
52 struct QV8ProfilerData
62 QByteArray toByteArray() const;
65 class QV8ProfilerClient : public QQmlDebugClient
80 enum ServiceState { NotRunning, Running } serviceState;
82 QV8ProfilerClient(QQmlDebugConnection *connection)
83 : QQmlDebugClient(QLatin1String("V8Profiler"), connection)
84 , serviceState(NotRunning)
88 void startProfiling(const QString &name) {
90 QDataStream stream(&message, QIODevice::WriteOnly);
91 stream << QByteArray("V8PROFILER") << QByteArray("start") << name;
95 void stopProfiling(const QString &name) {
97 QDataStream stream(&message, QIODevice::WriteOnly);
98 stream << QByteArray("V8PROFILER") << QByteArray("stop") << name;
102 void takeSnapshot() {
104 QDataStream stream(&message, QIODevice::WriteOnly);
105 stream << QByteArray("V8SNAPSHOT") << QByteArray("full");
106 sendMessage(message);
109 void deleteSnapshots() {
111 QDataStream stream(&message, QIODevice::WriteOnly);
112 stream << QByteArray("V8SNAPSHOT") << QByteArray("delete");
113 sendMessage(message);
116 QList<QV8ProfilerData> traceMessages;
117 QList<QByteArray> snapshotMessages;
125 void messageReceived(const QByteArray &message);
128 class tst_QV8ProfilerService : public QQmlDataTest
133 tst_QV8ProfilerService()
141 QQmlDebugProcess *m_process;
142 QQmlDebugConnection *m_connection;
143 QV8ProfilerClient *m_client;
145 bool connect(bool block, const QString &testFile, QString *error);
150 void blockingConnectWithTraceEnabled();
151 void blockingConnectWithTraceDisabled();
152 void nonBlockingConnect();
154 void profileOnExit();
158 void QV8ProfilerClient::messageReceived(const QByteArray &message)
160 QByteArray msg = message;
161 QDataStream stream(&msg, QIODevice::ReadOnly);
164 stream >> messageType;
166 QVERIFY(messageType >= 0);
167 QVERIFY(messageType < QV8ProfilerClient::V8MaximumMessage);
169 switch (messageType) {
170 case QV8ProfilerClient::V8Entry: {
171 QCOMPARE(serviceState, Running);
172 QV8ProfilerData entry;
173 stream >> entry.filename >> entry.functionname >> entry.lineNumber >> entry.totalTime >> entry.selfTime >> entry.treeLevel;
174 traceMessages.append(entry);
177 case QV8ProfilerClient::V8Complete:
178 QCOMPARE(serviceState, Running);
179 serviceState = NotRunning;
182 case QV8ProfilerClient::V8SnapshotChunk: {
185 snapshotMessages.append(json);
188 case QV8ProfilerClient::V8SnapshotComplete:
191 case QV8ProfilerClient::V8Started:
192 QCOMPARE(serviceState, NotRunning);
193 serviceState = Running;
197 QString failMessage = QString("Unknown message type: %1").arg(messageType);
198 QFAIL(qPrintable(failMessage));
201 QVERIFY(stream.atEnd());
204 bool tst_QV8ProfilerService::connect(bool block, const QString &testFile,
207 const QString executable = QLibraryInfo::location(QLibraryInfo::BinariesPath) + "/qmlscene";
208 QStringList arguments;
211 arguments << QString("-qmljsdebugger=port:"STR_PORT",block");
213 arguments << QString("-qmljsdebugger=port:"STR_PORT);
215 arguments << QQmlDataTest::instance()->testFile(testFile);
217 m_connection = new QQmlDebugConnection();
218 m_client = new QV8ProfilerClient(m_connection);
220 m_process = new QQmlDebugProcess(executable);
221 m_process->start(QStringList() << arguments);
222 if (!m_process->waitForSessionStart()) {
223 *error = QLatin1String("Could not launch app ") + executable;
227 m_connection->connectToHost(QLatin1String("127.0.0.1"), PORT);
228 if (!m_connection->waitForConnected()) {
229 *error = QLatin1String("Could not connect to debugger port.");
235 void tst_QV8ProfilerService::cleanup()
237 if (QTest::currentTestFailed())
238 qDebug() << "Application Output:" << m_process->output();
245 void tst_QV8ProfilerService::blockingConnectWithTraceEnabled()
248 if (!connect(true, "test.qml", &error))
249 QFAIL(qPrintable(error));
251 QTRY_COMPARE(m_client->state(), QQmlDebugClient::Enabled);
253 m_client->startProfiling("");
254 m_client->stopProfiling("");
255 QVERIFY2(QQmlDebugTest::waitForSignal(m_client, SIGNAL(complete())),
256 "No trace received in time.");
259 void tst_QV8ProfilerService::blockingConnectWithTraceDisabled()
262 if (!connect(true, "test.qml", &error))
263 QFAIL(qPrintable(error));
265 QTRY_COMPARE(m_client->state(), QQmlDebugClient::Enabled);
267 m_client->stopProfiling("");
268 if (QQmlDebugTest::waitForSignal(m_client, SIGNAL(complete()), 1000)) {
270 = QString("Unexpected trace received! App output: %1\n\n").arg(m_process->output());
271 QFAIL(qPrintable(failMsg));
273 m_client->startProfiling("");
274 m_client->stopProfiling("");
275 QVERIFY2(QQmlDebugTest::waitForSignal(m_client, SIGNAL(complete())),
276 "No trace received in time.");
279 void tst_QV8ProfilerService::nonBlockingConnect()
282 if (!connect(false, "test.qml", &error))
283 QFAIL(qPrintable(error));
285 QTRY_COMPARE(m_client->state(), QQmlDebugClient::Enabled);
287 m_client->startProfiling("");
288 m_client->stopProfiling("");
289 QVERIFY2(QQmlDebugTest::waitForSignal(m_client, SIGNAL(complete())),
290 "No trace received in time.");
293 void tst_QV8ProfilerService::snapshot()
296 if (!connect(false, "test.qml", &error))
297 QFAIL(qPrintable(error));
299 QTRY_COMPARE(m_client->state(), QQmlDebugClient::Enabled);
301 m_client->takeSnapshot();
302 QVERIFY2(QQmlDebugTest::waitForSignal(m_client, SIGNAL(snapshot())),
303 "No trace received in time.");
306 void tst_QV8ProfilerService::profileOnExit()
309 if (!connect(true, "exit.qml", &error))
310 QFAIL(qPrintable(error));
312 QTRY_COMPARE(m_client->state(), QQmlDebugClient::Enabled);
314 m_client->startProfiling("");
315 QVERIFY2(QQmlDebugTest::waitForSignal(m_client, SIGNAL(complete())),
316 "No trace received in time.");
319 void tst_QV8ProfilerService::console()
322 if (!connect(true, "console.qml", &error))
323 QFAIL(qPrintable(error));
325 QTRY_COMPARE(m_client->state(), QQmlDebugClient::Enabled);
327 m_client->stopProfiling("");
329 QVERIFY2(QQmlDebugTest::waitForSignal(m_client, SIGNAL(complete())),
330 "No trace received in time.");
331 QVERIFY(!m_client->traceMessages.isEmpty());
334 QTEST_MAIN(tst_QV8ProfilerService)
336 #include "tst_qv8profilerservice.moc"