Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

[FEATURE] Pull in any Java or JRuby environment variables that are already set #102

Merged
merged 5 commits into from Oct 9, 2012
View
@@ -98,6 +98,37 @@ make assertions about coloured output. Still, there might be cases where you wan
scenario with `@ansi`. Alternatively you can add your own Before
hook that sets `@aruba_keep_ansi = true`.
+### JRuby Tips
+
+Improve startup time by disabling JIT and forcing client JVM mode. This can be accomplished by adding
+
+ require 'aruba/java'
+
+or setting a hook like this example:
+
+```
+ Aruba.configure do |config|
+ config.before_cmd do |cmd|
+ set_env('JRUBY_OPTS', "-X-C #{ENV['JRUBY_OPTS']}") # disable JIT since these processes are so short lived
+ set_env('JAVA_OPTS', "-d32 #{ENV['JAVA_OPTS']}") # force jRuby to use client JVM for faster startup times
+ end
+ end if RUBY_PLATFORM == 'java'
+```
+
+*Note* - no conflict resolution on the JAVA/JRuby environment options is
+done; only merging. For more complex settings please manually set the
+environment variables in the hook or externally.
+
+A larger process timeout for java may be needed
+
+```
+ Before do
+ @aruba_timeout_seconds = RUBY_PLATFORM == 'java' ? 60 : 10
+ end
+```
+
+Refer to http://blog.headius.com/2010/03/jruby-startup-time-tips.html for other tips on startup time.
+
## Reporting
*Important* - you need [Pygments](http://pygments.org/) installed to use this feature.
View
@@ -3,7 +3,6 @@
require 'rspec/expectations'
require 'aruba/process'
require 'aruba/config'
-require 'aruba/jruby' if RUBY_PLATFORM == 'java'
module Aruba
module Api
View
@@ -1,7 +1,7 @@
Aruba.configure do |config|
config.before_cmd do
# ideas taken from: http://blog.headius.com/2010/03/jruby-startup-time-tips.html
- set_env('JRUBY_OPTS', '-X-C') # disable JIT since these processes are so short lived
- set_env('JAVA_OPTS', '-d32') # force jRuby to use client JVM for faster startup times
+ set_env('JRUBY_OPTS', "-X-C #{ENV['JRUBY_OPTS']}") # disable JIT since these processes are so short lived
+ set_env('JAVA_OPTS', "-d32 #{ENV['JAVA_OPTS']}") # force jRuby to use client JVM for faster startup times
end
-end
+end if RUBY_PLATFORM == 'java'
View
@@ -0,0 +1,33 @@
+require 'spec_helper'
+require 'aruba/config'
+require 'aruba/api'
+include Aruba::Api
+
+describe "Aruba JRuby Startup Helper" do
+ before(:all) do
+ @fake_env = ENV.clone
+ end
+ before(:each) do
+ Aruba.config = Aruba::Config.new
+ @fake_env['JRUBY_OPTS'] = "--1.9"
+ @fake_env['JAVA_OPTS'] = "-Xdebug"
+ end
+
+ it 'configuration does not load when RUBY_PLATFORM is not java' do
+ with_constants :ENV => @fake_env, :RUBY_PLATFORM => 'x86_64-chocolate' do
+ load 'aruba/jruby.rb'
+ Aruba.config.hooks.execute :before_cmd, self
+ ENV['JRUBY_OPTS'].should == "--1.9"
+ ENV['JAVA_OPTS'].should == "-Xdebug"
+ end
+ end
+
+ it 'configuration loads for java and merges existing environment variables' do
+ with_constants :ENV => @fake_env, :RUBY_PLATFORM => 'java' do
+ load 'aruba/jruby.rb'
+ Aruba.config.hooks.execute :before_cmd, self
+ ENV['JRUBY_OPTS'].should == "-X-C --1.9"
+ ENV['JAVA_OPTS'].should == "-d32 -Xdebug"
+ end
+ end
+end
View
@@ -6,3 +6,35 @@
config.run_all_when_everything_filtered = true
config.treat_symbols_as_metadata_keys_with_true_values = true
end
+
+# http://digitaldumptruck.jotabout.com/?p=551
+def with_constants(constants, &block)
+ saved_constants = {}
+ constants.each do |constant, val|
+ saved_constants[ constant ] = Object.const_get( constant )
+ Kernel::silence_warnings { Object.const_set( constant, val ) }
+ end
+
+ begin
+ block.call
+ ensure
+ constants.each do |constant, val|
+ Kernel::silence_warnings { Object.const_set( constant, saved_constants[ constant ] ) }
+ end
+ end
+end
+
+# http://mislav.uniqpath.com/2011/06/ruby-verbose-mode/
+# these methods are already present in Active Support
+module Kernel
+ def silence_warnings
+ with_warnings(nil) { yield }
+ end
+
+ def with_warnings(flag)
+ old_verbose, $VERBOSE = $VERBOSE, flag
+ yield
+ ensure
+ $VERBOSE = old_verbose
+ end
+end unless Kernel.respond_to? :silence_warnings