Created
September 16, 2018 15:41
-
-
Save thorvn/fd7b3c7cdc5d1fd370c7f4a532b78e9f to your computer and use it in GitHub Desktop.
measure Ruby performance
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
def measure(no_gc = true, &block) | |
if no_gc | |
GC.disable | |
else | |
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 | |
# puts ObjectSpace.count_objects | |
unless no_gc | |
GC.start(full_mark: true, immediate_sweep: true, immediate_mark: false) | |
end | |
# puts ObjectSpace.count_objects | |
gc_stat_after = GC.stat | |
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