]> git.saurik.com Git - wxWidgets.git/blob - tests/benchmarks/bench.cpp
updated Hindi translations
[wxWidgets.git] / tests / benchmarks / bench.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: tests/benchmarks/bench.cpp
3 // Purpose: Main file of the benchmarking suite
4 // Author: Vadim Zeitlin
5 // Created: 2008-07-19
6 // RCS-ID: $Id$
7 // Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows license
9 /////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 #include "wx/app.h"
20 #include "wx/cmdline.h"
21 #include "wx/stopwatch.h"
22
23 #include "bench.h"
24
25 // ----------------------------------------------------------------------------
26 // constants
27 // ----------------------------------------------------------------------------
28
29 static const char OPTION_LIST = 'l';
30
31 static const char OPTION_AVG_COUNT = 'a';
32 static const char OPTION_NUM_RUNS = 'n';
33
34 // ----------------------------------------------------------------------------
35 // BenchApp declaration
36 // ----------------------------------------------------------------------------
37
38 #if wxUSE_GUI
39 typedef wxApp BenchAppBase;
40 #else
41 typedef wxAppConsole BenchAppBase;
42 #endif
43
44 class BenchApp : public BenchAppBase
45 {
46 public:
47 BenchApp();
48
49 // standard overrides
50 virtual void OnInitCmdLine(wxCmdLineParser& parser);
51 virtual bool OnCmdLineParsed(wxCmdLineParser& parser);
52 virtual bool OnInit();
53 virtual int OnRun();
54 virtual int OnExit();
55
56 private:
57 // list all registered benchmarks
58 void ListBenchmarks();
59
60 // command lines options/parameters
61 wxSortedArrayString m_toRun;
62 long m_numRuns,
63 m_avgCount;
64 };
65
66 // ============================================================================
67 // Bench::Function implementation
68 // ============================================================================
69
70 Bench::Function *Bench::Function::ms_head = NULL;
71
72 // ============================================================================
73 // BenchApp implementation
74 // ============================================================================
75
76 IMPLEMENT_APP_CONSOLE(BenchApp)
77
78 BenchApp::BenchApp()
79 {
80 m_avgCount = 10;
81 m_numRuns = 10000; // just some default (TODO: switch to time-based one)
82 }
83
84 bool BenchApp::OnInit()
85 {
86 if ( !BenchAppBase::OnInit() )
87 return false;
88
89 wxPrintf("wxWidgets benchmarking program\n"
90 "Build: %s\n", WX_BUILD_OPTIONS_SIGNATURE);
91
92 #if wxUSE_GUI
93 // create a hidden parent window to be used as parent for the GUI controls
94 new wxFrame(NULL, wxID_ANY, "Hidden wx benchmark frame");
95 #endif // wxUSE_GUI
96
97 return true;
98 }
99
100 void BenchApp::OnInitCmdLine(wxCmdLineParser& parser)
101 {
102 BenchAppBase::OnInitCmdLine(parser);
103
104 parser.AddSwitch(OPTION_LIST,
105 "list",
106 "list all the existing benchmarks");
107
108 parser.AddOption(OPTION_AVG_COUNT,
109 "avg-count",
110 "number of times to run benchmarking loop",
111 wxCMD_LINE_VAL_NUMBER);
112 parser.AddOption(OPTION_NUM_RUNS,
113 "num-runs",
114 "number of times to run each benchmark in a loop",
115 wxCMD_LINE_VAL_NUMBER);
116 parser.AddParam("benchmark name",
117 wxCMD_LINE_VAL_STRING,
118 wxCMD_LINE_PARAM_OPTIONAL | wxCMD_LINE_PARAM_MULTIPLE);
119 }
120
121 bool BenchApp::OnCmdLineParsed(wxCmdLineParser& parser)
122 {
123 if ( parser.Found(OPTION_LIST) )
124 {
125 ListBenchmarks();
126
127 return false;
128 }
129
130 const size_t count = parser.GetParamCount();
131 if ( !count )
132 {
133 parser.Usage();
134
135 ListBenchmarks();
136
137 return false;
138 }
139
140 parser.Found(OPTION_AVG_COUNT, &m_avgCount);
141 parser.Found(OPTION_NUM_RUNS, &m_numRuns);
142
143 // construct sorted array for quick verification of benchmark names
144 wxSortedArrayString benchmarks;
145 for ( Bench::Function *func = Bench::Function::GetFirst();
146 func;
147 func = func->GetNext() )
148 {
149 benchmarks.push_back(func->GetName());
150 }
151
152 for ( size_t n = 0; n < count; n++ )
153 {
154 const wxString name = parser.GetParam(n);
155 if ( benchmarks.Index(name) == wxNOT_FOUND )
156 {
157 wxFprintf(stderr, "No benchmark named \"%s\".\n", name);
158 return false;
159 }
160
161 m_toRun.push_back(name);
162 }
163
164 return BenchAppBase::OnCmdLineParsed(parser);
165 }
166
167 int BenchApp::OnRun()
168 {
169 int rc = EXIT_SUCCESS;
170 for ( Bench::Function *func = Bench::Function::GetFirst();
171 func;
172 func = func->GetNext() )
173 {
174 if ( m_toRun.Index(func->GetName()) == wxNOT_FOUND )
175 continue;
176
177 wxPrintf("Benchmarking %s: ", func->GetName());
178
179 long timeMin = LONG_MAX,
180 timeMax = 0,
181 timeTotal = 0;
182 bool ok = true;
183 for ( long a = 0; a < m_avgCount; a++ )
184 {
185 wxStopWatch sw;
186 for ( long n = 0; n < m_numRuns && ok; n++ )
187 {
188 ok = func->Run();
189 }
190
191 sw.Pause();
192
193 if ( !ok )
194 break;
195
196 const long t = sw.Time();
197 if ( t < timeMin )
198 timeMin = t;
199 if ( t > timeMax )
200 timeMax = t;
201 timeTotal += t;
202 }
203
204 if ( !ok )
205 {
206 wxPrintf("ERROR\n");
207 rc = EXIT_FAILURE;
208 }
209 else
210 {
211 wxPrintf("%ldms total, ", timeTotal);
212
213 long times = m_avgCount;
214 if ( m_avgCount > 2 )
215 {
216 timeTotal -= timeMin + timeMax;
217 times -= 2;
218 }
219
220 wxPrintf("%.2f avg (min=%ld, max=%ld)\n",
221 (float)timeTotal / times, timeMin, timeMax);
222 }
223 }
224
225 return rc;
226 }
227
228 int BenchApp::OnExit()
229 {
230 #if wxUSE_GUI
231 delete GetTopWindow();
232 #endif // wxUSE_GUI
233
234 return 0;
235 }
236
237 /* static */
238 void BenchApp::ListBenchmarks()
239 {
240 wxPrintf("Available benchmarks:\n");
241 for ( Bench::Function *func = Bench::Function::GetFirst();
242 func;
243 func = func->GetNext() )
244 {
245 wxPrintf("\t%s\n", func->GetName());
246 }
247 }