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';
34 // ----------------------------------------------------------------------------
35 // BenchApp declaration
36 // ----------------------------------------------------------------------------
39 typedef wxApp BenchAppBase
;
41 typedef wxAppConsole BenchAppBase
;
44 class BenchApp
: public BenchAppBase
50 virtual void OnInitCmdLine(wxCmdLineParser
& parser
);
51 virtual bool OnCmdLineParsed(wxCmdLineParser
& parser
);
52 virtual bool OnInit();
57 // list all registered benchmarks
58 void ListBenchmarks();
60 // command lines options/parameters
61 wxSortedArrayString m_toRun
;
66 // ============================================================================
67 // Bench::Function implementation
68 // ============================================================================
70 Bench
::Function
*Bench
::Function
::ms_head
= NULL
;
72 // ============================================================================
73 // BenchApp implementation
74 // ============================================================================
76 IMPLEMENT_APP_CONSOLE(BenchApp
)
81 m_numRuns
= 10000; // just some default (TODO: switch to time-based one)
84 bool BenchApp
::OnInit()
86 if ( !BenchAppBase
::OnInit() )
89 wxPrintf("wxWidgets benchmarking program\n"
90 "Build: %s\n", WX_BUILD_OPTIONS_SIGNATURE
);
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");
100 void BenchApp
::OnInitCmdLine(wxCmdLineParser
& parser
)
102 BenchAppBase
::OnInitCmdLine(parser
);
104 parser
.AddSwitch(OPTION_LIST
,
106 "list all the existing benchmarks");
108 parser
.AddOption(OPTION_AVG_COUNT
,
112 "number of times to run benchmarking loop (default: %ld)",
115 wxCMD_LINE_VAL_NUMBER
);
116 parser
.AddOption(OPTION_NUM_RUNS
,
120 "number of times to run each benchmark in a loop "
124 wxCMD_LINE_VAL_NUMBER
);
125 parser
.AddParam("benchmark name",
126 wxCMD_LINE_VAL_STRING
,
127 wxCMD_LINE_PARAM_OPTIONAL
| wxCMD_LINE_PARAM_MULTIPLE
);
130 bool BenchApp
::OnCmdLineParsed(wxCmdLineParser
& parser
)
132 if ( parser
.Found(OPTION_LIST
) )
139 const size_t count
= parser
.GetParamCount();
149 parser
.Found(OPTION_AVG_COUNT
, &m_avgCount
);
150 parser
.Found(OPTION_NUM_RUNS
, &m_numRuns
);
152 // construct sorted array for quick verification of benchmark names
153 wxSortedArrayString benchmarks
;
154 for ( Bench
::Function
*func
= Bench
::Function
::GetFirst();
156 func
= func
->GetNext() )
158 benchmarks
.push_back(func
->GetName());
161 for ( size_t n
= 0; n
< count
; n
++ )
163 const wxString name
= parser
.GetParam(n
);
164 if ( benchmarks
.Index(name
) == wxNOT_FOUND
)
166 wxFprintf(stderr
, "No benchmark named \"%s\".\n", name
);
170 m_toRun
.push_back(name
);
173 return BenchAppBase
::OnCmdLineParsed(parser
);
176 int BenchApp
::OnRun()
178 int rc
= EXIT_SUCCESS
;
179 for ( Bench
::Function
*func
= Bench
::Function
::GetFirst();
181 func
= func
->GetNext() )
183 if ( m_toRun
.Index(func
->GetName()) == wxNOT_FOUND
)
186 wxPrintf("Benchmarking %s: ", func
->GetName());
188 long timeMin
= LONG_MAX
,
192 for ( long a
= 0; a
< m_avgCount
; a
++ )
195 for ( long n
= 0; n
< m_numRuns
&& ok
; n
++ )
205 const long t
= sw
.Time();
220 wxPrintf("%ldms total, ", timeTotal
);
222 long times
= m_avgCount
;
223 if ( m_avgCount
> 2 )
225 timeTotal
-= timeMin
+ timeMax
;
229 wxPrintf("%.2f avg (min=%ld, max=%ld)\n",
230 (float)timeTotal
/ times
, timeMin
, timeMax
);
237 int BenchApp
::OnExit()
240 delete GetTopWindow();
247 void BenchApp
::ListBenchmarks()
249 wxPrintf("Available benchmarks:\n");
250 for ( Bench
::Function
*func
= Bench
::Function
::GetFirst();
252 func
= func
->GetNext() )
254 wxPrintf("\t%s\n", func
->GetName());