Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance benchmarks #32

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions test/benchmarks.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using Unitful
using BenchmarkTools
using Base.Test
using DataFrames
Copy link
Collaborator

@ajkeller34 ajkeller34 Oct 10, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DataFrames no longer needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed. Sorry, the pull-request is a bit dirty. In part, it's because I'm not sure how benchmarks are integrated into packages. As part of the testing framework? outside of it? as a report? not all?

In any case, I'll correct that and the unused function.


function benchmark()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this function used anywhere? If I try it out I get the following:

julia> benchmark()
ERROR: UndefVarError: benchmark! not defined
 in benchmark() at /Users/ajkeller/.julia/v0.5/Unitful/test/benchmarks.jl:10

suite = BenchmarkGroup(["Unitful"])
bterms = (3u"kg", 3.5u"kg", 2u"m^-1")
for a in (2u"m", 2.1u"m"), b in bterms, op in (:*, :/)
benchmark!(suite, :($op($a, $b)))
end
for a in (2u"m", 2.1u"m"), b in (3u"m", 3.5u"m"), op in (:+, :-)
benchmark!(suite, :($op($a, $b)))
end

performance_summary(suite)
end

function judge_unit_benchmark(units::Expr, nounits::Expr; kwargs...)
op = units.args[1]
a = eval(units.args[2])
b = eval(units.args[3])
bench_units = @benchmarkable $op($a, $b)
bench_nounits = @benchmarkable $nounits
tune!(bench_units)
tune!(bench_nounits)
judge(
median(run(bench_units; kwargs...)),
median(run(bench_nounits; kwargs...))
)
end

function judge_unit_benchmark(units::Expr; kwargs...)
op = units.args[1]
a = ustrip(eval(units.args[2]))
b = ustrip(eval(units.args[3]))
judge_unit_benchmark(units, :($op($a, $b)); kwargs...)
end

function test_benchmark(unit::Expr...; kwargs...)
j = judge_unit_benchmark(unit...; kwargs...)
j.time == :invariant && j.memory == :invariant
end

@testset "Benchmarks" begin
as, ops, bs = (2u"m", 2.1u"m"), (:*, :/), (3u"kg", 3.5u"kg")
@testset "$a $op $b" for a = as, b = bs, op = ops
@test test_benchmark(:($op($a, $b)), time_tolerance=0.01)
end

@testset "unit to unitless" begin
@test test_benchmark(:((1u"m") / (2u"m")), time_tolerance=0.01)
@test test_benchmark(:(1u"m" * 2u"m^-1"), time_tolerance=0.01)
@test test_benchmark(:(1u"m" / 2u"km"), time_tolerance=0.01)
end


a = 1//2 * u"m"
bs = (1u"kg", 2//3 * u"kg")
@testset "rational numbers: $a $op $b" for b = bs, op in (://, :*)
@test_broken test_benchmark(:($op($a, $b)))
end

@testset "summations" begin
@test test_benchmark(:(1u"m" + 2u"m"), time_tolerance=0.01)
@test_broken test_benchmark(:(1u"km" + 2u"m"), time_tolerance=0.01)
end

@testset "Arrays" begin
as, ops, bs = ([2, 1]u"m", [2.1, 3.1]u"m"), (:.*, :./, :.+, :.-),
(3u"kg", [3, 2]u"kg")
@testset "$a $op $b" for a=as, b=bs, op=ops
@test_broken test_benchmark(:($op($a, $b)), time_tolerance=0.01)
end
end
end