Skip to content

Commit

Permalink
First Pass at PkgJogger
Browse files Browse the repository at this point in the history
  • Loading branch information
awadell1 committed Aug 11, 2021
1 parent f66f188 commit eaa2995
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 2 deletions.
8 changes: 8 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@ uuid = "10150987-6cc1-4b76-abee-b1c1cbd91c01"
authors = ["Alexius Wadell <[email protected]> and contributors"]
version = "0.1.0"

[deps]
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
Revise = "295af30f-e4ad-537b-8983-00126c2a3abe"

[compat]
Revise = "3"
MacroTools = "0.5"
BenchmarkTools = "1"
julia = "1.6"

[extras]
Expand Down
5 changes: 5 additions & 0 deletions benchmark/bench_test.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using BenchmarkTools

suite = BenchmarkGroup()
suite["sin"] = @benchmarkable sin(rand())
suite["sincos"] = @benchmarkable sincos(rand())
113 changes: 112 additions & 1 deletion src/PkgJogger.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,116 @@
module PkgJogger

# Write your package code here.
using MacroTools
using BenchmarkTools

export @jog

macro jog(pkg)
# Module Name
modname = Symbol(:Jog, pkg)

# Locate benchmark folder
bench_dir = @eval benchmark_dir($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))
end

# Dispatch to PkgJogger functions
dispatch_funcs = Expr[]
for (m, f) in [(:BenchmarkTools, :run), (:BenchmarkTools, :warmup), (:PkgJogger, :benchmark)]
exp = quote
$f(args...; kwargs...) = $(m).$(f)(suite(), args...; kwargs...)
end
push!(dispatch_funcs, exp)
end

# Flatten out modules into a Vector{Expr}
suite_exp = getfield(MacroTools.flatten(quote $(suite_modules...) end), :args)

# Generate Module for Jogging pkg
quote
@eval module $modname
using $pkg
using BenchmarkTools
using PkgJogger
using Revise

# Set Revise Mode and put submodules here
__revise_mode__ = :eval
$(suite_exp...)

"""
suite()
Gets the BenchmarkTools suite for $($pkg)
"""
function suite()
suite = BenchmarkGroup()
for (n, m) in zip([$(string.(benchmarks)...)], [$(benchmarks...)])
suite[n] = m.suite
end
suite
end

$(dispatch_funcs...)
end
end
end

"""
benchmark_dir(pkg)
Expected location of benchmarks for `pkg`
"""
function benchmark_dir(pkg::Module)
pkg_dir = joinpath(dirname(pathof(pkg)), "..")
joinpath(pkg_dir, "benchmark") |> abspath
end


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
end
end
suite
end

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


"""
benchmark(s::BenchmarkGroup)
Warmup, tune and run a benchmark suite
"""
function benchmark(s::BenchmarkTools.BenchmarkGroup; kwargs...)
warmup(s)
tune!(s)
BenchmarkTools.run(s)
end


end
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ using PkgJogger
using Test

@testset "PkgJogger.jl" begin
# Write your tests here.
include("smoke.jl")
end
21 changes: 21 additions & 0 deletions test/smoke.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Test
using PkgJogger
import BenchmarkTools

benchmark_dir = PkgJogger.benchmark_dir(PkgJogger)

@testset "canonical" begin
@jog PkgJogger
@test @isdefined JogPkgJogger

# Run Benchmarks
r = JogPkgJogger.benchmark()
@test typeof(r) <: BenchmarkTools.BenchmarkGroup

# Warmup
@test_nowarn JogPkgJogger.warmup()

# Running
r = JogPkgJogger.run()
@test typeof(r) <: BenchmarkTools.BenchmarkGroup
end

0 comments on commit eaa2995

Please sign in to comment.