summaryrefslogtreecommitdiffstats
path: root/tests/benchmarks/declarative/qmltime/qmltime.cpp
blob: 918c197ec721890caa17a8ed7dcd4847bd1e462a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <QmlEngine>
#include <QmlComponent>
#include <QDebug>
#include <QApplication>
#include <QTime>
#include <QmlContext>

class Timer : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QmlComponent *component READ component WRITE setComponent);

public:
    Timer();

    QmlComponent *component() const;
    void setComponent(QmlComponent *);

    static Timer *timerInstance();

    void run(uint);

private:
    QmlComponent *m_component;
    static Timer *m_timer;
};
QML_DECLARE_TYPE(Timer);
QML_DEFINE_TYPE(QmlTime, 1, 0, Timer, Timer);

Timer *Timer::m_timer = 0;

Timer::Timer()
: m_component(0)
{
    if (m_timer)
        qWarning("Timer: Timer already registered");
    m_timer = this;
}

QmlComponent *Timer::component() const
{
    return m_component;
}

void Timer::setComponent(QmlComponent *c)
{
    m_component = c;
}

Timer *Timer::timerInstance()
{
    return m_timer;
}

void Timer::run(uint iterations)
{
    QmlContext context(qmlContext(this));

    QTime t;
    t.start();
    for (uint ii = 0; ii < iterations; ++ii) {
        QObject *o = m_component->create(&context);
        delete o;
    }

    int e = t.elapsed();

    qWarning() << "Total:" << e << "ms, Per iteration:" << qreal(e) / qreal(iterations) << "ms";

}

void usage(const char *name)
{
    qWarning("Usage: %s [-iterations <count>] <qml file>", name);
    exit(-1);
}

int main(int argc, char ** argv)
{
    QApplication app(argc, argv);

    uint iterations = 1024;
    QString filename;

    for (int ii = 1; ii < argc; ++ii) {
        QByteArray arg(argv[ii]);

        if (arg == "-iterations") {
            if (ii + 1 < argc) {
                ++ii;
                QByteArray its(argv[ii]);
                bool ok = false;
                iterations = its.toUInt(&ok);
                if (!ok)
                    usage(argv[0]);
            } else {
                usage(argv[0]);
            }
        } else {
            filename = QLatin1String(argv[ii]);
        }
    }

    QmlEngine engine;
    QmlComponent component(&engine, filename);
    if (component.isError()) {
        qWarning() << component.errors();
        return -1;
    }

    QObject *obj = component.create();
    if (!obj) {
        qWarning() << component.errors();
        return -1;
    }

    Timer *timer = Timer::timerInstance();
    if (!timer) {
        qWarning() << "A Tester.Timer instance is required.";
        return -1;
    }

    if (!timer->component()) {
        qWarning() << "The timer has no component";
        return -1;
    }

    timer->run(iterations);

    return 0;
}

#include "tst_delegate.moc"