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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/waypoints/tracer.rb', line 43
def trace(filepath)
graph = GraphViz.new :G, type: :digraph
graph.node[:fontname] = "Arial, Helvetica, SansSerif"
graph.node[:style] = "rounded, filled"
graph.node[:color] = "#000000"
graph.node[:fillcolor] = "#FFFFFF"
graph.node[:fontcolor] = "#000000"
graph.edge[:fontname] = "Helvetica Neue Thin, Helvetica, Arial, SansSerif"
graph.edge[:fontsize] = 12
stack = []
roots = []
nodes = {}
request = graph.add_node "Request", shape: "circle"
trace = TracePoint.new :call, :return do |event|
next unless trace? event
case event.event
when :return
stack.pop
when :call
caller =
if stack.last
nodes.fetch stack.last do |_key|
nodes[_key] = graph.add_node _key, shape: "record"
end
end
callee =
if key(event)
nodes.fetch key(event) do |_key|
nodes[_key] = graph.add_node _key, shape: "record"
end
end
graph.add_edge caller, callee if caller && callee
roots << callee if stack.empty?
stack << key(event)
end
end
trace.enable
result = yield
trace.disable
roots.each do |root|
graph.add_edge request, root
end
graph.output png: filepath
result
end
|