summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLamont Granquist <lamont@scriptkiddie.org>2013-11-18 12:50:10 -0800
committerLamont Granquist <lamont@scriptkiddie.org>2013-11-18 12:50:10 -0800
commit665936aec015e11a47fb0ff2ad7849d4418f2692 (patch)
treedefb235178a42afc0f477b2cf7d38d80b1e6f7ec
parentdefe402b07d35f47ad33adefee69a06cbf2bc306 (diff)
downloadffi-yajl-665936aec015e11a47fb0ff2ad7849d4418f2692.tar.gz
add profiling option to benchmarks
-rwxr-xr-xbin/ffi-yajl-bench19
-rw-r--r--lib/ffi_yajl/benchmark.rb1
-rw-r--r--lib/ffi_yajl/benchmark/encode.rb7
-rw-r--r--lib/ffi_yajl/benchmark/encode_profile.rb38
4 files changed, 64 insertions, 1 deletions
diff --git a/bin/ffi-yajl-bench b/bin/ffi-yajl-bench
index 0ab2f1e..13725a0 100755
--- a/bin/ffi-yajl-bench
+++ b/bin/ffi-yajl-bench
@@ -1,8 +1,25 @@
#!/usr/bin/env ruby
+
$: << File.expand_path(File.join(File.dirname( File.symlink?(__FILE__) ? File.readlink(__FILE__) : __FILE__ ), "../lib"))
+require 'optparse'
require 'ffi_yajl/benchmark'
-FFI_Yajl::Benchmark::Encode.new().run()
+opts = {}
+optparse = OptionParser.new do |o|
+ o.banner = "Usage: ffi-yajl-bench"
+
+ opts[:profile] = false
+ o.on( '-p', '--profile', 'Run perftools.rb profiling' ) do
+ opts[:profile] = true
+ end
+end
+
+optparse.parse!
+if opts[:profile]
+ FFI_Yajl::Benchmark::EncodeProfile.new().run()
+else
+ FFI_Yajl::Benchmark::Encode.new().run()
+end
diff --git a/lib/ffi_yajl/benchmark.rb b/lib/ffi_yajl/benchmark.rb
index 146e1ca..6fb929c 100644
--- a/lib/ffi_yajl/benchmark.rb
+++ b/lib/ffi_yajl/benchmark.rb
@@ -1,4 +1,5 @@
require 'ffi_yajl/benchmark/encode.rb'
+require 'ffi_yajl/benchmark/encode_profile.rb'
#require 'ffi_yajl/benchmark/parse.rb'
diff --git a/lib/ffi_yajl/benchmark/encode.rb b/lib/ffi_yajl/benchmark/encode.rb
index c9d0dc2..fbeb503 100644
--- a/lib/ffi_yajl/benchmark/encode.rb
+++ b/lib/ffi_yajl/benchmark/encode.rb
@@ -49,6 +49,13 @@ module FFI_Yajl
}
}
+ ffi_string_encoder = FFI_Yajl::Encoder.new
+ x.report("FFI_Yajl::Encoder#encode (to a String)") {
+ times.times {
+ output = ffi_string_encoder.encode(hash)
+ }
+ }
+
if defined?(Yajl::Encoder)
x.report("Yajl::Encoder.encode (to a String)") {
times.times {
diff --git a/lib/ffi_yajl/benchmark/encode_profile.rb b/lib/ffi_yajl/benchmark/encode_profile.rb
new file mode 100644
index 0000000..ce98a3c
--- /dev/null
+++ b/lib/ffi_yajl/benchmark/encode_profile.rb
@@ -0,0 +1,38 @@
+# 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 EncodeProfile
+
+ def run
+ if defined?(PerfTools)
+ filename = File.expand_path(File.join(File.dirname(__FILE__), "subjects", "ohai.json"))
+ hash = File.open(filename, 'rb') { |f| FFI_Yajl::Parser.parse(f.read) }
+
+ times = 1000
+ puts "Starting profiling encoding #{filename} #{times} times\n\n"
+
+ ffi_string_encoder = FFI_Yajl::Encoder.new
+ PerfTools::CpuProfiler.start("/tmp/ffi_yajl_encode_profile.out") do
+ times.times {
+ output = ffi_string_encoder.encode(hash)
+ }
+ end
+ system("pprof.rb --text /tmp/ffi_yajl_encode_profile.out")
+ end
+ end
+
+ end
+ end
+end