print_metrics do
#...your code...#
end
# Or by single method
print_memory_usage do
#...your code...#
end
print_time_spent do
#...your code...#
end
require 'benchmark'
def print_metrics
print_memory_usage do
print_time_spent do
yield
end
end
end
def print_memory_usage
memory_before = `ps -o rss= -p #{Process.pid}`.to_i
yield
memory_after = `ps -o rss= -p #{Process.pid}`.to_i
puts "Memory: #{((memory_after - memory_before) / 1024.0).round(2)} MB"
end
def print_time_spent
time = Benchmark.realtime do
yield
end
puts "Time: #{time.round(2)}"
end