diff --git a/src/lib/bench/benchmark.h b/src/lib/bench/benchmark.h index 22ae7e70c7..7f77aa19ec 100644 --- a/src/lib/bench/benchmark.h +++ b/src/lib/bench/benchmark.h @@ -17,7 +17,6 @@ #include -#include #include #include @@ -211,9 +210,9 @@ public: /// \param target The templated class object that /// implements the code to be benchmarked. BenchMark(const int iterations, T target) : - iterations_(iterations), sub_iterations_(0) + iterations_(iterations), sub_iterations_(0), target_(target) { - initialize(target, true); + initialize(true); } /// \brief Constructor for finer-grained control. @@ -231,9 +230,9 @@ public: /// \param immediate If \c true the benchmark will be performed within /// the constructor; otherwise it only does initialization. BenchMark(const int iterations, T& target, const bool immediate) : - iterations_(iterations), sub_iterations_(0), target_(&target) + iterations_(iterations), sub_iterations_(0), target_(target) { - initialize(target, immediate); + initialize(immediate); } //@} @@ -242,14 +241,14 @@ public: /// This method will be called from \c run() before starting the benchmark. /// By default it's empty, but can be customized via template /// specialization. - void setUp(T&) {} + void setUp() {} /// \brief Hook to be called after benchmark. /// /// This method will be called from \c run() when the benchmark completes. /// By default it's empty, but can be customized via template /// specialization. - void tearDown(T&) {} + void tearDown() {} /// \brief Perform benchmark. /// @@ -258,8 +257,17 @@ public: /// of times specified on construction, and records the time on completion. /// Finally, it calls \c tearDown(). void run() { - assert(target_ != NULL); - run(*target_); + setUp(); + + struct timeval beg, end; + gettimeofday(&beg, NULL); + for (unsigned int i = 0; i < iterations_; ++i) { + sub_iterations_ += target_.run(); + } + gettimeofday(&end, NULL); + tv_diff_ = tv_subtract(end, beg); + + tearDown(); } /// \brief Print the benchmark result. @@ -353,23 +361,9 @@ public: /// performed implicitly. static const int ITERATION_FAILURE = -1; private: - void run(T& target) { - setUp(target); - - struct timeval beg, end; - gettimeofday(&beg, NULL); - for (unsigned int i = 0; i < iterations_; ++i) { - sub_iterations_ += target.run(); - } - gettimeofday(&end, NULL); - tv_diff_ = tv_subtract(end, beg); - - tearDown(target); - } - - void initialize(T& target, const bool immediate) { + void initialize(const bool immediate) { if (immediate) { - run(target); + run(); printResult(); } } @@ -394,7 +388,7 @@ private: static const int ONE_MILLION = 1000000; const unsigned int iterations_; unsigned int sub_iterations_; - T* target_; + T& target_; struct timeval tv_diff_; }; diff --git a/src/lib/bench/example/search_bench.cc b/src/lib/bench/example/search_bench.cc index 84f95d953b..851d815e88 100644 --- a/src/lib/bench/example/search_bench.cc +++ b/src/lib/bench/example/search_bench.cc @@ -79,9 +79,9 @@ namespace isc { namespace bench { template<> void -BenchMark::setUp(SetSearchBenchMark& target) { +BenchMark::setUp() { cout << "Benchmark for searching std::set (size=" - << target.data_.size() << ")" << endl; + << target_.data_.size() << ")" << endl; } } } diff --git a/src/lib/bench/tests/benchmark_unittest.cc b/src/lib/bench/tests/benchmark_unittest.cc index dfe7df9c75..9b476cdffc 100644 --- a/src/lib/bench/tests/benchmark_unittest.cc +++ b/src/lib/bench/tests/benchmark_unittest.cc @@ -46,14 +46,14 @@ namespace isc { namespace bench { template <> void -BenchMark::setUp(TestBenchMark& target) { - target.setup_completed_ = true; +BenchMark::setUp() { + target_.setup_completed_ = true; }; template <> void -BenchMark::tearDown(TestBenchMark& target) { - target.teardown_completed_ = true; +BenchMark::tearDown() { + target_.teardown_completed_ = true; }; // XXX: some compilers cannot find class static constants used in