blob: 7e602fa2d19bbec24e8a9791e367bd615de86158 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
require 'rubygems'
require 'benchmark'
require 'yaml'
require 'yajl'
require 'ffi_yajl'
if !defined?(RUBY_ENGINE) || RUBY_ENGINE !~ /jruby/
begin
require 'yajl'
rescue LoadError
puts "INFO: yajl-ruby not installed"
end
else
puts "INFO: skipping yajl-ruby on jruby"
end
begin
require 'json'
rescue LoadError
end
begin
require 'psych'
rescue LoadError
end
begin
require 'active_support'
rescue LoadError
end
begin
require 'oj'
rescue LoadError
end
class FFI_Yajl::Benchmark::Parse
def run
filename = File.expand_path(File.join(File.dirname(__FILE__), "subjects", "item.json"))
json = File.new(filename, 'r')
json_str = json.read
times = ARGV[1] ? ARGV[1].to_i : 10_000
puts "Starting benchmark parsing #{File.size(filename)} bytes of JSON data #{times} times\n\n"
Benchmark.bmbm { |x|
x.report {
puts "FFI_Yajl::Parser.parse (from a String)"
times.times {
FFI_Yajl::Parser.parse(json_str)
}
}
# ffi_parser = FFI_Yajl::Parser.new
# x.report {
# puts "FFI_Yajl::Parser#parse (from a String)"
# times.times {
# json.rewind
# ffi_parser.parse(json.read)
# }
# }
if defined?(Yajl::Parser)
x.report {
puts "Yajl::Parser.parse (from a String)"
times.times {
Yajl::Parser.parse(json_str)
}
}
io_parser = Yajl::Parser.new
io_parser.on_parse_complete = lambda { |obj| } if times > 1
x.report {
puts "Yajl::Parser#parse (from an IO)"
times.times {
json.rewind
io_parser.parse(json)
}
}
string_parser = Yajl::Parser.new
string_parser.on_parse_complete = lambda { |obj| } if times > 1
x.report {
puts "Yajl::Parser#parse (from a String)"
times.times {
json.rewind
string_parser.parse(json_str)
}
}
end
if defined?(Oj)
x.report {
puts "Oj.load"
times.times {
json.rewind
Oj.load(json.read)
}
}
end
if defined?(JSON)
x.report {
puts "JSON.parse"
times.times {
json.rewind
JSON.parse(json.read, max_nesting: false)
}
}
end
if defined?(ActiveSupport::JSON)
x.report {
puts "ActiveSupport::JSON.decode"
times.times {
json.rewind
ActiveSupport::JSON.decode(json.read)
}
}
end
x.report {
puts "YAML.load (from an IO)"
times.times {
json.rewind
YAML.load(json)
}
}
x.report {
puts "YAML.load (from a String)"
times.times {
YAML.load(json_str)
}
}
if defined?(Psych)
x.report {
puts "Psych.load (from an IO)"
times.times {
json.rewind
Psych.load(json)
}
}
x.report {
puts "Psych.load (from a String)"
times.times {
Psych.load(json_str)
}
}
end
}
json.close
end
end
|