diff options
Diffstat (limited to 'lib/ffi_yajl/benchmark/parse.rb')
-rw-r--r-- | lib/ffi_yajl/benchmark/parse.rb | 78 |
1 files changed, 49 insertions, 29 deletions
diff --git a/lib/ffi_yajl/benchmark/parse.rb b/lib/ffi_yajl/benchmark/parse.rb index 2665382..e091343 100644 --- a/lib/ffi_yajl/benchmark/parse.rb +++ b/lib/ffi_yajl/benchmark/parse.rb @@ -3,6 +3,19 @@ require 'benchmark' require 'yaml' require 'yajl' require 'ffi_yajl' +if !defined?(RUBY_ENGINE) || RUBY_ENGINE !~ /jruby/ + if ENV['FORCE_FFI_YAJL'] != 'ext' + begin + require 'yajl' + rescue Exception + puts "INFO: yajl-ruby not installed" + end + else + puts "INFO: skipping yajl-ruby because we're using the C extension" + end +else + puts "INFO: skipping yajl-ruby on jruby" +end begin require 'json' rescue LoadError @@ -19,8 +32,9 @@ end class FFI_Yajl::Benchmark::Parse def run - filename = ARGV[0] || 'benchmark/subjects/item.json' + 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" @@ -28,35 +42,43 @@ class FFI_Yajl::Benchmark::Parse x.report { puts "FFI_Yajl::Parser.parse (from a String)" times.times { - json.rewind - FFI_Yajl::Parser.parse(json.read) + FFI_Yajl::Parser.parse(json_str) } } - x.report { - puts "Yajl::Parser.parse (from a String)" - times.times { - json.rewind - Yajl::Parser.parse(json.read) +# 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) + 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.read) + 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?(JSON) x.report { puts "JSON.parse" @@ -85,8 +107,7 @@ class FFI_Yajl::Benchmark::Parse x.report { puts "YAML.load (from a String)" times.times { - json.rewind - YAML.load(json.read) + YAML.load(json_str) } } if defined?(Psych) @@ -100,8 +121,7 @@ class FFI_Yajl::Benchmark::Parse x.report { puts "Psych.load (from a String)" times.times { - json.rewind - Psych.load(json.read) + Psych.load(json_str) } } end |