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/benchmark_utils.h b/benchmark/benchmark_utils.h new file mode 100644 index 0000000..1a712c7 --- /dev/null +++ b/benchmark/benchmark_utils.h @@ -0,0 +1,7 @@ +#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 new file mode 100644 index 0000000..3a2d6d5 --- /dev/null +++ b/benchmark/region_query_benchmark.cxx @@ -0,0 +1,86 @@ +#include +#include "generate_sam_benchmark.h" +#include "benchmark_utils.h" +#include "ramcore/SamToTTree.h" +#include "ramcore/SamToNTuple.h" +#include +#include + +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"; + + suppress_output(); + samtoram(sam_file_.c_str(), root_file.c_str(), true, true, true, 1, 0); + restore_output(); + + 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"; + + suppress_output(); + samtoramntuple(sam_file_.c_str(), root_file.c_str(), true, true, true, 505, 0); + restore_output(); + + 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(); diff --git a/benchmark/sam_to_ram_benchmark.cxx b/benchmark/sam_to_ram_benchmark.cxx index 7baa1e2..7413c98 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);