Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
require "json"
require "benchmark"
def measure(no_gc = false, &block)
if no_gc
GC.disable
else
GC.enable
# collect memory allocated during library loading
# and our own code before the measurement
GC.start
end
memory_before = `ps -o rss= -p #{Process.pid}`.to_i/1024
gc_stat_before = GC.stat
time = Benchmark.realtime do
yield
end
gc_stat_after = GC.stat
GC.start unless no_gc
memory_after = `ps -o rss= -p #{Process.pid}`.to_i/1024
puts({
RUBY_VERSION => {
gc: no_gc ? 'disabled' : 'enabled',
time: time.round(2),
gc_count: gc_stat_after[:count] - gc_stat_before[:count],
memory: "%d MB" % (memory_after - memory_before)
}
}.to_json)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.