b34ae8b938f4d25c93d4de1c450dd5b081b8ddfe
[profile/ivi/qtdeclarative.git] / tests / benchmarks / declarative / qmltime / qmltime.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
6 **
7 ** This file is part of the test suite of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 #include <QDeclarativeEngine>
42 #include <QDeclarativeComponent>
43 #include <QDebug>
44 #include <QApplication>
45 #include <QTime>
46 #include <QDeclarativeContext>
47 #include <QGraphicsScene>
48 #include <QGraphicsRectItem>
49
50 class Timer : public QObject
51 {
52     Q_OBJECT
53     Q_PROPERTY(QDeclarativeComponent *component READ component WRITE setComponent)
54
55 public:
56     Timer();
57
58     QDeclarativeComponent *component() const;
59     void setComponent(QDeclarativeComponent *);
60
61     static Timer *timerInstance();
62
63     void run(uint);
64
65     bool willParent() const;
66     void setWillParent(bool p);
67
68 private:
69     void runTest(QDeclarativeContext *, uint);
70
71     QDeclarativeComponent *m_component;
72     static Timer *m_timer;
73
74     bool m_willparent;
75     QGraphicsScene m_scene;
76     QGraphicsRectItem m_item;
77 };
78 QML_DECLARE_TYPE(Timer);
79
80 Timer *Timer::m_timer = 0;
81
82 Timer::Timer()
83 : m_component(0), m_willparent(false)
84 {
85     if (m_timer)
86         qWarning("Timer: Timer already registered");
87     m_timer = this;
88
89     m_scene.setItemIndexMethod(QGraphicsScene::NoIndex);
90     m_scene.addItem(&m_item);
91 }
92
93 QDeclarativeComponent *Timer::component() const
94 {
95     return m_component;
96 }
97
98 void Timer::setComponent(QDeclarativeComponent *c)
99 {
100     m_component = c;
101 }
102
103 Timer *Timer::timerInstance()
104 {
105     return m_timer;
106 }
107
108 void Timer::run(uint iterations)
109 {
110     QDeclarativeContext context(qmlContext(this));
111
112     QObject *o = m_component->create(&context);
113     QGraphicsObject *go = qobject_cast<QGraphicsObject *>(o);
114     if (m_willparent && go) 
115         go->setParentItem(&m_item);
116     delete o;
117     
118     runTest(&context, iterations);
119 }
120
121 bool Timer::willParent() const
122 {
123     return m_willparent;
124 }
125
126 void Timer::setWillParent(bool p)
127 {
128     m_willparent = p;
129 }
130
131 void Timer::runTest(QDeclarativeContext *context, uint iterations)
132 {
133     QTime t;
134     t.start();
135     for (uint ii = 0; ii < iterations; ++ii) {
136         QObject *o = m_component->create(context);
137         QGraphicsObject *go = qobject_cast<QGraphicsObject *>(o);
138         if (m_willparent && go) 
139             go->setParentItem(&m_item);
140         delete o;
141     }
142
143     int e = t.elapsed();
144
145     qWarning() << "Total:" << e << "ms, Per iteration:" << qreal(e) / qreal(iterations) << "ms";
146
147 }
148
149 void usage(const char *name)
150 {
151     qWarning("Usage: %s [-iterations <count>] [-parent] <qml file>\n", name);
152
153     qWarning("qmltime is a tool for benchmarking the runtime cost of instantiating\n"
154     "a QML component. It is typically run as follows:\n"
155     "\n"
156     "%s path/to/benchmark.qml\n"
157     "\n"
158     "If the -parent option is specified, the component being measured will also\n"
159     "be parented to an item already in the scene.\n"
160     "\n"
161     "If the -iterations option is specified, the benchmark will run the specified\n"
162     "number of iterations. If -iterations is not specified, 1024 iterations\n"
163     "are performed.\n"
164     "\n"
165     "qmltime expects the file to be benchmarked to contain a certain structure.\n"
166     "Specifically, it requires the presence of a QmlTime.Timer element. For example,\n"
167     "say we wanted to benchmark the following list delegate:\n"
168     "\n"
169     "Rectangle {\n"
170     "    color: \"green\"\n"
171     "    width: 400; height: 100\n"
172     "    Text {\n"
173     "        anchors.centerIn: parent\n"
174     "        text: name\n"
175     "    }\n"
176     "}\n"
177     "\n"
178     "we would create a benchmark file that looks like this:\n"
179     "\n"
180     "import QtQuick 1.0\n"
181     "import QmlTime 1.0 as QmlTime\n"
182     "\n"
183     "Item {\n"
184     "\n"
185     "    property string name: \"Bob Smith\"\n"
186     "\n"
187     "    QmlTime.Timer {\n"
188     "        component: Rectangle {\n"
189     "            color: \"green\"\n"
190     "            width: 400; height: 100\n"
191     "            Text {\n"
192     "                anchors.centerIn: parent\n"
193     "                text: name\n"
194     "            }\n"
195     "        }\n"
196     "    }\n"
197     "}\n"
198     "\n"
199     "The outer Item functions as a dummy data provider for any additional\n"
200     "data required by the bindings in the component being benchmarked (in the\n"
201     "example above we provide a \"name\" property).\n"
202     "\n"
203     "When started, the component is instantiated once before running\n"
204     "the benchmark, which means that the reported time does not include\n"
205     "compile time (as the results of compilation are cached internally).\n"
206     "In this sense the times reported by qmltime best correspond to the\n"
207     "costs associated with delegate creation in the view classes, where the\n"
208     "same delegate is instantiated over and over. Conversely, it is not a\n"
209     "good approximation for e.g. Loader, which typically only instantiates\n"
210     "an element once (and so for Loader the compile time is very relevant\n"
211     "to the overall cost).", name);
212
213     exit(-1);
214 }
215
216 int main(int argc, char ** argv)
217 {
218     QApplication app(argc, argv);
219
220     qmlRegisterType<Timer>("QmlTime", 1, 0, "Timer");
221
222     uint iterations = 1024;
223     QString filename;
224     bool willParent = false;
225
226     for (int ii = 1; ii < argc; ++ii) {
227         QByteArray arg(argv[ii]);
228
229         if (arg == "-iterations") {
230             if (ii + 1 < argc) {
231                 ++ii;
232                 QByteArray its(argv[ii]);
233                 bool ok = false;
234                 iterations = its.toUInt(&ok);
235                 if (!ok)
236                     usage(argv[0]);
237             } else {
238                 usage(argv[0]);
239             }
240         } else if (arg == "-parent") {
241             willParent = true;
242         } else if (arg == "-help") {
243             usage(argv[0]);
244         } else {
245             filename = QLatin1String(argv[ii]);
246         }
247     }
248
249     if (filename.isEmpty())
250         usage(argv[0]);
251
252     QDeclarativeEngine engine;
253     QDeclarativeComponent component(&engine, filename);
254     if (component.isError()) {
255         qWarning() << component.errors();
256         return -1;
257     }
258
259     QObject *obj = component.create();
260     if (!obj) {
261         qWarning() << component.errors();
262         return -1;
263     }
264
265     Timer *timer = Timer::timerInstance();
266     if (!timer) {
267         qWarning() << "A Tester.Timer instance is required.";
268         return -1;
269     }
270
271     timer->setWillParent(willParent);
272
273     if (!timer->component()) {
274         qWarning() << "The timer has no component";
275         return -1;
276     }
277
278     timer->run(iterations);
279
280     return 0;
281 }
282
283 #include "qmltime.moc"