libwreport 3.38
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
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
38
41{
42 virtual ~Progress() {}
43
44 virtual void start_benchmark(const Benchmark& b) = 0;
45 virtual void end_benchmark(const Benchmark& b) = 0;
46 virtual void start_iteration(const Benchmark& b, unsigned cur, unsigned total) = 0;
47 virtual void end_iteration(const Benchmark& b, unsigned cur, unsigned total) = 0;
48 virtual void test_failed(const Benchmark& b, std::exception& e) = 0;
49};
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, unsigned total) override;
65 void end_iteration(const Benchmark& b, unsigned cur, unsigned total) override;
66 void end_benchmark(const Benchmark& b) override;
67 void test_failed(const Benchmark& b, std::exception& e) override;
68};
69
70
75{
76 // Name of this benchmark
77 std::string name;
78 // Number of repetitions
79 unsigned repetitions = 10;
80 // Unmanaged pointers to the tasks in this benchmark
81 std::vector<Task*> tasks;
82 // Main task, collecting timings for the toplevel run
83 Task task_main;
84
85 Benchmark(const std::string& name);
86 virtual ~Benchmark();
87
93 virtual void setup_main() {}
94
100 virtual void teardown_main() {}
101
107 virtual void setup_iteration() {}
108
114 virtual void teardown_iteration() {}
115
117 void run(Progress& progress);
118
121
123 virtual void main() = 0;
124};
125
128{
129 std::vector<Benchmark*> benchmarks;
130
132 void add(Benchmark* b);
133
137 static Registry& get();
138
156 static void basic_run(int argc, const char* argv[]);
157};
158
159}
160}
161
162#endif
String functions.
Definition benchmark.h:13
Basic progress implementation writing progress information to the given output stream.
Definition benchmark.h:57
Base class for all benchmarks.
Definition benchmark.h:75
virtual void teardown_main()
Tear down the environment for this benchmark.
Definition benchmark.h:100
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:114
void print_timings()
Print timings to stdout.
virtual void setup_main()
Set up the environment for this benchmark.
Definition benchmark.h:93
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:107
Notify of progress during benchmark execution.
Definition benchmark.h:41
Collect all existing benchmarks.
Definition benchmark.h:128
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