Skip to content

Commit 821fe42

Browse files
committed
Add validate benchmark
1 parent 2f45f2f commit 821fe42

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

benchmark/validate.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
require "benchmark/ips"
2+
require "json"
3+
require "oj"
4+
require "yajl"
5+
require "fast_jsonparser"
6+
require "rapidjson"
7+
8+
if ENV["ONLY"]
9+
RUN = ENV["ONLY"].split(/[,: ]/).map{|x| [x.to_sym, true] }.to_h
10+
RUN.default = false
11+
elsif ENV["EXCEPT"]
12+
RUN = ENV["only"].split(/[,: ]/).map{|x| [x.to_sym, false] }.to_h
13+
RUN.default = true
14+
else
15+
RUN = Hash.new(true)
16+
end
17+
18+
def benchmark_parsing(name, json_output)
19+
puts "== Validating #{name} (#{json_output.size} bytes)"
20+
21+
Benchmark.ips do |x|
22+
x.report("parse") { RapidJSON.parse(json_output) } if RUN[:parse]
23+
x.report("validate") { RapidJSON.valid_json?(json_output) } if RUN[:validate]
24+
end
25+
puts
26+
end
27+
28+
benchmark_parsing "small nested array", JSON.dump([[1,2,3,4,5]]*10)
29+
benchmark_parsing "small hash", JSON.dump({ "username" => "jhawthorn", "id" => 123, "event" => "wrote json serializer" })
30+
31+
benchmark_parsing "test from oj", <<JSON
32+
{"a":"Alpha","b":true,"c":12345,"d":[true,[false,[-123456789,null],3.9676,["Something else.",false],null]],"e":{"zero":null,"one":1,"two":2,"three":[3],"four":[0,1,2,3,4]},"f":null,"h":{"a":{"b":{"c":{"d":{"e":{"f":{"g":null}}}}}}},"i":[[[[[[[null]]]]]]]}
33+
JSON
34+
35+
benchmark_parsing "twitter.json", File.read("#{__dir__}/../test/data/twitter.json")
36+
benchmark_parsing "citm_catalog.json", File.read("#{__dir__}/../test/data/citm_catalog.json")
37+
benchmark_parsing "canada.json", File.read("#{__dir__}/../test/data/canada.json")

0 commit comments

Comments
 (0)