diff options
-rwxr-xr-x | bin/ffi-yajl-bench | 4 | ||||
-rw-r--r-- | lib/ffi_yajl/benchmark/parse.rb | 78 | ||||
-rw-r--r-- | lib/ffi_yajl/benchmark/parse_profile.rb | 37 | ||||
-rw-r--r-- | lib/ffi_yajl/benchmark/parse_profile_ruby_prof.rb | 39 |
4 files changed, 127 insertions, 31 deletions
diff --git a/bin/ffi-yajl-bench b/bin/ffi-yajl-bench index 410c5e0..3502311 100755 --- a/bin/ffi-yajl-bench +++ b/bin/ffi-yajl-bench @@ -30,7 +30,7 @@ ENV['FORCE_FFI_YAJL'] = 'ffi' if opts[:ffi] ENV['FORCE_FFI_YAJL'] = 'ext' if opts[:ext] if opts[:profile] - FFI_Yajl::Benchmark::EncodeProfile.new().run() + FFI_Yajl::Benchmark::ParseProfileRubyProf.new().run() else - FFI_Yajl::Benchmark::Encode.new().run() + FFI_Yajl::Benchmark::Parse.new().run() end 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 diff --git a/lib/ffi_yajl/benchmark/parse_profile.rb b/lib/ffi_yajl/benchmark/parse_profile.rb new file mode 100644 index 0000000..5efb1e2 --- /dev/null +++ b/lib/ffi_yajl/benchmark/parse_profile.rb @@ -0,0 +1,37 @@ +# Portions Originally Copyright (c) 2008-2011 Brian Lopez - http://github.com/brianmario +# See MIT-LICENSE + +require 'rubygems' +require 'ffi_yajl' +begin + require 'perftools' +rescue Exception + puts "INFO: perftools.rb gem not installed" +end + +ENV['CPUPROFILE_FREQUENCY'] = "4000" + +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 + + times = 1000 + puts "Starting profiling encoding #{filename} #{times} times\n\n" + + PerfTools::CpuProfiler.start("/tmp/ffi_yajl_encode_profile.out") do + times.times { + output = FFI_Yajl::Parser.parse(json) + } + end + system("pprof.rb --text /tmp/ffi_yajl_encode_profile.out") + end + end + + 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 new file mode 100644 index 0000000..5b9f73e --- /dev/null +++ b/lib/ffi_yajl/benchmark/parse_profile_ruby_prof.rb @@ -0,0 +1,39 @@ +# Portions Originally Copyright (c) 2008-2011 Brian Lopez - http://github.com/brianmario +# See MIT-LICENSE + +require 'rubygems' +require 'ffi_yajl' + +module FFI_Yajl + class Benchmark + class ParseProfileRubyProf + + def run + begin + require 'ruby-prof' + rescue Exception + 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 + + times = 1000 + puts "Starting profiling encoding #{filename} #{times} times\n\n" + + result = RubyProf.profile do + times.times { + output = FFI_Yajl::Parser.parse(json) + } + end + + printer = RubyProf::GraphPrinter.new(result) + printer.print(STDOUT, {}) + + end + end + + end + end +end |