From 4198e2d2736d244bf736da60fb4a1f3b39a5bde8 Mon Sep 17 00:00:00 2001 From: Valentin Churavy Date: Thu, 22 Aug 2024 15:29:00 +0200 Subject: [PATCH] add BenchmarkTools integration --- lib/ValgrindBenchmarkTools/Project.toml | 9 ++++++ .../examples/callgrind.jl | 15 ++++++++++ .../src/ValgrindBenchmarkTools.jl | 30 +++++++++++++++++++ 3 files changed, 54 insertions(+) create mode 100644 lib/ValgrindBenchmarkTools/Project.toml create mode 100644 lib/ValgrindBenchmarkTools/examples/callgrind.jl create mode 100644 lib/ValgrindBenchmarkTools/src/ValgrindBenchmarkTools.jl diff --git a/lib/ValgrindBenchmarkTools/Project.toml b/lib/ValgrindBenchmarkTools/Project.toml new file mode 100644 index 0000000..fad45a5 --- /dev/null +++ b/lib/ValgrindBenchmarkTools/Project.toml @@ -0,0 +1,9 @@ +name = "ValgrindBenchmarkTools" +uuid = "cddf0403-163e-426d-bfc7-5afd20e79fa3" +authors = ["Valentin Churavy "] +version = "0.1.0" + +[deps] +BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf" +Reexport = "189a3867-3050-52da-a836-e630ba90ab69" +Valgrind = "a52a33a2-b08a-4c46-b084-8041b090a67d" diff --git a/lib/ValgrindBenchmarkTools/examples/callgrind.jl b/lib/ValgrindBenchmarkTools/examples/callgrind.jl new file mode 100644 index 0000000..b5f81b4 --- /dev/null +++ b/lib/ValgrindBenchmarkTools/examples/callgrind.jl @@ -0,0 +1,15 @@ +using BenchmarkTools +using ValgrindBenchmarkTools + +@noinline function g(a) + c = 0 + for x in a + if x > 0 + c += 1 + end + end + c +end +const data = zeros(10000) + +@show @benchmark g($data) \ No newline at end of file diff --git a/lib/ValgrindBenchmarkTools/src/ValgrindBenchmarkTools.jl b/lib/ValgrindBenchmarkTools/src/ValgrindBenchmarkTools.jl new file mode 100644 index 0000000..fc4d6a0 --- /dev/null +++ b/lib/ValgrindBenchmarkTools/src/ValgrindBenchmarkTools.jl @@ -0,0 +1,30 @@ +module ValgrindBenchmarkTools + +using Valgrind, Reexport +@reexport using BenchmarkTools + +@inline function prehook() + Valgrind.Callgrind.zero_stats() + Valgrind.Callgrind.start_instrumentation() +end + +@inline function posthook(id) + Valgrind.Callgrind.stop_instrumentation() + Valgrind.Callgrind.dump_stats_at(id) +end + +function __init__() + BenchmarkTools.DEFAULT_PARAMETERS = BenchmarkTools.Parameters(; + prehook = prehook, + posthook = posthook, + enable_customizable_func = :ALL, + ) + + # Serialization + BenchmarkTools.VERSIONS["Valgrind"] = pkgversion(Valgrind) + BenchmarkTools.VERSIONS["ValgrindBenchmarkTools.jl"] = + pkgversion(ValgrindBenchmarkTools) +end + + +end # module ValgrindBenchmarkTools