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