1 | //===----------------------------------------------------------------------===// |
2 | // |
3 | // DuckDB |
4 | // |
5 | // benchmark.hpp |
6 | // |
7 | // Author: Mark Raasveldt |
8 | // |
9 | //===----------------------------------------------------------------------===// |
10 | |
11 | #pragma once |
12 | |
13 | #include <memory> |
14 | #include <string> |
15 | #include <vector> |
16 | |
17 | namespace duckdb { |
18 | |
19 | using std::string; |
20 | using std::unique_ptr; |
21 | using std::vector; |
22 | |
23 | //! Base class for any state that has to be kept by a Benchmark |
24 | struct BenchmarkState { |
25 | virtual ~BenchmarkState() { |
26 | } |
27 | }; |
28 | |
29 | //! The base Benchmark class is a base class that is used to create and register |
30 | //! new benchmarks |
31 | class Benchmark { |
32 | constexpr static size_t DEFAULT_NRUNS = 5; |
33 | constexpr static size_t DEFAULT_TIMEOUT = 30; |
34 | |
35 | Benchmark(Benchmark &) = delete; |
36 | |
37 | public: |
38 | //! The name of the benchmark |
39 | string name; |
40 | //! The benchmark group this benchmark belongs to |
41 | string group; |
42 | |
43 | Benchmark(bool register_benchmark, string name, string group); |
44 | |
45 | //! Initialize the benchmark state |
46 | virtual unique_ptr<BenchmarkState> Initialize() { |
47 | return nullptr; |
48 | } |
49 | //! Run the benchmark |
50 | virtual void Run(BenchmarkState *state) = 0; |
51 | //! Cleanup the benchmark, called after each Run |
52 | virtual void Cleanup(BenchmarkState *state) = 0; |
53 | //! Verify that the output of the benchmark was correct |
54 | virtual string Verify(BenchmarkState *state) = 0; |
55 | //! Finalize the benchmark runner |
56 | virtual void Finalize() { |
57 | } |
58 | //! Interrupt the benchmark because of a timeout |
59 | virtual void Interrupt(BenchmarkState *state) = 0; |
60 | //! Returns information about the benchmark |
61 | virtual string BenchmarkInfo() = 0; |
62 | |
63 | string GetInfo() { |
64 | return name + " - " + group + "\n" + BenchmarkInfo(); |
65 | } |
66 | |
67 | virtual string GetLogOutput(BenchmarkState *state) = 0; |
68 | |
69 | //! Whether or not Initialize() should be called once for every run or just |
70 | //! once |
71 | virtual bool RequireReinit() { |
72 | return false; |
73 | } |
74 | //! The amount of runs to do for this benchmark |
75 | virtual size_t NRuns() { |
76 | return DEFAULT_NRUNS; |
77 | } |
78 | //! The timeout for this benchmark (in seconds) |
79 | virtual size_t Timeout() { |
80 | return DEFAULT_TIMEOUT; |
81 | } |
82 | }; |
83 | |
84 | } // namespace duckdb |
85 | |