benchmark.cpp 1.49 KB
Newer Older
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
#include <QObject>
#include <benchmark.h>
#include <QtDebug>
#include <QString>
#include <QElapsedTimer>
#include <QQmlApplicationEngine>
#include <QQuickWindow>
#include <QGuiApplication>


void Benchmark::startTimer(){

}

void Benchmark::countFrame(){
    qint64 frametime = timer.restart();
    int thisframe = static_cast<int>(frametime);
    try{
        frametimes[frame] = thisframe;
    } catch (int e){
        printf("frame.");
        //No need to handle this. We probably just rendered extra frames before finished() completed.
    }
    printf("frame %d: %d ms\n", frame, thisframe);
    frame+=1;
    if (frame >= maxframes){
        this->finished();
    }
}

void Benchmark::measureFrame(qint64 value){
   printf("%d,", static_cast<int>(value));
}

void Benchmark::finished(){
    QCoreApplication::quit();
}

int Benchmark::runBenchmark(QGuiApplication &app){
        this->timer.start();
        QQmlApplicationEngine engine(QUrl("qrc:/main.qml"));
        if (engine.rootObjects().size() != 1)
            return -1;
        QQuickWindow *window = qobject_cast<QQuickWindow*>(engine.rootObjects().first());
        //Finish timing after the first frame is ready.
        QObject::connect(window, &QQuickWindow::frameSwapped,
           this, &Benchmark::countFrame);
                         //QCoreApplication::instance(), &QCoreApplication::quit);
        if (app.exec() != 0)
            return -1;
        //return timer.elapsed(); //change to print averages.
    //return runBenchmark(startup);
}