Last active
August 29, 2015 14:18
-
-
Save jbodah/a32991626c2715f33b4b to your computer and use it in GitHub Desktop.
measurer
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
| # Measures runtime, memory usage, | |
| # of GC calls | |
| class Measurer | |
| require "json" | |
| require "benchmark" | |
| def self.measure(opts = {}, &block) | |
| use_gc = opts.has_key?(:use_gc) ? opts[:use_gc] : true | |
| if use_gc | |
| # collect memory allocated during library loading | |
| # and our own code before the measurement | |
| GC.start | |
| else | |
| GC.disable | |
| 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 if use_gc | |
| memory_after = `ps -o rss= -p #{Process.pid}`.to_i/1024 | |
| GC.enable | |
| puts({ | |
| RUBY_VERSION => { | |
| gc: use_gc ? 'enabled' : 'disabled', | |
| time: time.round(2), | |
| gc_count: gc_stat_after[:count] - gc_stat_before[:count], memory: "%dM" % (memory_after - memory_before) | |
| } }.to_json) | |
| end | |
| end |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: