]> git.saurik.com Git - wxWidgets.git/blob - tests/benchmarks/bench.h
wxMessageBox off the main thread lost result code.
[wxWidgets.git] / tests / benchmarks / bench.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: tests/benchmarks/bench.h
3 // Purpose: Main header of the benchmarking suite
4 // Author: Vadim Zeitlin
5 // Created: 2008-07-19
6 // Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #ifndef _WX_TESTS_BENCHMARKS_BENCH_H_
11 #define _WX_TESTS_BENCHMARKS_BENCH_H_
12
13 #include "wx/cpp.h"
14 #include "wx/defs.h"
15
16 namespace Bench
17 {
18
19 /**
20 Utility object used to register the benchmark functions.
21
22 @see BENCHMARK_FUNC
23 */
24 class Function
25 {
26 public:
27 typedef bool (*InitType)();
28 typedef bool (*FuncType)();
29 typedef void (*DoneType)();
30
31 /// Ctor is used implicitly by BENCHMARK_FUNC().
32 Function(const char *name,
33 FuncType func,
34 InitType init = NULL,
35 DoneType done = NULL)
36 : m_name(name),
37 m_func(func),
38 m_init(init),
39 m_done(done),
40 m_next(ms_head)
41 {
42 ms_head = this;
43 }
44
45 /// Get the name of this function
46 const char *GetName() const { return m_name; }
47
48 /// Perform once-only initialization prior to Run().
49 bool Init() { return m_init ? (*m_init)() : true; }
50
51 /// Run the function, return its return value.
52 bool Run() { return (*m_func)(); }
53
54 /// Clean up after performing the benchmark.
55 void Done() { if ( m_done ) (*m_done)(); }
56
57 /// Get the head of the linked list of benchmark objects
58 static Function *GetFirst() { return ms_head; }
59
60 /// Get the next object in the linked list or NULL
61 Function *GetNext() const { return m_next; }
62
63 private:
64 // the head of the linked list of Bench::Function objects
65 static Function *ms_head;
66
67 // name of and pointer to the function, as passed to the ctor
68 const char * const m_name;
69 const FuncType m_func;
70 const InitType m_init;
71 const DoneType m_done;
72
73 // pointer to the next object in the linked list or NULL
74 Function * const m_next;
75
76 DECLARE_NO_COPY_CLASS(Function)
77 };
78
79 /**
80 Get the numeric parameter.
81
82 Tests may use this parameter in whatever way they see fit, by default it is
83 1 but can be set to a different value by user from the command line.
84 */
85 long GetNumericParameter();
86
87 /**
88 Get the string parameter.
89
90 Tests may use this parameter in whatever way they see fit, by default it is
91 empty but can be set to a different value by user from the command line.
92 */
93 wxString GetStringParameter();
94
95 } // namespace Bench
96
97 /**
98 This macro defines a benchmark function.
99
100 All these functions return a boolean value and take no parameters. The
101 return value is needed to prevent the compiler from optimizing the
102 benchmark entirely away and so typically will be constructed using the
103 results of the benchmark actions, even though normally benchmark should
104 always return true.
105
106 Once benchmark is defined, it can be invoked from the command line of the
107 main benchmarking program.
108 */
109 #define BENCHMARK_FUNC(name) \
110 static bool name(); \
111 static Bench::Function wxMAKE_UNIQUE_NAME(name)(#name, name); \
112 bool name()
113
114 /**
115 Define a benchmark function requiring initialization and shutdown.
116
117 This macro is similar to BENCHMARK_FUNC() but ensures that @a init is
118 called before the benchmark is ran and @a done afterwards.
119 */
120 #define BENCHMARK_FUNC_WITH_INIT(name, init, done) \
121 static bool name(); \
122 static Bench::Function wxMAKE_UNIQUE_NAME(name)(#name, name, init, done); \
123 bool name()
124
125 #endif // _WX_TESTS_BENCHMARKS_BENCH_H_