@@ -4,22 +4,22 @@ using ArgParse
4
4
using JSON
5
5
6
6
7
- function bench (f, simulate= false )
7
+ function bench (f, flags, parsefile, simulate= false )
8
8
fp = joinpath (JSON_DATA_DIR, string (f, " .json" ))
9
9
if ! isfile (fp)
10
10
println (" Downloading benchmark file..." )
11
11
download (DATA_SOURCES[f], fp)
12
12
end
13
13
GC. gc () # run gc so it doesn't affect benchmarks
14
- t = if args[ " parse " ][ " parse-file " ]
14
+ t = if parsefile
15
15
@elapsed JSON. parsefile (fp)
16
16
else
17
17
data = read (fp, String)
18
18
@elapsed JSON. Parser. parse (data)
19
19
end
20
20
21
21
if ! simulate
22
- printstyled (" [Bench$FLAGS ] " ; color= :yellow )
22
+ printstyled (" [Bench$flags ] " ; color= :yellow )
23
23
println (f, " " , t, " seconds" )
24
24
end
25
25
t
@@ -35,58 +35,64 @@ const DATA_SOURCES = Dict(
35
35
" citylots" => " https://raw.githubusercontent.com/zemirco/sf-city-lots-json/master/citylots.json" ,
36
36
" twitter" => " https://raw.githubusercontent.com/miloyip/nativejson-benchmark/v1.0.0/data/twitter.json" )
37
37
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
46
47
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
58
59
59
- const args = parse_args (ARGS , s)
60
+ args = parse_args (ARGS , s)
60
61
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" ]
65
65
66
- if args[ " parse " ][ " file " ] ≠ nothing
67
- const file = args[ " parse " ][ " file " ]
66
+ flags = string (include_compile ? " C " : " " ,
67
+ parsefile ? " F " : " " )
68
68
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" )
80
89
end
90
+ elseif args[" %COMMAND%" ] == " list"
91
+ println (" Available benchmarks are:" )
81
92
for k in sort (collect (keys (DATA_SOURCES)))
82
- times *= bench (k) # do benchmark
93
+ println ( " • $k " )
83
94
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 " )
91
95
end
92
96
end
97
+
98
+ main ()
0 commit comments