Posted over 8 years ago. Visible to the public.

Speed up RSpec by deferring garbage collection

Update: This trick probably isn't very useful anymore in Ruby 2.x. The Ruby GC has improved a lot over the years.


Joe Van Dyk discovered that running the Ruby garbage collector only every X seconds can speed up your tests. I found that deferring garbage collection would speed up my RSpec examples by about 15%, but it probably depends on the nature of your tests. I also tried applying it to Cucumber features, but found no performance improvements.

Since Joe is using Test::Unit in his example, here is how to apply the same trick to RSpec.

First, copy this class to spec/support/deferred_garbage_collection.rb:

Copy
class DeferredGarbageCollection DEFERRED_GC_THRESHOLD = (ENV['DEFER_GC'] || 10.0).to_f @@last_gc_run = Time.now def self.start GC.disable if DEFERRED_GC_THRESHOLD > 0 end def self.reconsider 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

Then, in your spec_helper.rb, configure RSpec like seen below. Note that you might already have code hooked up to before(:all) and after(:all), so merge it together:

Copy
Spec::Runner.configure do |config| config.before(:all) do DeferredGarbageCollection.start end config.after(:all) do DeferredGarbageCollection.reconsider end end

You can now enjoy faster specs.

By refactoring problematic code and creating automated tests, makandra can vastly improve the maintainability of your Rails application.

Owner of this card:

Avatar
Henning Koch
Last edit:
over 4 years ago
by Henning Koch
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Henning Koch to makandra dev
This website uses cookies to improve usability and analyze traffic.
Accept or learn more