0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
TestUtils.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 3
9  * of the License, or any later version.
10  *
11  * Hypertable 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 Hypertable. If not, see <http://www.gnu.org/licenses/>
18  */
19 
25 #ifndef Common_TestUtils_h
26 #define Common_TestUtils_h
27 
28 #include "Config.h"
29 #include "SystemInfo.h"
30 #include "Stopwatch.h"
31 #include "Thread.h"
32 
33 #include <boost/bind.hpp>
34 
35 #include <cmath>
36 #include <limits>
37 #include <mutex>
38 
39 #define HT_BENCH_OUT(_label_, _n_, _w_) do { \
40  std::cout << ThisThread::get_id() <<": "<< (_label_) <<": " \
41  << (_n_) / _w_.elapsed() <<"/s ("<< _w_.elapsed() / (_n_) * 1e6 \
42  <<"ns per)"<< std::endl; \
43 } while(0)
44 
45 #define HT_BENCH(_label_, _code_, _n_) do { \
46  Stopwatch _w; \
47  for (int i = 0, _n = (_n_); i < _n; ++i) { _code_; } \
48  _w.stop(); \
49  HT_BENCH_OUT(_label_, _n_, _w); \
50 } while (0)
51 
52 #define HT_BENCH1(_label_, _code_, _n_) do { \
53  Stopwatch _w; _code_; _w.stop(); \
54  HT_BENCH_OUT(_label_, _n_, _w); \
55 } while (0)
56 
57 namespace Hypertable {
58 
64 inline void print_proc_stat() {
65  std::cout << ThisThread::get_id() << ": " << System::proc_stat() << "\n\n";
66 }
67 
73 class TestStat {
74  public:
76  : m_minv(std::numeric_limits<double>::max()),
77  m_maxv(std::numeric_limits<double>::min()), m_i(0), m_a0(0.),
78  m_a1(0.), m_q0(0.), m_q1(0.) {
79  }
80 
81  void operator()(double x) {
82  if (x < m_minv) m_minv = x;
83  if (x > m_maxv) m_maxv = x;
84 
85  m_a1 = m_a0 + (x - m_a0) / ++m_i;
86  m_q1 = m_q0 + (x - m_a0) * (x - m_a1);
87  m_a0 = m_a1;
88  m_q0 = m_q1;
89  }
90 
91  // Adds a new test result (synchronized version)
92  void add(double x) {
93  std::lock_guard<std::mutex> lock(m_mutex);
94  (*this)(x);
95  }
96 
97  double min() const { std::lock_guard<std::mutex> lock(m_mutex); return m_minv; }
98  double max() const { std::lock_guard<std::mutex> lock(m_mutex); return m_maxv; }
99  double mean() const { std::lock_guard<std::mutex> lock(m_mutex); return m_a1; }
100  double stdev() const { std::lock_guard<std::mutex> lock(m_mutex); return std::sqrt(m_q1/m_i); }
101 
102  private:
104  double m_minv, m_maxv;
105  size_t m_i;
106  double m_a0, m_a1, m_q0, m_q1;
107 };
108 
110 inline std::ostream &operator<<(std::ostream &out, const TestStat &stat) {
111  return out << "min=" << stat.min() << " max=" << stat.max()
112  << " mean=" << stat.mean() << " stdev=" << stat.stdev();
113 }
114 
119 template <typename FunT>
120 struct TestFun {
121  TestFun(FunT fun, bool proc_stat = false, TestStat *stat = NULL)
122  : fun(fun), proc_stat(proc_stat), stat_acc(stat) {
123  }
124 
125  void operator()() {
126  Stopwatch w;
127  fun();
128  if (stat_acc || proc_stat)
129  std::cout << ThisThread::get_id() << ": ";
130 
131  if (stat_acc) {
132  stat_acc->add(w.elapsed());
133  std::cout << "Elapsed: " << w.elapsed() << "s\n ";
134  }
135  if (proc_stat)
136  std::cout << System::proc_stat() << "\n";
137 
138  if (stat_acc || proc_stat)
139  std::cout << std::endl;
140  }
141 
142  FunT fun;
143  bool proc_stat;
145 };
146 
151 template <typename FunT>
152 void serial_run(FunT fun, size_t n, bool proc_stat = false) {
153  TestStat stat;
154 
155  while (n--) {
156  TestFun<FunT> f(fun, proc_stat, &stat);
157  f();
158  }
159  std::cout << "Elapsed times: " << stat << "s\n" << std::endl;
160 }
161 
166 template <typename FunT>
167 void parallel_run(FunT fun, size_t n, bool proc_stat = false) {
168  ThreadGroup pool;
169  TestStat stat;
170 
171  while (n--) {
172  pool.create_thread(TestFun<FunT>(fun, proc_stat, &stat));
173  }
174  pool.join_all();
175  std::cout << "Elapsed times: " << stat << "s\n" << std::endl;
176 }
177 
184 template <typename FunT>
185 void run_test(FunT fun, bool proc_stat = false, const Properties *props = 0) {
186  if (!props) {
188  props = Config::properties.get();
189  }
190 
191  size_t threads = props->get_i32("threads", 0);
192 
193  if (threads)
194  parallel_run(fun, threads, proc_stat);
195  else
196  serial_run(fun, props->get_i32("repeats", 3), proc_stat);
197 }
198 
199 namespace Config {
200 
205  static void init_options() {
206  cmdline_desc().add_options()
207  ("repeats,r", i32()->default_value(3), "Number of repeats")
208  ("threads,t", i32(), "Number of threads")
209  ("num-items,n", i32()->default_value(100000), "Number of items")
210  ;
211  cmdline_hidden_desc().add_options()
212  ("components", strs(), "test components")
213  ;
214  cmdline_positional_desc().add("components", -1);
215  }
216 };
217 
219 
222 }
223 }
224 
225 #endif /* Common_TestUtils_h */
double min() const
Definition: TestUtils.h:97
static std::mutex mutex
Definition: Logger.cc:43
Interface and base of config policy.
Definition: Config.h:149
double elapsed()
Returns the elapsed time.
Definition: Stopwatch.h:72
Cons< TestPolicy, DefaultPolicy > DefaultTestPolicy
Definition: TestUtils.h:218
PropertiesPtr properties
This singleton map stores all options.
Definition: Config.cc:47
The Stopwatch measures elapsed time.
void run_test(FunT fun, bool proc_stat=false, const Properties *props=0)
Runs a test based on command line parameters:
Definition: TestUtils.h:185
TestFun(FunT fun, bool proc_stat=false, TestStat *stat=NULL)
Definition: TestUtils.h:121
static const ProcStat & proc_stat()
Retrieves updated Process statistics (see SystemInfo.h)
Definition: SystemInfo.cc:382
void parallel_run(FunT fun, size_t n, bool proc_stat=false)
Runs a test in parallel n times in a row while accumulating the benchmark results.
Definition: TestUtils.h:167
STL namespace.
Desc & cmdline_desc(const char *usage)
A macro which definds global functions like get_bool(), get_str(), get_i16() etc. ...
Definition: Config.cc:72
#define HT_ASSERT(_e_)
Definition: Logger.h:396
boost::thread_group ThreadGroup
Definition: Thread.h:46
double max() const
Definition: TestUtils.h:98
Po::typed_value< int32_t > * i32(int32_t *v=0)
Definition: Properties.h:178
Helpers to compose init policies; allow to combine two policies into one.
Definition: Config.h:174
Manages a collection of program options.
Definition: Properties.h:208
std::ostream & operator<<(std::ostream &os, const crontab_entry &entry)
Helper function to write crontab_entry to an ostream.
Definition: Crontab.cc:301
Importing boost::thread and boost::thread_group into the Hypertable namespace.
void operator()(double x)
Definition: TestUtils.h:81
Po::typed_value< Strings > * strs(Strings *v=0)
Definition: Properties.h:170
Hypertable definitions
A Policy class for extending command line options.
Definition: TestUtils.h:204
std::mutex m_mutex
Definition: TestUtils.h:103
The Stopwatch class measures elapsed time between instantiation (or a call to start) and a call to st...
Definition: Stopwatch.h:40
Accumulates min, max, mean and stdev of the test results; based on the Welford method for numerical s...
Definition: TestUtils.h:73
void add(double x)
Definition: TestUtils.h:92
Configuration settings.
Desc & cmdline_hidden_desc()
Get the command line hidden options description (for positional options)
Definition: Config.cc:81
System information and statistics based on libsigar.
PositionalDesc & cmdline_positional_desc()
Get the command line positional options description.
Definition: Config.cc:90
Helper class wrapping the invocation of a single test function.
Definition: TestUtils.h:120
void print_proc_stat()
Prints statistics about the current process.
Definition: TestUtils.h:64
double mean() const
Definition: TestUtils.h:99
void serial_run(FunT fun, size_t n, bool proc_stat=false)
Runs a test serially n times in a row while accumulating the benchmark results.
Definition: TestUtils.h:152
TestStat * stat_acc
Definition: TestUtils.h:144
double stdev() const
Definition: TestUtils.h:100