# encoding: utf-8 require 'erb' require 'fileutils' 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. # def print(output = STDOUT, options = {}) @output = output setup_options(options) 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.top_method.total_time end @result.threads.each do |thread| @current_thread_id = thread.id @overall_time = thread.top_method.total_time @output.print "
Thread: #{thread.id} (#{"%4.2f%%" % ((@overall_time/@overall_threads_time)*100)} ~ #{@overall_time})
" @output.print "" end print_footer copy_image_files 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 "