Skip to content

Commit

Permalink
feat: Add support for benchmarks in sub folders
Browse files Browse the repository at this point in the history
Can now have benchmarks in subfolders ie.
- benchmarks/
 - bench_foo.jl
 - bench_group/
   - bench_foo2.jl

  Reworked jogger suite() to use generated code and gensym names

  Fixes: #19, sets up option to define suite module in `BenchModule`
  for #5
  • Loading branch information
awadell1 committed Nov 3, 2021
1 parent 0711590 commit 633d28d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/PkgJogger.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ const JOGGER_PKGS = [
PkgId(UUID("cf7118a7-6976-5b1a-9a39-7adc72f591a4"), "UUIDs"),
]

include("jogger.jl")
include("utils.jl")
include("jogger.jl")
include("ci.jl")

end
34 changes: 20 additions & 14 deletions src/jogger.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ macro jog(pkg)

# Generate modules
suite_modules = Expr[]
benchmarks = Symbol[]
for (name, file) in locate_benchmarks(bench_dir)
push!(suite_modules, build_module(name, file))
push!(benchmarks, Symbol(name))
suite_expressions = Expr[]
for s in locate_benchmarks(bench_dir)
suite_expr, suite_module = build_module(s)
push!(suite_modules, suite_module)
push!(suite_expressions, suite_expr)
end

# Flatten out modules into a Vector{Expr}
Expand Down Expand Up @@ -83,9 +84,7 @@ macro jog(pkg)
"""
function suite()
suite = BenchmarkTools.BenchmarkGroup()
for (n, m) in zip([$(string.(benchmarks)...)], [$(benchmarks...)])
suite[n] = m.suite
end
$(suite_expressions...)
suite
end

Expand Down Expand Up @@ -173,17 +172,24 @@ macro jog(pkg)
end

"""
build_module(name, file)
build_module(s::BenchModule)
Construct a module wrapping the BenchmarkGroup defined by `file` with `name`
Construct a module wrapping the BenchmarkGroup defined by `s::BenchModule`
"""
function build_module(name, file)
modname = Symbol(name)
exp = quote
function build_module(s::BenchModule)
modname = gensym(s.name[end])
module_expr = quote
module $modname
__revise_mode__ = :eval
include($file)
include($(s.filename))
end
Revise.track($modname, $file)
Revise.track($modname, $(s.filename))
end

# Build Expression for accessing suite
suite_expr = quote
suite[$(s.name)] = $(modname).suite
end

return suite_expr, module_expr
end
34 changes: 24 additions & 10 deletions src/utils.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
struct BenchModule
filename::String
name::Vector{String}
end

"""
benchmark_dir(pkg::Module)
benchmark_dir(pkg::PackageSpec)
Expand Down Expand Up @@ -32,23 +37,32 @@ end

"""
locate_benchmarks(pkg::Module)
locate_benchmarks(bench_dir::String)
locate_benchmarks(path::String, name=String[])
Returns a dict of `name => filename` of identified benchmark files
Returns a list of `BenchModule` for identified benchmark files
"""
function locate_benchmarks(dir)
suite = Dict{String, String}()
for file in readdir(dir; join=true)
m = match(r"bench_(.*?)\.jl$", file)
if m !== nothing
suite[m.captures[1]] = file
function locate_benchmarks(path, name=String[])
suite = BenchModule[]
for file in readdir(path)
# Check that path is named 'bench_*'
!startswith(file, "bench_") && continue

# Check if file is a valid target to add
cur_name = [name..., file]
filename = joinpath(path, file)
if isfile(filename) && endswith(file, ".jl")
# File is a julia file named bench_*.jl
push!(suite, BenchModule(filename, cur_name))
elseif isdir(filename)
# Subdirectory named bench_* -> Look for more modules
append!(suite, locate_benchmarks(filename, cur_name))
end
end
suite
@info suite
return suite
end
locate_benchmarks(pkg::Module) = benchmark_dir(pkg) |> locate_benchmarks


"""
judge(new, old; metric=Statistics.median, kwargs...)
Expand Down

0 comments on commit 633d28d

Please sign in to comment.