Module: Memory::Profiler

Defined in:
lib/memory/profiler.rb,
lib/memory/profiler/version.rb

Defined Under Namespace

Classes: Wrapper

Constant Summary collapse

PROFILE_CALL_PROC =
TracePoint.new(:call, :c_call, :b_call) do |tp|
  data = ObjectSpace.count_objects
  @@stack.push(data)
end
PROFILE_RETURN_PROC =
TracePoint.new(:return, :c_return, :b_return) do |tp|
  previous_data = @@stack.pop

  if previous_data
    data = ObjectSpace.count_objects
    key = Wrapper.new(tp.defined_class, tp.method_id)
    @@record[key] ||= [
      0, # number of object this method use
      0, # number of time this method is called
    ]
    @@record[key][0] += data[:T_OBJECT] - previous_data[:T_OBJECT]
    @@record[key][1] += 1
  end
end
VERSION =
"0.0.1"
@@stack =
nil
@@record =
nil

Class Method Summary collapse

Class Method Details

.end_profilingObject



51
52
53
54
# File 'lib/memory/profiler.rb', line 51

def end_profiling
  PROFILE_CALL_PROC.disable
  PROFILE_RETURN_PROC.disable
end


56
57
58
59
60
# File 'lib/memory/profiler.rb', line 56

def print_profiling
  @@record.each do |key, value|
    puts "#{key} #{value}"
  end
end

.start_profilingObject



44
45
46
47
48
49
# File 'lib/memory/profiler.rb', line 44

def start_profiling
  @@stack = []
  @@record = {}
  PROFILE_CALL_PROC.enable
  PROFILE_RETURN_PROC.enable
end