|
1 |
| -const VERSION_KEY = "__versions__" |
| 1 | +const VERSIONS = Dict("Julia" => string(VERSION), |
| 2 | + "BenchmarkTools" => string(BENCHMARKTOOLS_VERSION)) |
2 | 3 |
|
3 |
| -const VERSIONS = Dict("Julia" => string(VERSION), "BenchmarkTools" => string(BENCHMARKTOOLS_VERSION)) |
| 4 | +# TODO: Add any new types as they're added |
| 5 | +const SUPPORTED_TYPES = [Benchmark, BenchmarkGroup, Parameters, TagFilter, Trial, |
| 6 | + TrialEstimate, TrialJudgement, TrialRatio] |
4 | 7 |
|
5 |
| -mutable struct ParametersPreV006 |
6 |
| - seconds::Float64 |
7 |
| - samples::Int |
8 |
| - evals::Int |
9 |
| - overhead::Int |
10 |
| - gctrial::Bool |
11 |
| - gcsample::Bool |
12 |
| - time_tolerance::Float64 |
13 |
| - memory_tolerance::Float64 |
14 |
| -end |
15 |
| - |
16 |
| -mutable struct TrialPreV006 |
17 |
| - params::Parameters |
18 |
| - times::Vector{Int} |
19 |
| - gctimes::Vector{Int} |
20 |
| - memory::Int |
21 |
| - allocs::Int |
| 8 | +for T in SUPPORTED_TYPES |
| 9 | + @eval function JSON.lower(x::$T) |
| 10 | + d = Dict{String,Any}() |
| 11 | + for i = 1:nfields(x) |
| 12 | + name = String(fieldname($T, i)) |
| 13 | + field = getfield(x, i) |
| 14 | + value = typeof(field) in SUPPORTED_TYPES ? JSON.lower(field) : field |
| 15 | + push!(d, name => value) |
| 16 | + end |
| 17 | + [string(typeof(x)), d] |
| 18 | + end |
22 | 19 | end
|
23 | 20 |
|
24 |
| -function JLD.readas(p::ParametersPreV006) |
25 |
| - return Parameters(p.seconds, p.samples, p.evals, Float64(p.overhead), p.gctrial, |
26 |
| - p.gcsample, p.time_tolerance, p.memory_tolerance) |
| 21 | +function recover(x::Vector) |
| 22 | + length(x) == 2 || throw(ArgumentError("Expecting a vector of length 2")) |
| 23 | + typename = x[1]::String |
| 24 | + fields = x[2]::Dict |
| 25 | + T = eval(parse(typename))::Type |
| 26 | + fc = fieldcount(T) |
| 27 | + xs = Vector{Any}(fc) |
| 28 | + for i = 1:fc |
| 29 | + ft = fieldtype(T, i) |
| 30 | + fn = String(fieldname(T, i)) |
| 31 | + xs[i] = if ft in SUPPORTED_TYPES |
| 32 | + recover(fields[fn]) |
| 33 | + else |
| 34 | + convert(ft, fields[fn]) |
| 35 | + end |
| 36 | + end |
| 37 | + T(xs...) |
27 | 38 | end
|
28 | 39 |
|
29 |
| -function JLD.readas(t::TrialPreV006) |
30 |
| - new_times = convert(Vector{Float64}, t.times) |
31 |
| - new_gctimes = convert(Vector{Float64}, t.gctimes) |
32 |
| - return Trial(t.params, new_times, new_gctimes, t.memory, t.allocs) |
| 40 | +function badext(filename) |
| 41 | + noext, ext = splitext(filename) |
| 42 | + msg = if ext == ".jld" |
| 43 | + "JLD serialization is no longer supported. Benchmarks should now be saved in\n" * |
| 44 | + "JSON format using `save(\"$noext\".json, args...)` and loaded from JSON using\n" * |
| 45 | + "using `load(\"$noext\".json, args...)`. You will need to convert existing\n" * |
| 46 | + "saved benchmarks to JSON in order to use them with this version of BenchmarkTools." |
| 47 | + else |
| 48 | + "Only JSON serialization is supported." |
| 49 | + end |
| 50 | + throw(ArgumentError(msg)) |
33 | 51 | end
|
34 | 52 |
|
35 |
| -function save(filename, args...) |
36 |
| - JLD.save(filename, VERSION_KEY, VERSIONS, args...) |
37 |
| - JLD.jldopen(filename, "r+") do io |
38 |
| - JLD.addrequire(io, BenchmarkTools) |
| 53 | +function save(filename::AbstractString, args...) |
| 54 | + endswith(filename, ".json") || badext(filename) |
| 55 | + isempty(args) && throw(ArgumentError("Nothing to save")) |
| 56 | + goodargs = Any[] |
| 57 | + for arg in args |
| 58 | + if arg isa String |
| 59 | + warn("Naming variables in serialization is no longer supported.\nThe name " * |
| 60 | + "will be ignored and the object will be serialized in the order it appears " * |
| 61 | + "in the input.") |
| 62 | + continue |
| 63 | + elseif !any(T->arg isa T, SUPPORTED_TYPES) |
| 64 | + throw(ArgumentError("Only BenchmarkTools types can be serialized.")) |
| 65 | + end |
| 66 | + push!(goodargs, arg) |
| 67 | + end |
| 68 | + isempty(goodargs) && error("Nothing to save") |
| 69 | + open(filename, "w") do io |
| 70 | + JSON.print(io, [VERSIONS, goodargs]) |
39 | 71 | end
|
40 |
| - return nothing |
41 | 72 | end
|
42 | 73 |
|
43 |
| -@inline function load(filename, args...) |
44 |
| - # no version-based rules are needed for now, we just need |
45 |
| - # to check that version information exists in the file. |
46 |
| - if JLD.jldopen(file -> JLD.exists(file, VERSION_KEY), filename, "r") |
47 |
| - result = JLD.load(filename, args...) |
48 |
| - else |
49 |
| - JLD.translate("BenchmarkTools.Parameters", "BenchmarkTools.ParametersPreV006") |
50 |
| - JLD.translate("BenchmarkTools.Trial", "BenchmarkTools.TrialPreV006") |
51 |
| - result = JLD.load(filename, args...) |
52 |
| - JLD.translate("BenchmarkTools.Parameters", "BenchmarkTools.Parameters") |
53 |
| - JLD.translate("BenchmarkTools.Trial", "BenchmarkTools.Trial") |
| 74 | +function load(filename::AbstractString, args...) |
| 75 | + endswith(filename, ".json") || badext(filename) |
| 76 | + if !isempty(args) |
| 77 | + throw(ArgumentError("Looking up deserialized values by name is no longer supported, " * |
| 78 | + "as names are no longer saved.")) |
| 79 | + end |
| 80 | + parsed = open(JSON.parse, filename, "r") |
| 81 | + if !isa(parsed, Vector) || length(parsed) != 2 || !isa(parsed[1], Dict) || !isa(parsed[2], Vector) |
| 82 | + error("Unexpected JSON format. Was this file originally written by BenchmarkTools?") |
54 | 83 | end
|
55 |
| - return result |
| 84 | + versions = parsed[1]::Dict |
| 85 | + values = parsed[2]::Vector |
| 86 | + map!(recover, values, values) |
56 | 87 | end
|
0 commit comments