Skip to content

Commit

Permalink
Merge pull request #677 from eregon/fix-benchmarks
Browse files Browse the repository at this point in the history
Fix benchmarks and harness, compile libtest automatically
  • Loading branch information
larskanis committed Feb 18, 2019
2 parents a26e88c + 90d0d1c commit 476b9a8
Show file tree
Hide file tree
Showing 43 changed files with 151 additions and 170 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ language: ruby
script:
- bundle exec rake compile || bundle exec rake compile
- bundle exec rake test
- ITER=10 bundle exec rake bench:all
os:
- linux
- osx
Expand Down
4 changes: 2 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ task :test => [ :spec ]

namespace :bench do
ITER = ENV['ITER'] ? ENV['ITER'].to_i : 100000
bench_libs = "-Ilib -I#{BUILD_DIR}" unless RUBY_PLATFORM == "java"
bench_files = Dir["bench/bench_*.rb"].reject { |f| f == "bench_helper.rb" }
bench_libs = "-Ilib" unless RUBY_PLATFORM == "java"
bench_files = Dir["bench/bench_*.rb"].reject { |f| f == "bench/bench_helper.rb" }
bench_files.each do |bench|
task File.basename(bench, ".rb")[6..-1] => TEST_DEPS do
sh %{#{Gem.ruby} #{bench_libs} #{bench} #{ITER}}
Expand Down
2 changes: 1 addition & 1 deletion bench/bench_FFFrV.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require File.expand_path(File.join(File.dirname(__FILE__), "bench_helper"))
require_relative 'bench_helper'

module LibTest
extend FFI::Library
Expand Down
2 changes: 1 addition & 1 deletion bench/bench_FrV.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require File.expand_path(File.join(File.dirname(__FILE__), "bench_helper"))
require_relative 'bench_helper'

module LibTest
extend FFI::Library
Expand Down
2 changes: 1 addition & 1 deletion bench/bench_IIIIIIrV.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require File.expand_path(File.join(File.dirname(__FILE__), "bench_helper"))
require_relative 'bench_helper'

module LibTest
extend FFI::Library
Expand Down
6 changes: 2 additions & 4 deletions bench/bench_IIIrI.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require File.expand_path(File.join(File.dirname(__FILE__), "bench_helper"))
require_relative 'bench_helper'

module LibTest
extend FFI::Library
Expand All @@ -7,11 +7,9 @@ module LibTest
end


puts "Benchmark [ :int, :int, :int ], :void performance, #{ITER}x calls"

puts "Benchmark [ :int, :int, :int ], :int performance, #{ITER}x calls"
10.times {
puts Benchmark.measure {
ITER.times { LibTest.bench(0, 1, 2) }
}
}
puts "Benchmark Invoker.call [ :int, :int, :int ], :void performance, #{ITER}x calls"
2 changes: 1 addition & 1 deletion bench/bench_IIIrV.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require File.expand_path(File.join(File.dirname(__FILE__), "bench_helper"))
require_relative 'bench_helper'

module LibTest
extend FFI::Library
Expand Down
2 changes: 1 addition & 1 deletion bench/bench_IrV.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require File.expand_path(File.join(File.dirname(__FILE__), "bench_helper"))
require_relative 'bench_helper'

module LibTest
extend FFI::Library
Expand Down
2 changes: 1 addition & 1 deletion bench/bench_LLLrV.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require File.expand_path(File.join(File.dirname(__FILE__), "bench_helper"))
require_relative 'bench_helper'

module LibTest
extend FFI::Library
Expand Down
2 changes: 1 addition & 1 deletion bench/bench_PPPrV.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require File.expand_path(File.join(File.dirname(__FILE__), "bench_helper"))
require_relative 'bench_helper'

module LibTest
extend FFI::Library
Expand Down
2 changes: 1 addition & 1 deletion bench/bench_PPrV.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require File.expand_path(File.join(File.dirname(__FILE__), "bench_helper"))
require_relative 'bench_helper'

module LibTest
extend FFI::Library
Expand Down
2 changes: 1 addition & 1 deletion bench/bench_PrV.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require File.expand_path(File.join(File.dirname(__FILE__), "bench_helper"))
require_relative 'bench_helper'

module LibTest
extend FFI::Library
Expand Down
2 changes: 1 addition & 1 deletion bench/bench_SrV.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require File.expand_path(File.join(File.dirname(__FILE__), "bench_helper"))
require_relative 'bench_helper'

module LibTest
extend FFI::Library
Expand Down
2 changes: 1 addition & 1 deletion bench/bench_VrI.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require File.expand_path(File.join(File.dirname(__FILE__), "bench_helper"))
require_relative 'bench_helper'

module LibTest
extend FFI::Library
Expand Down
2 changes: 1 addition & 1 deletion bench/bench_VrV.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require File.expand_path(File.join(File.dirname(__FILE__), "bench_helper"))
require_relative 'bench_helper'

module LibTest
extend FFI::Library
Expand Down
9 changes: 3 additions & 6 deletions bench/bench_autoptr.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
require File.expand_path(File.join(File.dirname(__FILE__), "bench_helper"))
require_relative 'bench_helper'

require 'benchmark'
require 'ffi'
iter = ITER

class Ptr < FFI::AutoPointer
Expand All @@ -16,7 +14,7 @@ module LibC
attach_function :malloc, [ :long ], Ptr, :ignore_error => true
attach_function :malloc2, :malloc, [ :long ], :pointer, :ignore_error => true
attach_function :free, [ :pointer ], :void, :ignore_error => true
def self.finalizer(ptr)
def self.finalizer(ptr)
proc { LibC.free(ptr) }
end
end
Expand All @@ -32,11 +30,10 @@ def self.finalizer(ptr)
puts "Benchmark ObjectSpace finalizer performance, #{iter}x"
10.times {
puts Benchmark.measure {
iter.times {
iter.times {
ptr = LibC.malloc2(4)
ptr2 = FFI::Pointer.new(ptr)
ObjectSpace.define_finalizer(ptr2, LibC.finalizer(ptr))
}
}
}

5 changes: 1 addition & 4 deletions bench/bench_buffer.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
require File.expand_path(File.join(File.dirname(__FILE__), "bench_helper"))
require_relative 'bench_helper'

require 'benchmark'
require 'ffi'
iter = ITER

module BufferBench
Expand Down Expand Up @@ -109,4 +107,3 @@ class IntStruct < FFI::Struct
iter.times { BufferBench.bench_buffer_out(FFI::Buffer.alloc_out(4, 1, true), 0) }
}
}

5 changes: 1 addition & 4 deletions bench/bench_buffer_alloc.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
require File.expand_path(File.join(File.dirname(__FILE__), "bench_helper"))
require_relative 'bench_helper'

require 'benchmark'
require 'ffi'
iter = ITER

puts "Benchmark Buffer.new(:int, 1, true)) performance, #{iter}x"
Expand Down Expand Up @@ -43,4 +41,3 @@
end
}
}

8 changes: 3 additions & 5 deletions bench/bench_buffer_fill.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
require File.expand_path(File.join(File.dirname(__FILE__), "bench_helper"))
require_relative 'bench_helper'

require 'benchmark'
require 'ffi'
iter = ITER

puts "Benchmark Buffer#put_array_of_float performance, #{iter}x"

5.times {
ptr = FFI::Buffer.new(:float, 8, false)
puts Benchmark.measure {
iter.times {
iter.times {
ptr.put_array_of_float(0, [ 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8 ])
}
}
Expand All @@ -19,7 +17,7 @@
puts "Benchmark Buffer.new(:float, 8, false)).put_array_of_float performance, #{iter}x"
5.times {
puts Benchmark.measure {
iter.times {
iter.times {
ptr = FFI::Buffer.new(:float, 8, false)
ptr.put_array_of_float(0, [ 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8 ])
}
Expand Down
6 changes: 2 additions & 4 deletions bench/bench_chmod.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
require 'benchmark'
require 'ffi'
require 'ffi/platform'
require_relative 'bench_helper'

iter = 10_000
iter = ITER
file = "README"

module Posix
Expand Down
3 changes: 1 addition & 2 deletions bench/bench_closure_IIIrV.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require File.expand_path(File.join(File.dirname(__FILE__), "bench_helper"))
require_relative 'bench_helper'

module LibTest
extend FFI::Library
Expand Down Expand Up @@ -65,4 +65,3 @@ def call(a, b, c); nil; end
ITER.times { LibTest.rb_bench(1, 2, 3) {} }
}
}

3 changes: 1 addition & 2 deletions bench/bench_closure_IrV.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require File.expand_path(File.join(File.dirname(__FILE__), "bench_helper"))
require_relative 'bench_helper'

module LibTest
extend FFI::Library
Expand Down Expand Up @@ -37,4 +37,3 @@ def self.rb_bench(i, &block); nil; end
ITER.times { LibTest.rb_bench(1) {} }
}
}

7 changes: 3 additions & 4 deletions bench/bench_closure_VrV.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
require File.expand_path(File.join(File.dirname(__FILE__), "bench_helper"))
require_relative 'bench_helper'

module LibTest
extend FFI::Library
ffi_lib LIBTEST_PATH
callback :closureVrV, [ ], :void

attach_function :ffi_bench, :testClosureVrV, [ :closureVrV ], :void
@blocking = true
attach_function :threaded_bench, :testThreadedClosureVrV, [ :closureVrV, :int ], :void

def self.rb_bench(&block)
yield
end
Expand Down Expand Up @@ -51,4 +51,3 @@ def self.rb_bench(&block)
ITER.times { LibTest.rb_bench {} }
}
}

3 changes: 1 addition & 2 deletions bench/bench_enum.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require File.expand_path(File.join(File.dirname(__FILE__), "bench_helper"))
require_relative 'bench_helper'

module LibTest
extend FFI::Library
Expand Down Expand Up @@ -43,4 +43,3 @@ def self.rb_bench(i0); nil; end
ITER.times { LibTest.rb_bench(:a) }
}
}

2 changes: 1 addition & 1 deletion bench/bench_enum_i.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require File.expand_path(File.join(File.dirname(__FILE__), "bench_helper"))
require_relative 'bench_helper'

module LibTest
extend FFI::Library
Expand Down
9 changes: 4 additions & 5 deletions bench/bench_getlogin.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
require 'benchmark'
require 'ffi'
require_relative 'bench_helper'
require 'etc'

iter = 1000000
iter = ITER

module Posix
extend FFI::Library
Expand All @@ -13,15 +12,15 @@ module Posix
raise ArgumentError, "FFI getlogin returned incorrect value"
end

puts "Benchmark FFI getlogin(2) performance, #{iter}x"
puts "Benchmark FFI getlogin(2) performance, #{ITER}x"

10.times {
puts Benchmark.measure {
iter.times { Posix.getlogin }
}
}

puts "Benchmark Etc.getlogin performance, #{iter}x"
puts "Benchmark Etc.getlogin performance, #{ITER}x"
10.times {
puts Benchmark.measure {
iter.times { Etc.getlogin }
Expand Down
2 changes: 1 addition & 1 deletion bench/bench_getpid.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require File.expand_path(File.join(File.dirname(__FILE__), "bench_helper"))
require_relative 'bench_helper'

iter = ITER

Expand Down
2 changes: 1 addition & 1 deletion bench/bench_gettimeofday.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require File.expand_path(File.join(File.dirname(__FILE__), "bench_helper"))
require_relative 'bench_helper'

module Posix
extend FFI::Library
Expand Down
5 changes: 2 additions & 3 deletions bench/bench_getuid.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require 'benchmark'
require 'ffi'
require_relative 'bench_helper'

iter = 100000
iter = ITER

module Posix
extend FFI::Library
Expand Down
20 changes: 16 additions & 4 deletions bench/bench_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
require "rubygems"
#require 'ffi/times' if RUBY_PLATFORM =~ /java/
require 'benchmark'
require 'rbconfig'

lib = File.expand_path('../../lib', __FILE__)

cext = "#{lib}/ffi_c.#{RbConfig::CONFIG['DLEXT']}"
unless File.exist?(cext)
abort "#{cext} is not compiled. Compile it with 'rake compile' first."
end

$LOAD_PATH.unshift(lib)
require 'ffi'
ITER = ENV['ITER'] ? ENV['ITER'].to_i : 100000
LIBTEST_PATH = File.expand_path("../../spec/ffi/fixtures/libtest.#{FFI::Platform::LIBSUFFIX}", __FILE__)

require_relative '../spec/ffi/fixtures/compile'

ITER = ENV['ITER'] ? ENV['ITER'].to_i : 100_000

LIBTEST_PATH = TestLibrary::PATH
2 changes: 1 addition & 1 deletion bench/bench_math.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require File.expand_path(File.join(File.dirname(__FILE__), "bench_helper"))
require_relative 'bench_helper'

module FFIMath
extend FFI::Library
Expand Down
9 changes: 3 additions & 6 deletions bench/bench_memptr_alloc.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
require File.expand_path(File.join(File.dirname(__FILE__), "bench_helper"))
require_relative 'bench_helper'

require 'benchmark'
require 'ffi'
iter = ITER

module LibC
Expand Down Expand Up @@ -54,9 +52,8 @@ module LibC
if defined?(RUBY_ENGINE) && RUBY_ENGINE == "jruby"
require 'java'
puts "calling java gc"
10.times {
java.lang.System.gc
10.times {
java.lang.System.gc
sleep 1
}
end

8 changes: 3 additions & 5 deletions bench/bench_memptr_fill.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
require File.expand_path(File.join(File.dirname(__FILE__), "bench_helper"))
require_relative 'bench_helper'

require 'benchmark'
require 'ffi'
iter = ITER

puts "Benchmark MemoryPointer#put_array_of_float performance, #{iter}x"

5.times {
ptr = FFI::MemoryPointer.new(:float, 8, false)
puts Benchmark.measure {
iter.times {
iter.times {
ptr.put_array_of_float(0, [ 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8 ])
}
}
Expand All @@ -19,7 +17,7 @@
puts "Benchmark MemoryPointer.new(:float, 8, false)).put_array_of_float performance, #{iter}x"
5.times {
puts Benchmark.measure {
iter.times {
iter.times {
ptr = FFI::MemoryPointer.new(:float, 8, false)
ptr.put_array_of_float(0, [ 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8 ])
}
Expand Down

0 comments on commit 476b9a8

Please sign in to comment.