Skip to content

Commit

Permalink
Update to qgis_bench: use renderer job, allow parallel mode, print ot…
Browse files Browse the repository at this point in the history
…her time
  • Loading branch information
wonder-sk committed Dec 10, 2013
1 parent e814e8d commit 151bad5
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 29 deletions.
26 changes: 25 additions & 1 deletion tests/bench/main.cpp
Expand Up @@ -81,6 +81,8 @@ void usage( std::string const & appName )
<< "\t[--configpath path]\tuse the given path for all user configuration\n"
<< "\t[--prefix path]\tpath to a different build of qgis, may be used to test old versions\n"
<< "\t[--quality]\trenderer hint(s), comma separated, possible values: Antialiasing,TextAntialiasing,SmoothPixmapTransform,NonCosmeticDefaultPen\n"
<< "\t[--parallel]\trender layers in parallel instead of sequentially\n"
<< "\t[--print type]\twhat kind of time to print, possible values: wall,total,user,sys. Default is total.\n"
<< "\t[--help]\t\tthis text\n\n"
<< " FILES:\n"
<< " Files specified on the command line can include rasters,\n"
Expand Down Expand Up @@ -135,6 +137,8 @@ int main( int argc, char *argv[] )
int mySnapshotWidth = 800;
int mySnapshotHeight = 600;
QString myQuality = "";
bool myParallel = false;
QString myPrintTime = "total";

// This behaviour will set initial extent of map canvas, but only if
// there are no command line arguments. This gives a usable map
Expand Down Expand Up @@ -173,6 +177,8 @@ int main( int argc, char *argv[] )
{"configpath", required_argument, 0, 'c'},
{"prefix", required_argument, 0, 'r'},
{"quality", required_argument, 0, 'q'},
{"parallel", no_argument, 0, 'P'},
{"print", required_argument, 0, 'R'},
{0, 0, 0, 0}
};

Expand Down Expand Up @@ -242,6 +248,14 @@ int main( int argc, char *argv[] )
myQuality = optarg;
break;

case 'P':
myParallel = true;
break;

case 'R':
myPrintTime = optarg;
break;

case '?':
usage( argv[0] );
return 2; // XXX need standard exit codes
Expand Down Expand Up @@ -322,6 +336,14 @@ int main( int argc, char *argv[] )
{
myQuality = argv[++i];
}
else if ( arg == "--parallel" || arg == "-P" )
{
myParallel = true;
}
else if ( i + 1 < argc && ( arg == "--print" || arg == "-R" ) )
{
myPrintTime = argv[++i];
}
else
{
myFileList.append( QDir::convertSeparators( QFileInfo( QFile::decodeName( argv[i] ) ).absoluteFilePath() ) );
Expand Down Expand Up @@ -476,6 +498,8 @@ int main( int argc, char *argv[] )
qbench->setRenderHints( hints );
}

qbench->setParallel( myParallel );

/////////////////////////////////////////////////////////////////////
// autoload any file names that were passed in on the command line
/////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -551,7 +575,7 @@ int main( int argc, char *argv[] )
qbench->saveLog( myLogFileName );
}

qbench->printLog();
qbench->printLog( myPrintTime );

delete qbench;
delete myApp;
Expand Down
69 changes: 45 additions & 24 deletions tests/bench/qgsbench.cpp
Expand Up @@ -40,8 +40,10 @@
#include "qgsbench.h"
#include "qgslogger.h"
#include "qgsmaplayerregistry.h"
#include "qgsmaprendererjob.h"
#include "qgsproject.h"
#include "qgspallabeling.h"

const char *pre[] = { "user", "sys", "total", "wall" };

#ifdef Q_OS_WIN
// slightly adapted from http://anoncvs.postgresql.org/cvsweb.cgi/pgsql/src/port/getrusage.c?rev=1.18;content-type=text%2Fplain
Expand Down Expand Up @@ -117,14 +119,17 @@ int getrusage( int who, struct rusage * rusage )
#endif

QgsBench::QgsBench( int theWidth, int theHeight, int theIterations )
: QObject(), mWidth( theWidth ), mHeight( theHeight ), mIterations( theIterations ), mSetExtent( false )
: QObject()
, mWidth( theWidth )
, mHeight( theHeight )
, mIterations( theIterations )
, mSetExtent( false )
, mParallel( false )
{
QgsDebugMsg( "entered" );

QgsDebugMsg( QString( "mIterations = %1" ).arg( mIterations ) );

mMapRenderer = new QgsMapRenderer;

connect( QgsProject::instance(), SIGNAL( readProject( const QDomDocument & ) ),
this, SLOT( readProject( const QDomDocument & ) ) );
}
Expand Down Expand Up @@ -153,7 +158,7 @@ void QgsBench::readProject( const QDomDocument &doc )
if ( nodes.count() )
{
QDomNode node = nodes.item( 0 );
mMapRenderer->readXML( node );
mMapSettings.readXML( node );
}
else
{
Expand All @@ -171,17 +176,17 @@ void QgsBench::render()
{
QgsDebugMsg( "entered" );

QgsDebugMsg( "extent: " + mMapRenderer->extent().toString() );
QgsDebugMsg( "extent: " + mMapSettings.extent().toString() );

QMap<QString, QgsMapLayer*> layersMap = QgsMapLayerRegistry::instance()->mapLayers();

QStringList layers( layersMap.keys() );

mMapRenderer->setLayerSet( layers );
mMapSettings.setLayers( layers );

if ( mSetExtent )
{
mMapRenderer->setExtent( mExtent );
mMapSettings.setExtent( mExtent );
}

// Maybe in future
Expand All @@ -190,27 +195,34 @@ void QgsBench::render()
//mMapRenderer->setDestinationCrs( outputCRS );

// TODO: this should be probably set according to project
mMapRenderer->setProjectionsEnabled( true );
mMapSettings.setCrsTransformEnabled( true );

// Enable labeling
mMapRenderer->setLabelingEngine( new QgsPalLabeling() );

mImage = new QImage( mWidth, mHeight, QImage::Format_ARGB32_Premultiplied );
mImage->fill( 0 );
mMapSettings.setFlag( QgsMapSettings::DrawLabeling );

mMapRenderer->setOutputSize( QSize( mWidth, mHeight ), mImage->logicalDpiX() );
mMapSettings.setOutputSize( QSize( mWidth, mHeight ) );

QPainter painter( mImage );

painter.setRenderHints( mRendererHints );
// TODO: do we need the other QPainter flags?
mMapSettings.setFlag( QgsMapSettings::Antialiasing, mRendererHints.testFlag( QPainter::Antialiasing ) );

for ( int i = 0; i < mIterations; i++ )
{
QgsMapRendererQImageJob* job;
if ( mParallel )
job = new QgsMapRendererParallelJob( mMapSettings );
else
job = new QgsMapRendererSequentialJob( mMapSettings );

start();
mMapRenderer->render( &painter );
job->start();
job->waitForFinished();
elapsed();

mImage = job->renderedImage();
delete job;
}


mLogMap.insert( "iterations", mTimes.size() );
mLogMap.insert( "revision", QGSVERSION );

Expand Down Expand Up @@ -247,8 +259,6 @@ void QgsBench::render()
stdev[t] = sqrt( stdev[t] / mTimes.size() );
}

const char *pre[] = { "user", "sys", "total", "wall" };

QMap<QString, QVariant> map;

map.insert( "min", min[t] );
Expand All @@ -265,19 +275,30 @@ void QgsBench::render()
void QgsBench::saveSnapsot( const QString & fileName )
{
// If format is 0, QImage will attempt to guess the format by looking at fileName's suffix.
mImage->save( fileName );
mImage.save( fileName );
}

void QgsBench::printLog()
void QgsBench::printLog( const QString& printTime )
{
std::cout << "iterations: " << mLogMap["iterations"].toString().toAscii().constData() << std::endl;

bool validPrintTime = false;
for ( int x = 0; x < 4; ++x )
if ( printTime == pre[x] )
validPrintTime = true;

if ( !validPrintTime )
{
std::cout << "invalid --print option: " << printTime.toAscii().data() << std::endl;
return;
}

QMap<QString, QVariant> timesMap = mLogMap["times"].toMap();
QMap<QString, QVariant> totalMap = timesMap["total"].toMap();
QMap<QString, QVariant> totalMap = timesMap[printTime].toMap();
QMap<QString, QVariant>::iterator i = totalMap.begin();
while ( i != totalMap.end() )
{
QString s = "total_" + i.key() + ": " + i.value().toString();
QString s = printTime + "_" + i.key() + ": " + i.value().toString();
std::cout << s.toAscii().constData() << std::endl;
++i;
}
Expand Down
12 changes: 8 additions & 4 deletions tests/bench/qgsbench.h
Expand Up @@ -27,7 +27,7 @@
#include <QVariant>
#include <QVector>

#include "qgsmaprenderer.h"
#include "qgsmapsettings.h"

class QgsBench : public QObject
{
Expand All @@ -44,7 +44,7 @@ class QgsBench : public QObject

void render();

void printLog();
void printLog( const QString& printTime );

bool openProject( const QString & fileName );

Expand All @@ -58,6 +58,8 @@ class QgsBench : public QObject

void setRenderHints( QPainter::RenderHints hints ) { mRendererHints = hints; }

void setParallel( bool enabled ) { mParallel = enabled; }

public slots:
void readProject( const QDomDocument &doc );

Expand Down Expand Up @@ -87,9 +89,11 @@ class QgsBench : public QObject
// user, sys, total times
QVector<double*> mTimes;

QImage* mImage;
QImage mImage;

QgsMapSettings mMapSettings;

QgsMapRenderer *mMapRenderer;
bool mParallel;
};

#endif // QGSBENCH_H
Expand Down

0 comments on commit 151bad5

Please sign in to comment.