[libcxx] Add support for benchmark tests using Google Benchmark.
Summary:
This patch does the following:
1. Checks in a copy of the Google Benchmark library into the libc++ repo under `utils/google-benchmark`.
2. Teaches libc++ how to build Google Benchmark against both (A) in-tree libc++ and (B) the platforms native STL.
3. Allows performance benchmarks to be built as part of the libc++ build.
Building the benchmarks (and Google Benchmark) is off by default. It must be enabled using the CMake option `-DLIBCXX_INCLUDE_BENCHMARKS=ON`. When this option is enabled the tests under `libcxx/benchmarks` can be built using the `libcxx-benchmarks` target.
On Linux platforms where libstdc++ is the default STL the CMake option `-DLIBCXX_BUILD_BENCHMARKS_NATIVE_STDLIB=ON` can be used to build each benchmark test against libstdc++ as well. This is useful for comparing performance between standard libraries.
Support for benchmarks is currently very minimal. They must be manually run by the user and there is no mechanism for detecting performance regressions.
Known Issues:
* `-DLIBCXX_INCLUDE_BENCHMARKS=ON` is only supported for Clang, and not GCC, since the `-stdlib=libc++` option is needed to build Google Benchmark.
Reviewers: danalbert, dberlin, chandlerc, mclow.lists, jroelofs
Subscribers: chandlerc, dberlin, tberghammer, danalbert, srhines, hfinkel
Differential Revision: https://reviews.llvm.org/D22240
llvm-svn: 276049
2016-07-20 07:07:03 +08:00
|
|
|
#include <unordered_set>
|
|
|
|
#include <vector>
|
|
|
|
#include <cstdint>
|
|
|
|
|
|
|
|
#include "benchmark/benchmark_api.h"
|
|
|
|
#include "GenerateInput.hpp"
|
|
|
|
|
|
|
|
constexpr std::size_t TestNumInputs = 1024;
|
|
|
|
|
|
|
|
template <class GenInputs>
|
|
|
|
void BM_Sort(benchmark::State& st, GenInputs gen) {
|
|
|
|
using ValueType = typename decltype(gen(0))::value_type;
|
2016-08-10 02:56:48 +08:00
|
|
|
const auto in = gen(st.range(0));
|
[libcxx] Add support for benchmark tests using Google Benchmark.
Summary:
This patch does the following:
1. Checks in a copy of the Google Benchmark library into the libc++ repo under `utils/google-benchmark`.
2. Teaches libc++ how to build Google Benchmark against both (A) in-tree libc++ and (B) the platforms native STL.
3. Allows performance benchmarks to be built as part of the libc++ build.
Building the benchmarks (and Google Benchmark) is off by default. It must be enabled using the CMake option `-DLIBCXX_INCLUDE_BENCHMARKS=ON`. When this option is enabled the tests under `libcxx/benchmarks` can be built using the `libcxx-benchmarks` target.
On Linux platforms where libstdc++ is the default STL the CMake option `-DLIBCXX_BUILD_BENCHMARKS_NATIVE_STDLIB=ON` can be used to build each benchmark test against libstdc++ as well. This is useful for comparing performance between standard libraries.
Support for benchmarks is currently very minimal. They must be manually run by the user and there is no mechanism for detecting performance regressions.
Known Issues:
* `-DLIBCXX_INCLUDE_BENCHMARKS=ON` is only supported for Clang, and not GCC, since the `-stdlib=libc++` option is needed to build Google Benchmark.
Reviewers: danalbert, dberlin, chandlerc, mclow.lists, jroelofs
Subscribers: chandlerc, dberlin, tberghammer, danalbert, srhines, hfinkel
Differential Revision: https://reviews.llvm.org/D22240
llvm-svn: 276049
2016-07-20 07:07:03 +08:00
|
|
|
std::vector<ValueType> inputs[5];
|
|
|
|
auto reset_inputs = [&]() {
|
|
|
|
for (auto& C : inputs) {
|
|
|
|
C = in;
|
|
|
|
benchmark::DoNotOptimize(C.data());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
reset_inputs();
|
|
|
|
while (st.KeepRunning()) {
|
|
|
|
for (auto& I : inputs) {
|
|
|
|
std::sort(I.data(), I.data() + I.size());
|
|
|
|
benchmark::DoNotOptimize(I.data());
|
|
|
|
}
|
|
|
|
st.PauseTiming();
|
|
|
|
reset_inputs();
|
|
|
|
benchmark::ClobberMemory();
|
|
|
|
st.ResumeTiming();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BENCHMARK_CAPTURE(BM_Sort, random_uint32,
|
|
|
|
getRandomIntegerInputs<uint32_t>)->Arg(TestNumInputs);
|
|
|
|
|
|
|
|
BENCHMARK_CAPTURE(BM_Sort, sorted_ascending_uint32,
|
|
|
|
getSortedIntegerInputs<uint32_t>)->Arg(TestNumInputs);
|
|
|
|
|
|
|
|
BENCHMARK_CAPTURE(BM_Sort, sorted_descending_uint32,
|
|
|
|
getReverseSortedIntegerInputs<uint32_t>)->Arg(TestNumInputs);
|
|
|
|
|
|
|
|
BENCHMARK_CAPTURE(BM_Sort, single_element_uint32,
|
|
|
|
getDuplicateIntegerInputs<uint32_t>)->Arg(TestNumInputs);
|
|
|
|
|
|
|
|
BENCHMARK_CAPTURE(BM_Sort, pipe_organ_uint32,
|
|
|
|
getPipeOrganIntegerInputs<uint32_t>)->Arg(TestNumInputs);
|
|
|
|
|
|
|
|
BENCHMARK_CAPTURE(BM_Sort, random_strings,
|
|
|
|
getRandomStringInputs)->Arg(TestNumInputs);
|
|
|
|
|
|
|
|
BENCHMARK_CAPTURE(BM_Sort, sorted_ascending_strings,
|
|
|
|
getSortedStringInputs)->Arg(TestNumInputs);
|
|
|
|
|
|
|
|
BENCHMARK_CAPTURE(BM_Sort, sorted_descending_strings,
|
|
|
|
getReverseSortedStringInputs)->Arg(TestNumInputs);
|
|
|
|
|
|
|
|
BENCHMARK_CAPTURE(BM_Sort, single_element_strings,
|
|
|
|
getDuplicateStringInputs)->Arg(TestNumInputs);
|
|
|
|
|
|
|
|
|
|
|
|
BENCHMARK_MAIN()
|