benchmark.cpp 2.52 KB
Newer Older
1 2 3 4 5 6 7
#include <QObject>
#include <benchmark.h>
#include <QtDebug>
#include <QElapsedTimer>
#include <QQmlApplicationEngine>
#include <QQuickWindow>
#include <QGuiApplication>
Daniel Smith's avatar
Daniel Smith committed
8 9 10
#include <iostream>
#include <fstream>
#include <unistd.h>
11 12 13 14 15 16 17

void Benchmark::startTimer(){

}

void Benchmark::countFrame(){
    qint64 frametime = timer.restart();
Daniel Smith's avatar
Daniel Smith committed
18 19
    if (frame % 10 == 0)
        emit measureMemory();
20 21 22 23 24 25 26
    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.
    }
27
    qDebug() << "frame " << frame << ": " << thisframe << " ms";
28 29 30 31 32 33
    frame+=1;
    if (frame >= maxframes){
        this->finished();
    }
}

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__
Daniel Smith's avatar
Daniel Smith committed
49 50 51 52 53 54 55 56
    int tSize = 0, resident = 0, share = 0;
    std::ifstream buffer("/proc/self/statm");
    buffer >> tSize >> resident >> share;
    buffer.close();

    long page_size_kb = sysconf(_SC_PAGE_SIZE) / 1024; // in case x86-64 is configured to use 2MB pages
    double rss = resident * page_size_kb;
    double shared_mem = share * page_size_kb;
57 58 59
#else
#   error "Unknown compiler"
#endif
Daniel Smith's avatar
Daniel Smith committed
60
    qDebug() << "Virtual Memory (KB): " << shared_mem << " RSS (KB): " << rss << " Private Bytes (KB): " << (rss - shared_mem);
61 62 63 64 65 66 67
}

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

int Benchmark::runBenchmark(QGuiApplication &app){
Daniel Smith's avatar
Daniel Smith committed
68 69 70 71
    measureMem();
    QObject::connect(this, &Benchmark::measureMemory,
       this, &Benchmark::measureMem);

72 73 74 75 76 77 78 79
        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);
Daniel Smith's avatar
Daniel Smith committed
80

81 82 83 84 85 86
                         //QCoreApplication::instance(), &QCoreApplication::quit);
        if (app.exec() != 0)
            return -1;
        //return timer.elapsed(); //change to print averages.
    //return runBenchmark(startup);
}