# 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 @overall_threads_time = 0.0 @threads_totals = Hash.new @result.threads.each do |thread_id, methods| roots = methods.select{|m| m.root?} thread_total_time = sum(roots.map{|r| self.total_time(r.call_infos)}) @overall_threads_time += thread_total_time @threads_totals[thread_id] = thread_total_time end print_header @result.threads.keys.sort.each do |thread_id| @current_thread_id = thread_id @overall_time = @threads_totals[thread_id] @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 "