Skip to content

Commit

Permalink
Add function to generate benchmark results csv anywhere anyname defined
Browse files Browse the repository at this point in the history
  • Loading branch information
bennettm committed Mar 26, 2024
1 parent ca2d518 commit 0e3f5cb
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions src/additional_tools/benchmark_macro.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
@doc draw"""
macro benchmarked(args...)
This macro is needed for getting both benchmark results and a return value from a function.
@benchmarked <expr to benchmark> [setup=<setup expr>] [other keyword parameters...]
Run benchmarked on a given expression.
This macro is a customization of BenchmarkTools' `@benchmark`.
`@benchmark` only provides benchmark results.
With this customization, we can benchmark a function that has a return value.
Customization uses internals from BenchmarkTools.
Updates to this macro is needed when internals get changed from BenchmarkTools.
#Example
"""

using BenchmarkTools
using DataFrames


macro benchmarked(args...)
_, params = BenchmarkTools.prunekwargs(args...)
bench, trial, result = gensym(), gensym(), gensym()
Expand All @@ -19,15 +27,35 @@ macro benchmarked(args...)
$tune_phase
local $trial, $result = $BenchmarkTools.run_result($bench)
local $trialmin = $BenchmarkTools.minimum($trial)

display($trial)
local $trialallocs = $BenchmarkTools.allocs($trialmin)

println(
" ",
$BenchmarkTools.prettytime($BenchmarkTools.time($trialmin)),
" (",
$trialallocs,
" allocation",
$trialallocs == 1 ? "" : "s",
": ",
$BenchmarkTools.prettymemory($BenchmarkTools.memory($trialmin)),
")",
)

$result, $trial
end,
)
end


function generate_benchmark_csv(path::String, bm_file, bm_results::BenchmarkTools.Trial)
bm_df = DataFrame(
time_ms = bm_results.times ./ 1e6,
gctime_ms = bm_results.gctimes ./ 1e6,
bm_nt_ms = (bm_results.times - bm_results.gctimes) ./ 1e6,
memory_mb = bm_results.memory ./ 1e6,
allocs = bm_results.allocs
)
CSV.write(joinpath(path, bm_file, bm_df)
end



0 comments on commit 0e3f5cb

Please sign in to comment.