libdballe  7.29
core/benchmark.h
Go to the documentation of this file.
1 #ifndef WREPORT_BENCHMARK_H
2 #define WREPORT_BENCHMARK_H
3 
8 #include <string>
9 #include <vector>
10 #include <functional>
11 #include <cstdio>
12 #include <time.h>
13 #include <sys/time.h>
14 #include <sys/resource.h>
15 
16 namespace dballe {
17 namespace benchmark {
18 
19 struct Benchmark;
20 
22 struct Task
23 {
24  // Name of this task
25  std::string name;
26 
27  Task() {}
28  Task(const std::string& name) : name(name) {}
29  Task(const Task&) = delete;
30  Task(Task&&) = delete;
31  virtual ~Task() {}
32  Task& operator=(const Task&) = delete;
33  Task& operator=(Task&&) = delete;
34 
36  virtual void setup() {}
37 
44  virtual void run_once() = 0;
45 
47  virtual void teardown() {}
48 };
49 
50 struct Progress;
51 
52 struct TaskHolder
53 {
54  Task* task = nullptr;
55 
56  TaskHolder(Task* task) : task(task) {}
57  TaskHolder(TaskHolder&& o) : task(o.task) { o.task = nullptr; }
58  TaskHolder(const TaskHolder&) = delete;
59  TaskHolder& operator=(TaskHolder&& o)
60  {
61  if (task == o.task) return *this;
62  if (task) delete task;
63  task = o.task;
64  o.task = nullptr;
65  return *this;
66  }
67  TaskHolder& operator=(const TaskHolder&) = delete;
68  ~TaskHolder() { delete task; }
69 };
70 
72 {
74  unsigned repetitions;
75  struct timespec time_at_start;
76  struct timespec time_at_end;
77  struct rusage res_at_start;
78  struct rusage res_at_end;
79 
80  Timeit(Task* task, int repetitions=10) : TaskHolder(task), repetitions(repetitions) {}
81 
82  void run(Progress& progress);
83 };
84 
86 {
88  double run_time;
89  unsigned times_run = 0;
90 
91  Throughput(Task* task, double run_time=0.5) : TaskHolder(task), run_time(run_time) {}
92 
93  void run(Progress& progress);
94 };
95 
96 
98 struct Progress
99 {
100  virtual ~Progress() {}
101 
102  virtual void start_benchmark(const Benchmark& b) = 0;
103  virtual void end_benchmark(const Benchmark& b) = 0;
104 
105  virtual void start_timeit(const Timeit& t) = 0;
106  virtual void end_timeit(const Timeit& t) = 0;
107 
108  virtual void start_throughput(const Throughput& t) = 0;
109  virtual void end_throughput(const Throughput& t) = 0;
110 
111  virtual void test_failed(const Task& t, std::exception& e) = 0;
112 };
113 
114 
120 {
121  std::string prefix;
122  FILE* out;
123  FILE* err;
124  std::string cur_benchmark;
125 
126  BasicProgress(const std::string& prefix, FILE* out=stdout, FILE* err=stderr);
127 
128  void start_benchmark(const Benchmark& b) override;
129  void end_benchmark(const Benchmark& b) override;
130 
131  void start_timeit(const Timeit& t) override;
132  void end_timeit(const Timeit& t) override;
133 
134  void start_throughput(const Throughput& t) override;
135  void end_throughput(const Throughput& t) override;
136 
137  void test_failed(const Task& t, std::exception& e) override;
138 };
139 
140 
144 struct Benchmark
145 {
147  std::string name;
148 
150  std::vector<Timeit> timeit_tasks;
151 
153  std::vector<Throughput> throughput_tasks;
154 
155 
156  Benchmark(const std::string& name);
157  virtual ~Benchmark();
158 
159  virtual void setup() {}
160  virtual void teardown() {}
161 
163  void run(Progress& progress);
164 
166  void print_timings(const std::string& prefix);
167 
169  virtual void register_tasks() = 0;
170 };
171 
173 struct Registry
174 {
175  std::vector<Benchmark*> benchmarks;
176 
178  void add(Benchmark* b);
179 
183  static Registry& get();
184 
202  static void basic_run(int argc, const char* argv[]);
203 };
204 
205 }
206 }
207 
208 #endif
double run_time
How many seconds to run the task to see how many times per second it runs.
Definition: core/benchmark.h:88
std::string name
Name of this benchmark.
Definition: core/benchmark.h:147
virtual void run_once()=0
Run the task once.
void run(Progress &progress)
Run the benchmark and collect timings.
Basic progress implementation writing progress information to the given output stream.
Definition: core/benchmark.h:119
static void basic_run(int argc, const char *argv[])
Basic implementation of a main function that runs all benchmarks linked into the program.
virtual void register_tasks()=0
Register tasks to run on this benchmark.
Collect all existing benchmarks.
Definition: core/benchmark.h:173
Base class for all benchmarks.
Definition: core/benchmark.h:144
virtual void teardown()
Clean up after the task has been measured.
Definition: core/benchmark.h:47
Definition: core/benchmark.h:71
void print_timings(const std::string &prefix)
Print timings to stdout.
virtual void setup()
Set up the environment for running run_once()
Definition: core/benchmark.h:36
One task to be measured.
Definition: core/benchmark.h:22
void add(Benchmark *b)
Add a benchmark to this registry.
Definition: core/benchmark.h:52
Definition: core/benchmark.h:85
std::vector< Throughput > throughput_tasks
Tasks for which we time their throughput.
Definition: core/benchmark.h:153
unsigned repetitions
How many times to repeat the task for measuring how long it takes.
Definition: core/benchmark.h:74
std::vector< Timeit > timeit_tasks
Tasks for which we time their duration.
Definition: core/benchmark.h:150
Notify of progress during benchmark execution.
Definition: core/benchmark.h:98