00001
00002 #include "performancetest.h"
00003 #include <qpe/qpeapplication.h>
00004 #include <qpushbutton.h>
00005 #include <qframe.h>
00006 #include <qlayout.h>
00007 #include <qpainter.h>
00008 #include <qdatetime.h>
00009 #include <qmultilineedit.h>
00010 #include <qbuttongroup.h>
00011 #include <qcheckbox.h>
00012 #include <stdlib.h>
00013
00014 class TestItem
00015 {
00016 public:
00017 TestItem( const QString &d, int it ) : msecs(0), desc(d), iter(it)
00018 {
00019 }
00020
00021 void begin()
00022 {
00023 timeKeeper.start();
00024 }
00025
00026 void end()
00027 {
00028 msecs = timeKeeper.elapsed();
00029 }
00030
00031 QString report() const
00032 {
00033 QString rpt( "%1 (%2): %3msecs" );
00034 return rpt.arg(desc).arg(iter).arg(msecs);
00035 }
00036
00037 int iterations() const { return iter; }
00038 int elapsed() const { return msecs; }
00039
00040 private:
00041 int msecs;
00042 QString desc;
00043 int iter;
00044 QTime timeKeeper;
00045 };
00046
00047 void fillRect( QWidget *w, TestItem *ti )
00048 {
00049 QPainter p( w );
00050 ti->begin();
00051 for ( int i = 0; i < ti->iterations(); i++ ) {
00052 p.fillRect( rand() % 40, rand() % 40, 200, 200, Qt::red );
00053 }
00054 ti->end();
00055 }
00056
00057 void bitBlt( QWidget *w, TestItem *ti )
00058 {
00059 QPixmap pm( 200, 200 );
00060 QPainter pmp( &pm );
00061 pmp.setBrush( Qt::white );
00062 pmp.drawRect( 0, 0, 200, 200 );
00063 pmp.drawLine( 0, 0, 200, 200 );
00064 pmp.drawLine( 0, 200, 200, 0 );
00065 pmp.end();
00066
00067 QPainter p( w );
00068 ti->begin();
00069 for ( int i = 0; i < ti->iterations(); i++ ) {
00070 p.drawPixmap( rand() % 100, rand() % 100, pm );
00071 }
00072 ti->end();
00073 }
00074
00075 void alphaBlt( QWidget *w, TestItem *ti )
00076 {
00077 QImage img( 200, 200, 32 );
00078 img.setAlphaBuffer( TRUE );
00079 for ( int y = 0; y < img.height(); y++ ) {
00080 for ( int x = 0; x < img.width(); x++ ) {
00081 QColor col;
00082 col.setHsv( 360*x/img.width(), 255, 255 );
00083 QRgb rgb = col.rgb();
00084 rgb &= 0x00ffffff;
00085 rgb |= (255*y/img.height()) << 24;
00086 img.setPixel( x, y, rgb );
00087 }
00088 }
00089
00090 QPixmap pm;
00091 pm.convertFromImage( img );
00092 QPainter p( w );
00093 ti->begin();
00094 for ( int i = 0; i < ti->iterations(); i++ ) {
00095 p.drawPixmap( rand() % 20, rand() % 20, pm );
00096 }
00097 ti->end();
00098 }
00099
00100 void drawText( QWidget *w, TestItem *ti )
00101 {
00102 QPainter p( w );
00103 p.setPen( Qt::white );
00104 QString text( "The quick red fox jumps over the lazy brown dog." );
00105 ti->begin();
00106 for ( int i = 0; i < ti->iterations(); i++ ) {
00107 p.drawText( rand() % 100, rand() % 300, text );
00108 }
00109 ti->end();
00110 }
00111
00112
00113 struct {
00114 int id;
00115 const char *name;
00116 int iterations;
00117 void (*testFunc)(QWidget *, TestItem *);
00118 }
00119 perfTests[] =
00120 {
00121 { 0, "Fill Rect", 1000, &fillRect },
00122 { 1, "Bit Blt", 1000, &bitBlt },
00123 { 2, "Alpha Blt", 100, &alphaBlt },
00124 { 3, "Draw Text", 5000, &drawText },
00125 {-1, "", 0, 0 }
00126 };
00127
00128 PerformanceTest::PerformanceTest( QWidget *parent, const char *name, WFlags f )
00129 : PerformanceTestBase( parent, name, f )
00130 {
00131 connect( testButton, SIGNAL(clicked()), this, SLOT(doTest()) );
00132 connect( optionGroup, SIGNAL(clicked(int)), this, SLOT(testClicked(int)) );
00133 QVBoxLayout *vb = new QVBoxLayout( testFrame );
00134 testWidget = new QWidget( testFrame );
00135 testWidget->setBackgroundColor( black );
00136 vb->addWidget( testWidget );
00137
00138 int count = 0;
00139 while ( perfTests[count].id >= 0 ) {
00140 QCheckBox *cb = new QCheckBox( perfTests[count].name, optionGroup );
00141 cb->setChecked( TRUE );
00142 optionGroupLayout->addWidget( cb );
00143 count++;
00144 }
00145 enabledTests.resize( count );
00146 enabledTests.fill( TRUE );
00147 }
00148
00149 void PerformanceTest::testClicked( int btn )
00150 {
00151 enabledTests[btn] = optionGroup->find( btn )->isOn();
00152 }
00153
00154 void PerformanceTest::doTest()
00155 {
00156 testButton->setEnabled( FALSE );
00157 qApp->processEvents();
00158 int totalTime = 0;
00159 int i = 0;
00160 while ( perfTests[i].id >= 0 ) {
00161 if ( enabledTests[i] ) {
00162 srand( 1 );
00163 testButton->setText( perfTests[i].name );
00164 qApp->processEvents();
00165 TestItem ti( perfTests[i].name, perfTests[i].iterations );
00166 (perfTests[i].testFunc)(testWidget, &ti);
00167 resultsEdit->append( ti.report() );
00168 totalTime += ti.elapsed();
00169 testWidget->erase();
00170 }
00171 i++;
00172 }
00173 resultsEdit->append( QString("-> Total time: %1ms\n").arg(totalTime) );
00174 testButton->setText( "Test" );
00175 testButton->setEnabled( TRUE );
00176 }