Sha256: e759e29bc3623f8bcdf618270d5338b0df28684fb9a139ad210a9b6817d339c2

Contents?: true

Size: 1.22 KB

Versions: 12

Compression:

Stored size: 1.22 KB

Contents

# frozen_string_literal: true
###
#
# 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
      return unless enabled? && over_deferrment_threshold?

      cycle_garbage_collector

      self.garbage_last_collected_at = Time.now
    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.positive?
    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

12 entries across 12 versions & 1 rubygems

Version Path
rspeckled-0.0.30 lib/rspeckled/plugins/garbage_collection.rb
rspeckled-0.0.29 lib/rspeckled/plugins/garbage_collection.rb
rspeckled-0.0.28 lib/rspeckled/plugins/garbage_collection.rb
rspeckled-0.0.27 lib/rspeckled/plugins/garbage_collection.rb
rspeckled-0.0.26 lib/rspeckled/plugins/garbage_collection.rb
rspeckled-0.0.25 lib/rspeckled/plugins/garbage_collection.rb
rspeckled-0.0.24 lib/rspeckled/plugins/garbage_collection.rb
rspeckled-0.0.23 lib/rspeckled/plugins/garbage_collection.rb
rspeckled-0.0.22 lib/rspeckled/plugins/garbage_collection.rb
rspeckled-0.0.21 lib/rspeckled/plugins/garbage_collection.rb
rspeckled-0.0.20 lib/rspeckled/plugins/garbage_collection.rb
rspeckled-0.0.19 lib/rspeckled/plugins/garbage_collection.rb