Diffstat (limited to 'development/performance/performancetest.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | development/performance/performancetest.cpp | 176 |
1 files changed, 176 insertions, 0 deletions
diff --git a/development/performance/performancetest.cpp b/development/performance/performancetest.cpp new file mode 100644 index 0000000..1bc8ee0 --- a/dev/null +++ b/development/performance/performancetest.cpp | |||
@@ -0,0 +1,176 @@ | |||
1 | |||
2 | #include "performancetest.h" | ||
3 | #include <qpe/qpeapplication.h> | ||
4 | #include <qpushbutton.h> | ||
5 | #include <qframe.h> | ||
6 | #include <qlayout.h> | ||
7 | #include <qpainter.h> | ||
8 | #include <qdatetime.h> | ||
9 | #include <qmultilineedit.h> | ||
10 | #include <qbuttongroup.h> | ||
11 | #include <qcheckbox.h> | ||
12 | #include <stdlib.h> | ||
13 | |||
14 | class TestItem | ||
15 | { | ||
16 | public: | ||
17 | TestItem( const QString &d, int it ) : msecs(0), desc(d), iter(it) | ||
18 | { | ||
19 | } | ||
20 | |||
21 | void begin() | ||
22 | { | ||
23 | timeKeeper.start(); | ||
24 | } | ||
25 | |||
26 | void end() | ||
27 | { | ||
28 | msecs = timeKeeper.elapsed(); | ||
29 | } | ||
30 | |||
31 | QString report() const | ||
32 | { | ||
33 | QString rpt( "%1 (%2): %3msecs" ); | ||
34 | return rpt.arg(desc).arg(iter).arg(msecs); | ||
35 | } | ||
36 | |||
37 | int iterations() const { return iter; } | ||
38 | int elapsed() const { return msecs; } | ||
39 | |||
40 | private: | ||
41 | int msecs; | ||
42 | QString desc; | ||
43 | int iter; | ||
44 | QTime timeKeeper; | ||
45 | }; | ||
46 | |||
47 | void fillRect( QWidget *w, TestItem *ti ) | ||
48 | { | ||
49 | QPainter p( w ); | ||
50 | ti->begin(); | ||
51 | for ( int i = 0; i < ti->iterations(); i++ ) { | ||
52 | p.fillRect( rand() % 40, rand() % 40, 200, 200, Qt::red ); | ||
53 | } | ||
54 | ti->end(); | ||
55 | } | ||
56 | |||
57 | void bitBlt( QWidget *w, TestItem *ti ) | ||
58 | { | ||
59 | QPixmap pm( 200, 200 ); | ||
60 | QPainter pmp( &pm ); | ||
61 | pmp.setBrush( Qt::white ); | ||
62 | pmp.drawRect( 0, 0, 200, 200 ); | ||
63 | pmp.drawLine( 0, 0, 200, 200 ); | ||
64 | pmp.drawLine( 0, 200, 200, 0 ); | ||
65 | pmp.end(); | ||
66 | |||
67 | QPainter p( w ); | ||
68 | ti->begin(); | ||
69 | for ( int i = 0; i < ti->iterations(); i++ ) { | ||
70 | p.drawPixmap( rand() % 100, rand() % 100, pm ); | ||
71 | } | ||
72 | ti->end(); | ||
73 | } | ||
74 | |||
75 | void alphaBlt( QWidget *w, TestItem *ti ) | ||
76 | { | ||
77 | QImage img( 200, 200, 32 ); | ||
78 | img.setAlphaBuffer( TRUE ); | ||
79 | for ( int y = 0; y < img.height(); y++ ) { | ||
80 | for ( int x = 0; x < img.width(); x++ ) { | ||
81 | QColor col; | ||
82 | col.setHsv( 360*x/img.width(), 255, 255 ); | ||
83 | QRgb rgb = col.rgb(); | ||
84 | rgb &= 0x00ffffff; | ||
85 | rgb |= (255*y/img.height()) << 24; | ||
86 | img.setPixel( x, y, rgb ); | ||
87 | } | ||
88 | } | ||
89 | |||
90 | QPixmap pm; | ||
91 | pm.convertFromImage( img ); | ||
92 | QPainter p( w ); | ||
93 | ti->begin(); | ||
94 | for ( int i = 0; i < ti->iterations(); i++ ) { | ||
95 | p.drawPixmap( rand() % 20, rand() % 20, pm ); | ||
96 | } | ||
97 | ti->end(); | ||
98 | } | ||
99 | |||
100 | void drawText( QWidget *w, TestItem *ti ) | ||
101 | { | ||
102 | QPainter p( w ); | ||
103 | p.setPen( Qt::white ); | ||
104 | QString text( "The quick red fox jumps over the lazy brown dog." ); // No tr | ||
105 | ti->begin(); | ||
106 | for ( int i = 0; i < ti->iterations(); i++ ) { | ||
107 | p.drawText( rand() % 100, rand() % 300, text ); | ||
108 | } | ||
109 | ti->end(); | ||
110 | } | ||
111 | |||
112 | |||
113 | struct { | ||
114 | int id; | ||
115 | const char *name; | ||
116 | int iterations; | ||
117 | void (*testFunc)(QWidget *, TestItem *); | ||
118 | } | ||
119 | perfTests[] = | ||
120 | { | ||
121 | { 0, "Fill Rect", 1000, &fillRect }, // No tr | ||
122 | { 1, "Bit Blt", 1000, &bitBlt }, // No tr | ||
123 | { 2, "Alpha Blt", 100, &alphaBlt }, // No tr | ||
124 | { 3, "Draw Text", 5000, &drawText }, // No tr | ||
125 | {-1, "", 0, 0 } | ||
126 | }; | ||
127 | |||
128 | PerformanceTest::PerformanceTest( QWidget *parent, const char *name, WFlags f ) | ||
129 | : PerformanceTestBase( parent, name, f ) | ||
130 | { | ||
131 | connect( testButton, SIGNAL(clicked()), this, SLOT(doTest()) ); | ||
132 | connect( optionGroup, SIGNAL(clicked(int)), this, SLOT(testClicked(int)) ); | ||
133 | QVBoxLayout *vb = new QVBoxLayout( testFrame ); | ||
134 | testWidget = new QWidget( testFrame ); | ||
135 | testWidget->setBackgroundColor( black ); | ||
136 | vb->addWidget( testWidget ); | ||
137 | |||
138 | int count = 0; | ||
139 | while ( perfTests[count].id >= 0 ) { | ||
140 | QCheckBox *cb = new QCheckBox( perfTests[count].name, optionGroup ); | ||
141 | cb->setChecked( TRUE ); | ||
142 | optionGroupLayout->addWidget( cb ); | ||
143 | count++; | ||
144 | } | ||
145 | enabledTests.resize( count ); | ||
146 | enabledTests.fill( TRUE ); | ||
147 | } | ||
148 | |||
149 | void PerformanceTest::testClicked( int btn ) | ||
150 | { | ||
151 | enabledTests[btn] = optionGroup->find( btn )->isOn(); | ||
152 | } | ||
153 | |||
154 | void PerformanceTest::doTest() | ||
155 | { | ||
156 | testButton->setEnabled( FALSE ); | ||
157 | qApp->processEvents(); | ||
158 | int totalTime = 0; | ||
159 | int i = 0; | ||
160 | while ( perfTests[i].id >= 0 ) { | ||
161 | if ( enabledTests[i] ) { | ||
162 | srand( 1 ); | ||
163 | testButton->setText( perfTests[i].name ); | ||
164 | qApp->processEvents(); | ||
165 | TestItem ti( perfTests[i].name, perfTests[i].iterations ); | ||
166 | (perfTests[i].testFunc)(testWidget, &ti); | ||
167 | resultsEdit->append( ti.report() ); | ||
168 | totalTime += ti.elapsed(); | ||
169 | testWidget->erase(); | ||
170 | } | ||
171 | i++; | ||
172 | } | ||
173 | resultsEdit->append( QString("-> Total time: %1ms\n").arg(totalTime) ); // No tr | ||
174 | testButton->setText( "Test" ); // No tr | ||
175 | testButton->setEnabled( TRUE ); | ||
176 | } | ||