Class | RubyProf::ProfileTask |
In: |
lib/ruby-prof/profiletask.rb
|
Parent: | Rake::TestTask |
Create a profile task. 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, and :graph_html. min_percent - Methods that take less than the specified percent will not be written out.
Example:
require 'ruby-prof/task' ruby-prof::RubyProfTask.new do |t| t.test_files = FileList['test/test*.rb'] t.output_dir = "c:/temp" t.printer = :graph t.min_percent = 10 end
If the task 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 "options=options" command line option, then the given options are passed to ruby-prof.
If rake is invoked with a "ruby-profPATH=path/to/ruby-prof" command line option, then the given ruby-prof executable will be used; otherwise the one in your PATH will be used.
Examples:
rake ruby-prof # profiles all unit tests rake ruby-prof TEST=just_one_file.rb # profiles one unit test rake ruby-prof PATTERN=*.rb # profiles all files
min_percent | [W] | |
output_dir | [W] | |
printer | [W] |
# File lib/ruby-prof/profiletask.rb, line 62 62: def initialize(name=:profile) 63: @name = name 64: @libs = ["lib"] 65: @pattern = nil 66: @options = Array.new 67: @test_files = nil 68: @verbose = false 69: @warning = false 70: @loader = :ruby_prof 71: @ruby_opts = [] 72: @output_dir = File.join(Dir.getwd, "profile") 73: @printer = :graph 74: @min_percent = 0 75: yield self if block_given? 76: @pattern = 'test/test*.rb' if @pattern.nil? && @test_files.nil? 77: define 78: end
# File lib/ruby-prof/profiletask.rb, line 139 139: def create_output_directory 140: if not File.exist?(output_directory) 141: Dir.mkdir(output_directory) 142: end 143: end
Create the tasks defined by this task lib.
# File lib/ruby-prof/profiletask.rb, line 81 81: def define 82: create_output_directory 83: 84: lib_path = @libs.join(File::PATH_SEPARATOR) 85: desc "Profile" + (@name==:profile ? "" : " for #{@name}") 86: 87: task @name do 88: @ruby_opts.unshift( "-I#{lib_path}" ) 89: @ruby_opts.unshift( "-w" ) if @warning 90: @ruby_opts.push("-S ruby-prof") 91: @ruby_opts.push("--printer #{@printer}") 92: @ruby_opts.push("--min_percent #{@min_percent}") 93: 94: file_list.each do |file_path| 95: run_script(file_path) 96: end 97: end 98: self 99: end
# File lib/ruby-prof/profiletask.rb, line 135 135: def output_directory 136: File.expand_path(@output_dir) 137: end
Run script
# File lib/ruby-prof/profiletask.rb, line 102 102: def run_script(script_path) 103: run_code = '' 104: RakeFileUtils.verbose(@verbose) do 105: file_name = File.basename(script_path, File.extname(script_path)) 106: case @printer 107: when :flat, :graph 108: file_name += ".txt" 109: when :graph_html 110: file_name += ".html" 111: else 112: file_name += ".txt" 113: end 114: 115: output_file_path = File.join(output_directory, file_name) 116: 117: command_line = @ruby_opts.join(" ") + 118: " --file=" + output_file_path + 119: " " + script_path 120: 121: puts "ruby " + command_line 122: # We have to catch the exeption to continue on. However, 123: # the error message will have been output to STDERR 124: # already by the time we get here so we don't have to 125: # do that again 126: begin 127: ruby command_line 128: rescue 129: end 130: puts "" 131: puts "" 132: end 133: end