Skip to content

Instantly share code, notes, and snippets.

@haileys
Created November 21, 2013 01:32
Show Gist options
  • Save haileys/7574564 to your computer and use it in GitHub Desktop.
Save haileys/7574564 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require "./config/basic"
require "rblineprof"
#require "coderay"
req = File.expand_path(ARGV[0] || abort("Usage: lineprof.rb <file>"))
trace = File.expand_path(ARGV[1] || ARGV[0])
for file, (_, *lines) in lineprof(/./) { require(req) }
next unless file == trace
puts file
source = File.read(file)
source << "\n" unless source[-1] == "\n"
if defined?(CodeRay)
highlighted = CodeRay.scan(source, :ruby).terminal
else
highlighted = source
end
highlighted.lines.zip(lines).each do |line, (wall, cpu, calls)|
if calls and calls > 0
printf "% 8.1fms + % 8.1fms (% 8d) | %s", cpu/1000.0, (wall-cpu)/1000.0, calls, line
else
printf " | %s", line
end
end
puts
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment