Skip to content

Commit 6a4f664

Browse files
authored
Merge pull request #282 from JuliaIO/kms/update-benchmarks
Update benchmarks to Julia 1.x
2 parents 1027cb7 + e32711a commit 6a4f664

File tree

2 files changed

+73
-48
lines changed

2 files changed

+73
-48
lines changed

bench/bench.jl

Lines changed: 54 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,22 @@ using ArgParse
44
using JSON
55

66

7-
function bench(f, simulate=false)
7+
function bench(f, flags, parsefile, simulate=false)
88
fp = joinpath(JSON_DATA_DIR, string(f, ".json"))
99
if !isfile(fp)
1010
println("Downloading benchmark file...")
1111
download(DATA_SOURCES[f], fp)
1212
end
1313
GC.gc() # run gc so it doesn't affect benchmarks
14-
t = if args["parse"]["parse-file"]
14+
t = if parsefile
1515
@elapsed JSON.parsefile(fp)
1616
else
1717
data = read(fp, String)
1818
@elapsed JSON.Parser.parse(data)
1919
end
2020

2121
if !simulate
22-
printstyled(" [Bench$FLAGS] "; color=:yellow)
22+
printstyled(" [Bench$flags] "; color=:yellow)
2323
println(f, " ", t, " seconds")
2424
end
2525
t
@@ -35,58 +35,64 @@ const DATA_SOURCES = Dict(
3535
"citylots" => "https://raw.githubusercontent.com/zemirco/sf-city-lots-json/master/citylots.json",
3636
"twitter" => "https://raw.githubusercontent.com/miloyip/nativejson-benchmark/v1.0.0/data/twitter.json")
3737

38-
@add_arg_table s begin
39-
"parse"
40-
action = :command
41-
help = "Run a JSON parser benchmark"
42-
"list"
43-
action = :command
44-
help = "List available JSON files for use"
45-
end
38+
function main()
39+
@add_arg_table s begin
40+
"parse"
41+
action = :command
42+
help = "Run a JSON parser benchmark"
43+
"list"
44+
action = :command
45+
help = "List available JSON files for use"
46+
end
4647

47-
@add_arg_table s["parse"] begin
48-
"--include-compile", "-c"
49-
help = "If set, include the compile time in measurements"
50-
action = :store_true
51-
"--parse-file", "-f"
52-
help = "If set, measure JSON.parsefile, hence including IO time"
53-
action = :store_true
54-
"file"
55-
help = "The JSON file to benchmark (leave out to benchmark all)"
56-
required = false
57-
end
48+
@add_arg_table s["parse"] begin
49+
"--include-compile", "-c"
50+
help = "If set, include the compile time in measurements"
51+
action = :store_true
52+
"--parse-file", "-f"
53+
help = "If set, measure JSON.parsefile, hence including IO time"
54+
action = :store_true
55+
"file"
56+
help = "The JSON file to benchmark (leave out to benchmark all)"
57+
required = false
58+
end
5859

59-
const args = parse_args(ARGS, s)
60+
args = parse_args(ARGS, s)
6061

61-
if args["%COMMAND%"] == "parse"
62-
const FLAGS = string(
63-
args["parse"]["include-compile"] ? "C" : "",
64-
args["parse"]["parse-file"] ? "F" : "")
62+
if args["%COMMAND%"] == "parse"
63+
include_compile = args["parse"]["include-compile"]
64+
parsefile = args["parse"]["parse-file"]
6565

66-
if args["parse"]["file"] nothing
67-
const file = args["parse"]["file"]
66+
flags = string(include_compile ? "C" : "",
67+
parsefile ? "F" : "")
6868

69-
if !args["parse"]["include-compile"]
70-
bench(file, true)
71-
end
72-
bench(file)
73-
else
74-
times = 1.0
75-
if args["parse"]["include-compile"]
76-
error("Option --include-compile can only be used for single file.")
77-
end
78-
for k in sort(collect(keys(DATA_SOURCES)))
79-
bench(k, true) # warm up compiler
69+
if args["parse"]["file"] nothing
70+
file = args["parse"]["file"]
71+
72+
if !include_compile
73+
bench(file, flags, parsefile, true)
74+
end
75+
bench(file, flags, parsefile)
76+
else
77+
times = 1.0
78+
if include_compile
79+
error("Option --include-compile can only be used for single file.")
80+
end
81+
for k in sort(collect(keys(DATA_SOURCES)))
82+
bench(k, flags, parsefile, true) # warm up compiler
83+
end
84+
for k in sort(collect(keys(DATA_SOURCES)))
85+
times *= bench(k, flags, parsefile) # do benchmark
86+
end
87+
printstyled(" [Bench$flags] ", color=:yellow)
88+
println("Total (G.M.) ", times^(1/length(DATA_SOURCES)), " seconds")
8089
end
90+
elseif args["%COMMAND%"] == "list"
91+
println("Available benchmarks are:")
8192
for k in sort(collect(keys(DATA_SOURCES)))
82-
times *= bench(k) # do benchmark
93+
println("$k")
8394
end
84-
print_with_color(:yellow, " [Bench$FLAGS] ")
85-
println("Total (G.M.) ", times^(1/length(DATA_SOURCES)), " seconds")
86-
end
87-
elseif args["%COMMAND%"] == "list"
88-
println("Available benchmarks are:")
89-
for k in sort(collect(keys(DATA_SOURCES)))
90-
println("$k")
9195
end
9296
end
97+
98+
main()

bench/bench.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from functools import reduce
2+
from textwrap import dedent as dd
3+
from timeit import repeat
4+
5+
6+
sources = ["canada", "citm_catalog", "citylots", "twitter"]
7+
8+
min_times = []
9+
for source in sources:
10+
s = dd(f"""\
11+
with open("../data/{source}.json") as f:
12+
json.load(f)""")
13+
times = repeat(stmt=s, setup="import json", repeat=3, number=1)
14+
t = reduce(min, times)
15+
print(f"{source} {t:0.06f} seconds")
16+
min_times.append(t)
17+
18+
geo_mean = reduce(lambda a, b: a*b, min_times)**(1/len(min_times))
19+
print(f"Total (G.M): {geo_mean:0.06f}")

0 commit comments

Comments
 (0)