1 | #include "benchmark_api_internal.h" |
2 | |
3 | #include <cinttypes> |
4 | |
5 | #include "string_util.h" |
6 | |
7 | namespace benchmark { |
8 | namespace internal { |
9 | |
10 | BenchmarkInstance::BenchmarkInstance(Benchmark* benchmark, int family_idx, |
11 | int per_family_instance_idx, |
12 | const std::vector<int64_t>& args, |
13 | int thread_count) |
14 | : benchmark_(*benchmark), |
15 | family_index_(family_idx), |
16 | per_family_instance_index_(per_family_instance_idx), |
17 | aggregation_report_mode_(benchmark_.aggregation_report_mode_), |
18 | args_(args), |
19 | time_unit_(benchmark_.GetTimeUnit()), |
20 | measure_process_cpu_time_(benchmark_.measure_process_cpu_time_), |
21 | use_real_time_(benchmark_.use_real_time_), |
22 | use_manual_time_(benchmark_.use_manual_time_), |
23 | complexity_(benchmark_.complexity_), |
24 | complexity_lambda_(benchmark_.complexity_lambda_), |
25 | statistics_(benchmark_.statistics_), |
26 | repetitions_(benchmark_.repetitions_), |
27 | min_time_(benchmark_.min_time_), |
28 | min_warmup_time_(benchmark_.min_warmup_time_), |
29 | iterations_(benchmark_.iterations_), |
30 | threads_(thread_count) { |
31 | name_.function_name = benchmark_.name_; |
32 | |
33 | size_t arg_i = 0; |
34 | for (const auto& arg : args) { |
35 | if (!name_.args.empty()) { |
36 | name_.args += '/'; |
37 | } |
38 | |
39 | if (arg_i < benchmark->arg_names_.size()) { |
40 | const auto& arg_name = benchmark_.arg_names_[arg_i]; |
41 | if (!arg_name.empty()) { |
42 | name_.args += StrFormat(format: "%s:" , arg_name.c_str()); |
43 | } |
44 | } |
45 | |
46 | name_.args += StrFormat(format: "%" PRId64, arg); |
47 | ++arg_i; |
48 | } |
49 | |
50 | if (!IsZero(n: benchmark->min_time_)) { |
51 | name_.min_time = StrFormat(format: "min_time:%0.3f" , benchmark_.min_time_); |
52 | } |
53 | |
54 | if (!IsZero(n: benchmark->min_warmup_time_)) { |
55 | name_.min_warmup_time = |
56 | StrFormat(format: "min_warmup_time:%0.3f" , benchmark_.min_warmup_time_); |
57 | } |
58 | |
59 | if (benchmark_.iterations_ != 0) { |
60 | name_.iterations = StrFormat( |
61 | format: "iterations:%lu" , static_cast<unsigned long>(benchmark_.iterations_)); |
62 | } |
63 | |
64 | if (benchmark_.repetitions_ != 0) { |
65 | name_.repetitions = StrFormat(format: "repeats:%d" , benchmark_.repetitions_); |
66 | } |
67 | |
68 | if (benchmark_.measure_process_cpu_time_) { |
69 | name_.time_type = "process_time" ; |
70 | } |
71 | |
72 | if (benchmark_.use_manual_time_) { |
73 | if (!name_.time_type.empty()) { |
74 | name_.time_type += '/'; |
75 | } |
76 | name_.time_type += "manual_time" ; |
77 | } else if (benchmark_.use_real_time_) { |
78 | if (!name_.time_type.empty()) { |
79 | name_.time_type += '/'; |
80 | } |
81 | name_.time_type += "real_time" ; |
82 | } |
83 | |
84 | if (!benchmark_.thread_counts_.empty()) { |
85 | name_.threads = StrFormat(format: "threads:%d" , threads_); |
86 | } |
87 | |
88 | setup_ = benchmark_.setup_; |
89 | teardown_ = benchmark_.teardown_; |
90 | } |
91 | |
92 | State BenchmarkInstance::Run( |
93 | IterationCount iters, int thread_id, internal::ThreadTimer* timer, |
94 | internal::ThreadManager* manager, |
95 | internal::PerfCountersMeasurement* perf_counters_measurement) const { |
96 | State st(name_.function_name, iters, args_, thread_id, threads_, timer, |
97 | manager, perf_counters_measurement); |
98 | benchmark_.Run(state&: st); |
99 | return st; |
100 | } |
101 | |
102 | void BenchmarkInstance::Setup() const { |
103 | if (setup_) { |
104 | State st(name_.function_name, /*iters*/ 1, args_, /*thread_id*/ 0, threads_, |
105 | nullptr, nullptr, nullptr); |
106 | setup_(st); |
107 | } |
108 | } |
109 | |
110 | void BenchmarkInstance::Teardown() const { |
111 | if (teardown_) { |
112 | State st(name_.function_name, /*iters*/ 1, args_, /*thread_id*/ 0, threads_, |
113 | nullptr, nullptr, nullptr); |
114 | teardown_(st); |
115 | } |
116 | } |
117 | } // namespace internal |
118 | } // namespace benchmark |
119 | |