Skip to content

Commit

Permalink
Restructure benchmark tests
Browse files Browse the repository at this point in the history
  • Loading branch information
winner245 committed Nov 12, 2024
1 parent 66ed5de commit 87f94b9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 50 deletions.
56 changes: 8 additions & 48 deletions libcxx/test/benchmarks/ContainerBenchmarks.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@

#include <cassert>

#include "Utilities.h"
#include "benchmark/benchmark.h"
#include "Utilities.h"
#include "test_iterators.h"

namespace ContainerBenchmarks {

Expand Down Expand Up @@ -48,55 +49,16 @@ void BM_Assignment(benchmark::State& st, Container) {
}
}

// Wrap any Iterator into an input iterator
template <typename Iterator>
class InputIterator {
using iter_traits = std::iterator_traits<Iterator>;

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 <typename Iterator>
InputIterator<Iterator> make_input_iterator(Iterator it) {
return InputIterator<Iterator>(it);
}

template <class Container,
class GenInputs,
typename std::enable_if<std::is_trivial<typename Container::value_type>::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();
}
}
Expand All @@ -106,14 +68,12 @@ template <class Container,
typename std::enable_if<!std::is_trivial<typename Container::value_type>::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();
}
}
Expand Down
13 changes: 11 additions & 2 deletions libcxx/test/benchmarks/GenerateInput.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,23 @@ inline std::vector<std::string> getRandomStringInputs(std::size_t N) {
return inputs;
}

inline std::vector<std::string> getRandomStringInputsWithLength(std::size_t N, std::size_t len) {
inline std::vector<std::string> getRandomStringInputsWithLength(std::size_t N, std::size_t len) { // N-by-len
std::vector<std::string> 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 <class IntT>
inline std::vector<std::vector<IntT>> getRandomIntegerInputsWithLength(std::size_t N, std::size_t len) { // N-by-len
std::vector<std::vector<IntT>> inputs;
inputs.reserve(N);
for (std::size_t i = 0; i < N; ++i)
inputs.push_back(getRandomIntegerInputs<IntT>(len));
return inputs;
}

inline std::vector<std::string> getPrefixedRandomStringInputs(std::size_t N) {
std::vector<std::string> inputs;
constexpr int kSuffixLength = 32;
Expand Down
4 changes: 4 additions & 0 deletions libcxx/test/benchmarks/vector_operations.bench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,8 @@ BENCHMARK_CAPTURE(BM_AssignInputIterIter, vector_int, std::vector<int>{}, getRan
BENCHMARK_CAPTURE(BM_AssignInputIterIter, vector_string, std::vector<std::string>{}, getRandomStringInputsWithLength)
->Args({TestNumInputs, TestNumInputs});

BENCHMARK_CAPTURE(
BM_AssignInputIterIter, vector_vector_int, std::vector<std::vector<int>>{}, getRandomIntegerInputsWithLength<int>)
->Args({TestNumInputs, TestNumInputs});

BENCHMARK_MAIN();

0 comments on commit 87f94b9

Please sign in to comment.