aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/testing/garbage_collection.rb
diff options
context:
space:
mode:
authorJamis Buck <jamis@37signals.com>2011-01-19 10:27:53 -0700
committerJamis Buck <jamis@37signals.com>2011-01-19 10:57:30 -0700
commit16a23a184e8b091392c0b6001a025bee8323ec8e (patch)
tree63919d866b0702d8446a74db9f2b0fb4110a69a7 /activesupport/lib/active_support/testing/garbage_collection.rb
parent79a06225ef0e148eb5da541711f74163b5efb18d (diff)
downloadrails-16a23a184e8b091392c0b6001a025bee8323ec8e.tar.gz
rails-16a23a184e8b091392c0b6001a025bee8323ec8e.tar.bz2
rails-16a23a184e8b091392c0b6001a025bee8323ec8e.zip
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.
Diffstat (limited to 'activesupport/lib/active_support/testing/garbage_collection.rb')
-rw-r--r--activesupport/lib/active_support/testing/garbage_collection.rb24
1 files changed, 24 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/testing/garbage_collection.rb b/activesupport/lib/active_support/testing/garbage_collection.rb
index 7bf9fbafa6..23ac745c3e 100644
--- a/activesupport/lib/active_support/testing/garbage_collection.rb
+++ b/activesupport/lib/active_support/testing/garbage_collection.rb
@@ -3,6 +3,9 @@ module ActiveSupport
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 @@ module ActiveSupport
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