summaryrefslogtreecommitdiffstats
path: root/tests/benchmarks/script
diff options
context:
space:
mode:
authorKent Hansen <kent.hansen@nokia.com>2011-02-03 14:48:02 (GMT)
committerKent Hansen <kent.hansen@nokia.com>2011-02-04 11:12:59 (GMT)
commit6163c36249eea0a2a578b9990470fcaa2f7bcf5f (patch)
tree490e647d331d12c017b86af5641d9d038485e486 /tests/benchmarks/script
parentda48bbe66b7821416bee1ebc0215bc93e68f0a2a (diff)
downloadQt-6163c36249eea0a2a578b9990470fcaa2f7bcf5f.zip
Qt-6163c36249eea0a2a578b9990470fcaa2f7bcf5f.tar.gz
Qt-6163c36249eea0a2a578b9990470fcaa2f7bcf5f.tar.bz2
Add Context2D QtScript benchmark
Reuse the Context2D implementation from examples/script/context2d and create a benchmark out of it. This benchmark actually measures a meaningful, real-world use case. Task-number: QTBUG-17192 Reviewed-by: Jedrzej Nowacki
Diffstat (limited to 'tests/benchmarks/script')
-rw-r--r--tests/benchmarks/script/context2d/context2d.pro22
-rw-r--r--tests/benchmarks/script/context2d/tst_context2d.cpp177
-rw-r--r--tests/benchmarks/script/script.pro3
3 files changed, 201 insertions, 1 deletions
diff --git a/tests/benchmarks/script/context2d/context2d.pro b/tests/benchmarks/script/context2d/context2d.pro
new file mode 100644
index 0000000..bc94c4f
--- /dev/null
+++ b/tests/benchmarks/script/context2d/context2d.pro
@@ -0,0 +1,22 @@
+load(qttest_p4)
+TEMPLATE = app
+TARGET = tst_bench_context2d
+
+SOURCES += tst_context2d.cpp
+
+CONTEXT2D_EXAMPLE_DIR = $$QT_SOURCE_TREE/examples/script/context2d
+INCLUDEPATH += $$CONTEXT2D_EXAMPLE_DIR
+
+HEADERS += $$CONTEXT2D_EXAMPLE_DIR/qcontext2dcanvas.h \
+ $$CONTEXT2D_EXAMPLE_DIR/context2d.h \
+ $$CONTEXT2D_EXAMPLE_DIR/domimage.h \
+ $$CONTEXT2D_EXAMPLE_DIR/environment.h
+
+SOURCES += $$CONTEXT2D_EXAMPLE_DIR/qcontext2dcanvas.cpp \
+ $$CONTEXT2D_EXAMPLE_DIR/context2d.cpp \
+ $$CONTEXT2D_EXAMPLE_DIR/domimage.cpp \
+ $$CONTEXT2D_EXAMPLE_DIR/environment.cpp
+
+RESOURCES += $$CONTEXT2D_EXAMPLE_DIR/context2d.qrc
+
+QT += script
diff --git a/tests/benchmarks/script/context2d/tst_context2d.cpp b/tests/benchmarks/script/context2d/tst_context2d.cpp
new file mode 100644
index 0000000..8401590
--- /dev/null
+++ b/tests/benchmarks/script/context2d/tst_context2d.cpp
@@ -0,0 +1,177 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the test suite of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** No Commercial Usage
+** This file contains pre-release code and may not be distributed.
+** You may use this file in accordance with the terms and conditions
+** contained in the Technology Preview License Agreement accompanying
+** this package.
+**
+** 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.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** If you have questions regarding the use of this file, please contact
+** Nokia at qt-info@nokia.com.
+**
+**
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include <qtest.h>
+#include <QtCore/qdir.h>
+#include <QtCore/qfile.h>
+#include <QtCore/qtextstream.h>
+#include <QtScript/qscriptengine.h>
+#include <QtScript/qscriptvalue.h>
+#include "context2d.h"
+#include "environment.h"
+#include "qcontext2dcanvas.h"
+
+static QString readFile(const QString &filename)
+{
+ QFile file(filename);
+ if (!file.open(QFile::ReadOnly))
+ return QString();
+ QTextStream stream(&file);
+ stream.setCodec("UTF-8");
+ return stream.readAll();
+}
+
+class tst_Context2D : public QObject
+{
+ Q_OBJECT
+
+public:
+ tst_Context2D();
+ ~tst_Context2D();
+
+private slots:
+ void singleExecution_data();
+ void singleExecution();
+ void repeatedExecution_data();
+ void repeatedExecution();
+
+private:
+ void newEnvironment();
+
+private:
+ QDir testsDir;
+ Environment *m_env;
+ QContext2DCanvas *m_canvas;
+};
+
+tst_Context2D::tst_Context2D()
+ : m_env(0), m_canvas(0)
+{
+ testsDir = QDir(":/scripts");
+ if (!testsDir.exists())
+ qWarning("*** no scripts/ dir!");
+}
+
+tst_Context2D::~tst_Context2D()
+{
+ delete m_canvas;
+ delete m_env;
+}
+
+void tst_Context2D::newEnvironment()
+{
+ delete m_canvas;
+ delete m_env;
+ m_env = new Environment();
+ Context2D *context = new Context2D(m_env);
+ context->setSize(150, 150); // Hard-coded in many of the scripts.
+ m_canvas = new QContext2DCanvas(context, m_env);
+ m_canvas->setFixedSize(context->size());
+ m_canvas->setObjectName("tutorial"); // Acts as the DOM element ID.
+ m_env->addCanvas(m_canvas);
+}
+
+void tst_Context2D::singleExecution_data()
+{
+ QTest::addColumn<QString>("testName");
+ QFileInfoList testFileInfos = testsDir.entryInfoList(QStringList() << "*.js", QDir::Files);
+ foreach (QFileInfo tfi, testFileInfos) {
+ QString name = tfi.baseName();
+ QTest::newRow(name.toLatin1().constData()) << name;
+ }
+}
+
+void tst_Context2D::singleExecution()
+{
+ QFETCH(QString, testName);
+ QString script = readFile(testsDir.absoluteFilePath(testName + ".js"));
+ QVERIFY(!script.isEmpty());
+
+ newEnvironment();
+ QBENCHMARK {
+ m_env->evaluate(script, testName);
+ // Some of the scripts (e.g. plasma.js) merely start a timer and do
+ // the actual drawing in the timer event. Trigger the timers now to
+ // ensure that the real work is done.
+ m_env->triggerTimers();
+ }
+ QVERIFY(!m_env->engine()->hasUncaughtException());
+}
+
+void tst_Context2D::repeatedExecution_data()
+{
+ // We look for scripts that register an interval timer.
+ // Such scripts run a function every n milliseconds to update the canvas.
+ // The benchmark will execute this function repeatedly, which can allow
+ // us to observe potential effects of profiling-based JIT optimizations.
+ QTest::addColumn<QString>("testName");
+ QTest::addColumn<QString>("script");
+ QFileInfoList testFileInfos = testsDir.entryInfoList(QStringList() << "*.js", QDir::Files);
+ foreach (QFileInfo tfi, testFileInfos) {
+ QString script = readFile(tfi.absoluteFilePath());
+ QString name = tfi.baseName();
+ newEnvironment();
+ m_env->evaluate(script, name);
+ if (m_env->engine()->hasUncaughtException())
+ continue;
+ if (m_env->hasIntervalTimers())
+ QTest::newRow(name.toLatin1().constData()) << name << script;
+ }
+}
+
+void tst_Context2D::repeatedExecution()
+{
+ QFETCH(QString, testName);
+ QFETCH(QString, script);
+
+ newEnvironment();
+ m_env->evaluate(script, testName);
+ QBENCHMARK {
+ // Trigger the update function repeatedly, effectively
+ // performing several frames of animation.
+ for (int i = 0; i < 16; ++i)
+ m_env->triggerTimers();
+ }
+ QVERIFY(!m_env->engine()->hasUncaughtException());
+}
+
+QTEST_MAIN(tst_Context2D)
+#include "tst_context2d.moc"
diff --git a/tests/benchmarks/script/script.pro b/tests/benchmarks/script/script.pro
index b0770ca..da7a5a1 100644
--- a/tests/benchmarks/script/script.pro
+++ b/tests/benchmarks/script/script.pro
@@ -1,5 +1,6 @@
TEMPLATE = subdirs
SUBDIRS = \
+ context2d \
qscriptclass \
qscriptengine \
qscriptvalue \
@@ -13,4 +14,4 @@ TRUSTED_BENCHMARKS += \
qscriptvalue \
qscriptengine
-include(../trusted-benchmarks.pri) \ No newline at end of file
+include(../trusted-benchmarks.pri)