allow specifying initialization and cleanup functions for benchmarks

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@56359 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2008-10-16 12:34:47 +00:00
parent 04021d6fac
commit fa8825a01d
2 changed files with 32 additions and 8 deletions

View File

@@ -254,8 +254,8 @@ int BenchApp::OnRun()
long timeMin = LONG_MAX, long timeMin = LONG_MAX,
timeMax = 0, timeMax = 0,
timeTotal = 0; timeTotal = 0;
bool ok = true; bool ok = func->Init();
for ( long a = 0; a < m_avgCount; a++ ) for ( long a = 0; ok && a < m_avgCount; a++ )
{ {
wxStopWatch sw; wxStopWatch sw;
for ( long n = 0; n < m_numRuns && ok; n++ ) for ( long n = 0; n < m_numRuns && ok; n++ )
@@ -265,9 +265,6 @@ int BenchApp::OnRun()
sw.Pause(); sw.Pause();
if ( !ok )
break;
const long t = sw.Time(); const long t = sw.Time();
if ( t < timeMin ) if ( t < timeMin )
timeMin = t; timeMin = t;
@@ -276,6 +273,8 @@ int BenchApp::OnRun()
timeTotal += t; timeTotal += t;
} }
func->Done();
if ( !ok ) if ( !ok )
{ {
wxPrintf("ERROR\n"); wxPrintf("ERROR\n");

View File

@@ -25,12 +25,19 @@ namespace Bench
class Function class Function
{ {
public: public:
typedef bool (*Type)(); typedef bool (*InitType)();
typedef bool (*FuncType)();
typedef void (*DoneType)();
/// Ctor is used implicitly by BENCHMARK_FUNC(). /// Ctor is used implicitly by BENCHMARK_FUNC().
Function(const char *name, Type func) Function(const char *name,
FuncType func,
InitType init = NULL,
DoneType done = NULL)
: m_name(name), : m_name(name),
m_func(func), m_func(func),
m_init(init),
m_done(done),
m_next(ms_head) m_next(ms_head)
{ {
ms_head = this; ms_head = this;
@@ -39,9 +46,14 @@ public:
/// Get the name of this function /// Get the name of this function
const char *GetName() const { return m_name; } const char *GetName() const { return m_name; }
/// Perform once-only initialization prior to Run().
bool Init() { return m_init ? (*m_init)() : true; }
/// Run the function, return its return value. /// Run the function, return its return value.
bool Run() { return (*m_func)(); } bool Run() { return (*m_func)(); }
/// Clean up after performing the benchmark.
void Done() { if ( m_done ) (*m_done)(); }
/// Get the head of the linked list of benchmark objects /// Get the head of the linked list of benchmark objects
static Function *GetFirst() { return ms_head; } static Function *GetFirst() { return ms_head; }
@@ -55,7 +67,9 @@ private:
// name of and pointer to the function, as passed to the ctor // name of and pointer to the function, as passed to the ctor
const char * const m_name; const char * const m_name;
const Type m_func; const FuncType m_func;
const InitType m_init;
const DoneType m_done;
// pointer to the next object in the linked list or NULL // pointer to the next object in the linked list or NULL
Function * const m_next; Function * const m_next;
@@ -98,4 +112,15 @@ wxString GetStringParameter();
static Bench::Function wxMAKE_UNIQUE_NAME(name)(#name, name); \ static Bench::Function wxMAKE_UNIQUE_NAME(name)(#name, name); \
bool name() bool name()
/**
Define a benchmark function requiring initialization and shutdown.
This macro is similar to BENCHMARK_FUNC() but ensures that @a init is
called before the benchmark is ran and @a done afterwards.
*/
#define BENCHMARK_FUNC_WITH_INIT(name, init, done) \
static bool name(); \
static Bench::Function wxMAKE_UNIQUE_NAME(name)(#name, name, init, done); \
bool name()
#endif // _WX_TESTS_BENCHMARKS_BENCH_H_ #endif // _WX_TESTS_BENCHMARKS_BENCH_H_