Module: Memory::RSpec::Profiler

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

Class Method Summary collapse

Class Method Details

.profile(scope) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/memory/rspec/profiler.rb', line 28

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