cb66c5d68e
bazel / bazel.ubuntu-latest (push) Failing after 1s
build-and-test-perfcounters / ubuntu-latest.Debug (push) Failing after 0s
build-and-test-perfcounters / ubuntu-latest.Release (push) Failing after 1s
doxygen / Build HTML documentation (push) Failing after 1s
python + Bazel pre-commit checks / pre-commit (push) Failing after 1s
sanitizer / tsan.Debug (push) Failing after 1s
sanitizer / ubsan.Debug (push) Failing after 0s
sanitizer / ubsan.RelWithDebInfo (push) Failing after 1s
build-and-test-min-cmake / ubuntu-latest.min-cmake (push) Failing after 1s
clang-format-lint / check-clang-format (push) Failing after 1s
sanitizer / asan.Debug (push) Failing after 1s
sanitizer / msan.RelWithDebInfo (push) Failing after 1s
clang-tidy / run-clang-tidy (push) Failing after 1s
sanitizer / msan.Debug (push) Failing after 1s
sanitizer / asan.RelWithDebInfo (push) Failing after 0s
sanitizer / tsan.RelWithDebInfo (push) Failing after 0s
test-bindings / Test Rust bindings on ubuntu-latest (push) Failing after 0s
test-bindings / Test Python 3.10 bindings on ubuntu-latest (push) Failing after 0s
test-bindings / Test Python 3.11 bindings on ubuntu-latest (push) Failing after 0s
test-bindings / Test Python 3.12 bindings on ubuntu-latest (push) Failing after 1s
test-bindings / Test Python 3.13 bindings on ubuntu-latest (push) Failing after 0s
build-and-test / macos-latest.Debug.shared.clang++ (push) Has been cancelled
build-and-test / ubuntu-22.04.Debug.shared.clang++ (push) Has been cancelled
build-and-test / ubuntu-24.04.Debug.shared.clang++ (push) Has been cancelled
build-and-test / ubuntu-24.04-arm.Debug.shared.clang++ (push) Has been cancelled
bazel / bazel.macos-latest (push) Has been cancelled
bazel / bazel.windows-latest (push) Has been cancelled
build-and-test-min-cmake / macos-latest.min-cmake (push) Has been cancelled
build-and-test / ubuntu-22.04.Debug.static.clang++ (push) Has been cancelled
build-and-test / macos-latest.Debug.shared.g++ (push) Has been cancelled
build-and-test / ubuntu-22.04.Debug.shared.g++ (push) Has been cancelled
build-and-test / ubuntu-24.04.Debug.shared.g++ (push) Has been cancelled
build-and-test / ubuntu-24.04-arm.Debug.shared.g++ (push) Has been cancelled
build-and-test / macos-latest.Debug.static.g++ (push) Has been cancelled
build-and-test / ubuntu-22.04.Debug.static.g++ (push) Has been cancelled
build-and-test / ubuntu-24.04.Debug.static.g++ (push) Has been cancelled
build-and-test / ubuntu-24.04-arm.Debug.static.g++ (push) Has been cancelled
build-and-test / macos-latest.Release.shared.clang++ (push) Has been cancelled
build-and-test / ubuntu-22.04.Release.shared.clang++ (push) Has been cancelled
build-and-test / ubuntu-24.04.Release.shared.clang++ (push) Has been cancelled
build-and-test / ubuntu-24.04-arm.Release.shared.clang++ (push) Has been cancelled
build-and-test / macos-latest.Release.static.clang++ (push) Has been cancelled
build-and-test / ubuntu-22.04.Release.static.clang++ (push) Has been cancelled
build-and-test / macos-latest.Debug.static.clang++ (push) Has been cancelled
build-and-test / ubuntu-24.04.Debug.static.clang++ (push) Has been cancelled
build-and-test / ubuntu-24.04-arm.Debug.static.clang++ (push) Has been cancelled
build-and-test / macos-latest.Release.static.g++ (push) Has been cancelled
build-and-test / ubuntu-22.04.Release.static.g++ (push) Has been cancelled
build-and-test / windows-2025.Debug.static.VS-18-2025 (push) Has been cancelled
build-and-test / windows-2022.Release.shared.VS-17-2022 (push) Has been cancelled
build-and-test / windows-2022.Release.static.VS-17-2022 (push) Has been cancelled
build-and-test / windows-2025.Release.shared.VS-18-2025 (push) Has been cancelled
build-and-test / windows-2025.Release.static.VS-18-2025 (push) Has been cancelled
build-and-test / windows-latest.Debug.shared.CLANG64 (push) Has been cancelled
build-and-test / windows-latest.Debug.shared.MINGW64 (push) Has been cancelled
build-and-test / windows-latest.Debug.shared.UCRT64 (push) Has been cancelled
build-and-test / windows-latest.Debug.static.CLANG64 (push) Has been cancelled
build-and-test / windows-latest.Debug.static.MINGW64 (push) Has been cancelled
build-and-test / windows-latest.Debug.static.UCRT64 (push) Has been cancelled
build-and-test / ubuntu-24.04.Release.static.clang++ (push) Has been cancelled
build-and-test / ubuntu-24.04-arm.Release.static.clang++ (push) Has been cancelled
build-and-test / macos-latest.Release.shared.g++ (push) Has been cancelled
build-and-test / ubuntu-22.04.Release.shared.g++ (push) Has been cancelled
build-and-test / ubuntu-24.04.Release.shared.g++ (push) Has been cancelled
build-and-test / ubuntu-24.04-arm.Release.shared.g++ (push) Has been cancelled
build-and-test / ubuntu-24.04.Release.static.g++ (push) Has been cancelled
build-and-test / ubuntu-24.04-arm.Release.static.g++ (push) Has been cancelled
build-and-test / windows-2022.Debug.shared.VS-17-2022 (push) Has been cancelled
build-and-test / windows-2022.Debug.static.VS-17-2022 (push) Has been cancelled
build-and-test / windows-2025.Debug.shared.VS-18-2025 (push) Has been cancelled
build-and-test / windows-latest.Release.static.MINGW64 (push) Has been cancelled
build-and-test / windows-latest.Release.static.UCRT64 (push) Has been cancelled
test-bindings / Test Python 3.10 bindings on macos-latest (push) Has been cancelled
test-bindings / Test Python 3.11 bindings on macos-latest (push) Has been cancelled
test-bindings / Test Python 3.12 bindings on macos-latest (push) Has been cancelled
test-bindings / Test Python 3.13 bindings on macos-latest (push) Has been cancelled
build-and-test / windows-latest.Release.shared.CLANG64 (push) Has been cancelled
build-and-test / windows-latest.Release.shared.MINGW64 (push) Has been cancelled
build-and-test / windows-latest.Release.shared.UCRT64 (push) Has been cancelled
build-and-test / windows-latest.Release.static.CLANG64 (push) Has been cancelled
test-bindings / Test Python 3.12 bindings on windows-latest (push) Has been cancelled
test-bindings / Test Python 3.13 bindings on windows-latest (push) Has been cancelled
test-bindings / Test Python 3.10 bindings on windows-latest (push) Has been cancelled
test-bindings / Test Python 3.11 bindings on windows-latest (push) Has been cancelled
test-bindings / Test Rust bindings on macos-latest (push) Has been cancelled
test-bindings / Test Rust bindings on windows-latest (push) Has been cancelled
247 lines
7.9 KiB
C++
247 lines
7.9 KiB
C++
// Copyright 2016 Ismael Jimenez Martinez. All rights reserved.
|
|
// Copyright 2017 Roman Lebedev. All rights reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
#include "statistics.h"
|
|
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <numeric>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include "benchmark/reporter.h"
|
|
#include "benchmark/statistics.h"
|
|
#include "benchmark/types.h"
|
|
#include "check.h"
|
|
|
|
namespace benchmark {
|
|
|
|
const auto StatisticsSum = [](const std::vector<double>& v) {
|
|
return std::accumulate(v.begin(), v.end(), 0.0);
|
|
};
|
|
|
|
double StatisticsMean(const std::vector<double>& v) {
|
|
if (v.empty()) {
|
|
return 0.0;
|
|
}
|
|
return StatisticsSum(v) * (1.0 / static_cast<double>(v.size()));
|
|
}
|
|
|
|
double StatisticsMedian(const std::vector<double>& v) {
|
|
if (v.size() < 3) {
|
|
return StatisticsMean(v);
|
|
}
|
|
std::vector<double> copy(v);
|
|
|
|
auto center = copy.begin() + v.size() / 2;
|
|
std::nth_element(copy.begin(), center, copy.end());
|
|
|
|
// Did we have an odd number of samples? If yes, then center is the median.
|
|
// If not, then we are looking for the average between center and the value
|
|
// before. Instead of resorting, we just look for the max value before it,
|
|
// which is not necessarily the element immediately preceding `center` Since
|
|
// `copy` is only partially sorted by `nth_element`.
|
|
if (v.size() % 2 == 1) {
|
|
return *center;
|
|
}
|
|
auto center2 = std::max_element(copy.begin(), center);
|
|
return (*center + *center2) / 2.0;
|
|
}
|
|
|
|
// Return the sum of the squares of this sample set
|
|
const auto SumSquares = [](const std::vector<double>& v) {
|
|
return std::inner_product(v.begin(), v.end(), v.begin(), 0.0);
|
|
};
|
|
|
|
const auto Sqr = [](const double dat) { return dat * dat; };
|
|
const auto Sqrt = [](const double dat) {
|
|
// Avoid NaN due to imprecision in the calculations
|
|
if (dat < 0.0) {
|
|
return 0.0;
|
|
}
|
|
return std::sqrt(dat);
|
|
};
|
|
|
|
double StatisticsStdDev(const std::vector<double>& v) {
|
|
const auto mean = StatisticsMean(v);
|
|
if (v.empty()) {
|
|
return mean;
|
|
}
|
|
|
|
// Sample standard deviation is undefined for n = 1
|
|
if (v.size() == 1) {
|
|
return 0.0;
|
|
}
|
|
|
|
const double avg_squares =
|
|
SumSquares(v) * (1.0 / static_cast<double>(v.size()));
|
|
return Sqrt(static_cast<double>(v.size()) /
|
|
(static_cast<double>(v.size()) - 1.0) *
|
|
(avg_squares - Sqr(mean)));
|
|
}
|
|
|
|
double StatisticsCV(const std::vector<double>& v) {
|
|
if (v.size() < 2) {
|
|
return 0.0;
|
|
}
|
|
|
|
const auto stddev = StatisticsStdDev(v);
|
|
const auto mean = StatisticsMean(v);
|
|
|
|
if (std::fpclassify(mean) == FP_ZERO) {
|
|
return 0.0;
|
|
}
|
|
|
|
return stddev / mean;
|
|
}
|
|
|
|
std::vector<BenchmarkReporter::Run> ComputeStats(
|
|
const std::vector<BenchmarkReporter::Run>& reports) {
|
|
typedef BenchmarkReporter::Run Run;
|
|
std::vector<Run> results;
|
|
|
|
const auto is_successful = [](Run const& run) {
|
|
return run.skipped == internal::NotSkipped;
|
|
};
|
|
auto successful_run =
|
|
std::find_if(reports.begin(), reports.end(), is_successful);
|
|
const auto successful_count = static_cast<size_t>(
|
|
std::count_if(reports.begin(), reports.end(), is_successful));
|
|
|
|
if (successful_count < 2) {
|
|
// We don't report aggregated data if there was a single run.
|
|
return results;
|
|
}
|
|
|
|
// Accumulators.
|
|
std::vector<double> real_accumulated_time_stat;
|
|
std::vector<double> cpu_accumulated_time_stat;
|
|
|
|
real_accumulated_time_stat.reserve(successful_count);
|
|
cpu_accumulated_time_stat.reserve(successful_count);
|
|
|
|
// All repetitions should be run with the same number of iterations so we
|
|
// can take this information from the first benchmark.
|
|
const IterationCount run_iterations = successful_run->iterations;
|
|
// create stats for user counters
|
|
struct CounterStat {
|
|
Counter c;
|
|
std::vector<double> s;
|
|
};
|
|
std::map<std::string, CounterStat> counter_stats;
|
|
for (Run const& r : reports) {
|
|
if (!is_successful(r)) {
|
|
continue;
|
|
}
|
|
for (auto const& cnt : r.counters) {
|
|
auto it = counter_stats.find(cnt.first);
|
|
if (it == counter_stats.end()) {
|
|
it = counter_stats
|
|
.emplace(cnt.first,
|
|
CounterStat{cnt.second, std::vector<double>{}})
|
|
.first;
|
|
it->second.s.reserve(successful_count);
|
|
} else {
|
|
BM_CHECK_EQ(it->second.c.flags, cnt.second.flags);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Populate the accumulators.
|
|
for (Run const& run : reports) {
|
|
BM_CHECK_EQ(successful_run->benchmark_name(), run.benchmark_name());
|
|
if (!is_successful(run)) {
|
|
continue;
|
|
}
|
|
BM_CHECK_EQ(run_iterations, run.iterations);
|
|
real_accumulated_time_stat.emplace_back(run.real_accumulated_time);
|
|
cpu_accumulated_time_stat.emplace_back(run.cpu_accumulated_time);
|
|
// user counters
|
|
for (auto const& cnt : run.counters) {
|
|
auto it = counter_stats.find(cnt.first);
|
|
BM_CHECK_NE(it, counter_stats.end());
|
|
it->second.s.emplace_back(cnt.second);
|
|
}
|
|
}
|
|
|
|
// Only add label if it is same for all runs
|
|
std::string report_label = successful_run->report_label;
|
|
for (const Run& run : reports) {
|
|
if (!is_successful(run)) {
|
|
continue;
|
|
}
|
|
if (run.report_label != report_label) {
|
|
report_label = "";
|
|
break;
|
|
}
|
|
}
|
|
|
|
const double iteration_rescale_factor =
|
|
static_cast<double>(successful_count) /
|
|
static_cast<double>(run_iterations);
|
|
|
|
for (const auto& Stat : *successful_run->statistics) {
|
|
// Get the data from the accumulator to BenchmarkReporter::Run's.
|
|
Run data;
|
|
data.run_name = successful_run->run_name;
|
|
data.family_index = successful_run->family_index;
|
|
data.per_family_instance_index = successful_run->per_family_instance_index;
|
|
data.run_type = BenchmarkReporter::Run::RT_Aggregate;
|
|
data.threads = successful_run->threads;
|
|
data.repetitions = successful_run->repetitions;
|
|
data.repetition_index = Run::no_repetition_index;
|
|
data.aggregate_name = Stat.name_;
|
|
data.aggregate_unit = Stat.unit_;
|
|
data.report_label = report_label;
|
|
|
|
// It is incorrect to say that an aggregate is computed over
|
|
// run's iterations, because those iterations already got averaged.
|
|
// Similarly, if there are N repetitions with 1 iterations each,
|
|
// an aggregate will be computed over N measurements, not 1.
|
|
// Thus it is best to simply use the count of separate reports.
|
|
data.iterations = static_cast<IterationCount>(successful_count);
|
|
|
|
data.real_accumulated_time = Stat.compute_(real_accumulated_time_stat);
|
|
data.cpu_accumulated_time = Stat.compute_(cpu_accumulated_time_stat);
|
|
|
|
if (data.aggregate_unit == StatisticUnit::kTime) {
|
|
// We will divide these times by data.iterations when reporting, but the
|
|
// data.iterations is not necessarily the scale of these measurements,
|
|
// because in each repetition, these timers are sum over all the iters.
|
|
// And if we want to say that the stats are over N repetitions and not
|
|
// M iterations, we need to multiply these by (N/M).
|
|
data.real_accumulated_time *= iteration_rescale_factor;
|
|
data.cpu_accumulated_time *= iteration_rescale_factor;
|
|
}
|
|
|
|
data.time_unit = successful_run->time_unit;
|
|
|
|
// user counters
|
|
for (auto const& kv : counter_stats) {
|
|
// Do *NOT* rescale the custom counters. They are already properly scaled.
|
|
const auto uc_stat = Stat.compute_(kv.second.s);
|
|
auto c = Counter(uc_stat, counter_stats[kv.first].c.flags,
|
|
counter_stats[kv.first].c.oneK);
|
|
data.counters[kv.first] = c;
|
|
}
|
|
|
|
results.push_back(data);
|
|
}
|
|
|
|
return results;
|
|
}
|
|
|
|
} // end namespace benchmark
|