Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-39663: [C++] Ensure top-level benchmarks present informative metrics #40091

Merged
merged 1 commit into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions cpp/src/arrow/builder_benchmark.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ constexpr int64_t kRounds = 256;
static VectorType kData = AlmostU8CompressibleVector();
constexpr int64_t kBytesProcessPerRound = kNumberOfElements * sizeof(ValueType);
constexpr int64_t kBytesProcessed = kRounds * kBytesProcessPerRound;
constexpr int64_t kItemsProcessed = kRounds * kNumberOfElements;

static const char* kBinaryString = "12345678";
static std::string_view kBinaryView(kBinaryString);
Expand All @@ -73,6 +74,7 @@ static void BuildIntArrayNoNulls(benchmark::State& state) { // NOLINT non-const
}

state.SetBytesProcessed(state.iterations() * kBytesProcessed);
state.SetItemsProcessed(state.iterations() * kItemsProcessed);
}

static void BuildAdaptiveIntNoNulls(
Expand All @@ -89,6 +91,7 @@ static void BuildAdaptiveIntNoNulls(
}

state.SetBytesProcessed(state.iterations() * kBytesProcessed);
state.SetItemsProcessed(state.iterations() * kItemsProcessed);
}

static void BuildAdaptiveIntNoNullsScalarAppend(
Expand All @@ -107,6 +110,7 @@ static void BuildAdaptiveIntNoNullsScalarAppend(
}

state.SetBytesProcessed(state.iterations() * kBytesProcessed);
state.SetItemsProcessed(state.iterations() * kItemsProcessed);
}

static void BuildBooleanArrayNoNulls(
Expand All @@ -127,6 +131,7 @@ static void BuildBooleanArrayNoNulls(
}

state.SetBytesProcessed(state.iterations() * kBytesProcessed);
state.SetItemsProcessed(state.iterations() * kItemsProcessed);
}

static void BuildBinaryArray(benchmark::State& state) { // NOLINT non-const reference
Expand All @@ -142,6 +147,7 @@ static void BuildBinaryArray(benchmark::State& state) { // NOLINT non-const ref
}

state.SetBytesProcessed(state.iterations() * kBytesProcessed);
state.SetItemsProcessed(state.iterations() * kItemsProcessed);
}

static void BuildChunkedBinaryArray(
Expand All @@ -161,6 +167,7 @@ static void BuildChunkedBinaryArray(
}

state.SetBytesProcessed(state.iterations() * kBytesProcessed);
state.SetItemsProcessed(state.iterations() * kItemsProcessed);
}

static void BuildFixedSizeBinaryArray(
Expand All @@ -179,6 +186,7 @@ static void BuildFixedSizeBinaryArray(
}

state.SetBytesProcessed(state.iterations() * kBytesProcessed);
state.SetItemsProcessed(state.iterations() * kItemsProcessed);
}

static void BuildDecimalArray(benchmark::State& state) { // NOLINT non-const reference
Expand All @@ -199,6 +207,7 @@ static void BuildDecimalArray(benchmark::State& state) { // NOLINT non-const re
}

state.SetBytesProcessed(state.iterations() * kRounds * kNumberOfElements * 16);
state.SetItemsProcessed(state.iterations() * kRounds * kNumberOfElements);
}

// ----------------------------------------------------------------------
Expand Down Expand Up @@ -317,6 +326,7 @@ static void BenchmarkDictionaryArray(
fodder_nbytes = fodder.size() * sizeof(Scalar);
}
state.SetBytesProcessed(state.iterations() * fodder_nbytes * kRounds);
state.SetItemsProcessed(state.iterations() * fodder.size() * kRounds);
}

static void BuildInt64DictionaryArrayRandom(
Expand Down Expand Up @@ -361,6 +371,7 @@ static void ArrayDataConstructDestruct(
InitArrays();
arrays.clear();
}
state.SetItemsProcessed(state.iterations() * kNumArrays);
}

// ----------------------------------------------------------------------
Expand Down Expand Up @@ -430,6 +441,7 @@ static void ReferenceBuildVectorNoNulls(
}

state.SetBytesProcessed(state.iterations() * kBytesProcessed);
state.SetItemsProcessed(state.iterations() * kItemsProcessed);
}

BENCHMARK(ReferenceBuildVectorNoNulls);
Expand Down
13 changes: 11 additions & 2 deletions cpp/src/arrow/memory_pool_benchmark.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

namespace arrow {

static constexpr int64_t kCacheLineSize = 64;

struct SystemAlloc {
static Result<MemoryPool*> GetAllocator() { return system_memory_pool(); }
};
Expand Down Expand Up @@ -51,8 +53,8 @@ static void TouchCacheLines(uint8_t* data, int64_t nbytes) {
uint8_t total = 0;
while (nbytes > 0) {
total += *data;
data += 64;
nbytes -= 64;
data += kCacheLineSize;
nbytes -= kCacheLineSize;
}
benchmark::DoNotOptimize(total);
}
Expand All @@ -71,6 +73,8 @@ static void TouchArea(benchmark::State& state) { // NOLINT non-const reference
}

pool->Free(data, nbytes);
state.SetItemsProcessed(state.iterations());
state.SetBytesProcessed(state.iterations() * nbytes);
}

// Benchmark the raw cost of allocating memory.
Expand All @@ -88,6 +92,9 @@ static void AllocateDeallocate(benchmark::State& state) { // NOLINT non-const r
ARROW_CHECK_OK(pool->Allocate(nbytes, &data));
pool->Free(data, nbytes);
}
state.SetItemsProcessed(state.iterations());
// SetBytesProcessed() would give nonsensical figures since the data is not
// actually processed.
}

// Benchmark the cost of allocating memory plus accessing it.
Expand All @@ -103,6 +110,8 @@ static void AllocateTouchDeallocate(
TouchCacheLines(data, nbytes);
pool->Free(data, nbytes);
}
state.SetItemsProcessed(state.iterations());
state.SetBytesProcessed(state.iterations() * nbytes);
}

#define BENCHMARK_ALLOCATE_ARGS \
Expand Down
4 changes: 4 additions & 0 deletions cpp/src/arrow/util/int_util_benchmark.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ static void DetectUIntWidthNoNulls(
benchmark::DoNotOptimize(result);
}
state.SetBytesProcessed(state.iterations() * values.size() * sizeof(uint64_t));
state.SetItemsProcessed(state.iterations() * values.size());
}

static void DetectUIntWidthNulls(benchmark::State& state) { // NOLINT non-const reference
Expand All @@ -76,6 +77,7 @@ static void DetectUIntWidthNulls(benchmark::State& state) { // NOLINT non-const
benchmark::DoNotOptimize(result);
}
state.SetBytesProcessed(state.iterations() * values.size() * sizeof(uint64_t));
state.SetItemsProcessed(state.iterations() * values.size());
}

static void DetectIntWidthNoNulls(
Expand All @@ -87,6 +89,7 @@ static void DetectIntWidthNoNulls(
benchmark::DoNotOptimize(result);
}
state.SetBytesProcessed(state.iterations() * values.size() * sizeof(uint64_t));
state.SetItemsProcessed(state.iterations() * values.size());
}

static void DetectIntWidthNulls(benchmark::State& state) { // NOLINT non-const reference
Expand All @@ -99,6 +102,7 @@ static void DetectIntWidthNulls(benchmark::State& state) { // NOLINT non-const
benchmark::DoNotOptimize(result);
}
state.SetBytesProcessed(state.iterations() * values.size() * sizeof(uint64_t));
state.SetItemsProcessed(state.iterations() * values.size());
}

static void CheckIndexBoundsInt32(
Expand Down
6 changes: 6 additions & 0 deletions cpp/src/arrow/util/range_benchmark.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ void for_loop(benchmark::State& state) {
for (auto _ : state) {
for (int64_t index = 0; index < kSize; ++index) target[index] = source[index] + 1;
}
state.SetItemsProcessed(state.iterations() * kSize);
}

BENCHMARK(for_loop);
Expand All @@ -58,6 +59,7 @@ void std_copy(benchmark::State& state) {
for (auto _ : state) {
std::copy(source.begin(), source.end(), target.begin());
}
state.SetItemsProcessed(state.iterations() * kSize);
}

BENCHMARK(std_copy);
Expand All @@ -71,6 +73,7 @@ void std_copy_converting(benchmark::State& state) {
for (auto _ : state) {
std::copy(source.begin(), source.end(), target.begin());
}
state.SetItemsProcessed(state.iterations() * kSize);
}

BENCHMARK(std_copy_converting);
Expand All @@ -85,6 +88,7 @@ void lazy_copy(benchmark::State& state) {
for (auto _ : state) {
std::copy(lazy_range.begin(), lazy_range.end(), target.begin());
}
state.SetItemsProcessed(state.iterations() * kSize);
}

BENCHMARK(lazy_copy);
Expand All @@ -101,6 +105,7 @@ void lazy_copy_converting(benchmark::State& state) {
for (auto _ : state) {
std::copy(lazy_range.begin(), lazy_range.end(), target.begin());
}
state.SetItemsProcessed(state.iterations() * kSize);
}

BENCHMARK(lazy_copy_converting);
Expand All @@ -119,6 +124,7 @@ void lazy_postinc(benchmark::State& state) {

while (lazy_iter != lazy_end) *(target_iter++) = *(lazy_iter++);
}
state.SetItemsProcessed(state.iterations() * kSize);
}

BENCHMARK(lazy_postinc);
Expand Down
Loading