benchmark  1.9.4
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 <memory>
19 #include <thread>
20 #include <vector>
21 
22 #include "benchmark_api_internal.h"
23 #include "perf_counters.h"
24 #include "thread_manager.h"
25 
26 namespace benchmark {
27 
28 namespace internal {
29 
30 extern MemoryManager* memory_manager;
31 extern ProfilerManager* profiler_manager;
32 
33 struct RunResults {
34  std::vector<BenchmarkReporter::Run> non_aggregates;
35  std::vector<BenchmarkReporter::Run> aggregates_only;
36 
37  bool display_report_aggregates_only = false;
38  bool file_report_aggregates_only = false;
39 };
40 
41 struct BENCHMARK_EXPORT BenchTimeType {
42  enum { UNSPECIFIED, ITERS, TIME } tag;
43  union {
44  IterationCount iters;
45  double time;
46  };
47 };
48 
49 BENCHMARK_EXPORT
50 BenchTimeType ParseBenchMinTime(const std::string& value);
51 
53  public:
56  BenchmarkReporter::PerFamilyRunReports* reports_for_family);
57 
58  int GetNumRepeats() const { return repeats; }
59 
60  bool HasRepeatsRemaining() const {
61  return GetNumRepeats() != num_repetitions_done;
62  }
63 
64  void DoOneRepetition();
65 
66  RunResults&& GetResults();
67 
68  BenchmarkReporter::PerFamilyRunReports* GetReportsForFamily() const {
69  return reports_for_family;
70  }
71 
72  double GetMinTime() const { return min_time; }
73 
74  bool HasExplicitIters() const { return has_explicit_iteration_count; }
75 
76  IterationCount GetIters() const { return iters; }
77 
78  private:
79  RunResults run_results;
80 
82  BenchmarkReporter::PerFamilyRunReports* reports_for_family;
83 
84  BenchTimeType parsed_benchtime_flag;
85  const double min_time;
86  const double min_warmup_time;
87  bool warmup_done;
88  const int repeats;
89  const bool has_explicit_iteration_count;
90 
91  int num_repetitions_done = 0;
92 
93  std::unique_ptr<ThreadRunnerBase> thread_runner;
94 
95  IterationCount iters; // preserved between repetitions!
96  // So only the first repetition has to find/calculate it,
97  // the other repetitions will just use that precomputed iteration count.
98 
99  PerfCountersMeasurement* const perf_counters_measurement_ptr = nullptr;
100 
101  struct IterationResults {
103  IterationCount iters;
104  double seconds;
105  };
106  IterationResults DoNIterations();
107 
108  MemoryManager::Result RunMemoryManager(IterationCount memory_iterations);
109 
110  void RunProfilerManager(IterationCount profile_iterations);
111 
112  IterationCount PredictNumItersNeeded(const IterationResults& i) const;
113 
114  bool ShouldReportIterationResults(const IterationResults& i) const;
115 
116  double GetMinTimeToApply() const;
117 
118  void FinishWarmUp(const IterationCount& i);
119 
120  void RunWarmUp();
121 };
122 
123 } // namespace internal
124 
125 } // end namespace benchmark
126 
127 #endif // BENCHMARK_RUNNER_H_
Definition: benchmark_api_internal.h:18
Definition: benchmark_runner.h:52
Definition: perf_counters.h:149
Definition: benchmark.h:417
Definition: benchmark_runner.h:41
Definition: benchmark_runner.h:33
Definition: thread_manager.h:24