|
| 1 | +#include <benchmark/benchmark.h> |
| 2 | +#include "generate_sam_benchmark.h" |
| 3 | +#include "ramcore/SamToTTree.h" |
| 4 | +#include "ramcore/SamToNTuple.h" |
| 5 | +#include <filesystem> |
| 6 | +#include <iostream> |
| 7 | +#include <cstdio> |
| 8 | +#include <cstring> |
| 9 | + |
| 10 | +static void BM_SamToRamComparison(benchmark::State& state) { |
| 11 | + int num_reads = state.range(0); |
| 12 | + |
| 13 | + std::string sam_file = "benchmark_test.sam"; |
| 14 | + std::string ttree_file = "benchmark_ttree.root"; |
| 15 | + std::string rntuple_file = "benchmark_rntuple.root"; |
| 16 | + |
| 17 | + GenerateSAMFile(sam_file, num_reads); |
| 18 | + |
| 19 | + for (auto _ : state) { |
| 20 | + |
| 21 | + FILE* original_stdout = stdout; |
| 22 | + stdout = fopen("/dev/null", "w"); |
| 23 | + |
| 24 | + samtoram(sam_file.c_str(), ttree_file.c_str(), |
| 25 | + true, true, true, 1, 0); |
| 26 | + |
| 27 | + samtoramntuple(sam_file.c_str(), rntuple_file.c_str(), |
| 28 | + true, true, true, 505, 0); |
| 29 | + |
| 30 | + fclose(stdout); |
| 31 | + stdout = original_stdout; |
| 32 | + |
| 33 | + if (std::filesystem::exists(ttree_file)) { |
| 34 | + auto ttree_size = std::filesystem::file_size(ttree_file); |
| 35 | + state.counters["ttree_size_mb"] = ttree_size / (1024.0 * 1024.0); |
| 36 | + } |
| 37 | + |
| 38 | + if (std::filesystem::exists(rntuple_file)) { |
| 39 | + auto rntuple_size = std::filesystem::file_size(rntuple_file); |
| 40 | + state.counters["rntuple_size_mb"] = rntuple_size / (1024.0 * 1024.0); |
| 41 | + } |
| 42 | + |
| 43 | + if (state.counters["ttree_size_mb"] > 0 && state.counters["rntuple_size_mb"] > 0) { |
| 44 | + state.counters["compression_ratio"] = |
| 45 | + state.counters["ttree_size_mb"] / state.counters["rntuple_size_mb"]; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + std::remove(sam_file.c_str()); |
| 50 | + std::remove(ttree_file.c_str()); |
| 51 | + std::remove(rntuple_file.c_str()); |
| 52 | + |
| 53 | + state.counters["reads"] = num_reads; |
| 54 | + state.counters["reads_per_second"] = benchmark::Counter( |
| 55 | + num_reads, benchmark::Counter::kIsRate); |
| 56 | +} |
| 57 | + |
| 58 | +int main(int argc, char** argv) { |
| 59 | + std::cout << "SAM to RAM Conversion Benchmark" << std::endl; |
| 60 | + std::cout << "Comparing TTree vs RNTuple performance and file sizes" << std::endl; |
| 61 | + std::cout << std::endl; |
| 62 | + |
| 63 | + ::benchmark::RegisterBenchmark("BM_SamToRamComparison", BM_SamToRamComparison) |
| 64 | + ->Args({1000}) |
| 65 | + ->Args({10000}) |
| 66 | + ->Args({100000}) |
| 67 | + ->Unit(benchmark::kMillisecond); |
| 68 | + |
| 69 | + ::benchmark::Initialize(&argc, argv); |
| 70 | + ::benchmark::RunSpecifiedBenchmarks(); |
| 71 | + return 0; |
| 72 | +} |
| 73 | + |
0 commit comments