diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/ffi_yajl/benchmark/encode.rb | 101 | ||||
-rw-r--r-- | lib/ffi_yajl/benchmark/encode_json_and_marshal.rb | 28 | ||||
-rw-r--r-- | lib/ffi_yajl/benchmark/encode_json_and_yaml.rb | 32 | ||||
-rw-r--r-- | lib/ffi_yajl/benchmark/encode_profile.rb | 4 | ||||
-rw-r--r-- | lib/ffi_yajl/benchmark/http.rb | 20 | ||||
-rw-r--r-- | lib/ffi_yajl/benchmark/parse.rb | 180 | ||||
-rw-r--r-- | lib/ffi_yajl/benchmark/parse_json_and_marshal.rb | 30 | ||||
-rw-r--r-- | lib/ffi_yajl/benchmark/parse_json_and_yaml.rb | 34 | ||||
-rw-r--r-- | lib/ffi_yajl/benchmark/parse_profile.rb | 20 | ||||
-rw-r--r-- | lib/ffi_yajl/benchmark/parse_profile_ruby_prof.rb | 23 | ||||
-rw-r--r-- | lib/ffi_yajl/benchmark/parse_stream.rb | 30 | ||||
-rw-r--r-- | lib/ffi_yajl/ffi/encoder.rb | 6 | ||||
-rw-r--r-- | lib/ffi_yajl/ffi/parser.rb | 10 |
13 files changed, 202 insertions, 316 deletions
diff --git a/lib/ffi_yajl/benchmark/encode.rb b/lib/ffi_yajl/benchmark/encode.rb index 8e5c83f..fc03c06 100644 --- a/lib/ffi_yajl/benchmark/encode.rb +++ b/lib/ffi_yajl/benchmark/encode.rb @@ -20,16 +20,6 @@ rescue LoadError puts "INFO: json gem not installed" end begin - require 'psych' -rescue LoadError - puts "INFO: psych gem not installed" -end -begin - require 'active_support' -rescue LoadError - puts "INFO: active_support gem not installed" -end -begin require 'oj' rescue LoadError puts "INFO: oj gem not installed" @@ -45,88 +35,47 @@ module FFI_Yajl times = ARGV[1] ? ARGV[1].to_i : 1000 puts "Starting benchmark encoding #{filename} #{times} times\n\n" - ::Benchmark.bmbm { |x| - x.report("FFI_Yajl::Encoder.encode (to a String)") { - times.times { - FFI_Yajl::Encoder.encode(hash) - } - } + ::Benchmark.bmbm do |x| + x.report("FFI_Yajl::Encoder.encode (to a String)") do + times.times { FFI_Yajl::Encoder.encode(hash) } + end ffi_string_encoder = FFI_Yajl::Encoder.new - x.report("FFI_Yajl::Encoder#encode (to a String)") { - times.times { - ffi_string_encoder.encode(hash) - } - } + x.report("FFI_Yajl::Encoder#encode (to a String)") do + times.times { ffi_string_encoder.encode(hash) } + end if defined?(Oj) - x.report("Oj.dump (to a String)") { - times.times { - Oj.dump(hash) - } - } + x.report("Oj.dump (to a String)") do + times.times { Oj.dump(hash) } + end end if defined?(Yajl::Encoder) - x.report("Yajl::Encoder.encode (to a String)") { - times.times { - Yajl::Encoder.encode(hash) - } - } + x.report("Yajl::Encoder.encode (to a String)") do + times.times { Yajl::Encoder.encode(hash) } + end io_encoder = Yajl::Encoder.new - x.report("Yajl::Encoder#encode (to an IO)") { - times.times { - io_encoder.encode(hash, StringIO.new) - } - } + x.report("Yajl::Encoder#encode (to an IO)") do + times.times { io_encoder.encode(hash, StringIO.new) } + end string_encoder = Yajl::Encoder.new - x.report("Yajl::Encoder#encode (to a String)") { - times.times { - string_encoder.encode(hash) - } - } + x.report("Yajl::Encoder#encode (to a String)") do + times.times { string_encoder.encode(hash) } + end end if defined?(JSON) - x.report("JSON.generate") { - times.times { - JSON.generate(hash) - } - } - x.report("JSON.fast_generate") { - times.times { - JSON.fast_generate(hash) - } - } - end - if defined?(Psych) - x.report("Psych.to_json") { - times.times { - Psych.to_json(hash) - } - } - if defined?(Psych::JSON::Stream) - x.report("Psych::JSON::Stream") { - times.times { - io = StringIO.new - stream = Psych::JSON::Stream.new io - stream.start - stream.push hash - stream.finish - } - } + x.report("JSON.generate") do + times.times { JSON.generate(hash) } + end + x.report("JSON.fast_generate") do + times.times { JSON.fast_generate(hash) } end end - # if defined?(ActiveSupport::JSON) - # x.report("ActiveSupport::JSON.encode") { - # times.times { - # ActiveSupport::JSON.encode(hash) - # } - # } - # end - } + end end end end diff --git a/lib/ffi_yajl/benchmark/encode_json_and_marshal.rb b/lib/ffi_yajl/benchmark/encode_json_and_marshal.rb index 73a7d0c..e9d19bc 100644 --- a/lib/ffi_yajl/benchmark/encode_json_and_marshal.rb +++ b/lib/ffi_yajl/benchmark/encode_json_and_marshal.rb @@ -17,26 +17,26 @@ hash = Yajl::Parser.new.parse(json) json.close puts "Starting benchmark encoding #{filename} #{times} times\n\n" -Benchmark.bmbm { |x| +Benchmark.bmbm do |x| encoder = Yajl::Encoder.new - x.report { + x.report do puts "Yajl::Encoder#encode" - times.times { + times.times do encoder.encode(hash, StringIO.new) - } - } + end + end if defined?(JSON) - x.report { + x.report do puts "JSON's #to_json" - times.times { + times.times do JSON.generate(hash) - } - } + end + end end - x.report { + x.report do puts "Marshal.dump" - times.times { + times.times do Marshal.dump(hash) - } - } -} + end + end +end diff --git a/lib/ffi_yajl/benchmark/encode_json_and_yaml.rb b/lib/ffi_yajl/benchmark/encode_json_and_yaml.rb index 4ceb022..33dbbb2 100644 --- a/lib/ffi_yajl/benchmark/encode_json_and_yaml.rb +++ b/lib/ffi_yajl/benchmark/encode_json_and_yaml.rb @@ -18,23 +18,19 @@ json.close times = ARGV[0] ? ARGV[0].to_i : 1000 puts "Starting benchmark encoding #{filename} into JSON #{times} times\n\n" -Benchmark.bmbm { |x| +Benchmark.bmbm do |x| encoder = Yajl::Encoder.new - x.report { + x.report do puts "Yajl::Encoder#encode" - times.times { - encoder.encode(hash, StringIO.new) - } - } + times.times { encoder.encode(hash, StringIO.new) } + end if defined?(JSON) - x.report { + x.report do puts "JSON's #to_json" - times.times { - JSON.generate(hash) - } - } + times.times { JSON.generate(hash) } + end end -} +end # YAML Section filename = 'benchmark/subjects/ohai.yml' @@ -43,11 +39,9 @@ data = YAML.load_stream(yml) yml.close puts "Starting benchmark encoding #{filename} into YAML #{times} times\n\n" -Benchmark.bmbm { |x| - x.report { +Benchmark.bmbm do |x| + x.report do puts "YAML.dump" - times.times { - YAML.dump(data, StringIO.new) - } - } -} + times.times { YAML.dump(data, StringIO.new) } + end +end diff --git a/lib/ffi_yajl/benchmark/encode_profile.rb b/lib/ffi_yajl/benchmark/encode_profile.rb index 2cf19f1..36b92c7 100644 --- a/lib/ffi_yajl/benchmark/encode_profile.rb +++ b/lib/ffi_yajl/benchmark/encode_profile.rb @@ -25,9 +25,7 @@ module FFI_Yajl ffi_string_encoder = FFI_Yajl::Encoder.new PerfTools::CpuProfiler.start("/tmp/ffi_yajl_encode_profile.out") do - times.times { - ffi_string_encoder.encode(hash) - } + times.times { ffi_string_encoder.encode(hash) } end system("pprof.rb --text /tmp/ffi_yajl_encode_profile.out") end diff --git a/lib/ffi_yajl/benchmark/http.rb b/lib/ffi_yajl/benchmark/http.rb index 771e452..b94a46c 100644 --- a/lib/ffi_yajl/benchmark/http.rb +++ b/lib/ffi_yajl/benchmark/http.rb @@ -16,17 +16,13 @@ uri = URI.parse('http://search.twitter.com/search.json?q=github') times = ARGV[0] ? ARGV[0].to_i : 1 puts "Starting benchmark parsing #{uri} #{times} times\n\n" -Benchmark.bmbm { |x| - x.report { +Benchmark.bmbm do |x| + x.report do puts "Yajl::HttpStream.get" - times.times { - Yajl::HttpStream.get(uri) - } - } - x.report { + times.times { Yajl::HttpStream.get(uri) } + end + x.report do puts "JSON.parser" - times.times { - JSON.parse(Net::HTTP.get_response(uri).body, max_nesting: false) - } - } -} + times.times { JSON.parse(Net::HTTP.get_response(uri).body, max_nesting: false) } + end +end diff --git a/lib/ffi_yajl/benchmark/parse.rb b/lib/ffi_yajl/benchmark/parse.rb index 7e602fa..6aacc94 100644 --- a/lib/ffi_yajl/benchmark/parse.rb +++ b/lib/ffi_yajl/benchmark/parse.rb @@ -1,6 +1,5 @@ require 'rubygems' require 'benchmark' -require 'yaml' require 'yajl' require 'ffi_yajl' if !defined?(RUBY_ENGINE) || RUBY_ENGINE !~ /jruby/ @@ -17,123 +16,78 @@ begin 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 +module FFI_Yajl + class Benchmark + class 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) - } - } + 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 do |x| + x.report do + puts "FFI_Yajl::Parser.parse (from a String)" + times.times { FFI_Yajl::Parser.parse(json_str) } + end + # 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 do + puts "Yajl::Parser.parse (from a String)" + times.times { Yajl::Parser.parse(json_str) } + end + io_parser = Yajl::Parser.new + io_parser.on_parse_complete = ->(obj) {} if times > 1 + x.report do + puts "Yajl::Parser#parse (from an IO)" + times.times do + json.rewind + io_parser.parse(json) + end + end + string_parser = Yajl::Parser.new + string_parser.on_parse_complete = ->(obj) {} if times > 1 + x.report do + puts "Yajl::Parser#parse (from a String)" + times.times do + json.rewind + string_parser.parse(json_str) + end + end + end + if defined?(Oj) + x.report do + puts "Oj.load" + times.times do + json.rewind + Oj.load(json.read) + end + end + end + if defined?(JSON) + x.report do + puts "JSON.parse" + times.times do + json.rewind + JSON.parse(json.read, max_nesting: false) + end + end + end + end + json.close end - } - json.close + end end end diff --git a/lib/ffi_yajl/benchmark/parse_json_and_marshal.rb b/lib/ffi_yajl/benchmark/parse_json_and_marshal.rb index 204ab06..cd07d46 100644 --- a/lib/ffi_yajl/benchmark/parse_json_and_marshal.rb +++ b/lib/ffi_yajl/benchmark/parse_json_and_marshal.rb @@ -19,32 +19,32 @@ hash = {} times = ARGV[0] ? ARGV[0].to_i : 1000 puts "Starting benchmark parsing #{File.size(filename)} bytes of JSON data #{times} times\n\n" -Benchmark.bmbm { |x| - x.report { +Benchmark.bmbm do |x| + x.report do puts "Yajl::Parser#parse" yajl = Yajl::Parser.new - yajl.on_parse_complete = lambda { |obj| } if times > 1 - times.times { + yajl.on_parse_complete = ->(obj) {} if times > 1 + times.times do json.rewind hash = yajl.parse(json) - } - } + end + end if defined?(JSON) - x.report { + x.report do puts "JSON.parse" - times.times { + times.times do json.rewind JSON.parse(json.read, max_nesting: false) - } - } + end + end end - x.report { + x.report do puts "Marshal.load" - times.times { + times.times do marshal_file.rewind Marshal.load(marshal_file) - } - } -} + end + end +end json.close marshal_file.close diff --git a/lib/ffi_yajl/benchmark/parse_json_and_yaml.rb b/lib/ffi_yajl/benchmark/parse_json_and_yaml.rb index 5076f67..6e479ce 100644 --- a/lib/ffi_yajl/benchmark/parse_json_and_yaml.rb +++ b/lib/ffi_yajl/benchmark/parse_json_and_yaml.rb @@ -16,26 +16,26 @@ json = File.new(filename, 'r') times = ARGV[0] ? ARGV[0].to_i : 1000 puts "Starting benchmark parsing #{File.size(filename)} bytes of JSON data #{times} times\n\n" -Benchmark.bmbm { |x| +Benchmark.bmbm do |x| parser = Yajl::Parser.new - parser.on_parse_complete = lambda { |obj| } if times > 1 - x.report { + parser.on_parse_complete = ->(obj) {} if times > 1 + x.report do puts "Yajl::Parser#parse" - times.times { + times.times do json.rewind parser.parse(json) - } - } + end + end if defined?(JSON) - x.report { + x.report do puts "JSON.parse" - times.times { + times.times do json.rewind JSON.parse(json.read, max_nesting: false) - } - } + end + end end -} +end json.close # YAML section @@ -43,13 +43,13 @@ filename = 'benchmark/subjects/ohai.yml' yaml = File.new(filename, 'r') puts "Starting benchmark parsing #{File.size(filename)} bytes of YAML data #{times} times\n\n" -Benchmark.bmbm { |x| - x.report { +Benchmark.bmbm do |x| + x.report do puts "YAML.load_stream" - times.times { + times.times do yaml.rewind YAML.load(yaml) - } - } -} + end + end +end yaml.close diff --git a/lib/ffi_yajl/benchmark/parse_profile.rb b/lib/ffi_yajl/benchmark/parse_profile.rb index 9e53729..6ef00ac 100644 --- a/lib/ffi_yajl/benchmark/parse_profile.rb +++ b/lib/ffi_yajl/benchmark/parse_profile.rb @@ -15,20 +15,18 @@ module FFI_Yajl class Benchmark class ParseProfile def run - if defined?(PerfTools) - filename = File.expand_path(File.join(File.dirname(__FILE__), "subjects", "ohai.json")) - json = File.new(filename, 'r').read + return if defined?(PerfTools) - times = 1000 - puts "Starting profiling encoding #{filename} #{times} times\n\n" + filename = File.expand_path(File.join(File.dirname(__FILE__), "subjects", "ohai.json")) + json = File.new(filename, 'r').read - PerfTools::CpuProfiler.start("/tmp/ffi_yajl_encode_profile.out") do - times.times { - FFI_Yajl::Parser.parse(json) - } - end - system("pprof.rb --text /tmp/ffi_yajl_encode_profile.out") + times = 1000 + puts "Starting profiling encoding #{filename} #{times} times\n\n" + + PerfTools::CpuProfiler.start("/tmp/ffi_yajl_encode_profile.out") do + times.times { FFI_Yajl::Parser.parse(json) } end + system("pprof.rb --text /tmp/ffi_yajl_encode_profile.out") end end end diff --git a/lib/ffi_yajl/benchmark/parse_profile_ruby_prof.rb b/lib/ffi_yajl/benchmark/parse_profile_ruby_prof.rb index f71a756..51696c5 100644 --- a/lib/ffi_yajl/benchmark/parse_profile_ruby_prof.rb +++ b/lib/ffi_yajl/benchmark/parse_profile_ruby_prof.rb @@ -14,23 +14,20 @@ module FFI_Yajl puts "INFO: perftools.rb gem not installed" end - if defined?(RubyProf) - filename = File.expand_path(File.join(File.dirname(__FILE__), "subjects", "ohai.json")) - json = File.new(filename, 'r').read + return if defined?(RubyProf) - times = 1000 - puts "Starting profiling encoding #{filename} #{times} times\n\n" + filename = File.expand_path(File.join(File.dirname(__FILE__), "subjects", "ohai.json")) + json = File.new(filename, 'r').read - result = RubyProf.profile do - times.times { - FFI_Yajl::Parser.parse(json) - } - end - - printer = RubyProf::GraphPrinter.new(result) - printer.print(STDOUT, {}) + times = 1000 + puts "Starting profiling encoding #{filename} #{times} times\n\n" + result = RubyProf.profile do + times.times { FFI_Yajl::Parser.parse(json) } end + + printer = RubyProf::GraphPrinter.new(result) + printer.print(STDOUT, {}) end end end diff --git a/lib/ffi_yajl/benchmark/parse_stream.rb b/lib/ffi_yajl/benchmark/parse_stream.rb index acf776a..679eb92 100644 --- a/lib/ffi_yajl/benchmark/parse_stream.rb +++ b/lib/ffi_yajl/benchmark/parse_stream.rb @@ -18,37 +18,37 @@ json = File.new(filename, 'r') times = ARGV[0] ? ARGV[0].to_i : 100 puts "Starting benchmark parsing JSON stream (#{File.size(filename)} bytes of JSON data with 430 JSON separate strings) #{times} times\n\n" -Benchmark.bmbm { |x| +Benchmark.bmbm do |x| parser = Yajl::Parser.new - parser.on_parse_complete = lambda { |obj| } - x.report { + parser.on_parse_complete = ->(obj) {} + x.report do puts "Yajl::Parser#parse" - times.times { + times.times do json.rewind parser.parse(json) - } - } + end + end if defined?(JSON) - x.report { + x.report do puts "JSON.parse" - times.times { + times.times do json.rewind while chunk = json.gets JSON.parse(chunk, max_nesting: false) end - } - } + end + end end if defined?(ActiveSupport::JSON) - x.report { + x.report do puts "ActiveSupport::JSON.decode" - times.times { + times.times do json.rewind while chunk = json.gets ActiveSupport::JSON.decode(chunk) end - } - } + end + end end -} +end json.close diff --git a/lib/ffi_yajl/ffi/encoder.rb b/lib/ffi_yajl/ffi/encoder.rb index 51a3c7a..304c85f 100644 --- a/lib/ffi_yajl/ffi/encoder.rb +++ b/lib/ffi_yajl/ffi/encoder.rb @@ -159,7 +159,7 @@ class Fixnum def ffi_yajl(yajl_gen, state) str = to_s if str == "NaN" || str == "Infinity" || str == "-Infinity" - raise ::FFI_Yajl::EncodeError.new("'#{str}' is an invalid number") + raise ::FFI_Yajl::EncodeError, "'#{str}' is an invalid number" end if state[:processing_key] if ( status = FFI_Yajl.yajl_gen_string(yajl_gen, str, str.bytesize) ) != 0 @@ -177,7 +177,7 @@ class Bignum def ffi_yajl(yajl_gen, state) str = to_s if str == "NaN" || str == "Infinity" || str == "-Infinity" - raise ::FFI_Yajl::EncodeError.new("'#{str}' is an invalid number") + raise ::FFI_Yajl::EncodeError, "'#{str}' is an invalid number" end if state[:processing_key] if ( status = FFI_Yajl.yajl_gen_string(yajl_gen, str, str.bytesize) ) != 0 @@ -195,7 +195,7 @@ class Float def ffi_yajl(yajl_gen, state) str = to_s if str == "NaN" || str == "Infinity" || str == "-Infinity" - raise ::FFI_Yajl::EncodeError.new("'#{str}' is an invalid number") + raise ::FFI_Yajl::EncodeError, "'#{str}' is an invalid number" end if state[:processing_key] if ( status = FFI_Yajl.yajl_gen_string(yajl_gen, str, str.bytesize) ) != 0 diff --git a/lib/ffi_yajl/ffi/parser.rb b/lib/ffi_yajl/ffi/parser.rb index 598fecf..67df934 100644 --- a/lib/ffi_yajl/ffi/parser.rb +++ b/lib/ffi_yajl/ffi/parser.rb @@ -23,12 +23,12 @@ module FFI_Yajl module FFI module Parser - def set_value(val) + def set_value(val) # rubocop:disable Style/AccessorMethodName case stack.last when Hash - raise FFI_Yajl::ParseError.new("internal error: missing key in parse") if key.nil? + raise FFI_Yajl::ParseError, "internal error: missing key in parse" if key.nil? if @opts[:unique_key_checking] && stack.last.key?(key) - raise FFI_Yajl::ParseError.new("repeated key: #{key}") + raise FFI_Yajl::ParseError, "repeated key: #{key}" end stack.last[key] = val when Array @@ -146,12 +146,12 @@ module FFI_Yajl if ( ::FFI_Yajl.yajl_parse(yajl_handle, str, str.bytesize) != :yajl_status_ok ) # FIXME: dup the error and call yajl_free_error? error = ::FFI_Yajl.yajl_get_error(yajl_handle, 1, str, str.bytesize) - raise ::FFI_Yajl::ParseError.new(error) + raise ::FFI_Yajl::ParseError, error end if ( ::FFI_Yajl.yajl_complete_parse(yajl_handle) != :yajl_status_ok ) # FIXME: dup the error and call yajl_free_error? error = ::FFI_Yajl.yajl_get_error(yajl_handle, 1, str, str.bytesize) - raise ::FFI_Yajl::ParseError.new(error) + raise ::FFI_Yajl::ParseError, error end stack.pop ensure |