Class: SamplingProfiler::Sampling

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

Instance Method Summary collapse

Constructor Details

#initialize(output_handler) ⇒ Sampling

Returns a new instance of Sampling.



5
6
7
8
9
10
11
12
# File 'lib/sampling_prof/internal.rb', line 5

def initialize(output_handler)
  @samples = Hash.new{|h,k| h[k] = [0, 0] }
  @call_graph = Hash.new{|h,k| h[k] = 0}
  @nodes = {}
  @start_at = Time.now
  @output_handler = output_handler
  @stop = false
end

Instance Method Details

#call_element(loc) ⇒ Object



73
74
75
# File 'lib/sampling_prof/internal.rb', line 73

def call_element(loc)
  [loc.path, loc.lineno, loc.label].join(":")
end

#node_id(loc) ⇒ Object



69
70
71
# File 'lib/sampling_prof/internal.rb', line 69

def node_id(loc)
  @nodes[call_element(loc)] ||= @nodes.size
end

#outputObject



30
31
32
33
34
# File 'lib/sampling_prof/internal.rb', line 30

def output
  if sampling_data?
    @output_handler.call(result)
  end
end

#process(thread) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/sampling_prof/internal.rb', line 44

def process(thread)
  locations = thread.backtrace_locations
  from = -1
  paths = []
  calls = []
  top_index = locations.size - 1
  locations.reverse.each_with_index do |loc, i|
    node_id = node_id(loc)
    if i == top_index
      @samples[node_id][0] += 1
    end

    path = [from, node_id]
    if !paths.include?(path)
      paths << path
      @call_graph[path] += 1
    end
    if !calls.include?(node_id)
      calls << node_id
      @samples[node_id][1] += 1
    end
    from = node_id
  end
end

#resultObject



36
37
38
39
40
41
42
# File 'lib/sampling_prof/internal.rb', line 36

def result
  ret = [runtime * 1000]
  ret << @nodes.map {|node| node.join(',')}.join("\n")
  ret << @samples.map {|count| count.flatten.join(',')}.join("\n")
  ret << @call_graph.map {|v| v.flatten.join(',')}.join("\n")
  "#{ret.join("\n\n")}\n"
end

#runtimeObject



22
23
24
# File 'lib/sampling_prof/internal.rb', line 22

def runtime
  Time.now - @start_at
end

#sampling_data?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/sampling_prof/internal.rb', line 26

def sampling_data?
  !@nodes.empty?
end

#stopObject



14
15
16
# File 'lib/sampling_prof/internal.rb', line 14

def stop
  @stop = true
end

#stopped?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/sampling_prof/internal.rb', line 18

def stopped?
  @stop
end