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