No real changes, just flush output in the graphics benchmark.
[wxWidgets.git] / tests / benchmarks / graphics.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: graphics.cpp
3 // Purpose: Some benchmarks for measuring graphics operations performance
4 // Author: Vadim Zeitlin
5 // Created: 2008-04-13
6 // RCS-ID: $Id$
7 // Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #include "wx/app.h"
12 #include "wx/frame.h"
13 #include "wx/cmdline.h"
14 #include "wx/dcclient.h"
15 #include "wx/dcmemory.h"
16 #include "wx/dcgraph.h"
17 #include "wx/stopwatch.h"
18 #include "wx/crt.h"
19
20 struct GraphicsBenchmarkOptions
21 {
22 GraphicsBenchmarkOptions()
23 {
24 mapMode = 0;
25 penWidth = 0;
26
27 width = 800;
28 height = 600;
29
30 numLines = 10000;
31
32 testBitmaps =
33 testLines =
34 testRectangles = false;
35 }
36
37 long mapMode,
38 penWidth,
39 width,
40 height,
41 numLines;
42
43 bool testBitmaps,
44 testLines,
45 testRectangles;
46 } opts;
47
48 class GraphicsBenchmarkFrame : public wxFrame
49 {
50 public:
51 GraphicsBenchmarkFrame()
52 : wxFrame(NULL, wxID_ANY, "wxWidgets Graphics Benchmark")
53 {
54 Connect(wxEVT_PAINT,
55 wxPaintEventHandler(GraphicsBenchmarkFrame::OnPaint));
56
57 m_bitmap.Create(64, 64, 32);
58
59 Show();
60 SetClientSize(opts.width, opts.height);
61 }
62
63 private:
64 void OnPaint(wxPaintEvent& WXUNUSED(event))
65 {
66 {
67 wxPaintDC dc(this);
68 wxGCDC gcdc(dc);
69 BenchmarkDCAndGC("paint", dc, gcdc);
70 }
71
72 {
73 wxClientDC dc(this);
74 wxGCDC gcdc(dc);
75 BenchmarkDCAndGC("client", dc, gcdc);
76 }
77
78 {
79 wxBitmap bmp(opts.width, opts.height);
80 wxMemoryDC dc(bmp);
81 wxGCDC gcdc(dc);
82 BenchmarkDCAndGC("memory", dc, gcdc);
83 }
84
85 wxTheApp->ExitMainLoop();
86 }
87
88 void BenchmarkDCAndGC(const char* dckind, wxDC& dc, wxGCDC& gcdc)
89 {
90 BenchmarkAll(wxString::Format("%6s DC", dckind), dc);
91 BenchmarkAll(wxString::Format("%6s GC", dckind), gcdc);
92 }
93
94 void BenchmarkAll(const wxString& msg, wxDC& dc)
95 {
96 BenchmarkLines(msg, dc);
97 BenchmarkRectangles(msg, dc);
98 BenchmarkBitmaps(msg, dc);
99 }
100
101 void BenchmarkLines(const wxString& msg, wxDC& dc)
102 {
103 if ( !opts.testLines )
104 return;
105
106 if ( opts.mapMode != 0 )
107 dc.SetMapMode((wxMappingMode)opts.mapMode);
108 if ( opts.penWidth != 0 )
109 dc.SetPen(wxPen(*wxWHITE, opts.penWidth));
110
111 wxPrintf("Benchmarking %s: ", msg);
112 fflush(stdout);
113
114 wxStopWatch sw;
115 int x = 0,
116 y = 0;
117 for ( int n = 0; n < opts.numLines; n++ )
118 {
119 int x1 = rand() % opts.width,
120 y1 = rand() % opts.height;
121
122 dc.DrawLine(x, y, x1, y1);
123
124 x = x1;
125 y = y1;
126 }
127
128 const long t = sw.Time();
129
130 wxPrintf("%ld lines done in %ldms = %gus/line\n",
131 opts.numLines, t, (1000. * t)/opts.numLines);
132 }
133
134
135 void BenchmarkRectangles(const wxString& msg, wxDC& dc)
136 {
137 if ( !opts.testRectangles )
138 return;
139
140 if ( opts.mapMode != 0 )
141 dc.SetMapMode((wxMappingMode)opts.mapMode);
142 if ( opts.penWidth != 0 )
143 dc.SetPen(wxPen(*wxWHITE, opts.penWidth));
144
145 dc.SetBrush( *wxRED_BRUSH );
146
147 wxPrintf("Benchmarking %s: ", msg);
148 fflush(stdout);
149
150 wxStopWatch sw;
151 for ( int n = 0; n < opts.numLines; n++ )
152 {
153 int x = rand() % opts.width,
154 y = rand() % opts.height;
155
156 dc.DrawRectangle(x, y, 32, 32);
157 }
158
159 const long t = sw.Time();
160
161 wxPrintf("%ld rects done in %ldms = %gus/rect\n",
162 opts.numLines, t, (1000. * t)/opts.numLines);
163 }
164
165 void BenchmarkBitmaps(const wxString& msg, wxDC& dc)
166 {
167 if ( !opts.testBitmaps )
168 return;
169
170 if ( opts.mapMode != 0 )
171 dc.SetMapMode((wxMappingMode)opts.mapMode);
172 if ( opts.penWidth != 0 )
173 dc.SetPen(wxPen(*wxWHITE, opts.penWidth));
174
175 wxPrintf("Benchmarking %s: ", msg);
176 fflush(stdout);
177
178 wxStopWatch sw;
179 for ( int n = 0; n < opts.numLines; n++ )
180 {
181 int x = rand() % opts.width,
182 y = rand() % opts.height;
183
184 dc.DrawBitmap(m_bitmap, x, y, true);
185 }
186
187 const long t = sw.Time();
188
189 wxPrintf("%ld bitmaps done in %ldms = %gus/bitmap\n",
190 opts.numLines, t, (1000. * t)/opts.numLines);
191 }
192
193
194 wxBitmap m_bitmap;
195 };
196
197 class GraphicsBenchmarkApp : public wxApp
198 {
199 public:
200 virtual void OnInitCmdLine(wxCmdLineParser& parser)
201 {
202 static const wxCmdLineEntryDesc desc[] =
203 {
204 { wxCMD_LINE_SWITCH, "", "bitmaps" },
205 { wxCMD_LINE_SWITCH, "", "lines" },
206 { wxCMD_LINE_SWITCH, "", "rectangles" },
207 { wxCMD_LINE_OPTION, "m", "map-mode", "", wxCMD_LINE_VAL_NUMBER },
208 { wxCMD_LINE_OPTION, "p", "pen-width", "", wxCMD_LINE_VAL_NUMBER },
209 { wxCMD_LINE_OPTION, "w", "width", "", wxCMD_LINE_VAL_NUMBER },
210 { wxCMD_LINE_OPTION, "h", "height", "", wxCMD_LINE_VAL_NUMBER },
211 { wxCMD_LINE_OPTION, "L", "lines", "", wxCMD_LINE_VAL_NUMBER },
212 { wxCMD_LINE_NONE },
213 };
214
215 parser.SetDesc(desc);
216 }
217
218 virtual bool OnCmdLineParsed(wxCmdLineParser& parser)
219 {
220 if ( parser.Found("m", &opts.mapMode) &&
221 (opts.mapMode < 1 || opts.mapMode > wxMM_METRIC) )
222 return false;
223 if ( parser.Found("p", &opts.penWidth) && opts.penWidth < 1 )
224 return false;
225 if ( parser.Found("w", &opts.width) && opts.width < 1 )
226 return false;
227 if ( parser.Found("h", &opts.height) && opts.height < 1 )
228 return false;
229 if ( parser.Found("L", &opts.numLines) && opts.numLines < 1 )
230 return false;
231
232 opts.testBitmaps = parser.Found("bitmaps");
233 opts.testLines = parser.Found("lines");
234 opts.testRectangles = parser.Found("rectangles");
235 if ( !(opts.testBitmaps || opts.testLines || opts.testRectangles) )
236 {
237 // Do everything by default.
238 opts.testBitmaps =
239 opts.testLines =
240 opts.testRectangles = true;
241 }
242
243 return true;
244 }
245
246 virtual bool OnInit()
247 {
248 if ( !wxApp::OnInit() )
249 return false;
250
251 new GraphicsBenchmarkFrame;
252
253 return true;
254 }
255 };
256
257 IMPLEMENT_APP_CONSOLE(GraphicsBenchmarkApp)