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