Module: Memory::RSpec::Profiler

Defined in:
lib/memory/rspec/profiler.rb

Class Method Summary collapse

Class Method Details

.profile(scope) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/memory/rspec/profiler.rb', line 11

def self.profile(scope)
  memory_sampler = nil
  
  scope.before(:all) do |example_group|
    name = example_group.class.description.gsub(/[^\w]+/, "-")
    path = "#{name}.mprof"
    
    skip if File.exist?(path)
    
    memory_sampler = Memory::Sampler.new
    memory_sampler.start
  end
  
  scope.after(:all) do |example_group|
    name = example_group.class.description.gsub(/[^\w]+/, "-")
    path = "#{name}.mprof"
    
    if memory_sampler
      memory_sampler.stop
      
      File.open(path, "w", encoding: Encoding::BINARY) do |io|
        memory_sampler.dump(io)
      end
      
      memory_sampler = nil
    end
  end
  
  scope.after(:suite) do
    memory_sampler = Memory::Sampler.new
    
    Dir.glob("*.mprof") do |path|
      memory_sampler.load(File.read(
        path,
        encoding: Encoding::BINARY,
      ))
    end
    
    memory_sampler.report.print
  end
end