benchmark.cpp 1.87 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
#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.
    }
23
    qDebug() << "frame " << frame << ": " << thisframe << " ms";
24 25 26 27 28 29
    frame+=1;
    if (frame >= maxframes){
        this->finished();
    }
}

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
void Benchmark::measureMem(){
#ifdef _WIN32
   //define something for Windows (32-bit and 64-bit, this part is common)
#elif __APPLE__
    #include "TargetConditionals.h"
    #if TARGET_IPHONE_SIMULATOR
         // iOS Simulator
    #elif TARGET_OS_IPHONE
        // iOS device
    #elif TARGET_OS_MAC
        // Other kinds of Mac OS
    #else
    #   error "Unknown Apple platform"
    #endif
#elif __linux__
    // linux
#else
#   error "Unknown compiler"
#endif
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
}

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);
}