Update to qgis_bench: use renderer job, allow parallel mode, print other time

This commit is contained in:
Martin Dobias 2013-12-10 17:31:17 +07:00
parent e814e8df92
commit 151bad59cd
3 changed files with 78 additions and 29 deletions

View File

@ -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"
@ -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
@ -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}
};
@ -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
@ -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() ) );
@ -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
/////////////////////////////////////////////////////////////////////
@ -551,7 +575,7 @@ int main( int argc, char *argv[] )
qbench->saveLog( myLogFileName );
}
qbench->printLog();
qbench->printLog( myPrintTime );
delete qbench;
delete myApp;

View File

@ -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
@ -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 & ) ) );
}
@ -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
{
@ -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
@ -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() );
mMapSettings.setFlag( QgsMapSettings::DrawLabeling );
mImage = new QImage( mWidth, mHeight, QImage::Format_ARGB32_Premultiplied );
mImage->fill( 0 );
mMapSettings.setOutputSize( QSize( mWidth, mHeight ) );
mMapRenderer->setOutputSize( QSize( mWidth, mHeight ), mImage->logicalDpiX() );
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 );
@ -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] );
@ -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;
}

View File

@ -27,7 +27,7 @@
#include <QVariant>
#include <QVector>
#include "qgsmaprenderer.h"
#include "qgsmapsettings.h"
class QgsBench : public QObject
{
@ -44,7 +44,7 @@ class QgsBench : public QObject
void render();
void printLog();
void printLog( const QString& printTime );
bool openProject( const QString & fileName );
@ -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 );
@ -87,9 +89,11 @@ class QgsBench : public QObject
// user, sys, total times
QVector<double*> mTimes;
QImage* mImage;
QImage mImage;
QgsMapRenderer *mMapRenderer;
QgsMapSettings mMapSettings;
bool mParallel;
};
#endif // QGSBENCH_H