From c0ad8036f1a6a2dd7c8eb6bc42c2fa2b1e04b0da Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Sun, 29 Mar 2020 14:11:20 +0200 Subject: [PATCH 1/8] Improve error message in bench_getlogin.rb --- bench/bench_getlogin.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bench/bench_getlogin.rb b/bench/bench_getlogin.rb index c5824f4e1..12233afb3 100644 --- a/bench/bench_getlogin.rb +++ b/bench/bench_getlogin.rb @@ -10,7 +10,8 @@ module Posix attach_function :getlogin, [], :string end if Posix.getlogin != Etc.getlogin - raise ArgumentError, "FFI getlogin returned incorrect value" + raise ArgumentError, "FFI getlogin returned incorrect value: " \ + "#{Posix.getlogin.inspect} (FFI) vs #{Etc.getlogin.inspect} (Etc)" end puts "Benchmark FFI getlogin(2) performance, #{ITER}x" From bf2e8afcfadd38300c3f0f55ecf3042e9996f34e Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Sun, 29 Mar 2020 14:25:18 +0200 Subject: [PATCH 2/8] Relax check in bench_getlogin.rb --- bench/bench_getlogin.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/bench/bench_getlogin.rb b/bench/bench_getlogin.rb index 12233afb3..8cb51766c 100644 --- a/bench/bench_getlogin.rb +++ b/bench/bench_getlogin.rb @@ -9,7 +9,10 @@ module Posix ffi_lib FFI::Library::LIBC attach_function :getlogin, [], :string end - if Posix.getlogin != Etc.getlogin + + # getlogin(2) might return NULL, in which case Etc.getlogin uses ENV["USER"]. + # This should not matter for this benchmark, getlogin() is still called. + if (Posix.getlogin || ENV["USER"]) != Etc.getlogin raise ArgumentError, "FFI getlogin returned incorrect value: " \ "#{Posix.getlogin.inspect} (FFI) vs #{Etc.getlogin.inspect} (Etc)" end From dbf3795b7f1d746f44e0c275cf1e660a2ff56c6c Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Sun, 29 Mar 2020 14:06:55 +0200 Subject: [PATCH 3/8] Add GitHub Actions workflow for CI --- .github/workflows/ci.yml | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 .github/workflows/ci.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 000000000..02d933fd0 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,29 @@ +name: CI +on: [push, pull_request] +jobs: + specs: + strategy: + fail-fast: false + matrix: + os: [ ubuntu, macos ] + ruby: [ 2.3, 2.4, 2.5, 2.6, 2.7, ruby-head, truffleruby-head ] + runs-on: ${{ matrix.os }}-latest + steps: + - uses: actions/checkout@v2 + - uses: ruby/setup-ruby@v1 + with: + ruby-version: ${{ matrix.ruby }} + + - run: brew install automake + if: matrix.os == 'macos' + + - run: bundle install + - run: bundle exec rake libffi + - run: bundle exec rake compile + + - run: bundle exec rake test + + - run: bundle exec rake bench:all + if: matrix.ruby != 'truffleruby-head' + env: + ITER: 10 From c8200e3710e1a98d8eab2d035e40e04bd4acaad2 Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Sun, 29 Mar 2020 14:36:14 +0200 Subject: [PATCH 4/8] Workaround issue when installing rubygems-tasks on Ruby < 2.4 * Without this, we see in CI, only on macOS: Fetching gem metadata from https://rubygems.org/.............. Resolving dependencies... Fetching rake 13.0.1 Installing rake 13.0.1 Using bundler 1.17.3 Fetching diff-lcs 1.3 Installing diff-lcs 1.3 Fetching io-console 0.5.6 Installing io-console 0.5.6 with native extensions Gem::InstallError: io-console requires Ruby version >= 2.4.0. An error occurred while installing io-console (0.5.6), and Bundler cannot continue. Make sure that `gem install io-console -v '0.5.6' --source 'https://rubygems.org/'` succeeds before bundling. In Gemfile: rubygems-tasks was resolved to 0.2.5, which depends on irb was resolved to 1.2.3, which depends on reline was resolved to 0.1.3, which depends on io-console --- .github/workflows/ci.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 02d933fd0..fb8bda074 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -17,6 +17,8 @@ jobs: - run: brew install automake if: matrix.os == 'macos' + # Bundler 2 seems to fix installing rubygems-tasks on Ruby < 2.4 + - run: gem install bundler - run: bundle install - run: bundle exec rake libffi - run: bundle exec rake compile From 6cc11cf137e6dcce00b5740e465f6ff3ccb69662 Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Sun, 29 Mar 2020 14:44:11 +0200 Subject: [PATCH 5/8] Test on Windows too --- .github/workflows/ci.yml | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index fb8bda074..de98947a7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -5,9 +5,16 @@ jobs: strategy: fail-fast: false matrix: - os: [ ubuntu, macos ] + os: [ ubuntu, macos, windows ] ruby: [ 2.3, 2.4, 2.5, 2.6, 2.7, ruby-head, truffleruby-head ] + exclude: + - os: windows + ruby: truffleruby-head + - os: windows + ruby: 2.3 # compilation fails runs-on: ${{ matrix.os }}-latest + env: + MAKE: "make" # to not try to use gmake on Windows steps: - uses: actions/checkout@v2 - uses: ruby/setup-ruby@v1 From 08d0111ea3e99d033beaa88042b7f8d22fbad905 Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Sun, 29 Mar 2020 14:58:59 +0200 Subject: [PATCH 6/8] Skip getlogin/gettimeofday/getuid/umask benchmarks on Windows * These functions are not available on Windows. --- bench/bench_getlogin.rb | 2 +- bench/bench_gettimeofday.rb | 2 +- bench/bench_getuid.rb | 2 +- bench/bench_umask.rb | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/bench/bench_getlogin.rb b/bench/bench_getlogin.rb index 8cb51766c..2112cdf40 100644 --- a/bench/bench_getlogin.rb +++ b/bench/bench_getlogin.rb @@ -31,4 +31,4 @@ module Posix iter.times { Etc.getlogin } } } -end +end unless FFI::Platform.windows? diff --git a/bench/bench_gettimeofday.rb b/bench/bench_gettimeofday.rb index 5884d76c3..848fa87d3 100644 --- a/bench/bench_gettimeofday.rb +++ b/bench/bench_gettimeofday.rb @@ -50,4 +50,4 @@ class Timeval < FFI::Struct iter.times { Time.now } } } -end +end unless FFI::Platform.windows? diff --git a/bench/bench_getuid.rb b/bench/bench_getuid.rb index 25aec5398..e82722139 100644 --- a/bench/bench_getuid.rb +++ b/bench/bench_getuid.rb @@ -24,4 +24,4 @@ module Posix iter.times { Process.uid } } } -end +end unless FFI::Platform.windows? diff --git a/bench/bench_umask.rb b/bench/bench_umask.rb index 3692c35c7..1bb547387 100644 --- a/bench/bench_umask.rb +++ b/bench/bench_umask.rb @@ -59,4 +59,4 @@ def self.umask(mask = nil) ITER.times { NativeFile.umask } } } -end +end unless FFI::Platform.windows? From 114ae117623a4e0f5ff55299df27b774349fb724 Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Sun, 29 Mar 2020 15:29:45 +0200 Subject: [PATCH 7/8] Make bench_math.rb work on Windows --- bench/bench_math.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bench/bench_math.rb b/bench/bench_math.rb index bea286840..24f4b7be4 100644 --- a/bench/bench_math.rb +++ b/bench/bench_math.rb @@ -3,7 +3,7 @@ module BenchMath module FFIMath extend FFI::Library - ffi_lib 'm' + ffi_lib FFI::Platform.windows? ? FFI::Library::LIBC : 'm' attach_function :cos, [ :double ], :double attach_function :cosf, [ :float ], :float end From 9fc5800a7f1a20e67d957db5b43ad97a4487a1df Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Sun, 29 Mar 2020 15:37:14 +0200 Subject: [PATCH 8/8] Make sure benchmarks are executed in some deterministic order --- Rakefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Rakefile b/Rakefile index 644eab7b2..91eaba49e 100644 --- a/Rakefile +++ b/Rakefile @@ -45,7 +45,7 @@ task :test => [ :spec ] namespace :bench do ITER = ENV['ITER'] ? ENV['ITER'].to_i : 100000 - bench_files = Dir["bench/bench_*.rb"].reject { |f| f == "bench/bench_helper.rb" } + bench_files = Dir["bench/bench_*.rb"].sort.reject { |f| f == "bench/bench_helper.rb" } bench_files.each do |bench| task File.basename(bench, ".rb")[6..-1] => :compile do sh %{#{Gem.ruby} #{bench} #{ITER}}