benchmark  1.9.2
benchmark_runner.h
1 // Copyright 2015 Google Inc. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef BENCHMARK_RUNNER_H_
16 #define BENCHMARK_RUNNER_H_
17 
18 #include <thread>
19 #include <vector>
20 
21 #include "benchmark_api_internal.h"
22 #include "perf_counters.h"
23 #include "thread_manager.h"
24 
25 namespace benchmark {
26 
27 namespace internal {
28 
29 extern MemoryManager* memory_manager;
30 extern ProfilerManager* profiler_manager;
31 
32 struct RunResults {
33  std::vector<BenchmarkReporter::Run> non_aggregates;
34  std::vector<BenchmarkReporter::Run> aggregates_only;
35 
36  bool display_report_aggregates_only = false;
37  bool file_report_aggregates_only = false;
38 };
39 
40 struct BENCHMARK_EXPORT BenchTimeType {
41  enum { UNSPECIFIED, ITERS, TIME } tag;
42  union {
43  IterationCount iters;
44  double time;
45  };
46 };
47 
48 BENCHMARK_EXPORT
49 BenchTimeType ParseBenchMinTime(const std::string& value);
50 
52  public:
55  BenchmarkReporter::PerFamilyRunReports* reports_for_family);
56 
57  int GetNumRepeats() const { return repeats; }
58 
59  bool HasRepeatsRemaining() const {
60  return GetNumRepeats() != num_repetitions_done;
61  }
62 
63  void DoOneRepetition();
64 
65  RunResults&& GetResults();
66 
67  BenchmarkReporter::PerFamilyRunReports* GetReportsForFamily() const {
68  return reports_for_family;
69  }
70 
71  double GetMinTime() const { return min_time; }
72 
73  bool HasExplicitIters() const { return has_explicit_iteration_count; }
74 
75  IterationCount GetIters() const { return iters; }
76 
77  private:
78  RunResults run_results;
79 
81  BenchmarkReporter::PerFamilyRunReports* reports_for_family;
82 
83  BenchTimeType parsed_benchtime_flag;
84  const double min_time;
85  const double min_warmup_time;
86  bool warmup_done;
87  const int repeats;
88  const bool has_explicit_iteration_count;
89 
90  int num_repetitions_done = 0;
91 
92  std::vector<std::thread> pool;
93 
94  IterationCount iters; // preserved between repetitions!
95  // So only the first repetition has to find/calculate it,
96  // the other repetitions will just use that precomputed iteration count.
97 
98  PerfCountersMeasurement* const perf_counters_measurement_ptr = nullptr;
99 
100  struct IterationResults {
102  IterationCount iters;
103  double seconds;
104  };
105  IterationResults DoNIterations();
106 
107  MemoryManager::Result RunMemoryManager(IterationCount memory_iterations);
108 
109  void RunProfilerManager(IterationCount profile_iterations);
110 
111  IterationCount PredictNumItersNeeded(const IterationResults& i) const;
112 
113  bool ShouldReportIterationResults(const IterationResults& i) const;
114 
115  double GetMinTimeToApply() const;
116 
117  void FinishWarmUp(const IterationCount& i);
118 
119  void RunWarmUp();
120 };
121 
122 } // namespace internal
123 
124 } // end namespace benchmark
125 
126 #endif // BENCHMARK_RUNNER_H_
Definition: benchmark_api_internal.h:18
Definition: benchmark_runner.h:51
Definition: perf_counters.h:149
Definition: benchmark.h:414
Definition: benchmark_runner.h:40
Definition: benchmark_runner.h:32
Definition: thread_manager.h:39