From 87f94b9cb794888052b19ad2e8630f9c79a56693 Mon Sep 17 00:00:00 2001 From: Peng Liu Date: Tue, 12 Nov 2024 10:59:11 -0500 Subject: [PATCH] Restructure benchmark tests --- libcxx/test/benchmarks/ContainerBenchmarks.h | 56 +++---------------- libcxx/test/benchmarks/GenerateInput.h | 13 ++++- .../benchmarks/vector_operations.bench.cpp | 4 ++ 3 files changed, 23 insertions(+), 50 deletions(-) diff --git a/libcxx/test/benchmarks/ContainerBenchmarks.h b/libcxx/test/benchmarks/ContainerBenchmarks.h index 63d5d9829464e4..111c2485ef33b6 100644 --- a/libcxx/test/benchmarks/ContainerBenchmarks.h +++ b/libcxx/test/benchmarks/ContainerBenchmarks.h @@ -12,8 +12,9 @@ #include -#include "Utilities.h" #include "benchmark/benchmark.h" +#include "Utilities.h" +#include "test_iterators.h" namespace ContainerBenchmarks { @@ -48,55 +49,16 @@ void BM_Assignment(benchmark::State& st, Container) { } } -// Wrap any Iterator into an input iterator -template -class InputIterator { - using iter_traits = std::iterator_traits; - -public: - using iterator_category = std::input_iterator_tag; - using value_type = typename iter_traits::value_type; - using difference_type = typename iter_traits::difference_type; - using pointer = typename iter_traits::pointer; - using reference = typename iter_traits::reference; - - InputIterator(Iterator it) : current_(it) {} - - reference operator*() { return *current_; } - InputIterator& operator++() { - ++current_; - return *this; - } - InputIterator operator++(int) { - InputIterator tmp = *this; - ++(*this); - return tmp; - } - - friend bool operator==(const InputIterator& lhs, const InputIterator& rhs) { return lhs.current_ == rhs.current_; } - friend bool operator!=(const InputIterator& lhs, const InputIterator& rhs) { return !(lhs == rhs); } - -private: - Iterator current_; -}; - -template -InputIterator make_input_iterator(Iterator it) { - return InputIterator(it); -} - template ::value>::type* = nullptr> void BM_AssignInputIterIter(benchmark::State& st, Container c, GenInputs gen) { auto in = gen(st.range(1)); + c.resize(st.range(0)); benchmark::DoNotOptimize(&in); + benchmark::DoNotOptimize(&c); for (auto _ : st) { - st.PauseTiming(); - c.resize(st.range(0)); - benchmark::DoNotOptimize(&c); - st.ResumeTiming(); - c.assign(make_input_iterator(in.begin()), make_input_iterator(in.end())); + c.assign(cpp17_input_iterator(in.begin()), cpp17_input_iterator(in.end())); benchmark::ClobberMemory(); } } @@ -106,14 +68,12 @@ template ::value>::type* = nullptr> void BM_AssignInputIterIter(benchmark::State& st, Container c, GenInputs gen) { auto v = gen(1, 100); + c.resize(st.range(0), v[0]); auto in = gen(st.range(1), 32); benchmark::DoNotOptimize(&in); + benchmark::DoNotOptimize(&c); for (auto _ : st) { - st.PauseTiming(); - c.resize(st.range(0), v[0]); - benchmark::DoNotOptimize(&c); - st.ResumeTiming(); - c.assign(make_input_iterator(in.begin()), make_input_iterator(in.end())); + c.assign(cpp17_input_iterator(in.begin()), cpp17_input_iterator(in.end())); benchmark::ClobberMemory(); } } diff --git a/libcxx/test/benchmarks/GenerateInput.h b/libcxx/test/benchmarks/GenerateInput.h index c815be7587c5ca..c5695ef2c64943 100644 --- a/libcxx/test/benchmarks/GenerateInput.h +++ b/libcxx/test/benchmarks/GenerateInput.h @@ -116,14 +116,23 @@ inline std::vector getRandomStringInputs(std::size_t N) { return inputs; } -inline std::vector getRandomStringInputsWithLength(std::size_t N, std::size_t len) { +inline std::vector getRandomStringInputsWithLength(std::size_t N, std::size_t len) { // N-by-len std::vector inputs; inputs.reserve(N); - for (size_t i = 0; i < N; ++i) + for (std::size_t i = 0; i < N; ++i) inputs.push_back(getRandomString(len)); return inputs; } +template +inline std::vector> getRandomIntegerInputsWithLength(std::size_t N, std::size_t len) { // N-by-len + std::vector> inputs; + inputs.reserve(N); + for (std::size_t i = 0; i < N; ++i) + inputs.push_back(getRandomIntegerInputs(len)); + return inputs; +} + inline std::vector getPrefixedRandomStringInputs(std::size_t N) { std::vector inputs; constexpr int kSuffixLength = 32; diff --git a/libcxx/test/benchmarks/vector_operations.bench.cpp b/libcxx/test/benchmarks/vector_operations.bench.cpp index ae09d3f5ec8831..0bae771b0904fc 100644 --- a/libcxx/test/benchmarks/vector_operations.bench.cpp +++ b/libcxx/test/benchmarks/vector_operations.bench.cpp @@ -75,4 +75,8 @@ BENCHMARK_CAPTURE(BM_AssignInputIterIter, vector_int, std::vector{}, getRan BENCHMARK_CAPTURE(BM_AssignInputIterIter, vector_string, std::vector{}, getRandomStringInputsWithLength) ->Args({TestNumInputs, TestNumInputs}); +BENCHMARK_CAPTURE( + BM_AssignInputIterIter, vector_vector_int, std::vector>{}, getRandomIntegerInputsWithLength) + ->Args({TestNumInputs, TestNumInputs}); + BENCHMARK_MAIN();