libwreport 3.40
benchmark.h
Go to the documentation of this file.
1#ifndef WREPORT_BENCHMARK_H
2#define WREPORT_BENCHMARK_H
3
7
8#include <cstdio>
9#include <functional>
10#include <string>
11#include <vector>
12
13namespace wreport {
14namespace benchmark {
15
16struct Benchmark;
17
19struct 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
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
56struct BasicProgress : Progress
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
75struct 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
122
124 virtual void main() = 0;
125};
126
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
String functions.
Definition benchmark.h:13
Base class for all benchmarks.
Definition benchmark.h:76
virtual void teardown_main()
Tear down the environment for this benchmark.
Definition benchmark.h:101
virtual void main()=0
Main body of this benchmark.
virtual void teardown_iteration()
Tear down the environment for an iteration of this benchmark.
Definition benchmark.h:115
void print_timings()
Print timings to stdout.
virtual void setup_main()
Set up the environment for this benchmark.
Definition benchmark.h:94
void run(Progress &progress)
Run the benchmark and collect timings.
virtual void setup_iteration()
Set up the environment for an iteration of this benchmark.
Definition benchmark.h:108
Notify of progress during benchmark execution.
Definition benchmark.h:40
Collect all existing benchmarks.
Definition benchmark.h:129
void add(Benchmark *b)
Add a benchmark to this registry.
static void basic_run(int argc, const char *argv[])
Basic implementation of a main function that runs all benchmarks linked into the program.
static Registry & get()
Get the static instance of the registry.
Collect timings for one task.
Definition benchmark.h:20