Skip to content
Browse files

rein in GC during tests by making them run (at most) once per second

this can provide a significant performance boost during testing, by
preventing the GC from running too frequently.
  • Loading branch information...
1 parent b5cf2b4 commit a0c761dc6b3d840852be35af59b143e2016acf9d @jamis jamis committed
Showing with 28 additions and 4 deletions.
  1. +24 −0 activesupport/lib/active_support/testing/garbage_collection.rb
  2. +4 −4 activesupport/test/test_test.rb
View
24 activesupport/lib/active_support/testing/garbage_collection.rb
@@ -3,6 +3,9 @@ module Testing
module GarbageCollection
def self.included(base)
base.teardown :scrub_leftover_instance_variables
+
+ base.setup :begin_gc_deferment
+ base.teardown :reconsider_gc_deferment
end
private
@@ -14,6 +17,27 @@ def scrub_leftover_instance_variables
remove_instance_variable(var)
end
end
+
+ # Minimum interval, in seconds, at which to run GC. Might be less
+ # frequently than this, if a single test takes longer than this to
+ # run.
+ DEFERRED_GC_THRESHOLD = (ENV['DEFERRED_GC_THRESHOLD'] || 1.0).to_f
+
+ @@last_gc_run = Time.now
+
+ def begin_gc_deferment
+ GC.disable if DEFERRED_GC_THRESHOLD > 0
+ end
+
+ def reconsider_gc_deferment
+ if DEFERRED_GC_THRESHOLD > 0 && Time.now - @@last_gc_run >= DEFERRED_GC_THRESHOLD
+ GC.enable
+ GC.start
+ GC.disable
+
+ @@last_gc_run = Time.now
+ end
+ end
end
end
end
View
8 activesupport/test/test_test.rb
@@ -141,9 +141,9 @@ class SetupAndTeardownTest < ActiveSupport::TestCase
teardown :foo, :sentinel, :foo
def test_inherited_setup_callbacks
- assert_equal [:reset_callback_record, :foo], self.class.setup_callback_chain.map(&:method)
+ assert_equal [:begin_gc_deferment, :reset_callback_record, :foo], self.class.setup_callback_chain.map(&:method)
assert_equal [:foo], @called_back
- assert_equal [:scrub_leftover_instance_variables, :foo, :sentinel, :foo], self.class.teardown_callback_chain.map(&:method)
+ assert_equal [:scrub_leftover_instance_variables, :reconsider_gc_deferment, :foo, :sentinel, :foo], self.class.teardown_callback_chain.map(&:method)
end
def setup
@@ -172,9 +172,9 @@ class SubclassSetupAndTeardownTest < SetupAndTeardownTest
teardown :bar
def test_inherited_setup_callbacks
- assert_equal [:reset_callback_record, :foo, :bar], self.class.setup_callback_chain.map(&:method)
+ assert_equal [:begin_gc_deferment, :reset_callback_record, :foo, :bar], self.class.setup_callback_chain.map(&:method)
assert_equal [:foo, :bar], @called_back
- assert_equal [:scrub_leftover_instance_variables, :foo, :sentinel, :foo, :bar], self.class.teardown_callback_chain.map(&:method)
+ assert_equal [:scrub_leftover_instance_variables, :reconsider_gc_deferment, :foo, :sentinel, :foo, :bar], self.class.teardown_callback_chain.map(&:method)
end
protected

0 comments on commit a0c761d

Please sign in to comment.
Something went wrong with that request. Please try again.