A microbenchmark support library
Go to file
Peter Hawkins 64b5d8cd11
Update benchmark Python bindings for nanobind 2.0, and update to nanobind 2.0. (#1817)
Incorporates the nanobind_bazel change from https://github.com/google/benchmark/pull/1795.

nanobind 2.0 reworked the nanobind::enum_ class so it uses a real Python enum or intenum rather than its previous hand-rolled implementation.
https://nanobind.readthedocs.io/en/latest/changelog.html#version-2-0-0-may-23-2024

As a consequence of that change, nanobind now checks when casting an integer to a enum value that the integer corresponds to a valid enum. Counter::Flags is a bitmask, and many combinations are not valid enum members.

This change:
a) sets nb::is_arithmetic(), which means Counter::Flags becomes an IntEnum that can be freely cast to an integer.
b) defines the | operator for flags to return an integer, not an enum, avoiding the error.
c) changes Counter's constructor to accept an int, not a Counter::Flags enum. Since Counter::Flags is an IntEnum now, it can be freely coerced to an int.

If https://github.com/wjakob/nanobind/pull/599 is merged into nanobind, then we can perhaps use a flag enum here instead.
2024-07-18 16:54:02 +01:00
.github update actions/checkout to v4 2024-07-17 16:47:54 +01:00
bazel Upgrade core bazel dependencies (#1711) 2023-12-07 15:00:43 +00:00
bindings/python/google_benchmark Update benchmark Python bindings for nanobind 2.0, and update to nanobind 2.0. (#1817) 2024-07-18 16:54:02 +01:00
cmake Add -lkstat to the .pc for Solaris (#1801) 2024-07-03 21:16:43 +03:00
docs [FR] Add API to provide custom profilers #1807 (#1809) 2024-07-16 09:56:40 +01:00
include/benchmark clang format yet again 2024-07-17 16:49:12 +01:00
src clang format yet again 2024-07-17 16:49:12 +01:00
test [FR] Add API to provide custom profilers #1807 (#1809) 2024-07-16 09:56:40 +01:00
tools Add Python bindings build using bzlmod (#1764) 2024-03-07 12:28:55 +00:00
_config.yml enable markdown rendering on github pages 2021-04-28 16:44:33 +01:00
.clang-format Fix .clang-format 2019-04-08 12:38:11 +03:00
.clang-tidy clang-tidy broke the world (#1766) 2024-03-18 11:01:25 +00:00
.gitignore ignore new bzlmod lock file 2024-01-29 13:06:57 +00:00
.pre-commit-config.yaml hotfix: Correct pypi-publishing action tag to v1.8.14 (#1791) 2024-05-24 09:51:41 +01:00
.ycm_extra_conf.py Add pre-commit config and GitHub Actions job (#1688) 2023-10-30 15:35:37 +00:00
appveyor.yml CMake: use full add_test(NAME <> COMMAND <>) signature (#901) 2019-11-05 22:46:13 +03:00
AUTHORS Audit MSVC references in cmake files to consider clang++ (#1669) 2023-09-26 12:31:24 +01:00
BUILD.bazel [bazel] Use includes instead of strip_include_prefix (#1803) 2024-06-17 09:38:32 +01:00
CMakeLists.txt preparing for v1.8.5 2024-07-18 11:13:04 +01:00
CONTRIBUTING.md Add information about CLAs. 2014-02-12 18:51:08 -05:00
CONTRIBUTORS [FR] Add API to provide custom profilers #1807 (#1809) 2024-07-16 09:56:40 +01:00
LICENSE Stop generating the export header and just check it in (#1435) 2022-07-20 20:34:39 +01:00
MODULE.bazel Update benchmark Python bindings for nanobind 2.0, and update to nanobind 2.0. (#1817) 2024-07-18 16:54:02 +01:00
pyproject.toml Add Python bindings build using bzlmod (#1764) 2024-03-07 12:28:55 +00:00
README.md Bump minimum required C++ version from C++11 to C++14 (#1800) 2024-06-10 11:08:49 +01:00
setup.py Modernize wheel building job config (#1783) 2024-04-15 17:44:09 +01:00
WORKSPACE Add Python bindings build using bzlmod (#1764) 2024-03-07 12:28:55 +00:00
WORKSPACE.bzlmod Add support for bzlmod (excluding Python bindings) (#1615) 2023-06-27 13:03:39 +01:00

Benchmark

build-and-test bazel pylint test-bindings Coverage Status

Discord

A library to benchmark code snippets, similar to unit tests. Example:

#include <benchmark/benchmark.h>

static void BM_SomeFunction(benchmark::State& state) {
  // Perform setup here
  for (auto _ : state) {
    // This code gets timed
    SomeFunction();
  }
}
// Register the function as a benchmark
BENCHMARK(BM_SomeFunction);
// Run the benchmark
BENCHMARK_MAIN();

Getting Started

To get started, see Requirements and Installation. See Usage for a full example and the User Guide for a more comprehensive feature overview.

It may also help to read the Google Test documentation as some of the structural aspects of the APIs are similar.

Resources

Discussion group

IRC channels:

Additional Tooling Documentation

Assembly Testing Documentation

Building and installing Python bindings

Requirements

The library can be used with C++03. However, it requires C++14 to build, including compiler and standard library support.

See dependencies.md for more details regarding supported compilers and standards.

If you have need for a particular compiler to be supported, patches are very welcome.

See Platform-Specific Build Instructions.

Installation

This describes the installation process using cmake. As pre-requisites, you'll need git and cmake installed.

See dependencies.md for more details regarding supported versions of build tools.

# Check out the library.
$ git clone https://github.com/google/benchmark.git
# Go to the library root directory
$ cd benchmark
# Make a build directory to place the build output.
$ cmake -E make_directory "build"
# Generate build system files with cmake, and download any dependencies.
$ cmake -E chdir "build" cmake -DBENCHMARK_DOWNLOAD_DEPENDENCIES=on -DCMAKE_BUILD_TYPE=Release ../
# or, starting with CMake 3.13, use a simpler form:
# cmake -DCMAKE_BUILD_TYPE=Release -S . -B "build"
# Build the library.
$ cmake --build "build" --config Release

This builds the benchmark and benchmark_main libraries and tests. On a unix system, the build directory should now look something like this:

/benchmark
  /build
    /src
      /libbenchmark.a
      /libbenchmark_main.a
    /test
      ...

Next, you can run the tests to check the build.

$ cmake -E chdir "build" ctest --build-config Release

If you want to install the library globally, also run:

sudo cmake --build "build" --config Release --target install

Note that Google Benchmark requires Google Test to build and run the tests. This dependency can be provided two ways:

  • Checkout the Google Test sources into benchmark/googletest.
  • Otherwise, if -DBENCHMARK_DOWNLOAD_DEPENDENCIES=ON is specified during configuration as above, the library will automatically download and build any required dependencies.

If you do not wish to build and run the tests, add -DBENCHMARK_ENABLE_GTEST_TESTS=OFF to CMAKE_ARGS.

Debug vs Release

By default, benchmark builds as a debug library. You will see a warning in the output when this is the case. To build it as a release library instead, add -DCMAKE_BUILD_TYPE=Release when generating the build system files, as shown above. The use of --config Release in build commands is needed to properly support multi-configuration tools (like Visual Studio for example) and can be skipped for other build systems (like Makefile).

To enable link-time optimisation, also add -DBENCHMARK_ENABLE_LTO=true when generating the build system files.

If you are using gcc, you might need to set GCC_AR and GCC_RANLIB cmake cache variables, if autodetection fails.

If you are using clang, you may need to set LLVMAR_EXECUTABLE, LLVMNM_EXECUTABLE and LLVMRANLIB_EXECUTABLE cmake cache variables.

To enable sanitizer checks (eg., asan and tsan), add:

 -DCMAKE_C_FLAGS="-g -O2 -fno-omit-frame-pointer -fsanitize=address -fsanitize=thread -fno-sanitize-recover=all"
 -DCMAKE_CXX_FLAGS="-g -O2 -fno-omit-frame-pointer -fsanitize=address -fsanitize=thread -fno-sanitize-recover=all "  

Stable and Experimental Library Versions

The main branch contains the latest stable version of the benchmarking library; the API of which can be considered largely stable, with source breaking changes being made only upon the release of a new major version.

Newer, experimental, features are implemented and tested on the v2 branch. Users who wish to use, test, and provide feedback on the new features are encouraged to try this branch. However, this branch provides no stability guarantees and reserves the right to change and break the API at any time.

Usage

Basic usage

Define a function that executes the code to measure, register it as a benchmark function using the BENCHMARK macro, and ensure an appropriate main function is available:

#include <benchmark/benchmark.h>

static void BM_StringCreation(benchmark::State& state) {
  for (auto _ : state)
    std::string empty_string;
}
// Register the function as a benchmark
BENCHMARK(BM_StringCreation);

// Define another benchmark
static void BM_StringCopy(benchmark::State& state) {
  std::string x = "hello";
  for (auto _ : state)
    std::string copy(x);
}
BENCHMARK(BM_StringCopy);

BENCHMARK_MAIN();

To run the benchmark, compile and link against the benchmark library (libbenchmark.a/.so). If you followed the build steps above, this library will be under the build directory you created.

# Example on linux after running the build steps above. Assumes the
# `benchmark` and `build` directories are under the current directory.
$ g++ mybenchmark.cc -std=c++11 -isystem benchmark/include \
  -Lbenchmark/build/src -lbenchmark -lpthread -o mybenchmark

Alternatively, link against the benchmark_main library and remove BENCHMARK_MAIN(); above to get the same behavior.

The compiled executable will run all benchmarks by default. Pass the --help flag for option information or see the User Guide.

Usage with CMake

If using CMake, it is recommended to link against the project-provided benchmark::benchmark and benchmark::benchmark_main targets using target_link_libraries. It is possible to use find_package to import an installed version of the library.

find_package(benchmark REQUIRED)

Alternatively, add_subdirectory will incorporate the library directly in to one's CMake project.

add_subdirectory(benchmark)

Either way, link to the library as follows.

target_link_libraries(MyTarget benchmark::benchmark)