/usr/include/wreport/benchmark.h is in libwreport-dev 3.6-1build2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | #ifndef WREPORT_BENCHMARK_H
#define WREPORT_BENCHMARK_H
/** @file
* Simple benchmark infrastructure.
*/
#include <string>
#include <vector>
#include <functional>
#include <cstdio>
namespace wreport {
namespace benchmark {
struct Benchmark;
/// Collect timings for one task
struct Task
{
// Unmanaged pointer to the benchmark we belong to
Benchmark* parent;
// Name of this task
std::string name;
// Number of time this task has run
unsigned run_count = 0;
// Total user time
clock_t utime = 0;
// Total system time
clock_t stime = 0;
Task(Benchmark* parent, const std::string& name);
// Run the given function and collect timings for it
void collect(std::function<void()> f);
};
/// Notify of progress during benchmark execution
struct Progress
{
virtual ~Progress() {}
virtual void start_benchmark(const Benchmark& b) = 0;
virtual void end_benchmark(const Benchmark& b) = 0;
virtual void start_iteration(const Benchmark& b, unsigned cur, unsigned total) = 0;
virtual void end_iteration(const Benchmark& b, unsigned cur, unsigned total) = 0;
virtual void test_failed(const Benchmark& b, std::exception& e) = 0;
};
/**
* Basic progress implementation writing progress information to the given
* output stream
*/
struct BasicProgress : Progress
{
FILE* out;
FILE* err;
BasicProgress(FILE* out=stdout, FILE* err=stderr);
void start_benchmark(const Benchmark& b) override;
void start_iteration(const Benchmark& b, unsigned cur, unsigned total) override;
void end_iteration(const Benchmark& b, unsigned cur, unsigned total) override;
void end_benchmark(const Benchmark& b) override;
void test_failed(const Benchmark& b, std::exception& e) override;
};
/**
* Base class for all benchmarks.
*/
struct Benchmark
{
// Name of this benchmark
std::string name;
// Number of repetitions
unsigned repetitions = 10;
// Unmanaged pointers to the tasks in this benchmark
std::vector<Task*> tasks;
// Main task, collecting timings for the toplevel run
Task task_main;
Benchmark(const std::string& name);
virtual ~Benchmark();
/**
* Set up the environment for this benchmark.
*
* This is run outside of timings. By default it does nothing.
*/
virtual void setup_main() {}
/**
* Tear down the environment for this benchmark.
*
* This is run outside of timings. By default it does nothing.
*/
virtual void teardown_main() {}
/**
* Set up the environment for an iteration of this benchmark.
*
* This is run outside of timings. By default it does nothing.
*/
virtual void setup_iteration() {}
/**
* Tear down the environment for an iteration of this benchmark.
*
* This is run outside of timings. By default it does nothing.
*/
virtual void teardown_iteration() {}
/// Run the benchmark and collect timings
void run(Progress& progress);
/// Print timings to stdout
void print_timings();
/// Main body of this benchmark
virtual void main() = 0;
};
/// Collect all existing benchmarks
struct Registry
{
std::vector<Benchmark*> benchmarks;
/// Add a benchmark to this registry
void add(Benchmark* b);
/**
* Get the static instance of the registry
*/
static Registry& get();
/**
* Basic implementation of a main function that runs all benchmarks linked
* into the program. This allows to make a benchmark runner tool with just
* this code:
*
* \code
* #include <wreport/benchmark.h>
*
* int main (int argc, const char* argv[])
* {
* wreport::benchmark::Registry::basic_run(argc, argv);
* }
* \endcode
*
* If you need different logic in your benchmark running code, you can use
* the source code of basic_run as a template for writing your own.
*/
static void basic_run(int argc, const char* argv[]);
};
}
}
#endif
|