Skip to content
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
19 changes: 19 additions & 0 deletions benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,25 @@ if(UNIX AND NOT APPLE)
endif()

if(PAIMON_BUILD_BENCHMARKS)
if(PAIMON_ENABLE_ORC)
add_paimon_benchmark(sequential_reader_benchmark
SOURCES
sequential_reader_benchmark.cpp
STATIC_LINK_LIBS
arrow
parquet
paimon_static
${PAIMON_LOCAL_FILE_SYSTEM_STATIC_LINK_LIBS}
${PAIMON_PARQUET_FILE_FORMAT_STATIC_LINK_LIBS}
${PAIMON_BLOB_FILE_FORMAT_STATIC_LINK_LIBS}
${PAIMON_ORC_FILE_FORMAT_STATIC_LINK_LIBS}
${PAIMON_AVRO_FILE_FORMAT_STATIC_LINK_LIBS}
test_utils_static
Threads::Threads
${PAIMON_BENCHMARK_PLATFORM_LINK_LIBS}
${PAIMON_BENCHMARK_LINK_TOOLCHAIN})
endif()

add_paimon_benchmark(read_write_benchmark
SOURCES
benchmark_helpers.cpp
Expand Down
233 changes: 233 additions & 0 deletions benchmark/sequential_reader_benchmark.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/

#include <chrono>
#include <cstdint>
#include <filesystem>
#include <memory>
#include <string>
#include <thread>
#include <utility>
#include <vector>

#include "arrow/c/abi.h"
#include "benchmark/benchmark.h"
#include "fmt/format.h"
#include "paimon/common/reader/complete_row_kind_batch_reader.h"
#include "paimon/common/reader/concat_batch_reader.h"
#include "paimon/common/reader/reader_utils.h"
#include "paimon/common/utils/arrow/mem_utils.h"
#include "paimon/common/utils/object_utils.h"
#include "paimon/core/io/data_file_meta.h"
#include "paimon/core/operation/raw_file_split_read.h"
#include "paimon/core/utils/file_store_path_factory.h"
#include "paimon/format/file_format.h"
#include "paimon/testing/utils/binary_row_generator.h"
#include "paimon/testing/utils/counting_reader_file_system.h"
#include "paimon/testing/utils/testharness.h"

namespace paimon::test {
namespace {

bool benchmark_failed = false;

struct Fixture {
std::unique_ptr<UniqueTestDirectory> directory;
std::string table_path;
std::shared_ptr<TableSchema> table_schema;
std::vector<std::shared_ptr<DataFileMeta>> files;
};

Result<Fixture> PrepareFiles(int64_t count) {
Fixture fixture;
fixture.directory = UniqueTestDirectory::Create();
if (!fixture.directory) {
return Status::IOError("cannot create benchmark directory");
}
fixture.table_path =
GetDataDir() + "/orc/multi_partition_append_table.db/multi_partition_append_table";
SchemaManager schemas(fixture.directory->GetFileSystem(), fixture.table_path);
PAIMON_ASSIGN_OR_RAISE(fixture.table_schema, schemas.ReadSchema(0));
const std::string source =
fixture.table_path + "/f1=10/f2=0/bucket-0/data-01b6a930-6564-409b-b8f4-ed1307790d72-0.orc";
PAIMON_ASSIGN_OR_RAISE(FileStatus source_status,
fixture.directory->GetFileSystem()->GetFileStatus(source));
for (int64_t i = 0; i < count; i++) {
const std::string name = fmt::format("data-{}.orc", i);
const std::string path = fixture.directory->Str() + "/" + name;
std::error_code error;
std::filesystem::copy_file(source, path, error);
if (error) {
return Status::IOError(error.message());
}
PAIMON_ASSIGN_OR_RAISE(
std::shared_ptr<DataFileMeta> file,
DataFileMeta::ForAppend(name, source_status.GetLen(), /*row_count=*/3,
SimpleStats::EmptyStats(), /*min_sequence_number=*/0,
/*max_sequence_number=*/2, /*schema_id=*/0,
FileSource::Append(), /*value_stats_cols=*/std::nullopt, path,
/*first_row_id=*/std::nullopt, /*write_cols=*/std::nullopt));
fixture.files.push_back(std::move(file));
}
return fixture;
}

struct ScanStats {
double seconds = 0;
double first_batch_us = 0;
uint64_t initial_footer_reads = 0;
uint64_t first_batch_opens = 0;
uint64_t opens = 0;
uint64_t peak_streams = 0;
uint64_t pool_peak_bytes = 0;
uint64_t read_bytes = 0;
int64_t rows = 0;
};

Result<ScanStats> Scan(const Fixture& fixture, bool lazy, bool first_only, int64_t open_delay_us) {
std::shared_ptr<MemoryPool> pool = GetMemoryPool();
auto fs = std::make_shared<CountingReaderFileSystem>(".orc");
if (open_delay_us > 0) {
fs->before_open = [open_delay_us](const std::string&) {
std::this_thread::sleep_for(std::chrono::microseconds(open_delay_us));
return Status::OK();
};
}
ReadContextBuilder builder(fixture.table_path);
builder.WithFileSystem(fs).WithMemoryPool(pool).EnablePrefetch(false);
builder.AddOption(Options::FILE_INDEX_READ_ENABLED, "false");
PAIMON_ASSIGN_OR_RAISE(std::shared_ptr<ReadContext> read_context, builder.Finish());
auto read_options = fixture.table_schema->Options();
for (const auto& [key, value] : read_context->GetOptions()) {
read_options.insert_or_assign(key, value);
}
PAIMON_ASSIGN_OR_RAISE(
std::shared_ptr<InternalReadContext> context,
InternalReadContext::Create(read_context, fixture.table_schema, read_options));
const auto& options = context->GetCoreOptions();
auto schema = DataField::ConvertDataFieldsToArrowSchema(fixture.table_schema->Fields());
PAIMON_ASSIGN_OR_RAISE(
std::shared_ptr<FileStorePathFactory> paths,
FileStorePathFactory::Create(
fixture.table_path, schema, fixture.table_schema->PartitionKeys(),
options.GetPartitionDefaultName(), options.GetFileFormat()->Identifier(),
options.DataFilePrefix(), options.LegacyPartitionNameEnabled(), {}, {},
options.IndexFileInDataFileDir(), {}, pool));
auto partition = BinaryRowGenerator::GenerateRow({10, 0}, pool.get());
PAIMON_ASSIGN_OR_RAISE(std::shared_ptr<DataFilePathFactory> data_paths,
paths->CreateDataFilePathFactory(partition, 0));
RawFileSplitRead split_read(paths, context, pool, context->GetExecutor());
auto arrow_pool = GetArrowPool(pool);
const auto start = std::chrono::steady_clock::now();
std::unique_ptr<BatchReader> reader;
if (lazy) {
PAIMON_ASSIGN_OR_RAISE(
reader, split_read.CreateReader(partition, 0, fixture.files, DeletionVector::Factory(),
std::nullopt));
} else {
PAIMON_ASSIGN_OR_RAISE(
std::vector<std::unique_ptr<FileBatchReader>> children,
split_read.CreateRawFileReaders(partition, fixture.files, context->GetReadSchema(),
nullptr, {}, std::nullopt, data_paths, {}));
auto concat = std::make_unique<ConcatBatchReader>(
ObjectUtils::MoveVector<std::unique_ptr<BatchReader>>(std::move(children)), arrow_pool);
reader = std::make_unique<CompleteRowKindBatchReader>(std::move(concat), arrow_pool);
}
ScanStats stats;
// With prefetch disabled and before NextBatch, these are the ORC footer/setup reads.
stats.initial_footer_reads = fs->counts->reads.load();
while (true) {
PAIMON_ASSIGN_OR_RAISE(BatchReader::ReadBatch batch, reader->NextBatch());
if (BatchReader::IsEofBatch(batch)) {
break;
}
if (stats.rows == 0) {
stats.first_batch_us =
std::chrono::duration<double, std::micro>(std::chrono::steady_clock::now() - start)
.count();
stats.first_batch_opens = fs->counts->opens.load();
}
stats.rows += batch.first->length;
ReaderUtils::ReleaseReadBatch(std::move(batch));
if (first_only) {
break;
}
}
reader->Close();
stats.seconds = std::chrono::duration<double>(std::chrono::steady_clock::now() - start).count();
stats.opens = fs->counts->opens.load();
stats.peak_streams = fs->counts->peak.load();
stats.read_bytes = fs->counts->bytes.load();
stats.pool_peak_bytes = pool->MaxMemoryUsage();
const int64_t expected = first_only ? 3 : 3 * static_cast<int64_t>(fixture.files.size());
if (stats.rows != expected || fs->counts->live.load() != 0) {
return Status::Invalid("unexpected row count or retained streams after Close");
}
return stats;
}

void BM_SequentialReaders(::benchmark::State& state) {
auto fixture = PrepareFiles(state.range(0));
if (!fixture.ok()) {
benchmark_failed = true;
state.SkipWithError(fixture.status().ToString().c_str());
return;
}
int64_t rows = 0;
for (auto _ : state) {
auto scan = Scan(fixture.value(), state.range(1) != 0, state.range(2) != 0, state.range(3));
if (!scan.ok()) {
benchmark_failed = true;
state.SkipWithError(scan.status().ToString().c_str());
return;
}
const auto& stats = scan.value();
state.SetIterationTime(stats.seconds);
rows += stats.rows;
state.counters["first_batch_us"] += stats.first_batch_us;
state.counters["initial_footer_reads"] += stats.initial_footer_reads;
state.counters["first_batch_opens"] += stats.first_batch_opens;
state.counters["opens"] += stats.opens;
state.counters["peak_streams"] += stats.peak_streams;
state.counters["pool_peak_bytes"] += stats.pool_peak_bytes;
state.counters["read_bytes"] += stats.read_bytes;
}
for (auto& [name, counter] : state.counters) {
counter.flags = ::benchmark::Counter::kAvgIterations;
}
state.SetItemsProcessed(rows);
}

BENCHMARK(BM_SequentialReaders)
->ArgsProduct({{128, 1024}, {0, 1}, {0, 1}, {0, 1000}})
->ArgNames({"files", "lazy", "first_only", "open_delay_us"})
->UseManualTime();

} // namespace
} // namespace paimon::test

int32_t main(int32_t argc, char** argv) {
::benchmark::Initialize(&argc, argv);
if (::benchmark::ReportUnrecognizedArguments(argc, argv)) {
return 1;
}
::benchmark::RunSpecifiedBenchmarks();
::benchmark::Shutdown();
return paimon::test::benchmark_failed ? 1 : 0;
}
28 changes: 28 additions & 0 deletions docs/source/examples/benchmark.rst
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,31 @@ Read batch size sweep, repeated for a stable comparison::
Null density on both sides, to see what definition levels cost::

paimon-parquet-format-benchmark --benchmark_filter='Parquet(Write|Read)_Nulls'

Sequential small-file reads
===========================

``paimon-sequential-reader-benchmark`` compares eager construction (``lazy:0``)
with the bounded raw-reader window (``lazy:1``). It requires ORC and
``PAIMON_BUILD_BENCHMARKS=ON``. Each case copies the small checked-in ORC fixture
to 128 or 1024 distinct temporary files, outside the measured interval.
``first_only:1`` closes after the first batch; ``first_only:0`` consumes every
file. Both paths use the same row-kind wrapper and disable batch prefetch to
isolate reader construction.

The measured interval includes reader construction, consumption and close.
Counters report first-batch latency, first-batch and total file opens, initial
footer/setup read calls, peak retained streams, peak Paimon pool allocation,
and bytes read. Pool allocation excludes memory allocated outside Paimon's
pool and is not process RSS. ``items_per_second`` reports returned rows per
second. An optional 1000-microsecond delay per open simulates remote setup
latency; it does not model remote bandwidth or cache behavior.

Run a repeated comparison and save the counters::

cmake --build build --target paimon-sequential-reader-benchmark
./build/debug/paimon-sequential-reader-benchmark \
--benchmark_filter='BM_SequentialReaders/files:1024/' \
--benchmark_repetitions=5 \
--benchmark_out=sequential-readers.json \
--benchmark_out_format=json
25 changes: 25 additions & 0 deletions docs/source/user_guide/prefetch.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,27 @@ Prefetch is enabled when the estimated compressed size of the selected columns
exceeds the configured threshold; otherwise the reader uses the normal
single-reader path.

Sequential file preparation
===========================

Append/raw reads construct a sliding window of at most four data-file reader
stacks per split, including construction tasks still in flight. The initial
window is built on the dedicated reader-build executor before ``CreateReader``
returns. As a file reaches EOF, its reader is closed, its metrics are retained,
and the next file is submitted for construction while prepared files are read.
This bounds open streams and format metadata independently of the number of
files in the split. Per-file metadata and owned creation inputs still scale
with the split's file count. Merge-on-read keeps its eager input construction.

Plan validation still runs during ``CreateReader``. File-open and reader-build
errors in the initial window are returned there; errors for later files are
returned through ``NextBatch`` or ``NextBatchWithBitmap`` when that file is
reached. These errors are terminal. The returned reader retains its creation
context, schemas, file system and memory pools, so it may outlive the object
that created it. Closing or destroying it cancels queued construction, waits
for already running construction, and releases the prepared readers without
scheduling the unopened tail.

Warmup
======

Expand All @@ -83,6 +104,10 @@ the default ``loser-tree`` sort engine additionally warms the first file of
every sorted run before merging. It applies to append-table reads and append
compaction as well, but does not cross split boundaries.

For lazy append/raw readers, warmup uses the next file once its construction
has completed; reading the current file never waits for lookahead construction.
The construction window is independent of the warmup level below.

How far the next file is prepared is set with
``ReadContextBuilder::SetWarmupLevel()``. Append compaction builds its own read
context and always uses the default level.
Expand Down
Loading
Loading