Skip to content

Instantly share code, notes, and snippets.

@marckohlbrugge
Last active October 10, 2018 10: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 marckohlbrugge/18a022713c2857f3d14dc14cb82e25af to your computer and use it in GitHub Desktop.
Save marckohlbrugge/18a022713c2857f3d14dc14cb82e25af to your computer and use it in GitHub Desktop.
Calculates estimated time it takes for a block to return zero. Useful when running operations in parallel (e.g. migrations).
# Calculates estimated time it takes for a block to return zero. Useful when
# running operations in parallel (e.g. migrations).
#
# Usage:
# Run in IRB/Rake/whatever: add_slugs_to_each_post # example of long-running operations
# Run in parallel IRB session: countdown { Post.where(slug: nil).count }
#
# sleep_time parameter is optional. The higher the value the more accurate the
# results, but the longer the method takes to run.
def countdown(sleep_time = 10)
first_count = yield
sleep sleep_time
second_count = yield
operations_per_second = (second_count - first_count) / sleep_time
p "#{operations_per_second} operations per second"
operations_remaining = second_count
p "#{operations_remaining} operations remaining"
seconds_remaining = operations_remaining / operations_per_second
p "#{seconds_remaining / 60} minutes remaining"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment