Type:
template <typename TClock>
class fcf::NTest::DurationBasic
Package: fcfTest
File:
test.hpp
( link_to_the_text_position_in_the_file )
Available from version: 1.2.5
A template-based benchmarking class that provides multi-level measurement, warmup iterations, and statistical analysis using histograms.
The fcf::NTest::DurationBasic class is a sophisticated template-based tool designed for high-precision micro-benchmarking. It allows for complex measurement scenarios by supporting multiple measurement levels, warmup iterations to stabilize the environment, and measurement steps to aggregate data.
Key features include:
- Multi-level timing: Measure nested code blocks at different depths.
- Warmup support: Execute code multiple times before actual measurement to account for CPU cache warming and JIT optimizations.
- Statistical analysis: Automatically collects data into fcf::NTest::HistogramBasic for calculating average, median, minimum, and maximum durations.
- Flexible execution: Supports both manual control via begin/end and automated execution using the operator() with a provided functor.
Template arguments
TClock
- A clock type that provides time points in nanoseconds (e.g., fcf::NTest::SteadyClock).
Nested classes
BeginOptions
- A configuration structure used to define the initial parameters for a measurement session in the DurationBasic class, such as iteration count and histogram resolution.
Options
- An extended configuration structure for benchmarking sessions, providing fine-grained control over iterations, warmup, measurement steps, and histogram resolution.
Methods
[CONSTRUCTOR]
DurationBasic(
unsigned long long a_iterationCount,
unsigned long long a_measurementStep
= 1,
unsigned long long a_warmupCount
= 0)
[CONSTRUCTOR]
DurationBasic(
const fcf::NTest::DurationBasic::Options& a_options)
- Initializes a new benchmarking session with specified iteration and measurement parameters.
fcf::NTest::DurationBasic<
TClock>
::Options
options()
const
void
options(
fcf::NTest::DurationBasic<
TClock>
::Options a_options)
- Provides access to or updates the current measurement options for the benchmarking session.
void
begin(
int a_beginLevel
= 0)
void
begin(
int a_beginLevel,
int a_endLevel)
void
begin(
const fcf::NTest::DurationBasic::BeginOptions& a_options,
int a_beginLevel
= 0)
void
begin(
const fcf::NTest::DurationBasic::BeginOptions& a_options,
int a_beginLevel,
int a_endLevel)
- Starts a measurement session at a specific level.
void
end(
int a_beginLevel
= 0)
void
end(
int a_beginLevel,
int a_endLevel)
- Ends a measurement session at a specific level.
void
reset(
int a_beginLevel
= 0)
void
reset(
int a_beginLevel,
int a_endLevel)
- Resets measurements for a specific range of levels.
fcf::NTest::TimeDuration
duration(
int a_level
= 0)
const
- Returns the total accumulated duration for a specific level.
void
operator()(
int a_beginLevel,
TFunction a_function)
void
operator()(
int a_beginLevel,
int a_endLevel,
TFunction a_function)
void
operator()(
const fcf::NTest::DurationBasic::Options& a_options,
TFunction a_function)
void
operator()(
const fcf::NTest::DurationBasic::Options& a_options,
int a_beginLevel,
TFunction a_function)
void
operator()(
const fcf::NTest::DurationBasic::Options& a_options,
int a_beginLevel,
int a_endLevel,
TFunction a_function)
- Executes a function and measures its duration.
fcf::NTest::TimeDuration
average(
int a_level
= 0)
const
- Returns the average duration for a specific level.
fcf::NTest::TimeDuration
min(
int a_level
= 0)
const
- Returns the minimum duration recorded for a specific level.
fcf::NTest::TimeDuration
max(
int a_level
= 0)
const
- Returns the maximum duration recorded for a specific level.
fcf::NTest::TimeDuration
median(
int a_level
= 0)
const
- Returns the median duration for a specific level using the internal histogram.
const fcf::NTest::HistogramBasic<
fcf::NTest::TimeDuration,
unsigned long long>&
histogram(
size_t a_level
= 0)
const
fcf::NTest::HistogramBasic<
fcf::NTest::TimeDuration,
unsigned long long>&
histogram(
size_t a_level
= 0)
- Provides access to the histogram of the measurement distribution.
Example: Automated Benchmarking with Warmup
Demonstrates the most efficient way to benchmark a function using the operator() overload, including warmup iterations and statistical reporting.
#define FCF_TEST_IMPLEMENTATION
#include <fcfTest/test.hpp>
#include <vector>
#include <algorithm>
FCF_TEST_DEFINE("Benchmark", "Performance", "SortTest"){
// 1. Configure measurement options:
// - 1000 total iterations
// - 100 warmup iterations to stabilize the environment
// - 50 iterations per measurement step
// - 20 bins for the histogram
fcf::NTest::Duration::Options options(1000, 20, 50, 100);
// 2. Create the duration object with specified options
fcf::NTest::Duration bench(options);
// 3. Execute the benchmark using the operator()
// The lambda will be executed 1000 times
bench([]() {
std::vector<int> data(100);
for(int i = 0; i < 100; ++i) data[i] = i;
std::sort(data.begin(), data.end());
});
// 4. Report the statistical results
fcf::NTest::log() << "--- Benchmark Results ---" << std::endl;
fcf::NTest::log() << "Average: " << bench.average() << std::endl;
fcf::NTest::log() << "Median: " << bench.median() << std::endl;
fcf::NTest::log() << "Min: " << bench.min() << std::endl;
fcf::NTest::log() << "Max: " << bench.max() << std::endl;
}
int main(int a_argc, char* a_argv[]) {
bool error = false;
fcf::NTest::cmdRun(a_argc, a_argv, fcf::NTest::CRM_RUN, &error);
return error ? 1 : 0;
}
Output:
Performing the test: "Benchmark" -> "Performance" -> "SortTest" ...
> --- Benchmark Results ---
> Average: 0.000`001`234 sec
> Median: 0.000`001`210 sec
> Min: 0.000`000`980 sec
> Max: 0.000`002`450 sec
[SUCCESS] Test completed successfully (0.001`234`567 sec)
[SUCCESS] All tests were completed.
Tests: 1 passed, 0 failed, 0 skipped, 1 total
Duration: 0.001`234`567 sec