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
,
110 "number of times to run benchmarking loop",
111 wxCMD_LINE_VAL_NUMBER
);
112 parser
.AddOption(OPTION_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
);
121 bool BenchApp::OnCmdLineParsed(wxCmdLineParser
& parser
)
123 if ( parser
.Found(OPTION_LIST
) )
130 const size_t count
= parser
.GetParamCount();
140 parser
.Found(OPTION_AVG_COUNT
, &m_avgCount
);
141 parser
.Found(OPTION_NUM_RUNS
, &m_numRuns
);
143 // construct sorted array for quick verification of benchmark names
144 wxSortedArrayString benchmarks
;
145 for ( Bench::Function
*func
= Bench::Function::GetFirst();
147 func
= func
->GetNext() )
149 benchmarks
.push_back(func
->GetName());
152 for ( size_t n
= 0; n
< count
; n
++ )
154 const wxString name
= parser
.GetParam(n
);
155 if ( benchmarks
.Index(name
) == wxNOT_FOUND
)
157 wxFprintf(stderr
, "No benchmark named \"%s\".\n", name
);
161 m_toRun
.push_back(name
);
164 return BenchAppBase::OnCmdLineParsed(parser
);
167 int BenchApp::OnRun()
169 int rc
= EXIT_SUCCESS
;
170 for ( Bench::Function
*func
= Bench::Function::GetFirst();
172 func
= func
->GetNext() )
174 if ( m_toRun
.Index(func
->GetName()) == wxNOT_FOUND
)
177 wxPrintf("Benchmarking %s: ", func
->GetName());
179 long timeMin
= LONG_MAX
,
183 for ( long a
= 0; a
< m_avgCount
; a
++ )
186 for ( long n
= 0; n
< m_numRuns
&& ok
; n
++ )
196 const long t
= sw
.Time();
211 wxPrintf("%ldms total, ", timeTotal
);
213 long times
= m_avgCount
;
214 if ( m_avgCount
> 2 )
216 timeTotal
-= timeMin
+ timeMax
;
220 wxPrintf("%.2f avg (min=%ld, max=%ld)\n",
221 (float)timeTotal
/ times
, timeMin
, timeMax
);
228 int BenchApp::OnExit()
231 delete GetTopWindow();
238 void BenchApp::ListBenchmarks()
240 wxPrintf("Available benchmarks:\n");
241 for ( Bench::Function
*func
= Bench::Function::GetFirst();
243 func
= func
->GetNext() )
245 wxPrintf("\t%s\n", func
->GetName());