class RubyProf::ProfileTask

Define a task library for profiling unit tests with ruby-prof.

All of the options provided by the Rake:TestTask are supported except the loader which is set to ruby-prof. For detailed information please refer to the Rake:TestTask documentation.

ruby-prof specific options include:

output_dir - For each file specified an output
             file with profile information will be
             written to the output directory.
             By default, the output directory is
             called "profile" and is created underneath
             the current working directory.

printer - Specifies the output printer.  Valid values include
          :flat, :graph, :graph_html and :call_tree.

min_percent - Methods that take less than the specified percent
              will not be written out.

Example:

require 'ruby-prof/task'

RubyProf::ProfileTask.new do |t|
  t.test_files = FileList['test/test*.rb']
  t.output_dir = "c:/temp"
  t.printer = :graph
  t.min_percent = 10
end

If rake is invoked with a “TEST=filename” command line option, then the list of test files will be overridden to include only the filename specified on the command line. This provides an easy way to run just one test.

If rake is invoked with a “TESTOPTS=options” command line option, then the given options are passed to the test process after a ‘–’. This allows Test::Unit options to be passed to the test suite.

Examples:

rake profile                           # run tests normally
rake profile TEST=just_one_file.rb     # run just one test file.
rake profile TESTOPTS="-v"             # run in verbose mode
rake profile TESTOPTS="--runner=fox"   # use the fox test runner

Attributes

min_percent[RW]
output_dir[RW]
printer[RW]

Public Class Methods

new(name = :profile) click to toggle source
Calls superclass method
   # File lib/ruby-prof/task.rb
65 def initialize(name = :profile)
66   super(name)
67 end

Public Instance Methods

clean_output_directory() click to toggle source
    # File lib/ruby-prof/task.rb
136 def clean_output_directory
137   if File.exist?(output_directory)
138     files = Dir.glob(output_directory + '/*')
139     FileUtils.rm(files)
140   end
141 end
create_output_directory() click to toggle source
    # File lib/ruby-prof/task.rb
130 def create_output_directory
131   if not File.exist?(output_directory)
132     Dir.mkdir(output_directory)
133   end
134 end
define() click to toggle source

Create the tasks defined by this task lib.

   # File lib/ruby-prof/task.rb
70 def define
71   lib_path = @libs.join(File::PATH_SEPARATOR)
72   desc "Profile" + (@name==:profile ? "" : " for #{@name}")
73 
74   task @name do
75     create_output_directory
76 
77     @ruby_opts.unshift( "-I#{lib_path}" )
78     @ruby_opts.unshift( "-w" ) if @warning
79     @ruby_opts.push("-S ruby-prof")
80     @ruby_opts.push("--printer #{@printer}")
81     @ruby_opts.push("--min_percent #{@min_percent}")
82 
83     file_list.each do |file_path|
84       run_script(file_path)
85     end
86   end
87   self
88 end
output_directory() click to toggle source
    # File lib/ruby-prof/task.rb
126 def output_directory
127   File.expand_path(@output_dir)
128 end
run_script(script_path) click to toggle source

Run script

    # File lib/ruby-prof/task.rb
 91 def run_script(script_path)
 92   run_code = ''
 93   RakeFileUtils.verbose(@verbose) do
 94     file_name = File.basename(script_path, File.extname(script_path))
 95     case @printer
 96       when :flat, :graph, :call_tree
 97         file_name += ".txt"
 98       when :graph_html
 99         file_name += ".html"
100       else
101         file_name += ".txt"
102     end
103 
104     output_file_path = File.join(output_directory, file_name)
105 
106     command_line = @ruby_opts.join(" ") +
107                   " --file=" + output_file_path +
108                   " " + script_path
109 
110     puts "ruby " + command_line
111     # We have to catch the exeption to continue on.  However,
112     # the error message will have been output to STDERR
113     # already by the time we get here so we don't have to
114     # do that again
115     begin
116       ruby command_line
117     rescue => e
118       STDOUT << e << "\n"
119       STDOUT.flush
120     end
121     puts ""
122     puts ""
123   end
124 end