tests/benchmarks/qobject/main.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 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 #include <QtCore>
       
    42 #include <QtGui>
       
    43 #include <qtest.h>
       
    44 #include "object.h"
       
    45 #include <qcoreapplication.h>
       
    46 #include <qdatetime.h>
       
    47 
       
    48 enum {
       
    49     CreationDeletionBenckmarkConstant = 34567,
       
    50     SignalsAndSlotsBenchmarkConstant = 456789
       
    51 };
       
    52 
       
    53 class QObjectBenchmark : public QObject
       
    54 {
       
    55 Q_OBJECT
       
    56 private slots:
       
    57     void signal_slot_benchmark();
       
    58     void signal_slot_benchmark_data();
       
    59     void qproperty_benchmark_data();
       
    60     void qproperty_benchmark();
       
    61     void dynamic_property_benchmark();
       
    62     void connect_disconnect_benchmark_data();
       
    63     void connect_disconnect_benchmark();
       
    64 };
       
    65 
       
    66 void QObjectBenchmark::signal_slot_benchmark_data()
       
    67 {
       
    68     QTest::addColumn<int>("type");
       
    69     QTest::newRow("simple function") << 0;
       
    70     QTest::newRow("single signal/slot") << 1;
       
    71     QTest::newRow("multi signal/slot") << 2;
       
    72 }
       
    73 
       
    74 void QObjectBenchmark::signal_slot_benchmark()
       
    75 {
       
    76     QFETCH(int, type);
       
    77 
       
    78     Object singleObject;
       
    79     Object multiObject;
       
    80     singleObject.setObjectName("single");
       
    81     multiObject.setObjectName("multi");
       
    82 
       
    83     singleObject.connect(&singleObject, SIGNAL(signal0()), SLOT(slot0()));
       
    84 
       
    85     multiObject.connect(&multiObject, SIGNAL(signal0()), SLOT(slot0()));
       
    86     // multiObject.connect(&multiObject, SIGNAL(signal0()), SLOT(signal1()));
       
    87     multiObject.connect(&multiObject, SIGNAL(signal1()), SLOT(slot1()));
       
    88     // multiObject.connect(&multiObject, SIGNAL(signal0()), SLOT(signal2()));
       
    89     multiObject.connect(&multiObject, SIGNAL(signal2()), SLOT(slot2()));
       
    90     // multiObject.connect(&multiObject, SIGNAL(signal0()), SLOT(signal3()));
       
    91     multiObject.connect(&multiObject, SIGNAL(signal3()), SLOT(slot3()));
       
    92     // multiObject.connect(&multiObject, SIGNAL(signal0()), SLOT(signal4()));
       
    93     multiObject.connect(&multiObject, SIGNAL(signal4()), SLOT(slot4()));
       
    94     // multiObject.connect(&multiObject, SIGNAL(signal0()), SLOT(signal5()));
       
    95     multiObject.connect(&multiObject, SIGNAL(signal5()), SLOT(slot5()));
       
    96     // multiObject.connect(&multiObject, SIGNAL(signal0()), SLOT(signal6()));
       
    97     multiObject.connect(&multiObject, SIGNAL(signal6()), SLOT(slot6()));
       
    98     // multiObject.connect(&multiObject, SIGNAL(signal0()), SLOT(signal7()));
       
    99     multiObject.connect(&multiObject, SIGNAL(signal7()), SLOT(slot7()));
       
   100     // multiObject.connect(&multiObject, SIGNAL(signal0()), SLOT(signal8()));
       
   101     multiObject.connect(&multiObject, SIGNAL(signal8()), SLOT(slot8()));
       
   102     // multiObject.connect(&multiObject, SIGNAL(signal0()), SLOT(signal9()));
       
   103     multiObject.connect(&multiObject, SIGNAL(signal9()), SLOT(slot9()));
       
   104 
       
   105     if (type == 0) {
       
   106         QBENCHMARK {
       
   107             singleObject.slot0();
       
   108         }
       
   109     } else if (type == 1) {
       
   110         QBENCHMARK {
       
   111             singleObject.emitSignal0();
       
   112         }
       
   113     } else {
       
   114         QBENCHMARK {
       
   115             multiObject.emitSignal0();   
       
   116         }
       
   117     }
       
   118 }
       
   119 
       
   120 void QObjectBenchmark::qproperty_benchmark_data()
       
   121 {
       
   122     QTest::addColumn<QByteArray>("name");
       
   123     const QMetaObject *mo = &QTreeView::staticMetaObject;
       
   124     for (int i = 0; i < mo->propertyCount(); ++i) {
       
   125         QMetaProperty prop = mo->property(i);
       
   126         QTest::newRow(prop.name()) << QByteArray(prop.name());
       
   127     }
       
   128 }
       
   129 
       
   130 void QObjectBenchmark::qproperty_benchmark()
       
   131 {
       
   132     QFETCH(QByteArray, name);
       
   133     const char *p = name.constData();
       
   134     QTreeView obj;
       
   135     QVariant v = obj.property(p);
       
   136     QBENCHMARK {
       
   137         obj.setProperty(p, v);
       
   138         (void)obj.property(p);
       
   139     }
       
   140 }
       
   141 
       
   142 void QObjectBenchmark::dynamic_property_benchmark()
       
   143 {
       
   144     QTreeView obj;
       
   145     QBENCHMARK {
       
   146         obj.setProperty("myProperty", 123);
       
   147         (void)obj.property("myProperty");
       
   148         obj.setProperty("myOtherProperty", 123);
       
   149         (void)obj.property("myOtherProperty");
       
   150     }
       
   151 }
       
   152 
       
   153 void QObjectBenchmark::connect_disconnect_benchmark_data()
       
   154 {
       
   155     QTest::addColumn<QByteArray>("signal");
       
   156     const QMetaObject *mo = &QTreeView::staticMetaObject;
       
   157     for (int i = 0; i < mo->methodCount(); ++i) {
       
   158         QMetaMethod method = mo->method(i);
       
   159         if (method.methodType() != QMetaMethod::Signal)
       
   160             continue;
       
   161         QByteArray sig = method.signature();
       
   162         QTest::newRow(sig) << sig;
       
   163     }
       
   164 }
       
   165 
       
   166 void QObjectBenchmark::connect_disconnect_benchmark()
       
   167 {
       
   168     QFETCH(QByteArray, signal);
       
   169     signal.prepend('2');
       
   170     const char *p = signal.constData();
       
   171     QTreeView obj;
       
   172     QBENCHMARK {
       
   173         QObject::connect(&obj, p, &obj, p);
       
   174         QObject::disconnect(&obj, p, &obj, p);
       
   175     }
       
   176 }
       
   177 
       
   178 QTEST_MAIN(QObjectBenchmark)
       
   179 
       
   180 #include "main.moc"