Skip to content

Commit 75639c6

Browse files
committed
Trying to switch to use PkgBenchmark.jl
1 parent bc4083f commit 75639c6

File tree

3 files changed

+47
-51
lines changed

3 files changed

+47
-51
lines changed

benchmark/REQUIRE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
BenchmarkTools

benchmark/benchmarks.jl

Lines changed: 36 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@
77
# It compares fixed-decimal types against the builtin Int and Float types of various sizes.
88
# The output is written to a .csv file in the same directory as this file.
99

10-
module DecimalRepresentationComparisons
11-
1210
using FixedPointDecimals
1311
using Random
1412
using BenchmarkTools, Statistics
15-
using DataFrames
16-
using CSV
13+
14+
# # TODO: remove this file once BenchmarkTools has a built-in solution for diffing two
15+
# # @benchmarkable runs
16+
# include("subtract-benchmarks.jl")
17+
18+
# Define a parent BenchmarkGroup to contain our suite
19+
const SUITE = BenchmarkGroup()
1720

1821
decimal_precision = 2
1922

@@ -42,8 +45,8 @@ type(T::Type{<:Union{Int32, Int64}}) = " $T"
4245
type(T::Type{Int128}) = " $T"
4346
type(::Type{FixedPointDecimals.FixedDecimal{T,f}}) where {T,f} = "FD{$T,$f}"
4447
type(::Type{FixedPointDecimals.FixedDecimal{T,f}}) where {T<:Union{Int32,Int64},f} = "FD{ $T,$f}"
45-
opname(f) = Symbol(f)
46-
opname(f::typeof(identity1)) = :identity
48+
opname(f) = string(Symbol(f))
49+
opname(f::typeof(identity1)) = "identity"
4750

4851
# --------- Define benchmark functions -------------
4952
# Some care is taken here to prevent the compiler from optimizing away the operations:
@@ -71,52 +74,34 @@ end
7174
end
7275
end
7376

74-
# ------------ Run the Benchmarks -------------------------
75-
function perform_benchmark()
76-
# Collect the results
77-
results = DataFrame(Operation=Symbol[], Category=String[], Type=String[],
78-
DurationNs=Float64[], Allocations=Int[], MinGcTime=Number[],
79-
Value=Number[])
80-
81-
# Run the benchmarks
82-
for op in allops
83-
println("$op")
84-
for T in alltypes
85-
print("$T ")
86-
87-
N = 1_000_000
88-
initial_value = zero(T)
89-
a = one(T)
90-
91-
# For some reason this is necessary to eliminate mysterious "1 allocation"
92-
fbase = @eval (out::Ref{$T})->baseline($T, $a, $N, out)
93-
fbench = @eval (out::Ref{$T})->benchmark($T, $op, $a, $N, out)
94-
95-
# Run the benchmark
96-
outbase = Ref(initial_value)
97-
bbase = median(@benchmark $fbase($outbase) evals=1 setup=($outbase[]=$initial_value))
98-
outbench = Ref(initial_value)
99-
bbench = median(@benchmark $fbench($outbench) evals=1 setup=($outbench[]=$initial_value))
100-
101-
# Compute results
102-
difftime = (bbench.time - bbase.time)
103-
println("$(round(difftime, digits=2)) ns ($(bbench.allocs) allocations)")
104-
println(outbench[])
105-
println(outbase[])
106-
value = outbench
107-
108-
push!(results, Dict(:Operation=>opname(op), :Category=>category(T), :Type=>type(T),
109-
:DurationNs=>difftime/N, # average (b.times reports ns)
110-
:Allocations=>bbench.allocs, :MinGcTime=>bbench.gctime,
111-
:Value=>value[]))
112-
end
77+
# Define the benchmark structure
78+
for op in allops
79+
SUITE[opname(op)] = BenchmarkGroup()
80+
for T in alltypes
81+
SUITE[opname(op)][type(T)] = BenchmarkGroup(["diff"])
11382
end
114-
115-
println(results)
116-
CSV.write("$(@__DIR__)/comparisons-benchmark-results.csv", results)
117-
return results
11883
end
11984

120-
results = perform_benchmark()
121-
85+
for op in allops
86+
println()
87+
println("$op")
88+
for T in alltypes
89+
print("$T ")
90+
91+
N = 1 # _000 #_000
92+
initial_value = zero(T)
93+
a = one(T)
94+
95+
# For some reason this is necessary to eliminate mysterious "1 allocation"
96+
fbase = @eval (out::Ref{$T})->baseline($T, $a, $N, out)
97+
fbench = @eval (out::Ref{$T})->benchmark($T, $op, $a, $N, out)
98+
99+
# Run the benchmark
100+
outbase = Ref(initial_value)
101+
bbase = @benchmarkable $fbase($outbase) evals=1 setup=($outbase[]=$initial_value)
102+
outbench = Ref(initial_value)
103+
bbench = @benchmarkable $fbench($outbench) evals=1 setup=($outbench[]=$initial_value)
104+
bdiff = bbench - bbase
105+
SUITE[opname(op)][type(T)]["diff"] = bdiff
106+
end
122107
end

benchmark/subtract-benchmarks.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import PkgBenchmark
2+
function PkgBenchmark._run(b::PkgBenchmark.BenchmarkTools.BenchmarkDiff, p::PkgBenchmark.BenchmarkTools.Parameters = b.params;
3+
prog = nothing, verbose::Bool = false, pad = "", hierarchy = [], kwargs...)
4+
res = BenchmarkTools.run_result(b, p; kwargs...)[1]
5+
if prog != nothing
6+
indent = 0
7+
ProgressMeter.next!(prog; showvalues = [map(id -> (" "^(indent += 1) * "[$(id[2])/$(id[3])]", id[1]), hierarchy)...])
8+
end
9+
return res
10+
end

0 commit comments

Comments
 (0)