1 /////////////////////////////////////////////////////////////////////////////
2 // Name: tests/benchmarks/bench.cpp
3 // Purpose: Main file of the benchmarking suite
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows license
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
20 #include "wx/cmdline.h"
21 #include "wx/stopwatch.h"
25 // ----------------------------------------------------------------------------
27 // ----------------------------------------------------------------------------
29 static const char OPTION_LIST
= 'l';
31 static const char OPTION_AVG_COUNT
= 'a';
32 static const char OPTION_NUM_RUNS
= 'n';
33 static const char OPTION_NUMERIC_PARAM
= 'p';
35 // ----------------------------------------------------------------------------
36 // BenchApp declaration
37 // ----------------------------------------------------------------------------
40 typedef wxApp BenchAppBase
;
42 typedef wxAppConsole BenchAppBase
;
45 class BenchApp
: public BenchAppBase
51 virtual void OnInitCmdLine(wxCmdLineParser
& parser
);
52 virtual bool OnCmdLineParsed(wxCmdLineParser
& parser
);
53 virtual bool OnInit();
58 int GetNumericParameter() const { return m_numParam
; }
61 // list all registered benchmarks
62 void ListBenchmarks();
64 // command lines options/parameters
65 wxSortedArrayString m_toRun
;
71 IMPLEMENT_APP_CONSOLE(BenchApp
)
73 // ============================================================================
74 // Bench namespace symbols implementation
75 // ============================================================================
77 Bench::Function
*Bench::Function::ms_head
= NULL
;
79 long Bench::GetNumericParameter()
81 return wxGetApp().GetNumericParameter();
84 // ============================================================================
85 // BenchApp implementation
86 // ============================================================================
91 m_numRuns
= 10000; // just some default (TODO: switch to time-based one)
95 bool BenchApp::OnInit()
97 if ( !BenchAppBase::OnInit() )
100 wxPrintf("wxWidgets benchmarking program\n"
101 "Build: %s\n", WX_BUILD_OPTIONS_SIGNATURE
);
104 // create a hidden parent window to be used as parent for the GUI controls
105 new wxFrame(NULL
, wxID_ANY
, "Hidden wx benchmark frame");
111 void BenchApp::OnInitCmdLine(wxCmdLineParser
& parser
)
113 BenchAppBase::OnInitCmdLine(parser
);
115 parser
.AddSwitch(OPTION_LIST
,
117 "list all the existing benchmarks");
119 parser
.AddOption(OPTION_AVG_COUNT
,
123 "number of times to run benchmarking loop (default: %ld)",
126 wxCMD_LINE_VAL_NUMBER
);
127 parser
.AddOption(OPTION_NUM_RUNS
,
131 "number of times to run each benchmark in a loop "
135 wxCMD_LINE_VAL_NUMBER
);
136 parser
.AddOption(OPTION_NUMERIC_PARAM
,
138 "numeric parameter used by some benchmark functions "
140 wxCMD_LINE_VAL_NUMBER
);
142 parser
.AddParam("benchmark name",
143 wxCMD_LINE_VAL_STRING
,
144 wxCMD_LINE_PARAM_OPTIONAL
| wxCMD_LINE_PARAM_MULTIPLE
);
147 bool BenchApp::OnCmdLineParsed(wxCmdLineParser
& parser
)
149 if ( parser
.Found(OPTION_LIST
) )
156 const size_t count
= parser
.GetParamCount();
166 parser
.Found(OPTION_AVG_COUNT
, &m_avgCount
);
167 parser
.Found(OPTION_NUM_RUNS
, &m_numRuns
);
168 parser
.Found(OPTION_NUMERIC_PARAM
, &m_numParam
);
170 // construct sorted array for quick verification of benchmark names
171 wxSortedArrayString benchmarks
;
172 for ( Bench::Function
*func
= Bench::Function::GetFirst();
174 func
= func
->GetNext() )
176 benchmarks
.push_back(func
->GetName());
179 for ( size_t n
= 0; n
< count
; n
++ )
181 const wxString name
= parser
.GetParam(n
);
182 if ( benchmarks
.Index(name
) == wxNOT_FOUND
)
184 wxFprintf(stderr
, "No benchmark named \"%s\".\n", name
);
188 m_toRun
.push_back(name
);
191 return BenchAppBase::OnCmdLineParsed(parser
);
194 int BenchApp::OnRun()
196 int rc
= EXIT_SUCCESS
;
197 for ( Bench::Function
*func
= Bench::Function::GetFirst();
199 func
= func
->GetNext() )
201 if ( m_toRun
.Index(func
->GetName()) == wxNOT_FOUND
)
204 wxPrintf("Benchmarking %s(%ld): ", func
->GetName(), m_numParam
);
206 long timeMin
= LONG_MAX
,
210 for ( long a
= 0; a
< m_avgCount
; a
++ )
213 for ( long n
= 0; n
< m_numRuns
&& ok
; n
++ )
223 const long t
= sw
.Time();
238 wxPrintf("%ldms total, ", timeTotal
);
240 long times
= m_avgCount
;
241 if ( m_avgCount
> 2 )
243 timeTotal
-= timeMin
+ timeMax
;
247 wxPrintf("%.2f avg (min=%ld, max=%ld)\n",
248 (float)timeTotal
/ times
, timeMin
, timeMax
);
255 int BenchApp::OnExit()
258 delete GetTopWindow();
265 void BenchApp::ListBenchmarks()
267 wxPrintf("Available benchmarks:\n");
268 for ( Bench::Function
*func
= Bench::Function::GetFirst();
270 func
= func
->GetNext() )
272 wxPrintf("\t%s\n", func
->GetName());