benchmark.h
Go to the documentation of this file.
#ifndef WREPORT_BENCHMARK_H
#define WREPORT_BENCHMARK_H
#include <string>
#include <vector>
#include <functional>
#include <cstdio>
namespace wreport {
namespace benchmark {
struct Benchmark;
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);
};
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;
};
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;
};
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();
virtual void setup_main() {}
virtual void teardown_main() {}
virtual void setup_iteration() {}
virtual void teardown_iteration() {}
void run(Progress& progress);
void print_timings();
virtual void main() = 0;
};
struct Registry
{
std::vector<Benchmark*> benchmarks;
void add(Benchmark* b);
static Registry& get();
static void basic_run(int argc, const char* argv[]);
};
}
}
#endif
wreport
String functions.
Definition benchmark.h:13
wreport::benchmark::Benchmark
Base class for all benchmarks.
Definition benchmark.h:75
wreport::benchmark::Benchmark::teardown_main
virtual void teardown_main()
Tear down the environment for this benchmark.
Definition benchmark.h:100
wreport::benchmark::Benchmark::main
virtual void main()=0
Main body of this benchmark.
wreport::benchmark::Benchmark::teardown_iteration
virtual void teardown_iteration()
Tear down the environment for an iteration of this benchmark.
Definition benchmark.h:114
wreport::benchmark::Benchmark::print_timings
void print_timings()
Print timings to stdout.
wreport::benchmark::Benchmark::setup_main
virtual void setup_main()
Set up the environment for this benchmark.
Definition benchmark.h:93
wreport::benchmark::Benchmark::run
void run(Progress &progress)
Run the benchmark and collect timings.
wreport::benchmark::Benchmark::setup_iteration
virtual void setup_iteration()
Set up the environment for an iteration of this benchmark.
Definition benchmark.h:107
wreport::benchmark::Progress
Notify of progress during benchmark execution.
Definition benchmark.h:41
wreport::benchmark::Registry
Collect all existing benchmarks.
Definition benchmark.h:128
wreport::benchmark::Registry::add
void add(Benchmark *b)
Add a benchmark to this registry.
wreport::benchmark::Registry::basic_run
static void basic_run(int argc, const char *argv[])
Basic implementation of a main function that runs all benchmarks linked into the program.
wreport::benchmark::Registry::get
static Registry & get()
Get the static instance of the registry.
wreport::benchmark::Task
Collect timings for one task.
Definition benchmark.h:20