Class RubyProf::ProfileTask
In: lib/ruby-prof/task.rb
Parent: Rake::TestTask

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

Methods

Attributes

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

Public Class methods

[Source]

    # File lib/ruby-prof/task.rb, line 64
64:     def initialize(name = :profile)
65:       super(name)
66:     end

Public Instance methods

[Source]

     # File lib/ruby-prof/task.rb, line 135
135:     def clean_output_directory
136:       if File.exist?(output_directory)
137:         files = Dir.glob(output_directory + '/*')
138:         FileUtils.rm(files)
139:       end
140:     end

[Source]

     # File lib/ruby-prof/task.rb, line 129
129:     def create_output_directory
130:       if not File.exist?(output_directory)
131:         Dir.mkdir(output_directory)
132:       end
133:     end

Create the tasks defined by this task lib.

[Source]

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

[Source]

     # File lib/ruby-prof/task.rb, line 125
125:     def output_directory
126:       File.expand_path(@output_dir)
127:     end

Run script

[Source]

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

[Validate]