# encoding: utf-8 require 'erb' require 'fileutils' require 'base64' module RubyProf # prints a HTML visualization of the call tree class CallStackPrinter < AbstractPrinter include ERB::Util # Specify print options. # # options - Hash table # :min_percent - Number 0 to 100 that specifes the minimum # %self (the methods self time divided by the # overall total time) that a method must take # for it to be printed out in the report. # Default value is 0. # # :print_file - True or false. Specifies if a method's source # file should be printed. Default value if false. # # :threshold - a float from 0 to 100 that sets the threshold of # results displayed. # Default value is 1.0 # # :title - a String to overide the default "ruby-prof call tree" # title of the report. # # :expansion - a float from 0 to 100 that sets the threshold of # results that are expanded, if the percent_total # exceeds it. # Default value is 10.0 # # :application - a String to overide the name of the application, # as it appears on the report. # # :editor_uri - Specifies editor uri scheme used for opening files # e.g. :atm or :mvim. For OS X default is :txmt. # Use RUBY_PROF_EDITOR_URI environment variable to overide. def print(output = STDOUT, options = {}) @output = output setup_options(options) @editor = editor_uri if @graph_html = options.delete(:graph) @graph_html = "file://" + @graph_html if @graph_html[0]=="/" end print_header @overall_threads_time = @result.threads.inject(0) do |val, thread| val += thread.total_time end @result.threads.each do |thread| @current_thread_id = thread.fiber_id @overall_time = thread.total_time thread_info = String.new("Thread: #{thread.id}") thread_info << ", Fiber: #{thread.fiber_id}" unless thread.id == thread.fiber_id thread_info << " (#{"%4.2f%%" % ((@overall_time/@overall_threads_time)*100)} ~ #{@overall_time})" @output.print "
#{thread_info}
" @output.print "" end print_footer end def print_stack(call_info, parent_time) total_time = call_info.total_time percent_parent = (total_time/parent_time)*100 percent_total = (total_time/@overall_time)*100 return unless percent_total > min_percent color = self.color(percent_total) kids = call_info.children visible = percent_total >= threshold expanded = percent_total >= expansion display = visible ? "block" : "none" @output.print "
  • " if kids.empty? @output.print "" else visible_children = kids.any?{|ci| (ci.total_time/@overall_time)*100 >= threshold} image = visible_children ? (expanded ? "minus" : "plus") : "empty" @output.print "" end @output.printf " %4.2f%% (%4.2f%%) %s %s\n", percent_total, percent_parent, link(call_info), graph_link(call_info) unless kids.empty? if expanded @output.print "