libpnicore
benchmark_runner.hpp
1 //
2 // (c) Copyright 2012 DESY, Eugen Wintersberger <eugen.wintersberger@desy.de>
3 //
4 // This file is part of libpniio.
5 //
6 // libpniio is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // libpniio is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with libpniio. If not, see <http://www.gnu.org/licenses/>.
18 //
19 // ============================================================================
20 //
21 // Created on: Oct 25, 2012
22 // Author: Eugen Wintersberger <eugen.wintersberger@desy.de>
23 //
24 #pragma once
25 #include "benchmark_result.hpp"
26 #include <list>
27 #include <functional>
28 #include <cmath>
29 
30 namespace pni{
31 namespace core{
32 
40  {
41  private:
43  std::list<benchmark_result> _results;
45  std::function<void()> _pre_run;
47  std::function<void()> _post_run;
48 
50  void do_nothing() {}
51 
52  public:
53  //======================public types================================
55  typedef std::list<benchmark_result>::iterator iterator;
57  typedef std::list<benchmark_result>::const_iterator const_iterator;
59  typedef std::function<void()> function_t;
60 
61  //====================constructors and destructor===================
63  benchmark_runner():_results(0)
64  {
65  _pre_run =
66  function_t(std::bind(&benchmark_runner::do_nothing,*this));
67  _post_run =
68  function_t(std::bind(&benchmark_runner::do_nothing,*this));
69  }
70 
71  //-----------------------------------------------------------------
73  virtual ~benchmark_runner(){}
74 
75  //-----------------------------------------------------------------
77  template<typename TIMERT> void run(size_t n,function_t &func);
78 
79  //-----------------------------------------------------------------
81  iterator begin() { return _results.begin(); }
82 
83  //-----------------------------------------------------------------
85  iterator end() { return _results.end(); }
86 
87  //-----------------------------------------------------------------
89  const_iterator begin() const { return _results.begin(); }
90 
91  //-----------------------------------------------------------------
93  const_iterator end() const { return _results.end(); }
94 
95  //-----------------------------------------------------------------
97  size_t size() const { return _results.size(); }
98 
99  //-----------------------------------------------------------------
101  void prerun(const function_t f) { _pre_run = f; }
102 
103  //-----------------------------------------------------------------
105  void postrun(const function_t f) { _post_run = f; }
106  };
107 
108  //-----------------------------------------------------------------------------
109  template<typename TIMERT> void benchmark_runner::run(size_t n,function_t &func)
110  {
111  for(size_t i=0;i<n;i++)
112  {
113  TIMERT timer;
114 
115  _pre_run(); //run function before benchmark
116 
117  //run the write cycle
118  timer.start();
119  func();
120  timer.stop();
121 
122  _post_run(); //run function after benchmark
123 
124  //getting the result
125  benchmark_result result(timer.duration(),timer.unit());
126  _results.push_back(result);
127  }
128 
129  }
130 
131  //--------------------------------------------------------------------------
143 
144  //-------------------------------------------------------------------------
156 
157 //end of namespace
158 }
159 }
const_iterator begin() const
get const iterator to first element
Definition: benchmark_runner.hpp:89
void do_nothing()
default pre- and post-run function
Definition: benchmark_runner.hpp:50
result of a single benchmark
Definition: benchmark_result.hpp:42
benchmark runner class
Definition: benchmark_runner.hpp:39
virtual ~benchmark_runner()
destructor
Definition: benchmark_runner.hpp:73
std::function< void()> _post_run
function to run after a benchmark run
Definition: benchmark_runner.hpp:47
benchmark_result standard_deviation(const benchmark_runner &bm)
compute result standard deviation
iterator end()
get iterator to last+1 element
Definition: benchmark_runner.hpp:85
Definition: add_op.hpp:29
iterator begin()
get iterator to first element
Definition: benchmark_runner.hpp:81
benchmark_runner()
default constructor
Definition: benchmark_runner.hpp:63
void postrun(const function_t f)
set post-run function
Definition: benchmark_runner.hpp:105
std::function< void()> _pre_run
function to run before a benchmark run
Definition: benchmark_runner.hpp:45
const_iterator end() const
get const iterator to last+1 element
Definition: benchmark_runner.hpp:93
benchmark_result average(const benchmark_runner &bm)
compute the average result
std::list< benchmark_result >::iterator iterator
iterator for benchmark results
Definition: benchmark_runner.hpp:55
std::list< benchmark_result >::const_iterator const_iterator
const iterator for benchnmark results
Definition: benchmark_runner.hpp:57
void prerun(const function_t f)
set pre-run function
Definition: benchmark_runner.hpp:101
size_t size() const
get size
Definition: benchmark_runner.hpp:97
void run(size_t n, function_t &func)
perform all runs
Definition: benchmark_runner.hpp:109
std::function< void()> function_t
function type for pre- and post-run functions
Definition: benchmark_runner.hpp:59
std::list< benchmark_result > _results
list with benchmark results
Definition: benchmark_runner.hpp:43