Class: Beasure

Inherits:
Object
  • Object
show all
Defined in:
lib/beasure.rb

Class Method Summary collapse

Class Method Details

.perform(&block) ⇒ Object



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/beasure.rb', line 5

def self.perform(&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
  gc_stat_before = GC.stat
  time = Benchmark.realtime do
    yield
  end
  GC.start(full_mark: true, immediate_sweep: true, immediate_mark: false) unless no_gc
  gc_stat_after = GC.stat
  memory_after = `ps -o rss= -p #{Process.pid}`.to_i / 1024

  puts("*******************************************************")
  puts("Ruby Version: #{RUBY_VERSION}")
  puts("Execution Time: #{time.round(2)}")
  puts("Garbage Collection Infos:")
  puts("  Usage: #{no_gc ? 'disabled' : 'enabled'}")
  puts("  Counter: #{gc_stat_after[:count] - gc_stat_before[:count]}")
  puts("  Memory: #{'%d MB' % (memory_after - memory_before)}")
  puts("*******************************************************")
end