|
| 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 | + |
| 9 | +#ifdef _WIN32 |
| 10 | +#define NULL_DEVICE "NUL" |
| 11 | +#else |
| 12 | +#define NULL_DEVICE "/dev/null" |
| 13 | +#endif |
| 14 | + |
| 15 | +static void BM_SamToTTree(benchmark::State &state) |
| 16 | +{ |
| 17 | + int num_reads = state.range(0); |
| 18 | + std::string sam_file = "ttree_test.sam"; |
| 19 | + std::string ttree_file = "ttree_output.root"; |
| 20 | + |
| 21 | + GenerateSAMFile(sam_file, num_reads); |
| 22 | + |
| 23 | + for (auto _ : state) { |
| 24 | + |
| 25 | + FILE *original_stdout = stdout; |
| 26 | + stdout = fopen(NULL_DEVICE, "w"); |
| 27 | + |
| 28 | + samtoram(sam_file.c_str(), ttree_file.c_str(), true, true, true, 1, 0); |
| 29 | + |
| 30 | + fclose(stdout); |
| 31 | + stdout = original_stdout; |
| 32 | + |
| 33 | + if (std::filesystem::exists(ttree_file)) { |
| 34 | + auto file_size = std::filesystem::file_size(ttree_file); |
| 35 | + state.counters["file_size_mb"] = file_size / (1024.0 * 1024.0); |
| 36 | + } |
| 37 | + |
| 38 | + std::remove(ttree_file.c_str()); |
| 39 | + } |
| 40 | + |
| 41 | + std::remove(sam_file.c_str()); |
| 42 | + state.counters["reads_per_second"] = benchmark::Counter(num_reads, benchmark::Counter::kIsRate); |
| 43 | +} |
| 44 | + |
| 45 | +static void BM_SamToRNTuple(benchmark::State &state) |
| 46 | +{ |
| 47 | + int num_reads = state.range(0); |
| 48 | + std::string sam_file = "rntuple_test.sam"; |
| 49 | + std::string rntuple_file = "rntuple_output.root"; |
| 50 | + |
| 51 | + GenerateSAMFile(sam_file, num_reads); |
| 52 | + |
| 53 | + for (auto _ : state) { |
| 54 | + |
| 55 | + FILE *original_stdout = stdout; |
| 56 | + stdout = fopen(NULL_DEVICE, "w"); |
| 57 | + |
| 58 | + samtoramntuple(sam_file.c_str(), rntuple_file.c_str(), true, true, true, 505, 0); |
| 59 | + |
| 60 | + fclose(stdout); |
| 61 | + stdout = original_stdout; |
| 62 | + |
| 63 | + if (std::filesystem::exists(rntuple_file)) { |
| 64 | + auto file_size = std::filesystem::file_size(rntuple_file); |
| 65 | + state.counters["file_size_mb"] = file_size / (1024.0 * 1024.0); |
| 66 | + } |
| 67 | + |
| 68 | + std::remove(rntuple_file.c_str()); |
| 69 | + } |
| 70 | + |
| 71 | + std::remove(sam_file.c_str()); |
| 72 | + state.counters["reads_per_second"] = benchmark::Counter(num_reads, benchmark::Counter::kIsRate); |
| 73 | +} |
| 74 | + |
| 75 | +int main(int argc, char **argv) |
| 76 | +{ |
| 77 | + std::cout << "Individual Conversion Time Benchmark" << std::endl; |
| 78 | + std::cout << "====================================" << std::endl; |
| 79 | + std::cout << "Measuring TTree and RNTuple conversion times separately" << std::endl; |
| 80 | + std::cout << std::endl; |
| 81 | + |
| 82 | + ::benchmark::RegisterBenchmark("TTree_Conversion", BM_SamToTTree) |
| 83 | + ->Args({1000}) |
| 84 | + ->Args({10000}) |
| 85 | + ->Args({100000}) |
| 86 | + ->Unit(benchmark::kMillisecond); |
| 87 | + |
| 88 | + ::benchmark::RegisterBenchmark("RNTuple_Conversion", BM_SamToRNTuple) |
| 89 | + ->Args({1000}) |
| 90 | + ->Args({10000}) |
| 91 | + ->Args({100000}) |
| 92 | + ->Unit(benchmark::kMillisecond); |
| 93 | + |
| 94 | + ::benchmark::Initialize(&argc, argv); |
| 95 | + ::benchmark::RunSpecifiedBenchmarks(); |
| 96 | + return 0; |
| 97 | +} |
0 commit comments