libwreport  3.40
benchmark.h
Go to the documentation of this file.
1 #ifndef WREPORT_BENCHMARK_H
2 #define WREPORT_BENCHMARK_H
3 
8 #include <cstdio>
9 #include <functional>
10 #include <string>
11 #include <vector>
12 
13 namespace wreport {
14 namespace benchmark {
15 
16 struct Benchmark;
17 
19 struct Task
20 {
21  // Unmanaged pointer to the benchmark we belong to
22  Benchmark* parent;
23  // Name of this task
24  std::string name;
25  // Number of time this task has run
26  unsigned run_count = 0;
27  // Total user time
28  clock_t utime = 0;
29  // Total system time
30  clock_t stime = 0;
31 
32  Task(Benchmark* parent, const std::string& name);
33 
34  // Run the given function and collect timings for it
35  void collect(std::function<void()> f);
36 };
37 
39 struct Progress
40 {
41  virtual ~Progress() {}
42 
43  virtual void start_benchmark(const Benchmark& b) = 0;
44  virtual void end_benchmark(const Benchmark& b) = 0;
45  virtual void start_iteration(const Benchmark& b, unsigned cur,
46  unsigned total) = 0;
47  virtual void end_iteration(const Benchmark& b, unsigned cur,
48  unsigned total) = 0;
49  virtual void test_failed(const Benchmark& b, std::exception& e) = 0;
50 };
51 
57 {
58  FILE* out;
59  FILE* err;
60 
61  BasicProgress(FILE* out = stdout, FILE* err = stderr);
62 
63  void start_benchmark(const Benchmark& b) override;
64  void start_iteration(const Benchmark& b, unsigned cur,
65  unsigned total) override;
66  void end_iteration(const Benchmark& b, unsigned cur,
67  unsigned total) override;
68  void end_benchmark(const Benchmark& b) override;
69  void test_failed(const Benchmark& b, std::exception& e) override;
70 };
71 
75 struct Benchmark
76 {
77  // Name of this benchmark
78  std::string name;
79  // Number of repetitions
80  unsigned repetitions = 10;
81  // Unmanaged pointers to the tasks in this benchmark
82  std::vector<Task*> tasks;
83  // Main task, collecting timings for the toplevel run
84  Task task_main;
85 
86  Benchmark(const std::string& name);
87  virtual ~Benchmark();
88 
94  virtual void setup_main() {}
95 
101  virtual void teardown_main() {}
102 
108  virtual void setup_iteration() {}
109 
115  virtual void teardown_iteration() {}
116 
118  void run(Progress& progress);
119 
121  void print_timings();
122 
124  virtual void main() = 0;
125 };
126 
128 struct Registry
129 {
130  std::vector<Benchmark*> benchmarks;
131 
133  void add(Benchmark* b);
134 
138  static Registry& get();
139 
157  static void basic_run(int argc, const char* argv[]);
158 };
159 
160 } // namespace benchmark
161 } // namespace wreport
162 
163 #endif
Basic progress implementation writing progress information to the given output stream.
Definition: benchmark.h:56
Collect all existing benchmarks.
Definition: benchmark.h:128
Base class for all benchmarks.
Definition: benchmark.h:75
virtual void teardown_iteration()
Tear down the environment for an iteration of this benchmark.
Definition: benchmark.h:115
static void basic_run(int argc, const char *argv[])
Basic implementation of a main function that runs all benchmarks linked into the program.
void run(Progress &progress)
Run the benchmark and collect timings.
virtual void teardown_main()
Tear down the environment for this benchmark.
Definition: benchmark.h:101
void print_timings()
Print timings to stdout.
virtual void setup_iteration()
Set up the environment for an iteration of this benchmark.
Definition: benchmark.h:108
String functions.
Definition: benchmark.h:13
Notify of progress during benchmark execution.
Definition: benchmark.h:39
virtual void main()=0
Main body of this benchmark.
Collect timings for one task.
Definition: benchmark.h:19
virtual void setup_main()
Set up the environment for this benchmark.
Definition: benchmark.h:94
void add(Benchmark *b)
Add a benchmark to this registry.