From f8f1443d8b2066e3784dfdb7ef64e8e7402eb6f8 Mon Sep 17 00:00:00 2001 From: Rong Ma Date: Fri, 9 Aug 2024 10:18:52 +0000 Subject: [PATCH] add benchmark --- velox/row/CMakeLists.txt | 10 ++- velox/row/benchmark/CMakeLists.txt | 22 ++++++ .../benchmark/UnsafeRowSerializeBenchmark.cpp | 76 ++++++++++++------- 3 files changed, 76 insertions(+), 32 deletions(-) create mode 100644 velox/row/benchmark/CMakeLists.txt diff --git a/velox/row/CMakeLists.txt b/velox/row/CMakeLists.txt index 9eb9cb524682e..2b626a9ad716c 100644 --- a/velox/row/CMakeLists.txt +++ b/velox/row/CMakeLists.txt @@ -16,6 +16,10 @@ velox_add_library(velox_row_fast CompactRow.cpp UnsafeRowFast.cpp) velox_link_libraries(velox_row_fast PUBLIC velox_vector) -if(${VELOX_BUILD_TESTING}) - add_subdirectory(tests) -endif() +if (${VELOX_BUILD_TESTING}) + add_subdirectory(tests) +endif () + +if (${VELOX_ENABLE_BENCHMARKS}) + add_subdirectory(benchmark) +endif () diff --git a/velox/row/benchmark/CMakeLists.txt b/velox/row/benchmark/CMakeLists.txt new file mode 100644 index 0000000000000..bb13eb1bf2523 --- /dev/null +++ b/velox/row/benchmark/CMakeLists.txt @@ -0,0 +1,22 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# 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. + +add_executable(velox_unsafe_row_serialize_benchmark UnsafeRowSerializeBenchmark.cpp) +target_link_libraries(velox_unsafe_row_serialize_benchmark PRIVATE + ${FOLLY_BENCHMARK} + velox_common_base + velox_exec + velox_row_fast + velox_vector_fuzzer + Folly::folly) \ No newline at end of file diff --git a/velox/row/benchmark/UnsafeRowSerializeBenchmark.cpp b/velox/row/benchmark/UnsafeRowSerializeBenchmark.cpp index 3f59de1a814c3..4ee3cfd9ea9e8 100644 --- a/velox/row/benchmark/UnsafeRowSerializeBenchmark.cpp +++ b/velox/row/benchmark/UnsafeRowSerializeBenchmark.cpp @@ -65,6 +65,44 @@ class SerializeBenchmark { VELOX_CHECK_EQ(serialized.size(), data->size()); } + void serializeCompactRange(const RowTypePtr& rowType) { + folly::BenchmarkSuspender suspender; + auto data = makeData(rowType); + suspender.dismiss(); + + auto numRows = data->size(); + std::vector offsets(numRows); + + CompactRow compact(data); + + size_t totalSize = 0; + if (auto fixedRowSize = CompactRow::fixedRowSize(rowType)) { + totalSize = fixedRowSize.value() * numRows; + for (auto i = 0; i < numRows; ++i) { + offsets[i] = fixedRowSize.value() * i; + } + } else { + for (auto i = 0; i < numRows; ++i) { + offsets[i] = totalSize; + totalSize += compact.rowSize(i); + } + } + + IndexRange indexRange{0, numRows}; + auto buffer = AlignedBuffer::allocate(totalSize, pool(), 0); + auto rawBuffer = buffer->asMutable(); + compact.serialize(indexRange, offsets, rawBuffer); + VELOX_CHECK_EQ(offsets.back(), totalSize); + + std::vector serialized; + serialized.push_back(std::string_view(rawBuffer, offsets[0])); + for (auto i = 1; i < numRows; ++i) { + serialized.push_back(std::string_view( + rawBuffer + offsets[i - 1], offsets[i] - offsets[i - 1])); + } + VELOX_CHECK_EQ(serialized.size(), data->size()); + } + void deserializeCompact(const RowTypePtr& rowType) { folly::BenchmarkSuspender suspender; auto data = makeData(rowType); @@ -205,35 +243,15 @@ class SerializeBenchmark { memory::memoryManager()->addLeafPool()}; }; -#define SERDE_BENCHMARKS(name, rowType) \ - BENCHMARK(unsafe_serialize_##name) { \ - SerializeBenchmark benchmark; \ - benchmark.serializeUnsafe(rowType); \ - } \ - \ - BENCHMARK(compact_serialize_##name) { \ - SerializeBenchmark benchmark; \ - benchmark.serializeCompact(rowType); \ - } \ - \ - BENCHMARK(container_serialize_##name) { \ - SerializeBenchmark benchmark; \ - benchmark.serializeContainer(rowType); \ - } \ - \ - BENCHMARK(unsafe_deserialize_##name) { \ - SerializeBenchmark benchmark; \ - benchmark.deserializeUnsafe(rowType); \ - } \ - \ - BENCHMARK(compact_deserialize_##name) { \ - SerializeBenchmark benchmark; \ - benchmark.deserializeCompact(rowType); \ - } \ - \ - BENCHMARK(container_deserialize_##name) { \ - SerializeBenchmark benchmark; \ - benchmark.deserializeContainer(rowType); \ +#define SERDE_BENCHMARKS(name, rowType) \ + BENCHMARK(compact_serialize_##name) { \ + SerializeBenchmark benchmark; \ + benchmark.serializeCompact(rowType); \ + } \ + \ + BENCHMARK(compact_serialize_range_##name) { \ + SerializeBenchmark benchmark; \ + benchmark.serializeCompactRange(rowType); \ } SERDE_BENCHMARKS(