Create benchmarks for your C++ codebase using google_benchmark
google_benchmark
library,
which is a compatibility layer to run both instrumented and walltime CodSpeed
benchmarks.
google_benchmark
library. Here is a small example
on how to declare benchmarks. Otherwise, any existing benchmarks of your project
can be reused.
benchmark::DoNotOptimize
and benchmark::ClobberMemory
.
Checkout the Google benchmark user guide on preventing optimization for more information.google_benchmark
library using both CMake
and Bazel
.
If you are using another build system, you may find guidelines in the
custom build systems section
google_benchmark
integration using
CMake
, you can declare a benchmark
executable as follows:
CMakeLists.txt
file for the benchmark executable.
You can also add an executable target to your existing project’s
CMakeLists.txt
. Make sure to link this target against the
benchmark::benchmark
library.CODSPEED_MODE
flag-DCODSPEED_MODE=instrumentation
flag in the cmake
command.
This will enable the CodSpeed instrumentation mode for the benchmark executable,
where each benchmark is run only once on a simulated CPU.
You can also use -DCODSPEED_MODE=walltime
if you are building for walltime
codspeed reports, see dedicated documentation for more
information.
If you omit the CODSPEED_MODE
cmake flag, CodSpeed will not be enabled in the
benchmark executable, and it will run as a regular benchmark.
CMAKE_BUILD_TYPE
to RelWithDebInfo
.
main
branch and every
pull request:
google_benchmark
integration with the
Bazel
integration.
WORKSPACE
file:
BUILD.bazel
file:
--@codspeed_cpp//core:codspeed_mode=instrumentation
enables the codspeed
features of the library, which can take the following values here:
off
: defaulted to when the cli flag is not provided, disables codspeed.instrumentation
: benchmarks are run only once on a simulated CPU.walltime
: used for walltime codspeed reports, see
dedicated documentation--compilation_mode=dbg
: enables debug symbols in the compiled binary, used
to generate meaningful CodSpeed reports.--copt=-O2
: sets the desired level of compiler optimizations in the
benchmarks binary..bazelrc
file at the root of the bazel workspace with the following contentmain
branch and every
pull request:
codspeed-cpp
repository. You can
either clone the repository, add it as a submodule or even download the sources
as a zip file.
google_benchmark
CodSpeed integration library are located in
the
google_benchmark
subdirectory. 3.
Make sure the following pre-processor variables are defined when you build the
library
When building the library, the tricky part is to make sure google_benchmark’s
fork has access to the
codspeed-core
library.
Additionally, the following pre-processor variables must be defined:
CODPSEED_ENABLED
: if not defined, google_benchmark
will the same as the
upstream library, with no CodSpeed features.CODSPEED_INSTRUMENTATION
: if running in instrumentation modeCODSPEED_WALLTIME
: if running in walltime modeCODSPEED_ROOT_DIR
: absolute path to the root directory of your project. This
is used in the report to display file path relative to your root projectgoogle_benchmark
library with
your project, please reach out and open an issue on the
codspeed-cpp repository.