Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
35 changes: 31 additions & 4 deletions benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
)

96 changes: 96 additions & 0 deletions benchmark/region_query_benchmark.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#include <benchmark/benchmark.h>
#include "generate_sam_benchmark.h"
#include "ramcore/SamToTTree.h"
#include "ramcore/SamToNTuple.h"
#include <string>
#include <cstdio>

#ifdef _WIN32
#define NULL_DEVICE "NUL"
#else
#define NULL_DEVICE "/dev/null"
#endif

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC, this was already put somewhere. Can we make sure we reuse code?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried to address this.


// 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<int>(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();