ctbench 1.3.4
|
Compiler-assisted benchmarking for the study of C++ metaprogram compile times.
ctbench allows you to declare and generate compile-time benchmark batches for given ranges, run them, aggregate and wrangle Clang profiling data, and plot them.
The project was made to fit the needs of scientific data collection and analysis, thus it is not a one-shot profiler, but a set of tools that enable reproductible data gathering from user-defined, variably sized compile-time benchmarks using Clang's time-trace feature to understand the impact of metaprogramming techniques on compile time. On top of that, ctbench is also able to measure compiler execution time to support compilers that do not have built-in profilers like GCC.
It has two main components: a C++ plotting toolset that can be used as a CLI program and as a library, and a CMake boilerplate library to generate benchmark and graph targets.
The CMake library contains all the boilerplate code to define benchmark targets compatible with the C++ plotting toolset called grapher
.
Rule of Cheese can be used as an example project for using ctbench.
As an example here are benchmark curves from the Poacher project. The benchmark case sources are available here.
Clang ExecuteCompiler time curve from poacher, generated by the compare_by
plotter
Clang Total Frontend time curve from poacher, generated by the compare_by
plotter
ArchLinux and Ubuntu 23.04 are officially supported as tests are compiled and executed on both of these Linux distributions. Others including Fedora or any other Linux distro that provides CMake 3.25 or higher should be compatible.
boost boost-libs catch2 clang cmake curl fmt git llvm llvm-libs ninja nlohmann-json tar tbb unzip zip
catch2 clang cmake curl git libboost-all-dev libclang-dev libfmt-dev libllvm15 libtbb-dev libtbb12 llvm llvm-dev ninja-build nlohmann-json3-dev pkg-config tar unzip zip
The Sciplot library is required too. It can be installed on ArchLinux using the sciplot-git
AUR package (NB: the non-git package isn't up-to-date). Otherwise, you can install it for your whole system using CMake or locally using vcpkg:
Note: The fmt
dependency is needed, as vcpkg breaks fmt's CMake integration if you have it already installed.
An AUR package is available for easier install and update.
ctbench can be integrated to a CMake project using find_package
:
The example project is provided as a reference project for ctbench integration and usage. For more details, an exhaustive CMake API reference is available.
A benchmark case is represented by a C++ file. It will be "instanciated", ie. compiled with BENCHMARK_SIZE
defined to values in a range that you provide.
BENCHMARK_SIZE
is intended to be used by the preprocessor to generate a benchmark instance of the desired size:
By default, only compiler execution time is measured. If you want to generate plots using Clang's profiler data, add the following:
Note that plotting profiler data takes more time and will generate a lot of plot files.
Then you can declare a benchmark case target in CMake with the following:
Once you have several benchmark cases, you can start writing a graph config.
Example configs can be found here, or by running ctbench-grapher-utils --plotter=<plotter> --command=get-default-config
. A list of available plotters can be retrieved by running ctbench-grapher-utils --help
.
This configuration uses the compare_by
plotter. It compares features targeted by the JSON pointers in key_ptrs
across all benchmark cases. This is the easiest way to extract and compare as many relevant time-trace features at once.
Back to CMake, you can now declare a graph target using this config to compare the time spent in the compiler execution, the frontend, and the backend between the benchmark cases you declared previously:
For each group descriptor, a graph will be generated with one curve per benchmark case. In this case, you would then get 3 graphs (ExecuteCompiler
, Frontend
, and Backend
) each with 5 curves (enable_if
, enable_if_t
, if_constexpr
, control
, and requires
).