benchmark/bindings/python/google_benchmark/benchmark.cc
Go to the documentation of this file.
1 // Benchmark for Python.
2 
3 #include <map>
4 #include <string>
5 #include <vector>
6 
7 #include "pybind11/operators.h"
8 #include "pybind11/pybind11.h"
9 #include "pybind11/stl.h"
10 #include "pybind11/stl_bind.h"
11 
12 #include "benchmark/benchmark.h"
13 
15 
16 namespace {
17 namespace py = ::pybind11;
18 
19 std::vector<std::string> Initialize(const std::vector<std::string>& argv) {
20  // The `argv` pointers here become invalid when this function returns, but
21  // benchmark holds the pointer to `argv[0]`. We create a static copy of it
22  // so it persists, and replace the pointer below.
23  static std::string executable_name(argv[0]);
24  std::vector<char*> ptrs;
25  ptrs.reserve(argv.size());
26  for (auto& arg : argv) {
27  ptrs.push_back(const_cast<char*>(arg.c_str()));
28  }
29  ptrs[0] = const_cast<char*>(executable_name.c_str());
30  int argc = static_cast<int>(argv.size());
31  benchmark::Initialize(&argc, ptrs.data());
32  std::vector<std::string> remaining_argv;
33  remaining_argv.reserve(argc);
34  for (int i = 0; i < argc; ++i) {
35  remaining_argv.emplace_back(ptrs[i]);
36  }
37  return remaining_argv;
38 }
39 
41  py::function f) {
43  name, [f](benchmark::State& state) { f(&state); });
44 }
45 
46 PYBIND11_MODULE(_benchmark, m) {
47  using benchmark::TimeUnit;
48  py::enum_<TimeUnit>(m, "TimeUnit")
49  .value("kNanosecond", TimeUnit::kNanosecond)
50  .value("kMicrosecond", TimeUnit::kMicrosecond)
51  .value("kMillisecond", TimeUnit::kMillisecond)
52  .value("kSecond", TimeUnit::kSecond)
53  .export_values();
54 
55  using benchmark::BigO;
56  py::enum_<BigO>(m, "BigO")
57  .value("oNone", BigO::oNone)
58  .value("o1", BigO::o1)
59  .value("oN", BigO::oN)
60  .value("oNSquared", BigO::oNSquared)
61  .value("oNCubed", BigO::oNCubed)
62  .value("oLogN", BigO::oLogN)
63  .value("oNLogN", BigO::oLogN)
64  .value("oAuto", BigO::oAuto)
65  .value("oLambda", BigO::oLambda)
66  .export_values();
67 
69  py::class_<Benchmark>(m, "Benchmark")
70  // For methods returning a pointer tor the current object, reference
71  // return policy is used to ask pybind not to take ownership oof the
72  // returned object and avoid calling delete on it.
73  // https://pybind11.readthedocs.io/en/stable/advanced/functions.html#return-value-policies
74  //
75  // For methods taking a const std::vector<...>&, a copy is created
76  // because a it is bound to a Python list.
77  // https://pybind11.readthedocs.io/en/stable/advanced/cast/stl.html
78  .def("unit", &Benchmark::Unit, py::return_value_policy::reference)
79  .def("arg", &Benchmark::Arg, py::return_value_policy::reference)
80  .def("args", &Benchmark::Args, py::return_value_policy::reference)
81  .def("range", &Benchmark::Range, py::return_value_policy::reference,
82  py::arg("start"), py::arg("limit"))
83  .def("dense_range", &Benchmark::DenseRange,
84  py::return_value_policy::reference, py::arg("start"),
85  py::arg("limit"), py::arg("step") = 1)
86  .def("ranges", &Benchmark::Ranges, py::return_value_policy::reference)
87  .def("args_product", &Benchmark::ArgsProduct,
88  py::return_value_policy::reference)
89  .def("arg_name", &Benchmark::ArgName, py::return_value_policy::reference)
90  .def("arg_names", &Benchmark::ArgNames,
91  py::return_value_policy::reference)
92  .def("range_pair", &Benchmark::RangePair,
93  py::return_value_policy::reference, py::arg("lo1"), py::arg("hi1"),
94  py::arg("lo2"), py::arg("hi2"))
95  .def("range_multiplier", &Benchmark::RangeMultiplier,
96  py::return_value_policy::reference)
97  .def("min_time", &Benchmark::MinTime, py::return_value_policy::reference)
98  .def("iterations", &Benchmark::Iterations,
99  py::return_value_policy::reference)
100  .def("repetitions", &Benchmark::Repetitions,
101  py::return_value_policy::reference)
102  .def("report_aggregates_only", &Benchmark::ReportAggregatesOnly,
103  py::return_value_policy::reference, py::arg("value") = true)
104  .def("display_aggregates_only", &Benchmark::DisplayAggregatesOnly,
105  py::return_value_policy::reference, py::arg("value") = true)
106  .def("measure_process_cpu_time", &Benchmark::MeasureProcessCPUTime,
107  py::return_value_policy::reference)
108  .def("use_real_time", &Benchmark::UseRealTime,
109  py::return_value_policy::reference)
110  .def("use_manual_time", &Benchmark::UseManualTime,
111  py::return_value_policy::reference)
112  .def(
113  "complexity",
115  py::return_value_policy::reference,
116  py::arg("complexity") = benchmark::oAuto);
117 
118  using benchmark::Counter;
119  py::class_<Counter> py_counter(m, "Counter");
120 
121  py::enum_<Counter::Flags>(py_counter, "Flags")
122  .value("kDefaults", Counter::Flags::kDefaults)
123  .value("kIsRate", Counter::Flags::kIsRate)
124  .value("kAvgThreads", Counter::Flags::kAvgThreads)
125  .value("kAvgThreadsRate", Counter::Flags::kAvgThreadsRate)
126  .value("kIsIterationInvariant", Counter::Flags::kIsIterationInvariant)
127  .value("kIsIterationInvariantRate",
128  Counter::Flags::kIsIterationInvariantRate)
129  .value("kAvgIterations", Counter::Flags::kAvgIterations)
130  .value("kAvgIterationsRate", Counter::Flags::kAvgIterationsRate)
131  .value("kInvert", Counter::Flags::kInvert)
132  .export_values()
133  .def(py::self | py::self);
134 
135  py::enum_<Counter::OneK>(py_counter, "OneK")
136  .value("kIs1000", Counter::OneK::kIs1000)
137  .value("kIs1024", Counter::OneK::kIs1024)
138  .export_values();
139 
140  py_counter
141  .def(py::init<double, Counter::Flags, Counter::OneK>(),
142  py::arg("value") = 0., py::arg("flags") = Counter::kDefaults,
143  py::arg("k") = Counter::kIs1000)
144  .def(py::init([](double value) { return Counter(value); }))
145  .def_readwrite("value", &Counter::value)
146  .def_readwrite("flags", &Counter::flags)
147  .def_readwrite("oneK", &Counter::oneK);
148  py::implicitly_convertible<py::float_, Counter>();
149  py::implicitly_convertible<py::int_, Counter>();
150 
151  py::bind_map<benchmark::UserCounters>(m, "UserCounters");
152 
153  using benchmark::State;
154  py::class_<State>(m, "State")
155  .def("__bool__", &State::KeepRunning)
156  .def_property_readonly("keep_running", &State::KeepRunning)
157  .def("pause_timing", &State::PauseTiming)
158  .def("resume_timing", &State::ResumeTiming)
159  .def("skip_with_error", &State::SkipWithError)
160  .def_property_readonly("error_occurred", &State::error_occurred)
161  .def("set_iteration_time", &State::SetIterationTime)
162  .def_property("bytes_processed", &State::bytes_processed,
163  &State::SetBytesProcessed)
164  .def_property("complexity_n", &State::complexity_length_n,
165  &State::SetComplexityN)
166  .def_property("items_processed", &State::items_processed,
167  &State::SetItemsProcessed)
168  .def("set_label", (void(State::*)(const char*)) & State::SetLabel)
169  .def("range", &State::range, py::arg("pos") = 0)
170  .def_property_readonly("iterations", &State::iterations)
171  .def_readwrite("counters", &State::counters)
172  .def_property_readonly("thread_index", &State::thread_index)
173  .def_property_readonly("threads", &State::threads);
174 
175  m.def("Initialize", Initialize);
176  m.def("RegisterBenchmark", RegisterBenchmark,
177  py::return_value_policy::reference);
178  m.def("RunSpecifiedBenchmarks",
180 };
181 } // namespace
benchmark::kSecond
@ kSecond
Definition: benchmark/include/benchmark/benchmark.h:443
Iterations
Iterations(1) -> Threads(1) ->MeasureProcessCPUTime() ->UseRealTime()
benchmark::oNSquared
@ oNSquared
Definition: benchmark/include/benchmark/benchmark.h:449
init
const char * init
Definition: upb/upb/bindings/lua/main.c:49
benchmark::oNone
@ oNone
Definition: benchmark/include/benchmark/benchmark.h:449
grpc::testing::RangePair
RangePair(524288, 524288, 1, 1024)
capstone.range
range
Definition: third_party/bloaty/third_party/capstone/bindings/python/capstone/__init__.py:6
Complexity
Complexity(benchmark::oN) -> Ranges(
Definition: benchmark/test/complexity_test.cc:211
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
ArgNames
ArgNames({"first", "", "third"})
setup.name
name
Definition: setup.py:542
Arg
Arg(64) -> Arg(128) ->Arg(256) ->Arg(512) ->Arg(1024) ->Arg(1536) ->Arg(2048) ->Arg(3072) ->Arg(4096) ->Arg(5120) ->Arg(6144) ->Arg(7168)
benchmark::RegisterBenchmark
internal::Benchmark * RegisterBenchmark(const char *name, internal::Function *fn)
Definition: benchmark/include/benchmark/benchmark.h:1093
benchmark::TimeUnit
TimeUnit
Definition: benchmark/include/benchmark/benchmark.h:443
absl::ABSL_NAMESPACE_BEGIN::DenseRange
DenseRange(0, integral_pow(2, 2) - 1)
threads
static uv_thread_t * threads
Definition: threadpool.c:38
benchmark::oLambda
@ oLambda
Definition: benchmark/include/benchmark/benchmark.h:449
benchmark::kMicrosecond
@ kMicrosecond
Definition: benchmark/include/benchmark/benchmark.h:443
benchmark::RunSpecifiedBenchmarks
size_t RunSpecifiedBenchmarks()
Definition: benchmark/src/benchmark.cc:437
autogen_x86imm.f
f
Definition: autogen_x86imm.py:9
hpack_encoder_fixtures::Args
Args({0, 16384})
benchmark::kMillisecond
@ kMillisecond
Definition: benchmark/include/benchmark/benchmark.h:443
benchmark::oAuto
@ oAuto
Definition: benchmark/include/benchmark/benchmark.h:449
benchmark::oN
@ oN
Definition: benchmark/include/benchmark/benchmark.h:449
arg
Definition: cmdline.cc:40
benchmark::Initialize
void Initialize(int *argc, char **argv)
Definition: benchmark/src/benchmark.cc:602
benchmark::Counter
Definition: benchmark/include/benchmark/benchmark.h:382
benchmark::internal::Benchmark
Definition: benchmark/include/benchmark/benchmark.h:834
value
const char * value
Definition: hpack_parser_table.cc:165
benchmark::UserCounters
std::map< std::string, Counter > UserCounters
Definition: benchmark/include/benchmark/benchmark.h:439
PYBIND11_MAKE_OPAQUE
PYBIND11_MAKE_OPAQUE(benchmark::UserCounters)
Range
Range(0, 512)
benchmark::oLogN
@ oLogN
Definition: benchmark/include/benchmark/benchmark.h:449
benchmark::kNanosecond
@ kNanosecond
Definition: benchmark/include/benchmark/benchmark.h:443
benchmark::State
Definition: benchmark/include/benchmark/benchmark.h:503
arg
struct arg arg
state
Definition: bloaty/third_party/zlib/contrib/blast/blast.c:41
benchmark::BigO
BigO
Definition: benchmark/include/benchmark/benchmark.h:449
benchmark::oNCubed
@ oNCubed
Definition: benchmark/include/benchmark/benchmark.h:449
flags
uint32_t flags
Definition: retry_filter.cc:632
function
std::function< bool(GrpcTool *, int, const char **, const CliCredentials &, GrpcToolOutputCallback)> function
Definition: grpc_tool.cc:250
self
PHP_PROTO_OBJECT_FREE_END PHP_PROTO_OBJECT_DTOR_END intern self
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/map.c:543
regress.m
m
Definition: regress/regress.py:25
compare.Benchmark
def Benchmark(outbase, bench_cpu=True, runs=12, fasttable=False)
Definition: upb/benchmarks/compare.py:56
benchmark::o1
@ o1
Definition: benchmark/include/benchmark/benchmark.h:449
RangeMultiplier
RangeMultiplier(2) -> Range(1<< 10, 1<< 16) ->Complexity(benchmark::oN)
Definition: benchmark/test/complexity_test.cc:131
Repetitions
Repetitions(3) -> ReportAggregatesOnly() ->Unit(benchmark::kMicrosecond)
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:46