Last active
February 27, 2018 17:40
-
-
Save wisetara/7b0010642e7d057f2e85b24fbc664347 to your computer and use it in GitHub Desktop.
Let's speed up RSpec, yo. This Singleton version totally based on @joevandyk and @triskweline's work
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DeferredGarbageCollection | |
include Singleton | |
# One can set a different time period for the garbage collection, but 15 seemed good. | |
DEFERRED_GC_THRESHOLD = (ENV['DEFER_GC'] || 15.0).to_f | |
def initialize | |
@last_gc_run = Time.zone.now | |
end | |
def self.start | |
instance.start | |
end | |
def start | |
GC.disable if DEFERRED_GC_THRESHOLD > 0 | |
end | |
def self.reconsider | |
instance.reconsider | |
end | |
# Checks if last run time exceeds the value of the time threshold and enables Garbage | |
# Collection for a short cleanup. After that Garbage Collection is deferred again. | |
def reconsider | |
return unless DEFERRED_GC_THRESHOLD > 0 && Time.zone.now - @last_gc_run >= DEFERRED_GC_THRESHOLD | |
GC.enable | |
GC.start | |
GC.disable | |
@last_gc_run = Time.zone.now | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment