Last active
October 10, 2018 10:47
-
-
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).
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
# 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