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 "QtQml/private/qv8profilerservice_p.h"
46 #include "../shared/debugutil_p.h"
47 #include "../../../shared/util.h"
50 #define STR_PORT "13774"
52 class QV8ProfilerClient : public QQmlDebugClient
57 QV8ProfilerClient(QQmlDebugConnection *connection)
58 : QQmlDebugClient(QLatin1String("V8Profiler"), connection)
62 void startProfiling(const QString &name) {
64 QDataStream stream(&message, QIODevice::WriteOnly);
65 stream << QByteArray("V8PROFILER") << QByteArray("start") << name;
69 void stopProfiling(const QString &name) {
71 QDataStream stream(&message, QIODevice::WriteOnly);
72 stream << QByteArray("V8PROFILER") << QByteArray("stop") << name;
78 QDataStream stream(&message, QIODevice::WriteOnly);
79 stream << QByteArray("V8SNAPSHOT") << QByteArray("full");
83 void deleteSnapshots() {
85 QDataStream stream(&message, QIODevice::WriteOnly);
86 stream << QByteArray("V8SNAPSHOT") << QByteArray("delete");
90 QList<QV8ProfilerData> traceMessages;
91 QList<QByteArray> snapshotMessages;
99 void messageReceived(const QByteArray &message);
102 class tst_QV8ProfilerService : public QQmlDataTest
107 tst_QV8ProfilerService()
115 QQmlDebugProcess *m_process;
116 QQmlDebugConnection *m_connection;
117 QV8ProfilerClient *m_client;
119 void connect(bool block, const QString &testFile);
124 void blockingConnectWithTraceEnabled();
125 void blockingConnectWithTraceDisabled();
126 void nonBlockingConnect();
128 void profileOnExit();
132 void QV8ProfilerClient::messageReceived(const QByteArray &message)
134 QByteArray msg = message;
135 QDataStream stream(&msg, QIODevice::ReadOnly);
138 stream >> messageType;
140 QVERIFY(messageType >= 0);
141 QVERIFY(messageType < QV8ProfilerService::V8MaximumMessage);
143 switch (messageType) {
144 case QV8ProfilerService::V8Entry: {
145 QV8ProfilerData entry;
146 stream >> entry.filename >> entry.functionname >> entry.lineNumber >> entry.totalTime >> entry.selfTime >> entry.treeLevel;
147 traceMessages.append(entry);
150 case QV8ProfilerService::V8Complete:
153 case QV8ProfilerService::V8SnapshotChunk: {
156 snapshotMessages.append(json);
159 case QV8ProfilerService::V8SnapshotComplete:
162 case QV8ProfilerService::V8Started:
166 QString failMessage = QString("Unknown message type: %1").arg(messageType);
167 QFAIL(qPrintable(failMessage));
170 QVERIFY(stream.atEnd());
173 void tst_QV8ProfilerService::connect(bool block, const QString &testFile)
175 const QString executable = QLibraryInfo::location(QLibraryInfo::BinariesPath) + "/qmlscene";
176 QStringList arguments;
179 arguments << QString("-qmljsdebugger=port:"STR_PORT",block");
181 arguments << QString("-qmljsdebugger=port:"STR_PORT);
183 arguments << QQmlDataTest::instance()->testFile(testFile);
185 m_process = new QQmlDebugProcess(executable);
186 m_process->start(QStringList() << arguments);
187 if (!m_process->waitForSessionStart()) {
188 QString failMsg = QString("Could not launch app '%1'.\nApplication output:\n%2").arg(
189 executable, m_process->output());
190 QFAIL(qPrintable(failMsg));
193 QQmlDebugConnection *m_connection = new QQmlDebugConnection();
194 m_client = new QV8ProfilerClient(m_connection);
196 m_connection->connectToHost(QLatin1String("127.0.0.1"), PORT);
199 void tst_QV8ProfilerService::cleanup()
201 if (QTest::currentTestFailed())
202 qDebug() << "Application Output:" << m_process->output();
209 void tst_QV8ProfilerService::blockingConnectWithTraceEnabled()
211 connect(true, "test.qml");
212 QTRY_COMPARE(m_client->state(), QQmlDebugClient::Enabled);
214 m_client->startProfiling("");
215 QVERIFY2(QQmlDebugTest::waitForSignal(m_client, SIGNAL(started())),
216 "No start signal received in time.");
217 m_client->stopProfiling("");
218 QVERIFY2(QQmlDebugTest::waitForSignal(m_client, SIGNAL(complete())),
219 "No trace received in time.");
222 void tst_QV8ProfilerService::blockingConnectWithTraceDisabled()
224 connect(true, "test.qml");
225 QTRY_COMPARE(m_client->state(), QQmlDebugClient::Enabled);
227 m_client->stopProfiling("");
228 if (QQmlDebugTest::waitForSignal(m_client, SIGNAL(complete()), 1000)) {
230 = QString("Unexpected trace received! App output: %1\n\n").arg(m_process->output());
231 QFAIL(qPrintable(failMsg));
233 m_client->startProfiling("");
234 QVERIFY2(QQmlDebugTest::waitForSignal(m_client, SIGNAL(started())),
235 "No start signal received in time.");
236 m_client->stopProfiling("");
237 QVERIFY2(QQmlDebugTest::waitForSignal(m_client, SIGNAL(complete())),
238 "No trace received in time.");
241 void tst_QV8ProfilerService::nonBlockingConnect()
243 connect(false, "test.qml");
244 QTRY_COMPARE(m_client->state(), QQmlDebugClient::Enabled);
246 m_client->startProfiling("");
247 QVERIFY2(QQmlDebugTest::waitForSignal(m_client, SIGNAL(started())),
248 "No start signal received in time.");
249 m_client->stopProfiling("");
250 QVERIFY2(QQmlDebugTest::waitForSignal(m_client, SIGNAL(complete())),
251 "No trace received in time.");
254 void tst_QV8ProfilerService::snapshot()
256 connect(false, "test.qml");
257 QTRY_COMPARE(m_client->state(), QQmlDebugClient::Enabled);
259 m_client->takeSnapshot();
260 QVERIFY2(QQmlDebugTest::waitForSignal(m_client, SIGNAL(snapshot())),
261 "No trace received in time.");
264 void tst_QV8ProfilerService::profileOnExit()
266 connect(true, "exit.qml");
267 QTRY_COMPARE(m_client->state(), QQmlDebugClient::Enabled);
269 m_client->startProfiling("");
270 QVERIFY2(QQmlDebugTest::waitForSignal(m_client, SIGNAL(started())),
271 "No start signal received in time.");
273 QVERIFY2(QQmlDebugTest::waitForSignal(m_client, SIGNAL(complete())),
274 "No trace received in time.");
275 //QVERIFY(!m_client->traceMessages.isEmpty());
278 void tst_QV8ProfilerService::console()
280 connect(true, "console.qml");
281 QTRY_COMPARE(m_client->state(), QQmlDebugClient::Enabled);
283 m_client->stopProfiling("");
285 QVERIFY2(QQmlDebugTest::waitForSignal(m_client, SIGNAL(started())),
286 "No start signal received in time.");
287 QVERIFY2(QQmlDebugTest::waitForSignal(m_client, SIGNAL(complete())),
288 "No trace received in time.");
289 QVERIFY(!m_client->traceMessages.isEmpty());
292 QTEST_MAIN(tst_QV8ProfilerService)
294 #include "tst_qv8profilerservice.moc"