Upgrade google-benchmark to v1.9.0
This project was upgraded with external_updater.
Usage: tools/external_updater/updater.sh update external/google-benchmark
For more info, check https://cs.android.com/android/platform/superproject/main/+/main:tools/external_updater/README.md
Test: TreeHugger
Change-Id: Ifeeddee4c4a4225046e6d6505329c78805487a23
diff --git a/.github/workflows/bazel.yml b/.github/workflows/bazel.yml
index a669cda..b50a8f6 100644
--- a/.github/workflows/bazel.yml
+++ b/.github/workflows/bazel.yml
@@ -17,7 +17,7 @@
- uses: actions/checkout@v4
- name: mount bazel cache
- uses: actions/cache@v3
+ uses: actions/cache@v4
env:
cache-name: bazel-cache
with:
diff --git a/.github/workflows/clang-format-lint.yml b/.github/workflows/clang-format-lint.yml
index c790a5a..8f089dc 100644
--- a/.github/workflows/clang-format-lint.yml
+++ b/.github/workflows/clang-format-lint.yml
@@ -10,7 +10,7 @@
steps:
- uses: actions/checkout@v4
- - uses: DoozyX/[email protected]
+ - uses: DoozyX/[email protected]
with:
source: './include/benchmark ./src ./test'
extensions: 'h,cc'
diff --git a/.github/workflows/pre-commit.yml b/.github/workflows/pre-commit.yml
index 5d65b99..8b217e9 100644
--- a/.github/workflows/pre-commit.yml
+++ b/.github/workflows/pre-commit.yml
@@ -27,7 +27,7 @@
- name: Install dependencies
run: python -m pip install ".[dev]"
- name: Cache pre-commit tools
- uses: actions/cache@v3
+ uses: actions/cache@v4
with:
path: |
${{ env.MYPY_CACHE_DIR }}
diff --git a/.github/workflows/wheels.yml b/.github/workflows/wheels.yml
index 591d709..1a00069 100644
--- a/.github/workflows/wheels.yml
+++ b/.github/workflows/wheels.yml
@@ -81,10 +81,11 @@
name: Publish google-benchmark wheels to PyPI
needs: [merge_wheels]
runs-on: ubuntu-latest
- permissions:
- id-token: write
steps:
- uses: actions/download-artifact@v4
with:
path: dist
- - uses: pypa/[email protected]
+ - uses: pypa/gh-action-pypi-publish@release/v1
+ with:
+ user: __token__
+ password: ${{ secrets.PYPI_PASSWORD }}
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index a019caf..99976d9 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -5,14 +5,14 @@
- id: buildifier
- id: buildifier-lint
- repo: https://github.com/pre-commit/mirrors-mypy
- rev: v1.10.0
+ rev: v1.11.0
hooks:
- id: mypy
types_or: [ python, pyi ]
args: [ "--ignore-missing-imports", "--scripts-are-modules" ]
- repo: https://github.com/astral-sh/ruff-pre-commit
- rev: v0.4.5
+ rev: v0.4.10
hooks:
- id: ruff
args: [ --fix, --exit-non-zero-on-fix ]
- - id: ruff-format
\ No newline at end of file
+ - id: ruff-format
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 216c1c9..40ff758 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,7 +1,7 @@
# Require CMake 3.10. If available, use the policies up to CMake 3.22.
cmake_minimum_required (VERSION 3.10...3.22)
-project (benchmark VERSION 1.8.5 LANGUAGES CXX)
+project (benchmark VERSION 1.9.0 LANGUAGES CXX)
option(BENCHMARK_ENABLE_TESTING "Enable testing of the benchmark library." ON)
option(BENCHMARK_ENABLE_EXCEPTIONS "Enable the use of exceptions in the benchmark library." ON)
diff --git a/METADATA b/METADATA
index 1f7cdfc..c138c5d 100644
--- a/METADATA
+++ b/METADATA
@@ -1,6 +1,6 @@
# This project was upgraded with external_updater.
# Usage: tools/external_updater/updater.sh update external/google-benchmark
-# For more info, check https://cs.android.com/android/platform/superproject/+/main:tools/external_updater/README.md
+# For more info, check https://cs.android.com/android/platform/superproject/main/+/main:tools/external_updater/README.md
name: "google-benchmark"
description: "A library to support the benchmarking of functions, similar to unit-tests."
@@ -8,13 +8,13 @@
license_type: NOTICE
last_upgrade_date {
year: 2024
- month: 7
- day: 24
+ month: 8
+ day: 28
}
homepage: "https://github.com/google/benchmark"
identifier {
type: "Git"
value: "https://github.com/google/benchmark.git"
- version: "v1.8.5"
+ version: "v1.9.0"
}
}
diff --git a/MODULE.bazel b/MODULE.bazel
index 8b98a7a..e4f170c 100644
--- a/MODULE.bazel
+++ b/MODULE.bazel
@@ -1,6 +1,6 @@
module(
name = "google_benchmark",
- version = "1.8.5",
+ version = "1.9.0",
)
bazel_dep(name = "bazel_skylib", version = "1.5.0")
@@ -38,4 +38,4 @@
# -- bazel_dep definitions -- #
-bazel_dep(name = "nanobind_bazel", version = "1.0.0", dev_dependency = True)
+bazel_dep(name = "nanobind_bazel", version = "2.1.0", dev_dependency = True)
diff --git a/bazel/benchmark_deps.bzl b/bazel/benchmark_deps.bzl
index 4fb45a5..cb908cd 100644
--- a/bazel/benchmark_deps.bzl
+++ b/bazel/benchmark_deps.bzl
@@ -45,7 +45,7 @@
new_git_repository(
name = "nanobind",
remote = "https://github.com/wjakob/nanobind.git",
- tag = "v1.8.0",
+ tag = "v1.9.2",
build_file = "@//bindings/python:nanobind.BUILD",
recursive_init_submodules = True,
)
diff --git a/bindings/python/google_benchmark/BUILD b/bindings/python/google_benchmark/BUILD
index 0c8e3c1..30e3893 100644
--- a/bindings/python/google_benchmark/BUILD
+++ b/bindings/python/google_benchmark/BUILD
@@ -1,4 +1,4 @@
-load("@nanobind_bazel//:build_defs.bzl", "nanobind_extension")
+load("@nanobind_bazel//:build_defs.bzl", "nanobind_extension", "nanobind_stubgen")
py_library(
name = "google_benchmark",
@@ -15,6 +15,12 @@
deps = ["//:benchmark"],
)
+nanobind_stubgen(
+ name = "benchmark_stubgen",
+ marker_file = "bindings/python/google_benchmark/py.typed",
+ module = ":_benchmark",
+)
+
py_test(
name = "example",
srcs = ["example.py"],
diff --git a/bindings/python/google_benchmark/benchmark.cc b/bindings/python/google_benchmark/benchmark.cc
index f444769..64ffb92 100644
--- a/bindings/python/google_benchmark/benchmark.cc
+++ b/bindings/python/google_benchmark/benchmark.cc
@@ -118,7 +118,7 @@
using benchmark::Counter;
nb::class_<Counter> py_counter(m, "Counter");
- nb::enum_<Counter::Flags>(py_counter, "Flags")
+ nb::enum_<Counter::Flags>(py_counter, "Flags", nb::is_arithmetic())
.value("kDefaults", Counter::Flags::kDefaults)
.value("kIsRate", Counter::Flags::kIsRate)
.value("kAvgThreads", Counter::Flags::kAvgThreads)
@@ -130,7 +130,9 @@
.value("kAvgIterationsRate", Counter::Flags::kAvgIterationsRate)
.value("kInvert", Counter::Flags::kInvert)
.export_values()
- .def(nb::self | nb::self);
+ .def("__or__", [](Counter::Flags a, Counter::Flags b) {
+ return static_cast<int>(a) | static_cast<int>(b);
+ });
nb::enum_<Counter::OneK>(py_counter, "OneK")
.value("kIs1000", Counter::OneK::kIs1000)
@@ -138,10 +140,15 @@
.export_values();
py_counter
- .def(nb::init<double, Counter::Flags, Counter::OneK>(),
- nb::arg("value") = 0., nb::arg("flags") = Counter::kDefaults,
- nb::arg("k") = Counter::kIs1000)
- .def("__init__", ([](Counter *c, double value) { new (c) Counter(value); }))
+ .def(
+ "__init__",
+ [](Counter* c, double value, int flags, Counter::OneK oneK) {
+ new (c) Counter(value, static_cast<Counter::Flags>(flags), oneK);
+ },
+ nb::arg("value") = 0., nb::arg("flags") = Counter::kDefaults,
+ nb::arg("k") = Counter::kIs1000)
+ .def("__init__",
+ ([](Counter* c, double value) { new (c) Counter(value); }))
.def_rw("value", &Counter::value)
.def_rw("flags", &Counter::flags)
.def_rw("oneK", &Counter::oneK)
diff --git a/include/benchmark/benchmark.h b/include/benchmark/benchmark.h
index 7dd72e2..4cdb451 100644
--- a/include/benchmark/benchmark.h
+++ b/include/benchmark/benchmark.h
@@ -1004,7 +1004,8 @@
State(std::string name, IterationCount max_iters,
const std::vector<int64_t>& ranges, int thread_i, int n_threads,
internal::ThreadTimer* timer, internal::ThreadManager* manager,
- internal::PerfCountersMeasurement* perf_counters_measurement);
+ internal::PerfCountersMeasurement* perf_counters_measurement,
+ ProfilerManager* profiler_manager);
void StartKeepRunning();
// Implementation of KeepRunning() and KeepRunningBatch().
@@ -1019,6 +1020,7 @@
internal::ThreadTimer* const timer_;
internal::ThreadManager* const manager_;
internal::PerfCountersMeasurement* const perf_counters_measurement_;
+ ProfilerManager* const profiler_manager_;
friend class internal::BenchmarkInstance;
};
diff --git a/setup.py b/setup.py
index 40cdc8d..d171476 100644
--- a/setup.py
+++ b/setup.py
@@ -99,7 +99,7 @@
bazel_argv = [
"bazel",
- "build",
+ "run",
ext.bazel_target,
f"--symlink_prefix={temp_path / 'bazel-'}",
f"--compilation_mode={'dbg' if self.debug else 'opt'}",
@@ -127,20 +127,42 @@
else:
suffix = ".abi3.so" if ext.py_limited_api else ".so"
- ext_name = ext.target_name + suffix
- ext_bazel_bin_path = temp_path / "bazel-bin" / ext.relpath / ext_name
- ext_dest_path = Path(self.get_ext_fullpath(ext.name)).with_name(
- ext_name
- )
- shutil.copyfile(ext_bazel_bin_path, ext_dest_path)
+ # copy the Bazel build artifacts into setuptools' libdir,
+ # from where the wheel is built.
+ pkgname = "google_benchmark"
+ pythonroot = Path("bindings") / "python" / "google_benchmark"
+ srcdir = temp_path / "bazel-bin" / pythonroot
+ libdir = Path(self.build_lib) / pkgname
+ for root, dirs, files in os.walk(srcdir, topdown=True):
+ # exclude runfiles directories and children.
+ dirs[:] = [d for d in dirs if "runfiles" not in d]
+
+ for f in files:
+ print(f)
+ fp = Path(f)
+ should_copy = False
+ # we do not want the bare .so file included
+ # when building for ABI3, so we require a
+ # full and exact match on the file extension.
+ if "".join(fp.suffixes) == suffix:
+ should_copy = True
+ elif fp.suffix == ".pyi":
+ should_copy = True
+ elif Path(root) == srcdir and f == "py.typed":
+ # copy py.typed, but only at the package root.
+ should_copy = True
+
+ if should_copy:
+ shutil.copyfile(root / fp, libdir / fp)
setuptools.setup(
cmdclass=dict(build_ext=BuildBazelExtension),
+ package_data={"google_benchmark": ["py.typed", "*.pyi"]},
ext_modules=[
BazelExtension(
name="google_benchmark._benchmark",
- bazel_target="//bindings/python/google_benchmark:_benchmark",
+ bazel_target="//bindings/python/google_benchmark:benchmark_stubgen",
py_limited_api=py_limited_api,
)
],
diff --git a/src/benchmark.cc b/src/benchmark.cc
index 374c514..b7767bd 100644
--- a/src/benchmark.cc
+++ b/src/benchmark.cc
@@ -168,7 +168,8 @@
State::State(std::string name, IterationCount max_iters,
const std::vector<int64_t>& ranges, int thread_i, int n_threads,
internal::ThreadTimer* timer, internal::ThreadManager* manager,
- internal::PerfCountersMeasurement* perf_counters_measurement)
+ internal::PerfCountersMeasurement* perf_counters_measurement,
+ ProfilerManager* profiler_manager)
: total_iterations_(0),
batch_leftover_(0),
max_iterations(max_iters),
@@ -182,7 +183,8 @@
threads_(n_threads),
timer_(timer),
manager_(manager),
- perf_counters_measurement_(perf_counters_measurement) {
+ perf_counters_measurement_(perf_counters_measurement),
+ profiler_manager_(profiler_manager) {
BM_CHECK(max_iterations != 0) << "At least one iteration must be run";
BM_CHECK_LT(thread_index_, threads_)
<< "thread_index must be less than threads";
@@ -207,7 +209,7 @@
#if defined(__INTEL_COMPILER)
#pragma warning push
#pragma warning(disable : 1875)
-#elif defined(__GNUC__)
+#elif defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Winvalid-offsetof"
#endif
@@ -225,7 +227,7 @@
offsetof(State, skipped_) <= (cache_line_size - sizeof(skipped_)), "");
#if defined(__INTEL_COMPILER)
#pragma warning pop
-#elif defined(__GNUC__)
+#elif defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic pop
#endif
#if defined(__NVCC__)
@@ -302,6 +304,8 @@
BM_CHECK(!started_ && !finished_);
started_ = true;
total_iterations_ = skipped() ? 0 : max_iterations;
+ if (BENCHMARK_BUILTIN_EXPECT(profiler_manager_ != nullptr, false))
+ profiler_manager_->AfterSetupStart();
manager_->StartStopBarrier();
if (!skipped()) ResumeTiming();
}
@@ -315,6 +319,8 @@
total_iterations_ = 0;
finished_ = true;
manager_->StartStopBarrier();
+ if (BENCHMARK_BUILTIN_EXPECT(profiler_manager_ != nullptr, false))
+ profiler_manager_->BeforeTeardownStop();
}
namespace internal {
diff --git a/src/benchmark_api_internal.cc b/src/benchmark_api_internal.cc
index 286f986..4b569d7 100644
--- a/src/benchmark_api_internal.cc
+++ b/src/benchmark_api_internal.cc
@@ -92,9 +92,10 @@
State BenchmarkInstance::Run(
IterationCount iters, int thread_id, internal::ThreadTimer* timer,
internal::ThreadManager* manager,
- internal::PerfCountersMeasurement* perf_counters_measurement) const {
+ internal::PerfCountersMeasurement* perf_counters_measurement,
+ ProfilerManager* profiler_manager) const {
State st(name_.function_name, iters, args_, thread_id, threads_, timer,
- manager, perf_counters_measurement);
+ manager, perf_counters_measurement, profiler_manager);
benchmark_.Run(st);
return st;
}
@@ -102,7 +103,7 @@
void BenchmarkInstance::Setup() const {
if (setup_) {
State st(name_.function_name, /*iters*/ 1, args_, /*thread_id*/ 0, threads_,
- nullptr, nullptr, nullptr);
+ nullptr, nullptr, nullptr, nullptr);
setup_(st);
}
}
@@ -110,7 +111,7 @@
void BenchmarkInstance::Teardown() const {
if (teardown_) {
State st(name_.function_name, /*iters*/ 1, args_, /*thread_id*/ 0, threads_,
- nullptr, nullptr, nullptr);
+ nullptr, nullptr, nullptr, nullptr);
teardown_(st);
}
}
diff --git a/src/benchmark_api_internal.h b/src/benchmark_api_internal.h
index 94f5165..659a714 100644
--- a/src/benchmark_api_internal.h
+++ b/src/benchmark_api_internal.h
@@ -44,7 +44,8 @@
State Run(IterationCount iters, int thread_id, internal::ThreadTimer* timer,
internal::ThreadManager* manager,
- internal::PerfCountersMeasurement* perf_counters_measurement) const;
+ internal::PerfCountersMeasurement* perf_counters_measurement,
+ ProfilerManager* profiler_manager) const;
private:
BenchmarkName name_;
diff --git a/src/benchmark_runner.cc b/src/benchmark_runner.cc
index 3a8c307..a380939 100644
--- a/src/benchmark_runner.cc
+++ b/src/benchmark_runner.cc
@@ -125,14 +125,15 @@
// Adds the stats collected for the thread into manager->results.
void RunInThread(const BenchmarkInstance* b, IterationCount iters,
int thread_id, ThreadManager* manager,
- PerfCountersMeasurement* perf_counters_measurement) {
+ PerfCountersMeasurement* perf_counters_measurement,
+ ProfilerManager* profiler_manager) {
internal::ThreadTimer timer(
b->measure_process_cpu_time()
? internal::ThreadTimer::CreateProcessCpuTime()
: internal::ThreadTimer::Create());
- State st =
- b->Run(iters, thread_id, &timer, manager, perf_counters_measurement);
+ State st = b->Run(iters, thread_id, &timer, manager,
+ perf_counters_measurement, profiler_manager);
BM_CHECK(st.skipped() || st.iterations() >= st.max_iterations)
<< "Benchmark returned before State::KeepRunning() returned false!";
{
@@ -268,12 +269,14 @@
// Run all but one thread in separate threads
for (std::size_t ti = 0; ti < pool.size(); ++ti) {
pool[ti] = std::thread(&RunInThread, &b, iters, static_cast<int>(ti + 1),
- manager.get(), perf_counters_measurement_ptr);
+ manager.get(), perf_counters_measurement_ptr,
+ /*profiler_manager=*/nullptr);
}
// And run one thread here directly.
// (If we were asked to run just one thread, we don't create new threads.)
// Yes, we need to do this here *after* we start the separate threads.
- RunInThread(&b, iters, 0, manager.get(), perf_counters_measurement_ptr);
+ RunInThread(&b, iters, 0, manager.get(), perf_counters_measurement_ptr,
+ /*profiler_manager=*/nullptr);
// The main thread has finished. Now let's wait for the other threads.
manager->WaitForAllThreads();
@@ -289,12 +292,6 @@
// And get rid of the manager.
manager.reset();
- // Adjust real/manual time stats since they were reported per thread.
- i.results.real_time_used /= b.threads();
- i.results.manual_time_used /= b.threads();
- // If we were measuring whole-process CPU usage, adjust the CPU time too.
- if (b.measure_process_cpu_time()) i.results.cpu_time_used /= b.threads();
-
BM_VLOG(2) << "Ran in " << i.results.cpu_time_used << "/"
<< i.results.real_time_used << "\n";
@@ -415,7 +412,8 @@
manager.reset(new internal::ThreadManager(1));
b.Setup();
RunInThread(&b, memory_iterations, 0, manager.get(),
- perf_counters_measurement_ptr);
+ perf_counters_measurement_ptr,
+ /*profiler_manager=*/nullptr);
manager->WaitForAllThreads();
manager.reset();
b.Teardown();
@@ -429,11 +427,10 @@
std::unique_ptr<internal::ThreadManager> manager;
manager.reset(new internal::ThreadManager(1));
b.Setup();
- profiler_manager->AfterSetupStart();
RunInThread(&b, profile_iterations, 0, manager.get(),
- /*perf_counters_measurement_ptr=*/nullptr);
+ /*perf_counters_measurement_ptr=*/nullptr,
+ /*profiler_manager=*/profiler_manager);
manager->WaitForAllThreads();
- profiler_manager->BeforeTeardownStop();
manager.reset();
b.Teardown();
}
diff --git a/src/complexity.cc b/src/complexity.cc
index eee3122..63acd50 100644
--- a/src/complexity.cc
+++ b/src/complexity.cc
@@ -27,7 +27,6 @@
// Internal function to calculate the different scalability forms
BigOFunc* FittingCurve(BigO complexity) {
- static const double kLog2E = 1.44269504088896340736;
switch (complexity) {
case oN:
return [](IterationCount n) -> double { return static_cast<double>(n); };
@@ -36,15 +35,12 @@
case oNCubed:
return [](IterationCount n) -> double { return std::pow(n, 3); };
case oLogN:
- /* Note: can't use log2 because Android's GNU STL lacks it */
- return [](IterationCount n) {
- return kLog2E * std::log(static_cast<double>(n));
+ return [](IterationCount n) -> double {
+ return std::log2(static_cast<double>(n));
};
case oNLogN:
- /* Note: can't use log2 because Android's GNU STL lacks it */
- return [](IterationCount n) {
- return kLog2E * static_cast<double>(n) *
- std::log(static_cast<double>(n));
+ return [](IterationCount n) -> double {
+ return static_cast<double>(n) * std::log2(static_cast<double>(n));
};
case o1:
default:
diff --git a/src/perf_counters.cc b/src/perf_counters.cc
index fa1cbb0..fc9586b 100644
--- a/src/perf_counters.cc
+++ b/src/perf_counters.cc
@@ -218,7 +218,7 @@
GetErrorLogInstance() << "***WARNING*** Failed to start counters. "
"Claring out all counters.\n";
- // Close all peformance counters
+ // Close all performance counters
for (int id : counter_ids) {
::close(id);
}
diff --git a/test/profiler_manager_test.cc b/test/profiler_manager_test.cc
index 1b3e36c..3b08a60 100644
--- a/test/profiler_manager_test.cc
+++ b/test/profiler_manager_test.cc
@@ -6,8 +6,12 @@
#include "output_test.h"
class TestProfilerManager : public benchmark::ProfilerManager {
- void AfterSetupStart() override {}
- void BeforeTeardownStop() override {}
+ public:
+ void AfterSetupStart() override { ++start_called; }
+ void BeforeTeardownStop() override { ++stop_called; }
+
+ int start_called = 0;
+ int stop_called = 0;
};
void BM_empty(benchmark::State& state) {
@@ -35,9 +39,12 @@
ADD_CASES(TC_CSVOut, {{"^\"BM_empty\",%csv_report$"}});
int main(int argc, char* argv[]) {
- std::unique_ptr<benchmark::ProfilerManager> pm(new TestProfilerManager());
+ std::unique_ptr<TestProfilerManager> pm(new TestProfilerManager());
benchmark::RegisterProfilerManager(pm.get());
RunOutputTests(argc, argv);
benchmark::RegisterProfilerManager(nullptr);
+
+ assert(pm->start_called == 1);
+ assert(pm->stop_called == 1);
}