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 "debugutil_p.h"
47 #include "qqmldebugclient.h"
48 #include "../../../shared/util.h"
51 #define STR_PORT "13774"
53 class QV8ProfilerClient : public QQmlDebugClient
58 QV8ProfilerClient(QQmlDebugConnection *connection)
59 : QQmlDebugClient(QLatin1String("V8Profiler"), connection)
63 void startProfiling(const QString &name) {
65 QDataStream stream(&message, QIODevice::WriteOnly);
66 stream << QByteArray("V8PROFILER") << QByteArray("start") << name;
70 void stopProfiling(const QString &name) {
72 QDataStream stream(&message, QIODevice::WriteOnly);
73 stream << QByteArray("V8PROFILER") << QByteArray("stop") << name;
79 QDataStream stream(&message, QIODevice::WriteOnly);
80 stream << QByteArray("V8SNAPSHOT") << QByteArray("full");
84 void deleteSnapshots() {
86 QDataStream stream(&message, QIODevice::WriteOnly);
87 stream << QByteArray("V8SNAPSHOT") << QByteArray("delete");
91 QList<QV8ProfilerData> traceMessages;
92 QList<QByteArray> snapshotMessages;
100 void messageReceived(const QByteArray &message);
103 class tst_QV8ProfilerService : public QQmlDataTest
108 tst_QV8ProfilerService()
116 QQmlDebugProcess *m_process;
117 QQmlDebugConnection *m_connection;
118 QV8ProfilerClient *m_client;
120 void connect(bool block, const QString &testFile);
125 void blockingConnectWithTraceEnabled();
126 void blockingConnectWithTraceDisabled();
127 void nonBlockingConnect();
129 void profileOnExit();
133 void QV8ProfilerClient::messageReceived(const QByteArray &message)
135 QByteArray msg = message;
136 QDataStream stream(&msg, QIODevice::ReadOnly);
139 stream >> messageType;
141 QVERIFY(messageType >= 0);
142 QVERIFY(messageType < QV8ProfilerService::V8MaximumMessage);
144 switch (messageType) {
145 case QV8ProfilerService::V8Entry: {
146 QV8ProfilerData entry;
147 stream >> entry.filename >> entry.functionname >> entry.lineNumber >> entry.totalTime >> entry.selfTime >> entry.treeLevel;
148 traceMessages.append(entry);
151 case QV8ProfilerService::V8Complete:
154 case QV8ProfilerService::V8SnapshotChunk: {
157 snapshotMessages.append(json);
160 case QV8ProfilerService::V8SnapshotComplete:
163 case QV8ProfilerService::V8Started:
167 QString failMessage = QString("Unknown message type: %1").arg(messageType);
168 QFAIL(qPrintable(failMessage));
171 QVERIFY(stream.atEnd());
174 void tst_QV8ProfilerService::connect(bool block, const QString &testFile)
176 const QString executable = QLibraryInfo::location(QLibraryInfo::BinariesPath) + "/qmlscene";
177 QStringList arguments;
180 arguments << QString("-qmljsdebugger=port:"STR_PORT",block");
182 arguments << QString("-qmljsdebugger=port:"STR_PORT);
184 arguments << QQmlDataTest::instance()->testFile(testFile);
186 m_process = new QQmlDebugProcess(executable);
187 m_process->start(QStringList() << arguments);
188 if (!m_process->waitForSessionStart()) {
189 QString failMsg = QString("Could not launch app '%1'.\nApplication output:\n%2").arg(
190 executable, m_process->output());
191 QFAIL(qPrintable(failMsg));
194 QQmlDebugConnection *m_connection = new QQmlDebugConnection();
195 m_client = new QV8ProfilerClient(m_connection);
197 m_connection->connectToHost(QLatin1String("127.0.0.1"), PORT);
200 void tst_QV8ProfilerService::cleanup()
202 if (QTest::currentTestFailed())
203 qDebug() << "Application Output:" << m_process->output();
210 void tst_QV8ProfilerService::blockingConnectWithTraceEnabled()
212 connect(true, "test.qml");
213 QTRY_COMPARE(m_client->state(), QQmlDebugClient::Enabled);
215 m_client->startProfiling("");
216 QVERIFY2(QQmlDebugTest::waitForSignal(m_client, SIGNAL(started())),
217 "No start signal received in time.");
218 m_client->stopProfiling("");
219 QVERIFY2(QQmlDebugTest::waitForSignal(m_client, SIGNAL(complete())),
220 "No trace received in time.");
223 void tst_QV8ProfilerService::blockingConnectWithTraceDisabled()
225 connect(true, "test.qml");
226 QTRY_COMPARE(m_client->state(), QQmlDebugClient::Enabled);
228 m_client->stopProfiling("");
229 if (QQmlDebugTest::waitForSignal(m_client, SIGNAL(complete()), 1000)) {
231 = QString("Unexpected trace received! App output: %1\n\n").arg(m_process->output());
232 QFAIL(qPrintable(failMsg));
234 m_client->startProfiling("");
235 QVERIFY2(QQmlDebugTest::waitForSignal(m_client, SIGNAL(started())),
236 "No start signal received in time.");
237 m_client->stopProfiling("");
238 QVERIFY2(QQmlDebugTest::waitForSignal(m_client, SIGNAL(complete())),
239 "No trace received in time.");
242 void tst_QV8ProfilerService::nonBlockingConnect()
244 connect(false, "test.qml");
245 QTRY_COMPARE(m_client->state(), QQmlDebugClient::Enabled);
247 m_client->startProfiling("");
248 QVERIFY2(QQmlDebugTest::waitForSignal(m_client, SIGNAL(started())),
249 "No start signal received in time.");
250 m_client->stopProfiling("");
251 QVERIFY2(QQmlDebugTest::waitForSignal(m_client, SIGNAL(complete())),
252 "No trace received in time.");
255 void tst_QV8ProfilerService::snapshot()
257 connect(false, "test.qml");
258 QTRY_COMPARE(m_client->state(), QQmlDebugClient::Enabled);
260 m_client->takeSnapshot();
261 QVERIFY2(QQmlDebugTest::waitForSignal(m_client, SIGNAL(snapshot())),
262 "No trace received in time.");
265 void tst_QV8ProfilerService::profileOnExit()
267 connect(true, "exit.qml");
268 QTRY_COMPARE(m_client->state(), QQmlDebugClient::Enabled);
270 m_client->startProfiling("");
271 QVERIFY2(QQmlDebugTest::waitForSignal(m_client, SIGNAL(started())),
272 "No start signal received in time.");
274 QVERIFY2(QQmlDebugTest::waitForSignal(m_client, SIGNAL(complete())),
275 "No trace received in time.");
276 //QVERIFY(!m_client->traceMessages.isEmpty());
279 void tst_QV8ProfilerService::console()
281 connect(true, "console.qml");
282 QTRY_COMPARE(m_client->state(), QQmlDebugClient::Enabled);
284 m_client->stopProfiling("");
286 QVERIFY2(QQmlDebugTest::waitForSignal(m_client, SIGNAL(started())),
287 "No start signal received in time.");
288 QVERIFY2(QQmlDebugTest::waitForSignal(m_client, SIGNAL(complete())),
289 "No trace received in time.");
290 QVERIFY(!m_client->traceMessages.isEmpty());
293 QTEST_MAIN(tst_QV8ProfilerService)
295 #include "tst_qv8profilerservice.moc"