Module: MeMeasure

Extended by:
MeMeasure
Included in:
MeMeasure
Defined in:
lib/memeasure.rb,
lib/memeasure/measure.rb

Constant Summary collapse

VERSION =
'0.0.3'

Instance Method Summary collapse

Instance Method Details

#run(options = {gc: :enable}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/memeasure/measure.rb', line 4

def run(options = {gc: :enable})
  if options[:gc] == :disable
    GC.disable
  elsif options[:gc] == :enable
    GC.start
  end

  memory_before = `ps -o rss= -p #{Process.pid}`.to_i / 1024
  gc_stat_before = GC.stat

  time = Benchmark.measure do
    yield
  end

  gc_start_after = GC.stat
  GC.start if options[:gc] == :enable

  memory_after = `ps -o rss= -p #{Process.pid}`.to_i / 1024

  puts({
    RUBY_VERSION => {
      gc: options[:gc],
      time: time.round(2),
      gc_acount: gc_state_after[:count].to_i - gc_stat_before[:count].to_i,
      memory: "%d MB" % (memory_after - memory_before)
    }
  }.to_json)
end