Coverage Report

Created: 2025-07-06 13:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/builds/MusicScience37Projects/utility-libraries/cpp-stat-bench/src/stat_bench/measurer/determine_iterations.cpp
Line
Count
Source
1
/*
2
 * Copyright 2021 MusicScience37 (Kenta Kabashima)
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
/*!
17
 * \file
18
 * \brief Definition of determine_iterations function.
19
 */
20
#include "stat_bench/measurer/determine_iterations.h"
21
22
#include <algorithm>  // std::min, std::max
23
#include <vector>
24
25
#include "stat_bench/clock/duration.h"
26
#include "stat_bench/measurer/measure_once.h"
27
#include "stat_bench/measurer/measurement.h"
28
29
namespace stat_bench {
30
namespace measurer {
31
32
auto determine_iterations(bench_impl::IBenchmarkCase* bench_case,
33
    const BenchmarkCondition& cond, const MeasurementType& measurement_type,
34
377
    double min_sample_duration_sec) -> std::size_t {
35
377
    std::size_t iterations = 1;
36
377
    constexpr std::size_t trials = 10;
37
1.65k
    for (std::size_t i = 0; i < trials; ++i) {
38
1.65k
        constexpr std::size_t samples = 2;
39
1.65k
        const auto data = measure_once(
40
1.65k
            bench_case, cond, measurement_type, iterations, samples, 0);
41
1.65k
        const double duration_sec = data.durations().at(0).at(1).seconds();
42
1.65k
        if (duration_sec > min_sample_duration_sec) {
43
374
            break;
44
374
        }
45
46
1.27k
        const double multiplier =
47
1.27k
            std::min(
48
1.27k
                min_sample_duration_sec / std::max(duration_sec, 1e-9), 10.0) *
49
1.27k
            1.2;
50
1.27k
        constexpr double max_iterations = 1e+6;
51
1.27k
        iterations = static_cast<std::size_t>(std::min(
52
1.27k
            max_iterations, static_cast<double>(iterations) * multiplier));
53
1.27k
    }
54
377
    return iterations;
55
377
}
56
57
}  // namespace measurer
58
}  // namespace stat_bench