libdballe 9.6
benchmark.h
Go to the documentation of this file.
1#ifndef DBALLE_CORE_BENCHMARK_H
2#define DBALLE_CORE_BENCHMARK_H
3
8#include <string>
9#include <vector>
10#include <functional>
11#include <memory>
12#include <cstdio>
13#include <time.h>
14#include <sys/time.h>
15#include <sys/resource.h>
16#include <dballe/message.h>
17#include <dballe/file.h>
18
19namespace dballe {
20namespace benchmark {
21
22struct Benchmark;
23
25struct 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
51struct Progress;
52
53struct 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
67{
68 std::string task_name;
70 double run_time = 0.5;
71 unsigned times_run = 0;
72
73 void run(Progress& progress, Task& task);
74};
75
76
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
91
97{
98 FILE* out;
99 FILE* err;
100
101 BasicProgress(FILE* out=stdout, FILE* err=stderr);
102
103 void start_timeit(const Timeit& t) override;
104 void end_timeit(const Timeit& t) override;
105
106 void start_throughput(const Throughput& t) override;
107 void end_throughput(const Throughput& t) override;
108
109 void test_failed(const Task& t, std::exception& e) override;
110};
111
112
117{
119 std::shared_ptr<Progress> progress;
120
122 std::vector<Timeit> timeit_tasks;
123
125 std::vector<Throughput> throughput_tasks;
126
127
128 Benchmark();
129 virtual ~Benchmark();
130
132 void timeit(Task& task, unsigned repetitions=1);
133
135 void throughput(Task& task, double run_time=0.5);
136
139};
140
141
145struct Messages : public std::vector<std::vector<std::shared_ptr<dballe::Message>>>
146{
147 void load(const std::string& pathname, dballe::Encoding encoding=dballe::Encoding::BUFR, const char* codec_options="accurate");
148
149 // Copy the first \a size messages, change their datetime, and append them
150 // to the vector
151 void duplicate(size_t size, const Datetime& datetime);
152};
153
154struct Whitelist : protected std::vector<std::string>
155{
156 Whitelist(int argc, const char* argv[]);
157
158 bool has(const std::string& val);
159};
160
161}
162}
163
164#endif
Date and time.
Definition: types.h:165
Basic progress implementation writing progress information to the given output stream.
Definition: benchmark.h:97
Base class for all benchmarks.
Definition: benchmark.h:117
std::vector< Throughput > throughput_tasks
Tasks for which we time their throughput.
Definition: benchmark.h:125
void print_timings()
Print timings to stdout.
void throughput(Task &task, double run_time=0.5)
Run the benchmark and collect timings.
std::shared_ptr< Progress > progress
Progress indicator.
Definition: benchmark.h:119
std::vector< Timeit > timeit_tasks
Tasks for which we time their duration.
Definition: benchmark.h:122
void timeit(Task &task, unsigned repetitions=1)
Run the benchmark and collect timings.
Container for parsed messages used for benchmarking.
Definition: benchmark.h:146
Notify of progress during benchmark execution.
Definition: benchmark.h:79
One task to be measured.
Definition: benchmark.h:26
virtual void teardown()
Clean up after the task has been measured.
Definition: benchmark.h:48
virtual void setup()
Set up the environment for running run_once()
Definition: benchmark.h:37
virtual void run_once()=0
Run the task once.
Definition: benchmark.h:67
double run_time
How many seconds to run the task to see how many times per second it runs.
Definition: benchmark.h:70
Definition: benchmark.h:54
unsigned repetitions
How many times to repeat the task for measuring how long it takes.
Definition: benchmark.h:57
Definition: benchmark.h:155