Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Code to process a log that I created using TracePoint
require 'json'
require 'csv'
class Line
attr_reader :thread_no, :line_no, :path, :klass, :method_id, :event, :code, :line
def initialize(row)
@thread_no, @line_no, @path, @klass, @method_id, @event, @code = row
end
def clean_path
path
.gsub(/\/Users\/zetter\/\.rbenv\/versions\/2\.6\.0\/lib\/ruby\/gems\/2\.6\.0\/gems\/[^\/]*\//, '')
.gsub('/Users/zetter/Projects/railstrace/blog/', '')
.gsub('/Users/zetter/.rbenv/versions/2.6.0/lib/ruby/2.6.0/', '')
end
def name
"#{method_prefix}#{method_id}"
end
def clean_klass
@klass.gsub(/^#<Class:/, '').gsub(/>$/, '')
end
def method_prefix
if @klass.start_with?('#<Class:')
'.'
else
'#'
end
end
def key
"#{clean_klass}-#{method_id}".downcase.gsub(/[^a-z0-9_]/, '-').gsub(/-+/, '-').gsub(/-$/, '')
end
def type
if path.include?('/Users/zetter/Projects/railstrace/blog/')
'application'
elsif path.include?('/lib/ruby/gems/2.6.0/gems')
gem_name = path.split('/')[11].split('-').first
"gem/#{gem_name}"
elsif path.include?('/lib/ruby/2.6.0/')
'standard_library'
else
'other'
end
end
end
lines = []
CSV.foreach("tracepoint8.csv") do |row|
lines << Line.new(row)
end
lines = lines.select {|line| line.thread_no == "5" && (line.event == 'call' || line.event == 'return')}
OUTPUT = []
TYPES = []
def process_lines(lines, x_offset = 0, y_offset = 0)
head, *tail = lines
if head.nil?
return x_offset, []
elsif head.event == 'call'
new_x_offset, tail, previous = process_lines(tail, x_offset + 1, y_offset + 1)
OUTPUT << {
key: "#{x_offset}-#{head.key}",
x: x_offset,
width: (new_x_offset - x_offset),
y: y_offset,
path: head.clean_path,
line_start: head.line_no,
name: head.name,
class: head.clean_klass,
type: head.type,
}
TYPES << head.type
process_lines(tail, new_x_offset + 1, y_offset)
elsif head.event == 'return'
return x_offset + 1, tail, head
end
end
process_lines(lines)
puts OUTPUT.to_json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.