Skip to content

Instantly share code, notes, and snippets.

@jmkoni
Created August 10, 2016 17:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmkoni/4e191be8ad53271ada3c481df8d74c22 to your computer and use it in GitHub Desktop.
Save jmkoni/4e191be8ad53271ada3c481df8d74c22 to your computer and use it in GitHub Desktop.
require "json"
require "benchmark"
class Measure
def self.run(&block)
no_gc = (ARGV[0] == "--no-gc")
if no_gc
GC.disable
else
# 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
time = Benchmark.realtime do
yield
end
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),
:memory => "%dM" % (memory_after - memory_before)
}
}.to_json)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment