Created
March 11, 2017 13:27
-
-
Save scottserok/6d7524dc915ce5b9e1b8dec5602013a3 to your computer and use it in GitHub Desktop.
Ruby benchmark utility module to print time spent, memory used, and gc usage.
This file contains hidden or 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 'benchmark' | |
# Found a couple of useful methods that make benchmarking my scripts easy | |
# Usage: | |
# print_gc_usage do | |
# print_memory_usage do | |
# print_time_spent do | |
# SomeClass.new | |
# end | |
# end | |
# end | |
module BenchmarkUtility | |
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)}s" | |
end | |
def print_gc_usage | |
gc_before = GC.stat | |
yield | |
gc_after = GC.stat | |
puts "GC Before: #{gc_before}" | |
puts "GC After: #{gc_after}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment