Skip to content

Commit 30c92b5

Browse files
SAM to RAM conversion time benchmark (#7)
1 parent c3278fc commit 30c92b5

File tree

3 files changed

+115
-4
lines changed

3 files changed

+115
-4
lines changed

benchmark/CMakeLists.txt

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,22 @@ ROOT_EXECUTABLE(sam_to_ram_benchmark
2020
sam_generator
2121
)
2222

23-
install(TARGETS sam_to_ram_benchmark
23+
ROOT_EXECUTABLE(conversion_time_benchmark
24+
conversion_time_benchmark.cxx
25+
LIBRARIES
26+
benchmark::benchmark
27+
ramcore
28+
sam_generator
29+
)
30+
31+
install(TARGETS sam_to_ram_benchmark conversion_time_benchmark
2432
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
2533
)
2634

2735
add_custom_target(benchmark
2836
COMMAND sam_to_ram_benchmark
29-
DEPENDS sam_to_ram_benchmark
37+
COMMAND conversion_time_benchmark
38+
DEPENDS sam_to_ram_benchmark conversion_time_benchmark
3039
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
31-
COMMENT "Running SAM to RAM conversion benchmark"
3240
)
3341

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
}

benchmark/sam_to_ram_benchmark.cxx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77
#include <cstdio>
88
#include <cstring>
99

10+
#ifdef _WIN32
11+
#define NULL_DEVICE "NUL"
12+
#else
13+
#define NULL_DEVICE "/dev/null"
14+
#endif
15+
1016
static void BM_SamToRamComparison(benchmark::State &state)
1117
{
1218
int num_reads = state.range(0);
@@ -20,7 +26,7 @@ static void BM_SamToRamComparison(benchmark::State &state)
2026
for (auto _ : state) {
2127

2228
FILE *original_stdout = stdout;
23-
stdout = fopen("/dev/null", "w");
29+
stdout = fopen(NULL_DEVICE, "w");
2430

2531
samtoram(sam_file.c_str(), ttree_file.c_str(), true, true, true, 1, 0);
2632

0 commit comments

Comments
 (0)