1 /**
2  * @file   benchmark.h
3  *
4  * @section LICENSE
5  *
6  * The MIT License
7  *
8  * @copyright Copyright (c) 2018-2021 TileDB, Inc.
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining a copy
11  * of this software and associated documentation files (the "Software"), to deal
12  * in the Software without restriction, including without limitation the rights
13  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14  * copies of the Software, and to permit persons to whom the Software is
15  * furnished to do so, subject to the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be included in
18  * all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26  * THE SOFTWARE.
27  *
28  * @section DESCRIPTION
29  *
30  * Declares common code for the benchmark programs.
31  */
32 
33 #ifndef TILEDB_BENCHMARK_H
34 #define TILEDB_BENCHMARK_H
35 
36 #include <cassert>
37 #include <string>
38 #include <vector>
39 
40 /**
41  * Base class for benchmarks.
42  */
43 class BenchmarkBase {
44  public:
45   /**
46    * Main method of the benchmark, which invokes setup, run, or teardown
47    * depending on the arguments given.
48    */
49   int main(int argc, char** argv);
50 
51   /**
52    * Benchmark setup (array creation, etc).
53    */
54   void setup_base();
55 
56   /** Benchmark cleanup (array removal, etc). */
57   void teardown_base();
58 
59   /** Benchmark run method to be timed. */
60   void run_base();
61 
62  protected:
63   /** Implemented by subclass: the setup phase. */
64   virtual void setup() = 0;
65 
66   /** Implemented by subclass: the cleanup phase. */
67   virtual void teardown() = 0;
68 
69   /**
70    * Implemented by subclass: anything that needs to happen in the same process
71    * as 'run' but should be excluded from the benchmark run time, e.g.
72    * query buffer allocation.
73    */
74   virtual void pre_run() = 0;
75 
76   /** Implemented by subclass: the run phase. */
77   virtual void run() = 0;
78 
79  private:
80   /**
81    * Prints metrics for a given task.
82    */
83   void print_task(
84       const std::string& name,
85       const uint64_t* ms,
86       const std::vector<uint64_t>* mem_samples_mb,
87       uint64_t baseline_mem_mb);
88 
89   /**
90    * Samples the current processes's used virtual memory every 50ms
91    * and stores it in 'mem_samples_mb'.
92    */
93   static void mem_sampling_thread_func(
94       bool* stop_mem_sampling, std::vector<uint64_t>* mem_samples_mb);
95 
96   /**
97    * Samples the current processes's used virtual memory and returns it.
98    */
99   static uint64_t sample_virt_mem_mb();
100 };
101 
102 #endif
103