Last active
May 4, 2018 17:45
-
-
Save frankolson/366fe419f00413d263863e8a01715341 to your computer and use it in GitHub Desktop.
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
module MemoryLogger | |
def log_memory(&block) | |
begin | |
log_start | |
thread = create_memory_log_thread | |
yield | |
ensure | |
Thread.kill(thread) | |
log_finish | |
end | |
end | |
private | |
def start_time | |
@start_time ||= Time.now | |
end | |
def log_start | |
Rails.logger.info "[MemoryLogger] Start time: #{start_time}" | |
end | |
def create_memory_log_thread | |
Thread.new do | |
while true | |
rss = `ps -eo pid,rss | grep #{$$} | awk '{print $2}'`.to_i | |
Rails.logger.info "[MemoryLogger] Resident memory size: #{rss * 0.001}Mb" | |
sleep 5 | |
end | |
end | |
end | |
def log_finish | |
Rails.logger.info "[MemoryLogger] Completed in: #{(Time.now - start_time) / 60} minutes" | |
end | |
end |
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
require 'memory_logger' | |
def foo_bar | |
# do some cool stuff... | |
end | |
log_memory do | |
foo_bar | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment