Skip to content

Commit d74c07a

Browse files
author
Brandon Black
committed
RUBY-482 making treatment of the jruby/c extensions configurable and consistent
1 parent 99e95c9 commit d74c07a

File tree

8 files changed

+115
-84
lines changed

8 files changed

+115
-84
lines changed

README.md

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -302,26 +302,22 @@ Before running the tests, make sure you install all test dependencies by running
302302

303303
$ gem install bundler; bundle install
304304

305-
To run all default test suites, just type:
305+
To run all default test suites (without the BSON extensions) just type:
306306

307307
$ rake test
308308

309-
If you have the source code, you can run the tests. Skip this test with the C extension if you're running JRuby.
309+
If you want to run the default test suite using the BSON extensions:
310310

311-
$ rake test:c
312-
313-
If you want to test the basic Ruby encoder, without the C extension, or if you're running JRuby:
314-
315-
$ rake test:ruby
311+
$ rake test:ext
316312

317313
These will run both unit and functional tests. To run these tests alone:
318314

319315
$ rake test:unit
320316
$ rake test:functional
321317

322-
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):
318+
To run any individual rake tasks with the BSON extension disabled, just pass BSON_EXT_DISABLED=true to the task:
323319

324-
$ rake test:unit C_EXT=true
320+
$ rake test:unit BSON_EXT_DISABLED=true
325321

326322
If you want to test replica set, you can run the following task:
327323

lib/bson.rb

100755100644
Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -46,38 +46,63 @@ def self.read_bson_document(io)
4646
bytebuf.rewind
4747
return BSON.deserialize(bytebuf)
4848
end
49-
end
5049

51-
if RUBY_PLATFORM =~ /java/
52-
require 'bson/bson_java'
53-
module BSON
54-
BSON_CODER = BSON_JAVA
50+
def self.extension?
51+
!((ENV.key?('BSON_EXT_DISABLED') && RUBY_PLATFORM =~ /java/) ||
52+
(ENV.key?('BSON_EXT_DISABLED') || "\x01\x00\x00\x00".unpack("i")[0] != 1))
5553
end
56-
else
57-
begin
58-
# Need this for running test with and without c ext in Ruby 1.9.
59-
raise LoadError if ENV['TEST_MODE'] && !ENV['C_EXT']
54+
end
6055

61-
# Raise LoadError unless little endian, since the C extensions
62-
# only work on little-endian architectures.
63-
raise LoadError unless "\x01\x00\x00\x00".unpack("i").first == 1
56+
begin
57+
# Skips loading extensions if one of the following is true:
58+
# 1) JRuby and BSON_EXT_DISABLED is set.
59+
# -OR-
60+
# 2) Ruby MRI and big endian or BSON_EXT_DISABLED is set.
61+
raise LoadError unless BSON.extension?
6462

63+
if RUBY_PLATFORM =~ /java/
64+
require 'bson/bson_java'
65+
module BSON
66+
BSON_CODER = BSON_JAVA
67+
end
68+
else
6569
require 'bson_ext/cbson'
6670
raise LoadError unless defined?(CBson::VERSION)
6771
require 'bson/bson_c'
6872
module BSON
6973
BSON_CODER = BSON_C
7074
end
71-
rescue LoadError
72-
require 'bson/bson_ruby'
73-
module BSON
74-
BSON_CODER = BSON_RUBY
75+
end
76+
rescue LoadError
77+
require 'bson/bson_ruby'
78+
module BSON
79+
BSON_CODER = BSON_RUBY
80+
end
81+
82+
if RUBY_PLATFORM =~ /java/
83+
unless ENV['TEST_MODE']
84+
warn <<-NOTICE
85+
** Notice: The BSON extension was not loaded. **
86+
87+
For optimal performance, use of the BSON extension is recommended. To
88+
enable the extension make sure ENV['BSON_EXT_DISABLED'] is not set.
89+
NOTICE
7590
end
91+
else
7692
unless ENV['TEST_MODE']
77-
warn "\n**Notice: C extension not loaded. This is required for optimum MongoDB Ruby driver performance."
78-
warn " You can install the extension as follows:\n gem install bson_ext\n"
79-
warn " If you continue to receive this message after installing, make sure that the"
80-
warn " bson_ext gem is in your load path and that the bson_ext and mongo gems are of the same version.\n"
93+
warn <<-NOTICE
94+
** Notice: The native BSON extension was not loaded. **
95+
96+
For optimal performance, use of the BSON extension is recommended.
97+
98+
To enable the extension make sure ENV['BSON_EXT_DISABLED'] is not set
99+
and run the following command:
100+
101+
gem install bson_ext
102+
103+
If you continue to receive this message after installing, make sure that
104+
the bson_ext gem is in your load path.
105+
NOTICE
81106
end
82107
end
83108
end

lib/bson/types/object_id.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ def self.machine_id
173173

174174
private
175175

176-
if RUBY_PLATFORM =~ /java/
176+
if RUBY_PLATFORM =~ /java/ && BSON.extension?
177177
@@generator = Java::OrgBsonTypes::ObjectId
178178
@@machine_id = [@@generator.genMachineId].pack("N")[0,3]
179179

tasks/benchmark/exp_series.rb

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,34 @@
77
require 'benchmark'
88
require 'test-unit'
99

10-
def set_mode(mode)
10+
def set_mode(mode.to_sym)
1111
case mode
12-
when 'c'
12+
when :c
1313
ENV.delete('TEST_MODE')
14-
ENV['C_EXT'] = 'TRUE'
15-
when 'ruby'
14+
ENV.delete('BSON_EXT_DISABLED')
15+
when :ruby
16+
ENV['TEST_MODE'] = 'TRUE'
17+
ENV['BSON_EXT_DISABLED'] = 'TRUE'
18+
when :java
1619
ENV['TEST_MODE'] = 'TRUE'
17-
ENV.delete('C_EXT')
20+
ENV.delete('BSON_EXT_DISABLED')
1821
else
19-
raise 'mode must be c or ruby'
22+
raise 'Valid modes include: c, ruby, java.'
2023
end
2124
return mode
2225
end
2326

24-
$description = 'Exploratory/Experimental/Exponential tests for Ruby-driver performance tuning'
27+
$description = 'Exploratory/Experimental/Exponential tests for Ruby-driver performance tuning'
2528
$calibration_runtime = 0.5
26-
$target_runtime = 15.0
27-
$db_name = 'benchmark'
28-
$collection_name = 'exp_series'
29-
$mode = set_mode('c')
30-
$hostname = `uname -n`[/([^.]*)/,1]
31-
$osname = `uname -s`.strip
32-
$tag = `git log -1 --format=oneline`.split[0]
33-
$date = Time.now.strftime('%Y%m%d-%H%M')
34-
$file_name_opt = nil
29+
$target_runtime = 15.0
30+
$db_name = 'benchmark'
31+
$collection_name = 'exp_series'
32+
$mode = set_mode('c')
33+
$hostname = `uname -n`[/([^.]*)/,1]
34+
$osname = `uname -s`.strip
35+
$tag = `git log -1 --format=oneline`.split[0]
36+
$date = Time.now.strftime('%Y%m%d-%H%M')
37+
$file_name_opt = nil
3538

3639
options_with_help = [
3740
[ '--help', '-h', GetoptLong::NO_ARGUMENT, '', 'show help' ],
@@ -251,28 +254,28 @@ def power_test(args)
251254
user_ops = (multi_iterations / utime.to_f).round(1)
252255
real_ops = (multi_iterations / rtime.to_f).round(1)
253256
result = {
254-
'base' => base,
255-
'power' => power,
256-
'size' => size,
257-
'exp2' => Math.log2(size).to_i,
257+
'base' => base,
258+
'power' => power,
259+
'size' => size,
260+
'exp2' => Math.log2(size).to_i,
258261
'multi_power' => multi_power,
259-
'multi_size' => multi_size,
260-
'generator' => generator.name.to_s,
261-
'operation' => operation.name.to_s,
262-
'iterations' => iterations,
263-
'user_time' => utime.round(2),
264-
'real_time' => rtime.round(2),
265-
'user_ops' => user_ops.nan? ? 0 : user_ops,
266-
'real_ops' => real_ops.nan? ? 0 : real_ops,
267-
'user_usec' => (1000000.0 * utime.to_f / [multi_iterations, 1].max).round(1),
268-
'real_usec' => (1000000.0 * rtime.to_f / [multi_iterations, 1].max).round(1),
269-
'est_time' => etime.round(2),
270-
'mode' => $mode,
271-
'hostname' => $hostname,
272-
'osname' => $osname,
273-
'date' => $date,
274-
'tag' => $tag,
275-
'status' => status,
262+
'multi_size' => multi_size,
263+
'generator' => generator.name.to_s,
264+
'operation' => operation.name.to_s,
265+
'iterations' => iterations,
266+
'user_time' => utime.round(2),
267+
'real_time' => rtime.round(2),
268+
'user_ops' => user_ops.nan? ? 0 : user_ops,
269+
'real_ops' => real_ops.nan? ? 0 : real_ops,
270+
'user_usec' => (1000000.0 * utime.to_f / [multi_iterations, 1].max).round(1),
271+
'real_usec' => (1000000.0 * rtime.to_f / [multi_iterations, 1].max).round(1),
272+
'est_time' => etime.round(2),
273+
'mode' => $mode,
274+
'hostname' => $hostname,
275+
'osname' => $osname,
276+
'date' => $date,
277+
'tag' => $tag,
278+
'status' => status,
276279
# 'nbench-int' => nbench.int, # thinking
277280
}
278281
STDERR.puts result.to_json #result.inspect

tasks/benchmark/profile_array.rb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
#!/usr/bin/env ruby
22
$LOAD_PATH.unshift(File.expand_path("../../lib", __FILE__))
33

4-
def set_mongo_driver_mode(mode)
4+
def set_mode(mode.to_sym)
55
case mode
66
when :c
77
ENV.delete('TEST_MODE')
8-
ENV['C_EXT'] = 'TRUE'
8+
ENV.delete('BSON_EXT_DISABLED')
99
when :ruby
10+
ENV['TEST_MODE'] = 'TRUE'
11+
ENV['BSON_EXT_DISABLED'] = 'TRUE'
12+
when :java
1013
ENV['TEST_MODE'] = 'TRUE'
11-
ENV.delete('C_EXT')
14+
ENV.delete('BSON_EXT_DISABLED')
1215
else
13-
raise 'mode must be :c or :ruby'
16+
raise 'Valid modes include: c, ruby, java.'
1417
end
15-
ENV['MONGO_DRIVER_MODE'] = mode.to_s
18+
return mode
1619
end
1720

1821
$mode = ARGV[0].to_sym if ARGV[0]
19-
set_mongo_driver_mode($mode || :c)
22+
set_mode($mode || :c)
2023

2124
require 'rubygems'
2225
require 'mongo'

tasks/testing.rake

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,23 @@ namespace :test do
1010

1111
RSpec::Core::RakeTask.new(:spec)
1212

13-
desc "Run default test suites with the BSON C-extension enabled."
14-
task :c do
15-
ENV['C_EXT'] = 'TRUE'
16-
Rake::Task['compile:cbson'].invoke
17-
Rake::Task['test:ruby'].invoke
18-
ENV['C_EXT'] = nil
13+
desc "Run default test suites with BSON extensions enabled."
14+
task :ext do
15+
ENV.delete('BSON_EXT_DISABLED')
16+
Rake::Task['compile'].invoke
17+
Rake::Task['test:default'].invoke
1918
end
2019

21-
desc "Runs default test suites"
20+
desc "Runs default test suites in pure Ruby."
2221
task :ruby do
23-
if RUBY_VERSION >= "1.9.0" && RUBY_ENGINE == 'ruby'
22+
ENV['BSON_EXT_DISABLED'] = 'TRUE'
23+
Rake::Task['test:default'].invoke
24+
ENV.delete('BSON_EXT_DISABLED')
25+
end
26+
27+
desc "Runs default test suites"
28+
task :default do
29+
if RUBY_VERSION >= '1.9.0' && RUBY_ENGINE == 'ruby'
2430
if ENV['COVERAGE']
2531
require 'simplecov'
2632
SimpleCov.start do

test/bson/bson_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ def test_dbref
349349
doc2 = @encoder.deserialize(bson)
350350

351351
# Java doesn't deserialize to DBRefs
352-
if RUBY_PLATFORM =~ /java/
352+
if RUBY_PLATFORM =~ /java/ && BSON.extension?
353353
assert_equal 'namespace', doc2['dbref']['$ns']
354354
assert_equal oid, doc2['dbref']['$id']
355355
else

test/test_helper.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,6 @@ def silently
7373
exit
7474
end
7575

76-
require 'bson_ext/cbson' if !(RUBY_PLATFORM =~ /java/) && ENV['C_EXT']
77-
7876
unless defined? MONGO_TEST_DB
7977
MONGO_TEST_DB = 'ruby-test-db'
8078
end

0 commit comments

Comments
 (0)