bench.h 3.8 KB

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