Skip to content

Instantly share code, notes, and snippets.

@aschmied
Last active April 17, 2018 16:03
Show Gist options
  • Save aschmied/b8a761a299c8816fe8fcb1791e75e7ec to your computer and use it in GitHub Desktop.
Save aschmied/b8a761a299c8816fe8fcb1791e75e7ec to your computer and use it in GitHub Desktop.
Profile a block in JRuby
# Remember to pass `--profile.api` to JRuby:
# jruby --profile.api prof.rb
require "jruby/profiler"
def main
prof = JRuby::Profiler.profile do
puts(f1)
puts(f2)
end
save_prof(prof)
end
def f1
sleep(1)
"f1 done"
end
def f2
x = 0
t0 = Time.now
while
x = x + 1
t = Time.now - t0
break if t > 1.0
end
"f2 done in #{t}"
end
def save_prof(prof)
f = java.io.File.new("profile.api.html")
pstream = java.io.PrintStream.new(f)
printer = JRuby::Profiler::HtmlProfilePrinter.new(prof)
printer.printHeader(pstream)
printer.printProfile(pstream)
printer.printFooter(pstream)
ensure
pstream.close
end
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment