Sha256: 4ba21313c4e8679278dd082e274651453cb227df3c19296d3ff2a63450fc5c56

Contents?: true

Size: 1.18 KB

Versions: 16

Compression:

Stored size: 1.18 KB

Contents

###
#
# Shamelessly stolen from http://ariejan.net/2011/09/24/rspec-speed-up-by-tweaking-ruby-garbage-collection/
#
module Rspeckled
  class DeferredGarbageCollection
    THRESHOLD = (ENV['DEFER_GC'] || 20.0).to_f

    @garbage_last_collected_at = nil

    def self.start
      cycle_garbage_collector if enabled?
    end

    def self.reconsider
      if enabled? && over_deferrment_threshold?
        cycle_garbage_collector

        self.garbage_last_collected_at = Time.now
      end
    end

    def self.cycle_garbage_collector
      GC.enable
      GC.start
      GC.disable
    end

    def self.over_deferrment_threshold?
      time_since_garbage_last_collected >= THRESHOLD
    end

    def self.enabled?
      THRESHOLD > 0
    end

    def self.time_since_garbage_last_collected
      Time.now - garbage_last_collected_at
    end

    def self.garbage_last_collected_at
      @garbage_last_collected_at || Time.now
    end

    class << self
      attr_writer :garbage_last_collected_at
    end
  end
end

RSpec.configure do |config|
  config.before(:all) do
    Rspeckled::DeferredGarbageCollection.start
  end

  config.after(:all) do
    Rspeckled::DeferredGarbageCollection.reconsider
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
rspeckled-0.0.16 lib/rspeckled/plugins/garbage_collection.rb
rspeckled-0.0.15 lib/rspeckled/plugins/garbage_collection.rb
rspeckled-0.0.14 lib/rspeckled/plugins/garbage_collection.rb
rspeckled-0.0.13 lib/rspeckled/plugins/garbage_collection.rb
rspeckled-0.0.12 lib/rspeckled/plugins/garbage_collection.rb
rspeckled-0.0.11 lib/rspeckled/plugins/garbage_collection.rb
rspeckled-0.0.10 lib/rspeckled/plugins/garbage_collection.rb
rspeckled-0.0.9 lib/rspeckled/plugins/garbage_collection.rb
rspeckled-0.0.8 lib/rspeckled/plugins/garbage_collection.rb
rspeckled-0.0.7 lib/rspeckled/plugins/garbage_collection.rb
rspeckled-0.0.6 lib/rspeckled/plugins/garbage_collection.rb
rspeckled-0.0.5 lib/rspeckled/plugins/garbage_collection.rb
rspeckled-0.0.4 lib/rspeckled/plugins/garbage_collection.rb
rspeckled-0.0.3 lib/rspeckled/support/garbage_collection.rb
rspeckled-0.0.2 lib/rspeckled/support/garbage_collection.rb
rspeckled-0.0.1 lib/rspeckled/support/garbage_collection.rb