Debian Codegraph @

tests/benchmarks/script/qscriptclass/tst_qscriptclass.cpp

raw symbols SCIP
1 /****************************************************************************
2 **
3 ** Copyright (C) 2018 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ** $QT_END_LICENSE$
26 **
27 ****************************************************************************/
28
29 #include <qtest.h>
30 #include <QtScript>
31
32 Q_DECLARE_METATYPE(QScriptContext*)
33 Q_DECLARE_METATYPE(QScriptValue)
34 Q_DECLARE_METATYPE(QScriptValueList)
35
36 // We want reliable numbers so we don't want to rely too much
37 // on the number of iterations done by testlib.
38 // this also make the results of valgrind more interesting
39 const int iterationNumber = 5000;
40
41 class tst_QScriptClass : public QObject
42 {
43 Q_OBJECT
44
45 private slots:
46 void noSuchProperty();
47 void property();
48 void setProperty();
49 void propertyFlags();
50 void call();
51 void hasInstance();
52 void iterate();
53 };
54
55 // Test the overhead of checking for an inexisting property of a QScriptClass
56 void tst_QScriptClass::noSuchProperty()
57 {
58 QScriptEngine eng;
59 QScriptClass cls(&eng);
60 QScriptValue obj = eng.newObject(&cls);
61 QString propertyName = QString::fromLatin1("foo");
62 QBENCHMARK {
63 for (int i = 0; i < iterationNumber; ++i)
64 (void)obj.property(propertyName);
65 }
66 Q_ASSERT(!obj.property(propertyName).isValid());
67 }
68
69
70 class FooScriptClass : public QScriptClass
71 {
72 public:
73 FooScriptClass(QScriptEngine *engine)
74 : QScriptClass(engine)
75 {
76 foo = engine->toStringHandle("foo");
77 }
78
79 QueryFlags queryProperty(const QScriptValue &,
80 const QScriptString &,
81 QueryFlags flags,
82 uint *id)
83 {
84 *id = 1;
85 return flags;
86 }
87
88 QScriptValue property(const QScriptValue &,
89 const QScriptString &,
90 uint)
91 {
92 return QScriptValue(engine(), 35);
93 }
94
95 void setProperty(QScriptValue &, const QScriptString &,
96 uint, const QScriptValue &)
97 {}
98
99 QScriptValue::PropertyFlags propertyFlags(const QScriptValue &, const QScriptString &, uint)
100 {
101 return QScriptValue::Undeletable;
102 }
103 private:
104 QScriptString foo;
105 };
106
107 // Test the overhead of getting a value of QScriptClass across the Javascript engine
108 void tst_QScriptClass::property()
109 {
110 QScriptEngine eng;
111 FooScriptClass cls(&eng);
112 QScriptValue obj = eng.newObject(&cls);
113 QScriptString foo = eng.toStringHandle("foo");
114 QBENCHMARK {
115 for (int i = 0; i < iterationNumber; ++i)
116 (void)obj.property(foo);
117 }
118 }
119
120 // Test the overhead of setting a value on QScriptClass across the Javascript engine
121 void tst_QScriptClass::setProperty()
122 {
123 QScriptEngine eng;
124 FooScriptClass cls(&eng);
125 QScriptValue obj = eng.newObject(&cls);
126 QScriptValue value(456);
127 QScriptString foo = eng.toStringHandle("foo");
128 QBENCHMARK {
129 for (int i = 0; i < iterationNumber; ++i)
130 obj.setProperty(foo, value);
131 }
132 }
133
134 // Test the time taken to get the propeties flags across the engine
135 void tst_QScriptClass::propertyFlags()
136 {
137 QScriptEngine eng;
138 FooScriptClass cls(&eng);
139 QScriptValue obj = eng.newObject(&cls);
140 QScriptString foo = eng.toStringHandle("foo");
141 QBENCHMARK {
142 for (int i = 0; i < iterationNumber; ++i)
143 (void)obj.propertyFlags(foo);
144 }
145 }
146
147
148
149 class ExtensionScriptClass : public QScriptClass
150 {
151 public:
152 ExtensionScriptClass(QScriptEngine *engine)
153 : QScriptClass(engine)
154 {
155 }
156
157 bool supportsExtension(Extension) const
158 {
159 return true;
160 }
161
162 QVariant extension(Extension, const QVariant &argument = QVariant())
163 {
164 Q_UNUSED(argument);
165 return QVariant();
166 }
167 };
168
169 // Check the overhead of the extension "call"
170 void tst_QScriptClass::call()
171 {
172 QScriptEngine eng;
173 ExtensionScriptClass cls(&eng);
174 QScriptValue obj = eng.newObject(&cls);
175 QScriptValue thisObject;
176 QScriptValueList args;
177 args.append(123);
178 QBENCHMARK {
179 for (int i = 0; i < iterationNumber; ++i)
180 (void)obj.call(thisObject, args);
181 }
182 }
183
184 // Check the overhead of the extension "instanceOf"
185 void tst_QScriptClass::hasInstance()
186 {
187 QScriptEngine eng;
188 ExtensionScriptClass cls(&eng);
189 QScriptValue obj = eng.newObject(&cls);
190 obj.setProperty("foo", 123);
191 QScriptValue plain = eng.newObject();
192 plain.setProperty("foo", obj.property("foo"));
193 QBENCHMARK {
194 for (int i = 0; i < iterationNumber; ++i)
195 (void)plain.instanceOf(obj);
196 }
197 }
198
199
200
201 static const int iteratorValuesNumber = 100;
202 class TestClassPropertyIterator : public QScriptClassPropertyIterator
203 {
204 public:
205 TestClassPropertyIterator(const QScriptValue &object, QVector<QScriptString> names)
206 : QScriptClassPropertyIterator(object)
207 , m_index(0)
208 , names(names)
209 {
210 }
211
212 bool hasNext() const
213 {
214 return m_index < iteratorValuesNumber - 1;
215 }
216 void next() { ++m_index; }
217
218 bool hasPrevious() const { return m_index > 0; }
219 void previous() { --m_index; }
220
221 void toFront() { m_index = 0; }
222 void toBack() { m_index = iteratorValuesNumber - 1; }
223
224 QScriptString name() const { return names[m_index]; }
225 uint id() const { return m_index; }
226 QScriptValue::PropertyFlags flags() const { return 0; }
227
228 private:
229 int m_index;
230 QVector<QScriptString> names;
231 };
232
233
234 class IteratorScriptClass : public QScriptClass
235 {
236 public:
237 IteratorScriptClass(QScriptEngine *engine)
238 : QScriptClass(engine)
239 {
240 for (int i = 0; i < iteratorValuesNumber; ++i)
241 names.append(engine->toStringHandle(QString("property%1").arg(i)));
242 }
243
244 QScriptClassPropertyIterator *newIterator(const QScriptValue &object)
245 {
246 return new TestClassPropertyIterator(object, names);
247 }
248 private:
249 QVector<QScriptString> names;
250 friend class TestClassPropertyIterator;
251 };
252
253 // Measure the performance of the interface to iterate over QScriptClassPropertyIterator
254 void tst_QScriptClass::iterate()
255 {
256 QScriptEngine eng;
257 IteratorScriptClass cls(&eng);
258 QScriptValue obj = eng.newObject(&cls);
259 int iterationNumberIterate = iterationNumber / iteratorValuesNumber;
260 QBENCHMARK {
261 for (int i = 0; i < iterationNumberIterate; ++i) {
262 QScriptValueIterator it(obj);
263 while (it.hasNext()) {
264 it.next();
265 (void)it.scriptName();
266 }
267 }
268 }
269 }
270
271 QTEST_MAIN(tst_QScriptClass)
272 #include "tst_qscriptclass.moc"