Skip to content

Instantly share code, notes, and snippets.

@zporter
Created January 20, 2011 20:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zporter/788626 to your computer and use it in GitHub Desktop.
Save zporter/788626 to your computer and use it in GitHub Desktop.
Speed up tests with the following gist
#
# Credit goes to jamis
# http://37signals.com/svn/posts/2742-the-road-to-faster-tests
#
# wipe almost all of the instance variables from the object
teardown :scrub_instance_variables
@@reserved_ivars = %w(@loaded_fixtures @test_passed @fixture_cache @method_name @_assertion_wrapped @_result)
def scrub_instance_variables
(instance_variables - @@reserved_ivars).each do |ivar|
instance_variable_set(ivar, nil)
end
end
#
# reduce the frequency at which the Garbage Collector runs
setup :begin_gc_deferment
teardown :reconsider_gc_deferment
DEFERRED_GC_THRESHOLD = (ENV['DEFER_GC'] || 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment