Permalink
Browse files

RUBY-482 making treatment of the jruby/c extensions configurable and …

…consistent
  • Loading branch information...
1 parent 99e95c9 commit d74c07a2d47adcc8acaeb754c7a7e757e65e9827 @brandonblack brandonblack committed Dec 18, 2012
Showing with 115 additions and 84 deletions.
  1. +5 −9 README.md
  2. +45 −20 lib/bson.rb
  3. +1 −1 lib/bson/types/object_id.rb
  4. +40 −37 tasks/benchmark/exp_series.rb
  5. +9 −6 tasks/benchmark/profile_array.rb
  6. +14 −8 tasks/testing.rake
  7. +1 −1 test/bson/bson_test.rb
  8. +0 −2 test/test_helper.rb
View
@@ -302,26 +302,22 @@ Before running the tests, make sure you install all test dependencies by running
$ gem install bundler; bundle install
-To run all default test suites, just type:
+To run all default test suites (without the BSON extensions) just type:
$ rake test
-If you have the source code, you can run the tests. Skip this test with the C extension if you're running JRuby.
+If you want to run the default test suite using the BSON extensions:
- $ rake test:c
-
-If you want to test the basic Ruby encoder, without the C extension, or if you're running JRuby:
-
- $ rake test:ruby
+ $ rake test:ext
These will run both unit and functional tests. To run these tests alone:
$ rake test:unit
$ rake test:functional
-To run any individual rake tasks with the C extension enabled, just pass C_EXT=true to the task (don't do this with JRuby):
+To run any individual rake tasks with the BSON extension disabled, just pass BSON_EXT_DISABLED=true to the task:
- $ rake test:unit C_EXT=true
+ $ rake test:unit BSON_EXT_DISABLED=true
If you want to test replica set, you can run the following task:
View
65 lib/bson.rb 100755 → 100644
@@ -46,38 +46,63 @@ def self.read_bson_document(io)
bytebuf.rewind
return BSON.deserialize(bytebuf)
end
-end
-if RUBY_PLATFORM =~ /java/
- require 'bson/bson_java'
- module BSON
- BSON_CODER = BSON_JAVA
+ def self.extension?
+ !((ENV.key?('BSON_EXT_DISABLED') && RUBY_PLATFORM =~ /java/) ||
+ (ENV.key?('BSON_EXT_DISABLED') || "\x01\x00\x00\x00".unpack("i")[0] != 1))
end
-else
- begin
- # Need this for running test with and without c ext in Ruby 1.9.
- raise LoadError if ENV['TEST_MODE'] && !ENV['C_EXT']
+end
- # Raise LoadError unless little endian, since the C extensions
- # only work on little-endian architectures.
- raise LoadError unless "\x01\x00\x00\x00".unpack("i").first == 1
+begin
+ # Skips loading extensions if one of the following is true:
+ # 1) JRuby and BSON_EXT_DISABLED is set.
+ # -OR-
+ # 2) Ruby MRI and big endian or BSON_EXT_DISABLED is set.
+ raise LoadError unless BSON.extension?
+ if RUBY_PLATFORM =~ /java/
+ require 'bson/bson_java'
+ module BSON
+ BSON_CODER = BSON_JAVA
+ end
+ else
require 'bson_ext/cbson'
raise LoadError unless defined?(CBson::VERSION)
require 'bson/bson_c'
module BSON
BSON_CODER = BSON_C
end
- rescue LoadError
- require 'bson/bson_ruby'
- module BSON
- BSON_CODER = BSON_RUBY
+ end
+rescue LoadError
+ require 'bson/bson_ruby'
+ module BSON
+ BSON_CODER = BSON_RUBY
+ end
+
+ if RUBY_PLATFORM =~ /java/
+ unless ENV['TEST_MODE']
+ warn <<-NOTICE
+ ** Notice: The BSON extension was not loaded. **
+
+ For optimal performance, use of the BSON extension is recommended. To
+ enable the extension make sure ENV['BSON_EXT_DISABLED'] is not set.
+ NOTICE
end
+ else
unless ENV['TEST_MODE']
- warn "\n**Notice: C extension not loaded. This is required for optimum MongoDB Ruby driver performance."
- warn " You can install the extension as follows:\n gem install bson_ext\n"
- warn " If you continue to receive this message after installing, make sure that the"
- warn " bson_ext gem is in your load path and that the bson_ext and mongo gems are of the same version.\n"
+ warn <<-NOTICE
+ ** Notice: The native BSON extension was not loaded. **
+
+ For optimal performance, use of the BSON extension is recommended.
+
+ To enable the extension make sure ENV['BSON_EXT_DISABLED'] is not set
+ and run the following command:
+
+ gem install bson_ext
+
+ If you continue to receive this message after installing, make sure that
+ the bson_ext gem is in your load path.
+ NOTICE
end
end
end
@@ -173,7 +173,7 @@ def self.machine_id
private
- if RUBY_PLATFORM =~ /java/
+ if RUBY_PLATFORM =~ /java/ && BSON.extension?
@@generator = Java::OrgBsonTypes::ObjectId
@@machine_id = [@@generator.genMachineId].pack("N")[0,3]
@@ -7,31 +7,34 @@
require 'benchmark'
require 'test-unit'
-def set_mode(mode)
+def set_mode(mode.to_sym)
case mode
- when 'c'
+ when :c
ENV.delete('TEST_MODE')
- ENV['C_EXT'] = 'TRUE'
- when 'ruby'
+ ENV.delete('BSON_EXT_DISABLED')
+ when :ruby
+ ENV['TEST_MODE'] = 'TRUE'
+ ENV['BSON_EXT_DISABLED'] = 'TRUE'
+ when :java
ENV['TEST_MODE'] = 'TRUE'
- ENV.delete('C_EXT')
+ ENV.delete('BSON_EXT_DISABLED')
else
- raise 'mode must be c or ruby'
+ raise 'Valid modes include: c, ruby, java.'
end
return mode
end
-$description = 'Exploratory/Experimental/Exponential tests for Ruby-driver performance tuning'
+$description = 'Exploratory/Experimental/Exponential tests for Ruby-driver performance tuning'
$calibration_runtime = 0.5
-$target_runtime = 15.0
-$db_name = 'benchmark'
-$collection_name = 'exp_series'
-$mode = set_mode('c')
-$hostname = `uname -n`[/([^.]*)/,1]
-$osname = `uname -s`.strip
-$tag = `git log -1 --format=oneline`.split[0]
-$date = Time.now.strftime('%Y%m%d-%H%M')
-$file_name_opt = nil
+$target_runtime = 15.0
+$db_name = 'benchmark'
+$collection_name = 'exp_series'
+$mode = set_mode('c')
+$hostname = `uname -n`[/([^.]*)/,1]
+$osname = `uname -s`.strip
+$tag = `git log -1 --format=oneline`.split[0]
+$date = Time.now.strftime('%Y%m%d-%H%M')
+$file_name_opt = nil
options_with_help = [
[ '--help', '-h', GetoptLong::NO_ARGUMENT, '', 'show help' ],
@@ -251,28 +254,28 @@ def power_test(args)
user_ops = (multi_iterations / utime.to_f).round(1)
real_ops = (multi_iterations / rtime.to_f).round(1)
result = {
- 'base' => base,
- 'power' => power,
- 'size' => size,
- 'exp2' => Math.log2(size).to_i,
+ 'base' => base,
+ 'power' => power,
+ 'size' => size,
+ 'exp2' => Math.log2(size).to_i,
'multi_power' => multi_power,
- 'multi_size' => multi_size,
- 'generator' => generator.name.to_s,
- 'operation' => operation.name.to_s,
- 'iterations' => iterations,
- 'user_time' => utime.round(2),
- 'real_time' => rtime.round(2),
- 'user_ops' => user_ops.nan? ? 0 : user_ops,
- 'real_ops' => real_ops.nan? ? 0 : real_ops,
- 'user_usec' => (1000000.0 * utime.to_f / [multi_iterations, 1].max).round(1),
- 'real_usec' => (1000000.0 * rtime.to_f / [multi_iterations, 1].max).round(1),
- 'est_time' => etime.round(2),
- 'mode' => $mode,
- 'hostname' => $hostname,
- 'osname' => $osname,
- 'date' => $date,
- 'tag' => $tag,
- 'status' => status,
+ 'multi_size' => multi_size,
+ 'generator' => generator.name.to_s,
+ 'operation' => operation.name.to_s,
+ 'iterations' => iterations,
+ 'user_time' => utime.round(2),
+ 'real_time' => rtime.round(2),
+ 'user_ops' => user_ops.nan? ? 0 : user_ops,
+ 'real_ops' => real_ops.nan? ? 0 : real_ops,
+ 'user_usec' => (1000000.0 * utime.to_f / [multi_iterations, 1].max).round(1),
+ 'real_usec' => (1000000.0 * rtime.to_f / [multi_iterations, 1].max).round(1),
+ 'est_time' => etime.round(2),
+ 'mode' => $mode,
+ 'hostname' => $hostname,
+ 'osname' => $osname,
+ 'date' => $date,
+ 'tag' => $tag,
+ 'status' => status,
# 'nbench-int' => nbench.int, # thinking
}
STDERR.puts result.to_json #result.inspect
@@ -1,22 +1,25 @@
#!/usr/bin/env ruby
$LOAD_PATH.unshift(File.expand_path("../../lib", __FILE__))
-def set_mongo_driver_mode(mode)
+def set_mode(mode.to_sym)
case mode
when :c
ENV.delete('TEST_MODE')
- ENV['C_EXT'] = 'TRUE'
+ ENV.delete('BSON_EXT_DISABLED')
when :ruby
+ ENV['TEST_MODE'] = 'TRUE'
+ ENV['BSON_EXT_DISABLED'] = 'TRUE'
+ when :java
ENV['TEST_MODE'] = 'TRUE'
- ENV.delete('C_EXT')
+ ENV.delete('BSON_EXT_DISABLED')
else
- raise 'mode must be :c or :ruby'
+ raise 'Valid modes include: c, ruby, java.'
end
- ENV['MONGO_DRIVER_MODE'] = mode.to_s
+ return mode
end
$mode = ARGV[0].to_sym if ARGV[0]
-set_mongo_driver_mode($mode || :c)
+set_mode($mode || :c)
require 'rubygems'
require 'mongo'
View
@@ -10,17 +10,23 @@ namespace :test do
RSpec::Core::RakeTask.new(:spec)
- desc "Run default test suites with the BSON C-extension enabled."
- task :c do
- ENV['C_EXT'] = 'TRUE'
- Rake::Task['compile:cbson'].invoke
- Rake::Task['test:ruby'].invoke
- ENV['C_EXT'] = nil
+ desc "Run default test suites with BSON extensions enabled."
+ task :ext do
+ ENV.delete('BSON_EXT_DISABLED')
+ Rake::Task['compile'].invoke
+ Rake::Task['test:default'].invoke
end
- desc "Runs default test suites"
+ desc "Runs default test suites in pure Ruby."
task :ruby do
- if RUBY_VERSION >= "1.9.0" && RUBY_ENGINE == 'ruby'
+ ENV['BSON_EXT_DISABLED'] = 'TRUE'
+ Rake::Task['test:default'].invoke
+ ENV.delete('BSON_EXT_DISABLED')
+ end
+
+ desc "Runs default test suites"
+ task :default do
+ if RUBY_VERSION >= '1.9.0' && RUBY_ENGINE == 'ruby'
if ENV['COVERAGE']
require 'simplecov'
SimpleCov.start do
View
@@ -349,7 +349,7 @@ def test_dbref
doc2 = @encoder.deserialize(bson)
# Java doesn't deserialize to DBRefs
- if RUBY_PLATFORM =~ /java/
+ if RUBY_PLATFORM =~ /java/ && BSON.extension?
assert_equal 'namespace', doc2['dbref']['$ns']
assert_equal oid, doc2['dbref']['$id']
else
View
@@ -73,8 +73,6 @@ def silently
exit
end
-require 'bson_ext/cbson' if !(RUBY_PLATFORM =~ /java/) && ENV['C_EXT']
-
unless defined? MONGO_TEST_DB
MONGO_TEST_DB = 'ruby-test-db'
end

1 comment on commit d74c07a

Contributor

TylerBrock commented on d74c07a Dec 21, 2012

This is fantastic.

Please sign in to comment.