From dc093b930a96028554e9b0f8ccda3e0d18735c39 Mon Sep 17 00:00:00 2001 From: AdityaPandeyCN Date: Wed, 24 Sep 2025 14:03:34 +0530 Subject: [PATCH 1/4] query performance Signed-off-by: AdityaPandeyCN --- benchmark/CMakeLists.txt | 35 ++++++++-- benchmark/region_query_benchmark.cxx | 100 +++++++++++++++++++++++++++ 2 files changed, 131 insertions(+), 4 deletions(-) create mode 100644 benchmark/region_query_benchmark.cxx diff --git a/benchmark/CMakeLists.txt b/benchmark/CMakeLists.txt index c248384..84f1946 100644 --- a/benchmark/CMakeLists.txt +++ b/benchmark/CMakeLists.txt @@ -9,8 +9,19 @@ target_include_directories(sam_generator PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ) -target_link_libraries(sam_generator PRIVATE benchmark::benchmark) -add_dependencies(sam_generator benchmark::benchmark) +target_link_libraries(sam_generator PRIVATE + benchmark::benchmark +) + +add_library(ramtools_views STATIC + ${CMAKE_SOURCE_DIR}/tools/ramview.cxx + ${CMAKE_SOURCE_DIR}/tools/ramntupleview.cxx +) + +target_link_libraries(ramtools_views PRIVATE + ramcore + ROOT::TreePlayer +) ROOT_EXECUTABLE(sam_to_ram_benchmark sam_to_ram_benchmark.cxx @@ -28,14 +39,30 @@ ROOT_EXECUTABLE(conversion_time_benchmark sam_generator ) -install(TARGETS sam_to_ram_benchmark conversion_time_benchmark +ROOT_EXECUTABLE(region_query_benchmark + region_query_benchmark.cxx + LIBRARIES + benchmark::benchmark + ramcore + sam_generator + ramtools_views +) + +install(TARGETS sam_to_ram_benchmark conversion_time_benchmark region_query_benchmark RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} ) add_custom_target(benchmark + COMMAND ${CMAKE_COMMAND} -E echo "=== SAM to RAM Benchmark ===" COMMAND sam_to_ram_benchmark + COMMAND ${CMAKE_COMMAND} -E echo "" + COMMAND ${CMAKE_COMMAND} -E echo "=== Conversion Time Benchmark ===" COMMAND conversion_time_benchmark - DEPENDS sam_to_ram_benchmark conversion_time_benchmark + COMMAND ${CMAKE_COMMAND} -E echo "" + COMMAND ${CMAKE_COMMAND} -E echo "=== Region Query Benchmark ===" + COMMAND region_query_benchmark + DEPENDS sam_to_ram_benchmark conversion_time_benchmark region_query_benchmark WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + COMMENT "Running all RAM tools benchmarks" ) diff --git a/benchmark/region_query_benchmark.cxx b/benchmark/region_query_benchmark.cxx new file mode 100644 index 0000000..e97e431 --- /dev/null +++ b/benchmark/region_query_benchmark.cxx @@ -0,0 +1,100 @@ +#include +#include "generate_sam_benchmark.h" +#include "ramcore/SamToTTree.h" +#include "ramcore/SamToNTuple.h" +#include +#include + +#ifdef _WIN32 +#define NULL_DEVICE "NUL" +#else +#define NULL_DEVICE "/dev/null" +#endif + +// Forward declarations +void ramview(const char *file, const char *query, bool cache = true, bool perfstats = false, + const char *perfstatsfilename = "perf.root"); +void ramntupleview(const char *file, const char *query, bool cache = true, + bool perfstats = false, const char *perfstatsfilename = "perf.root"); + +class RegionQueryFixture : public benchmark::Fixture { +public: + void SetUp(const benchmark::State& state) override { + num_reads_ = static_cast(state.range(0)); + sam_file_ = "region_query_test_" + std::to_string(num_reads_) + ".sam"; + + GenerateSAMFile(sam_file_, num_reads_); + } + + void TearDown(const benchmark::State&) override { + std::remove(sam_file_.c_str()); + } + +protected: + int num_reads_; + std::string sam_file_; + static constexpr const char* region_ = "chr1:1-100000000"; + + void suppress_output() { + freopen(NULL_DEVICE, "w", stdout); + } + + void restore_output() { + freopen("/dev/tty", "w", stdout); + } +}; + +BENCHMARK_DEFINE_F(RegionQueryFixture, TTree)(benchmark::State& state) { + std::string root_file = "ttree_" + std::to_string(num_reads_) + ".root"; + + // Convert once before timing + suppress_output(); + samtoram(sam_file_.c_str(), root_file.c_str(), true, true, true, 1, 0); + restore_output(); + + // Benchmark only the query performance + for (auto _ : state) { + suppress_output(); + ramview(root_file.c_str(), region_, true, false, "perf.root"); + restore_output(); + } + + std::remove(root_file.c_str()); + + state.counters["reads_per_sec"] = benchmark::Counter(num_reads_, benchmark::Counter::kIsRate); +} + +BENCHMARK_DEFINE_F(RegionQueryFixture, RNTuple)(benchmark::State& state) { + std::string root_file = "rntuple_" + std::to_string(num_reads_) + ".root"; + + // Convert once before timing + suppress_output(); + samtoramntuple(sam_file_.c_str(), root_file.c_str(), true, true, true, 505, 0); + restore_output(); + + // Benchmark only the query performance + for (auto _ : state) { + suppress_output(); + ramntupleview(root_file.c_str(), region_, true, false, "perf.root"); + restore_output(); + } + + std::remove(root_file.c_str()); + + state.counters["reads_per_sec"] = benchmark::Counter(num_reads_, benchmark::Counter::kIsRate); +} + +BENCHMARK_REGISTER_F(RegionQueryFixture, TTree) + ->Args({1000}) + ->Args({10000}) + ->Args({100000}) + ->Unit(benchmark::kMillisecond); + +BENCHMARK_REGISTER_F(RegionQueryFixture, RNTuple) + ->Args({1000}) + ->Args({10000}) + ->Args({100000}) + ->Unit(benchmark::kMillisecond); + +BENCHMARK_MAIN(); + From 56849d6fe6b27f493bccc3dca3020255b7342d31 Mon Sep 17 00:00:00 2001 From: AdityaPandeyCN Date: Wed, 24 Sep 2025 14:06:13 +0530 Subject: [PATCH 2/4] clang format Signed-off-by: AdityaPandeyCN --- benchmark/region_query_benchmark.cxx | 132 +++++++++++++-------------- 1 file changed, 64 insertions(+), 68 deletions(-) diff --git a/benchmark/region_query_benchmark.cxx b/benchmark/region_query_benchmark.cxx index e97e431..a537539 100644 --- a/benchmark/region_query_benchmark.cxx +++ b/benchmark/region_query_benchmark.cxx @@ -14,87 +14,83 @@ // Forward declarations void ramview(const char *file, const char *query, bool cache = true, bool perfstats = false, const char *perfstatsfilename = "perf.root"); -void ramntupleview(const char *file, const char *query, bool cache = true, - bool perfstats = false, const char *perfstatsfilename = "perf.root"); +void ramntupleview(const char *file, const char *query, bool cache = true, bool perfstats = false, + const char *perfstatsfilename = "perf.root"); class RegionQueryFixture : public benchmark::Fixture { public: - void SetUp(const benchmark::State& state) override { - num_reads_ = static_cast(state.range(0)); - sam_file_ = "region_query_test_" + std::to_string(num_reads_) + ".sam"; - - GenerateSAMFile(sam_file_, num_reads_); - } - - void TearDown(const benchmark::State&) override { - std::remove(sam_file_.c_str()); - } + void SetUp(const benchmark::State &state) override + { + num_reads_ = static_cast(state.range(0)); + sam_file_ = "region_query_test_" + std::to_string(num_reads_) + ".sam"; + + GenerateSAMFile(sam_file_, num_reads_); + } + + void TearDown(const benchmark::State &) override { std::remove(sam_file_.c_str()); } protected: - int num_reads_; - std::string sam_file_; - static constexpr const char* region_ = "chr1:1-100000000"; - - void suppress_output() { - freopen(NULL_DEVICE, "w", stdout); - } - - void restore_output() { - freopen("/dev/tty", "w", stdout); - } + int num_reads_; + std::string sam_file_; + static constexpr const char *region_ = "chr1:1-100000000"; + + void suppress_output() { freopen(NULL_DEVICE, "w", stdout); } + + void restore_output() { freopen("/dev/tty", "w", stdout); } }; -BENCHMARK_DEFINE_F(RegionQueryFixture, TTree)(benchmark::State& state) { - std::string root_file = "ttree_" + std::to_string(num_reads_) + ".root"; - - // Convert once before timing - suppress_output(); - samtoram(sam_file_.c_str(), root_file.c_str(), true, true, true, 1, 0); - restore_output(); - - // Benchmark only the query performance - for (auto _ : state) { - suppress_output(); - ramview(root_file.c_str(), region_, true, false, "perf.root"); - restore_output(); - } - - std::remove(root_file.c_str()); - - state.counters["reads_per_sec"] = benchmark::Counter(num_reads_, benchmark::Counter::kIsRate); +BENCHMARK_DEFINE_F(RegionQueryFixture, TTree)(benchmark::State &state) +{ + std::string root_file = "ttree_" + std::to_string(num_reads_) + ".root"; + + // Convert once before timing + suppress_output(); + samtoram(sam_file_.c_str(), root_file.c_str(), true, true, true, 1, 0); + restore_output(); + + // Benchmark only the query performance + for (auto _ : state) { + suppress_output(); + ramview(root_file.c_str(), region_, true, false, "perf.root"); + restore_output(); + } + + std::remove(root_file.c_str()); + + state.counters["reads_per_sec"] = benchmark::Counter(num_reads_, benchmark::Counter::kIsRate); } -BENCHMARK_DEFINE_F(RegionQueryFixture, RNTuple)(benchmark::State& state) { - std::string root_file = "rntuple_" + std::to_string(num_reads_) + ".root"; - - // Convert once before timing - suppress_output(); - samtoramntuple(sam_file_.c_str(), root_file.c_str(), true, true, true, 505, 0); - restore_output(); - - // Benchmark only the query performance - for (auto _ : state) { - suppress_output(); - ramntupleview(root_file.c_str(), region_, true, false, "perf.root"); - restore_output(); - } - - std::remove(root_file.c_str()); - - state.counters["reads_per_sec"] = benchmark::Counter(num_reads_, benchmark::Counter::kIsRate); +BENCHMARK_DEFINE_F(RegionQueryFixture, RNTuple)(benchmark::State &state) +{ + std::string root_file = "rntuple_" + std::to_string(num_reads_) + ".root"; + + // Convert once before timing + suppress_output(); + samtoramntuple(sam_file_.c_str(), root_file.c_str(), true, true, true, 505, 0); + restore_output(); + + // Benchmark only the query performance + for (auto _ : state) { + suppress_output(); + ramntupleview(root_file.c_str(), region_, true, false, "perf.root"); + restore_output(); + } + + std::remove(root_file.c_str()); + + state.counters["reads_per_sec"] = benchmark::Counter(num_reads_, benchmark::Counter::kIsRate); } BENCHMARK_REGISTER_F(RegionQueryFixture, TTree) - ->Args({1000}) - ->Args({10000}) - ->Args({100000}) - ->Unit(benchmark::kMillisecond); + ->Args({1000}) + ->Args({10000}) + ->Args({100000}) + ->Unit(benchmark::kMillisecond); BENCHMARK_REGISTER_F(RegionQueryFixture, RNTuple) - ->Args({1000}) - ->Args({10000}) - ->Args({100000}) - ->Unit(benchmark::kMillisecond); + ->Args({1000}) + ->Args({10000}) + ->Args({100000}) + ->Unit(benchmark::kMillisecond); BENCHMARK_MAIN(); - From 728e40b13225a26cdaa02e61d94f88341d93798c Mon Sep 17 00:00:00 2001 From: AdityaPandeyCN Date: Wed, 24 Sep 2025 17:10:04 +0530 Subject: [PATCH 3/4] code organization Signed-off-by: AdityaPandeyCN --- benchmark/benchmark_utils.h | 8 ++++++++ benchmark/region_query_benchmark.cxx | 13 ++----------- benchmark/sam_to_ram_benchmark.cxx | 8 ++------ 3 files changed, 12 insertions(+), 17 deletions(-) create mode 100644 benchmark/benchmark_utils.h diff --git a/benchmark/benchmark_utils.h b/benchmark/benchmark_utils.h new file mode 100644 index 0000000..7955af5 --- /dev/null +++ b/benchmark/benchmark_utils.h @@ -0,0 +1,8 @@ +#pragma once + +#ifdef _WIN32 +#define NULL_DEVICE "NUL" +#else +#define NULL_DEVICE "/dev/null" +#endif + diff --git a/benchmark/region_query_benchmark.cxx b/benchmark/region_query_benchmark.cxx index a537539..fa2ba73 100644 --- a/benchmark/region_query_benchmark.cxx +++ b/benchmark/region_query_benchmark.cxx @@ -1,17 +1,11 @@ #include #include "generate_sam_benchmark.h" +#include "benchmark_utils.h" #include "ramcore/SamToTTree.h" #include "ramcore/SamToNTuple.h" #include #include -#ifdef _WIN32 -#define NULL_DEVICE "NUL" -#else -#define NULL_DEVICE "/dev/null" -#endif - -// Forward declarations void ramview(const char *file, const char *query, bool cache = true, bool perfstats = false, const char *perfstatsfilename = "perf.root"); void ramntupleview(const char *file, const char *query, bool cache = true, bool perfstats = false, @@ -43,12 +37,10 @@ BENCHMARK_DEFINE_F(RegionQueryFixture, TTree)(benchmark::State &state) { std::string root_file = "ttree_" + std::to_string(num_reads_) + ".root"; - // Convert once before timing suppress_output(); samtoram(sam_file_.c_str(), root_file.c_str(), true, true, true, 1, 0); restore_output(); - // Benchmark only the query performance for (auto _ : state) { suppress_output(); ramview(root_file.c_str(), region_, true, false, "perf.root"); @@ -64,12 +56,10 @@ BENCHMARK_DEFINE_F(RegionQueryFixture, RNTuple)(benchmark::State &state) { std::string root_file = "rntuple_" + std::to_string(num_reads_) + ".root"; - // Convert once before timing suppress_output(); samtoramntuple(sam_file_.c_str(), root_file.c_str(), true, true, true, 505, 0); restore_output(); - // Benchmark only the query performance for (auto _ : state) { suppress_output(); ramntupleview(root_file.c_str(), region_, true, false, "perf.root"); @@ -94,3 +84,4 @@ BENCHMARK_REGISTER_F(RegionQueryFixture, RNTuple) ->Unit(benchmark::kMillisecond); BENCHMARK_MAIN(); + diff --git a/benchmark/sam_to_ram_benchmark.cxx b/benchmark/sam_to_ram_benchmark.cxx index 7baa1e2..a1da449 100644 --- a/benchmark/sam_to_ram_benchmark.cxx +++ b/benchmark/sam_to_ram_benchmark.cxx @@ -1,5 +1,6 @@ #include #include "generate_sam_benchmark.h" +#include "benchmark_utils.h" #include "ramcore/SamToTTree.h" #include "ramcore/SamToNTuple.h" #include @@ -7,12 +8,6 @@ #include #include -#ifdef _WIN32 -#define NULL_DEVICE "NUL" -#else -#define NULL_DEVICE "/dev/null" -#endif - static void BM_SamToRamComparison(benchmark::State &state) { int num_reads = state.range(0); @@ -74,3 +69,4 @@ int main(int argc, char **argv) ::benchmark::RunSpecifiedBenchmarks(); return 0; } + From a1063f4b8b21e1cbdef82d51fbaca0568bd64732 Mon Sep 17 00:00:00 2001 From: AdityaPandeyCN Date: Wed, 24 Sep 2025 17:10:20 +0530 Subject: [PATCH 4/4] clang changes Signed-off-by: AdityaPandeyCN --- benchmark/benchmark_utils.h | 1 - benchmark/region_query_benchmark.cxx | 3 +-- benchmark/sam_to_ram_benchmark.cxx | 3 +-- 3 files changed, 2 insertions(+), 5 deletions(-) diff --git a/benchmark/benchmark_utils.h b/benchmark/benchmark_utils.h index 7955af5..1a712c7 100644 --- a/benchmark/benchmark_utils.h +++ b/benchmark/benchmark_utils.h @@ -5,4 +5,3 @@ #else #define NULL_DEVICE "/dev/null" #endif - diff --git a/benchmark/region_query_benchmark.cxx b/benchmark/region_query_benchmark.cxx index fa2ba73..3a2d6d5 100644 --- a/benchmark/region_query_benchmark.cxx +++ b/benchmark/region_query_benchmark.cxx @@ -1,6 +1,6 @@ #include #include "generate_sam_benchmark.h" -#include "benchmark_utils.h" +#include "benchmark_utils.h" #include "ramcore/SamToTTree.h" #include "ramcore/SamToNTuple.h" #include @@ -84,4 +84,3 @@ BENCHMARK_REGISTER_F(RegionQueryFixture, RNTuple) ->Unit(benchmark::kMillisecond); BENCHMARK_MAIN(); - diff --git a/benchmark/sam_to_ram_benchmark.cxx b/benchmark/sam_to_ram_benchmark.cxx index a1da449..7413c98 100644 --- a/benchmark/sam_to_ram_benchmark.cxx +++ b/benchmark/sam_to_ram_benchmark.cxx @@ -1,6 +1,6 @@ #include #include "generate_sam_benchmark.h" -#include "benchmark_utils.h" +#include "benchmark_utils.h" #include "ramcore/SamToTTree.h" #include "ramcore/SamToNTuple.h" #include @@ -69,4 +69,3 @@ int main(int argc, char **argv) ::benchmark::RunSpecifiedBenchmarks(); return 0; } -