libdballe  9.14
benchmark.h
Go to the documentation of this file.
1 #ifndef DBALLE_CORE_BENCHMARK_H
2 #define DBALLE_CORE_BENCHMARK_H
3 
8 #include <cstdio>
9 #include <dballe/file.h>
10 #include <dballe/message.h>
11 #include <functional>
12 #include <memory>
13 #include <string>
14 #include <sys/resource.h>
15 #include <sys/time.h>
16 #include <time.h>
17 #include <vector>
18 
19 namespace dballe {
20 namespace benchmark {
21 
22 struct Benchmark;
23 
25 struct Task
26 {
27  Task() {}
28  Task(const Task&) = delete;
29  Task(Task&&) = delete;
30  virtual ~Task() {}
31  Task& operator=(const Task&) = delete;
32  Task& operator=(Task&&) = delete;
33 
34  virtual const char* name() const = 0;
35 
37  virtual void setup() {}
38 
45  virtual void run_once() = 0;
46 
48  virtual void teardown() {}
49 };
50 
51 struct Progress;
52 
53 struct Timeit
54 {
55  std::string task_name;
57  unsigned repetitions = 1;
58  struct timespec time_at_start;
59  struct timespec time_at_end;
60  struct rusage res_at_start;
61  struct rusage res_at_end;
62 
63  void run(Progress& progress, Task& task);
64 };
65 
66 struct Throughput
67 {
68  std::string task_name;
71  double run_time = 0.5;
72  unsigned times_run = 0;
73 
74  void run(Progress& progress, Task& task);
75 };
76 
78 struct Progress
79 {
80  virtual ~Progress() {}
81 
82  virtual void start_timeit(const Timeit& t) = 0;
83  virtual void end_timeit(const Timeit& t) = 0;
84 
85  virtual void start_throughput(const Throughput& t) = 0;
86  virtual void end_throughput(const Throughput& t) = 0;
87 
88  virtual void test_failed(const Task& t, std::exception& e) = 0;
89 };
90 
96 {
97  FILE* out;
98  FILE* err;
99 
100  BasicProgress(FILE* out = stdout, FILE* err = stderr);
101 
102  void start_timeit(const Timeit& t) override;
103  void end_timeit(const Timeit& t) override;
104 
105  void start_throughput(const Throughput& t) override;
106  void end_throughput(const Throughput& t) override;
107 
108  void test_failed(const Task& t, std::exception& e) override;
109 };
110 
114 struct Benchmark
115 {
117  std::shared_ptr<Progress> progress;
118 
120  std::vector<Timeit> timeit_tasks;
121 
123  std::vector<Throughput> throughput_tasks;
124 
125  Benchmark();
126  virtual ~Benchmark();
127 
129  void timeit(Task& task, unsigned repetitions = 1);
130 
132  void throughput(Task& task, double run_time = 0.5);
133 
135  void print_timings();
136 };
137 
141 struct Messages
142  : public std::vector<std::vector<std::shared_ptr<dballe::Message>>>
143 {
144  void load(const std::string& pathname,
145  dballe::Encoding encoding = dballe::Encoding::BUFR,
146  const char* codec_options = "accurate");
147 
148  // Copy the first \a size messages, change their datetime, and append them
149  // to the vector
150  void duplicate(size_t size, const Datetime& datetime);
151 };
152 
153 struct Whitelist : protected std::vector<std::string>
154 {
155  Whitelist(int argc, const char* argv[]);
156 
157  bool has(const std::string& val);
158 };
159 
160 } // namespace benchmark
161 } // namespace dballe
162 
163 #endif
double run_time
How many seconds to run the task to see how many times per second it runs.
Definition: benchmark.h:71
virtual void run_once()=0
Run the task once.
void print_timings()
Print timings to stdout.
Basic progress implementation writing progress information to the given output stream.
Definition: benchmark.h:95
void timeit(Task &task, unsigned repetitions=1)
Run the benchmark and collect timings.
std::shared_ptr< Progress > progress
Progress indicator.
Definition: benchmark.h:117
Definition: cmdline.h:18
Base class for all benchmarks.
Definition: benchmark.h:114
Container for parsed messages used for benchmarking.
Definition: benchmark.h:141
virtual void teardown()
Clean up after the task has been measured.
Definition: benchmark.h:48
Definition: benchmark.h:53
virtual void setup()
Set up the environment for running run_once()
Definition: benchmark.h:37
Definition: benchmark.h:153
One task to be measured.
Definition: benchmark.h:25
void throughput(Task &task, double run_time=0.5)
Run the benchmark and collect timings.
Date and time.
Definition: types.h:163
Definition: benchmark.h:66
std::vector< Throughput > throughput_tasks
Tasks for which we time their throughput.
Definition: benchmark.h:123
unsigned repetitions
How many times to repeat the task for measuring how long it takes.
Definition: benchmark.h:57
std::vector< Timeit > timeit_tasks
Tasks for which we time their duration.
Definition: benchmark.h:120
Notify of progress during benchmark execution.
Definition: benchmark.h:78