ruby-prof 1.7.2 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/{CHANGES → CHANGELOG.md} +112 -178
- data/README.md +5 -5
- data/bin/ruby-prof +1 -4
- data/docs/advanced-usage.md +132 -0
- data/docs/alternatives.md +98 -0
- data/docs/architecture.md +122 -0
- data/docs/best-practices.md +27 -0
- data/docs/getting-started.md +130 -0
- data/docs/history.md +11 -0
- data/docs/index.md +45 -0
- data/docs/profiling-rails.md +64 -0
- data/docs/public/examples/example.rb +33 -0
- data/docs/public/examples/generate_reports.rb +92 -0
- data/docs/public/examples/reports/call_info.txt +27 -0
- data/docs/public/examples/reports/call_stack.html +835 -0
- data/docs/public/examples/reports/callgrind.out +150 -0
- data/docs/public/examples/reports/flame_graph.html +408 -0
- data/docs/public/examples/reports/flat.txt +45 -0
- data/docs/public/examples/reports/graph.dot +129 -0
- data/docs/public/examples/reports/graph.html +1319 -0
- data/docs/public/examples/reports/graph.txt +100 -0
- data/docs/public/examples/reports/graphviz_viewer.html +1 -0
- data/docs/public/images/call_stack.png +0 -0
- data/docs/public/images/class_diagram.png +0 -0
- data/docs/public/images/dot_printer.png +0 -0
- data/docs/public/images/flame_graph.png +0 -0
- data/docs/public/images/flat.png +0 -0
- data/docs/public/images/graph.png +0 -0
- data/docs/public/images/graph_html.png +0 -0
- data/docs/public/images/ruby-prof-logo.svg +1 -0
- data/docs/reports.md +150 -0
- data/docs/stylesheets/extra.css +80 -0
- data/ext/ruby_prof/rp_allocation.c +0 -15
- data/ext/ruby_prof/rp_allocation.h +29 -33
- data/ext/ruby_prof/rp_call_tree.c +3 -0
- data/ext/ruby_prof/rp_call_tree.h +1 -4
- data/ext/ruby_prof/rp_call_trees.h +1 -4
- data/ext/ruby_prof/rp_measurement.c +0 -5
- data/ext/ruby_prof/rp_measurement.h +49 -53
- data/ext/ruby_prof/rp_method.c +3 -0
- data/ext/ruby_prof/rp_method.h +1 -4
- data/ext/ruby_prof/rp_profile.c +1 -1
- data/ext/ruby_prof/rp_profile.h +1 -5
- data/ext/ruby_prof/rp_stack.h +50 -53
- data/ext/ruby_prof/rp_thread.h +1 -4
- data/ext/ruby_prof/ruby_prof.h +1 -4
- data/ext/ruby_prof/vc/ruby_prof.vcxproj +7 -8
- data/lib/ruby-prof/assets/call_stack_printer.html.erb +746 -711
- data/lib/ruby-prof/assets/flame_graph_printer.html.erb +412 -0
- data/lib/ruby-prof/assets/graph_printer.html.erb +355 -355
- data/lib/ruby-prof/call_tree.rb +57 -57
- data/lib/ruby-prof/call_tree_visitor.rb +36 -36
- data/lib/ruby-prof/measurement.rb +17 -17
- data/lib/ruby-prof/printers/abstract_printer.rb +19 -33
- data/lib/ruby-prof/printers/call_info_printer.rb +53 -53
- data/lib/ruby-prof/printers/call_stack_printer.rb +168 -180
- data/lib/ruby-prof/printers/call_tree_printer.rb +132 -145
- data/lib/ruby-prof/printers/dot_printer.rb +177 -132
- data/lib/ruby-prof/printers/flame_graph_printer.rb +79 -0
- data/lib/ruby-prof/printers/flat_printer.rb +52 -52
- data/lib/ruby-prof/printers/graph_html_printer.rb +62 -63
- data/lib/ruby-prof/printers/graph_printer.rb +112 -113
- data/lib/ruby-prof/printers/multi_printer.rb +134 -127
- data/lib/ruby-prof/profile.rb +13 -0
- data/lib/ruby-prof/rack.rb +114 -105
- data/lib/ruby-prof/task.rb +147 -147
- data/lib/ruby-prof/thread.rb +20 -20
- data/lib/ruby-prof/version.rb +1 -1
- data/lib/ruby-prof.rb +50 -52
- data/lib/unprof.rb +10 -10
- data/ruby-prof.gemspec +5 -5
- data/test/abstract_printer_test.rb +25 -27
- data/test/alias_test.rb +203 -117
- data/test/call_tree_builder.rb +126 -126
- data/test/call_tree_visitor_test.rb +27 -27
- data/test/call_trees_test.rb +66 -66
- data/test/duplicate_names_test.rb +32 -32
- data/test/dynamic_method_test.rb +50 -50
- data/test/exceptions_test.rb +24 -24
- data/test/exclude_threads_test.rb +48 -48
- data/test/fiber_test.rb +72 -72
- data/test/inverse_call_tree_test.rb +174 -174
- data/test/line_number_test.rb +138 -1
- data/test/marshal_test.rb +144 -145
- data/test/measure_allocations.rb +26 -26
- data/test/measure_allocations_test.rb +340 -1
- data/test/measure_process_time_test.rb +3098 -3142
- data/test/measure_times.rb +56 -56
- data/test/measure_wall_time_test.rb +511 -372
- data/test/measurement_test.rb +82 -82
- data/test/merge_test.rb +48 -48
- data/test/multi_printer_test.rb +52 -66
- data/test/no_method_class_test.rb +15 -15
- data/test/pause_resume_test.rb +171 -171
- data/test/prime.rb +54 -54
- data/test/prime_script.rb +5 -5
- data/test/printer_call_stack_test.rb +28 -27
- data/test/printer_call_tree_test.rb +30 -30
- data/test/printer_flame_graph_test.rb +82 -0
- data/test/printer_flat_test.rb +99 -99
- data/test/printer_graph_html_test.rb +62 -59
- data/test/printer_graph_test.rb +42 -40
- data/test/printers_test.rb +28 -44
- data/test/printing_recursive_graph_test.rb +81 -81
- data/test/profile_test.rb +101 -101
- data/test/rack_test.rb +103 -93
- data/test/recursive_test.rb +139 -139
- data/test/scheduler.rb +4 -0
- data/test/singleton_test.rb +39 -38
- data/test/stack_printer_test.rb +61 -61
- data/test/start_stop_test.rb +106 -106
- data/test/test_helper.rb +4 -0
- data/test/thread_test.rb +29 -29
- data/test/unique_call_path_test.rb +123 -123
- data/test/yarv_test.rb +56 -56
- metadata +53 -11
- data/ext/ruby_prof/rp_measure_memory.c +0 -46
- data/lib/ruby-prof/compatibility.rb +0 -113
- data/test/compatibility_test.rb +0 -49
- data/test/measure_memory_test.rb +0 -1193
data/lib/ruby-prof/rack.rb
CHANGED
|
@@ -1,105 +1,114 @@
|
|
|
1
|
-
# encoding: utf-8
|
|
2
|
-
require 'tmpdir'
|
|
3
|
-
|
|
4
|
-
module Rack
|
|
5
|
-
class RubyProf
|
|
6
|
-
def initialize(app,
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
end
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
result
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
require 'tmpdir'
|
|
3
|
+
|
|
4
|
+
module Rack
|
|
5
|
+
class RubyProf
|
|
6
|
+
def initialize(app, path: Dir.tmpdir, printers: nil, skip_paths: nil,
|
|
7
|
+
only_paths: nil, merge_fibers: false,
|
|
8
|
+
measure_mode: ::RubyProf::WALL_TIME, track_allocations: false,
|
|
9
|
+
exclude_common: false, ignore_existing_threads: false,
|
|
10
|
+
request_thread_only: false, min_percent: 1,
|
|
11
|
+
sort_method: :total_time)
|
|
12
|
+
@app = app
|
|
13
|
+
|
|
14
|
+
@tmpdir = path.to_s
|
|
15
|
+
FileUtils.mkdir_p(@tmpdir)
|
|
16
|
+
|
|
17
|
+
@printer_klasses = printers || {::RubyProf::FlatPrinter => 'flat.txt',
|
|
18
|
+
::RubyProf::GraphPrinter => 'graph.txt',
|
|
19
|
+
::RubyProf::GraphHtmlPrinter => 'graph.html',
|
|
20
|
+
::RubyProf::CallStackPrinter => 'call_stack.html'}
|
|
21
|
+
|
|
22
|
+
@skip_paths = skip_paths || [%r{^/assets}, %r{\.(css|js|png|jpeg|jpg|gif)$}]
|
|
23
|
+
@only_paths = only_paths
|
|
24
|
+
@merge_fibers = merge_fibers
|
|
25
|
+
@measure_mode = measure_mode
|
|
26
|
+
@track_allocations = track_allocations
|
|
27
|
+
@exclude_common = exclude_common
|
|
28
|
+
@ignore_existing_threads = ignore_existing_threads
|
|
29
|
+
@request_thread_only = request_thread_only
|
|
30
|
+
@min_percent = min_percent
|
|
31
|
+
@sort_method = sort_method
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def call(env)
|
|
35
|
+
request = Rack::Request.new(env)
|
|
36
|
+
|
|
37
|
+
if should_profile?(request.path)
|
|
38
|
+
begin
|
|
39
|
+
result = nil
|
|
40
|
+
profile = ::RubyProf::Profile.profile(**profiling_options) do
|
|
41
|
+
result = @app.call(env)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
if @merge_fibers
|
|
45
|
+
profile.merge!
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
path = request.path.gsub('/', '-')
|
|
50
|
+
path.slice!(0)
|
|
51
|
+
|
|
52
|
+
print(profile, path)
|
|
53
|
+
result
|
|
54
|
+
end
|
|
55
|
+
else
|
|
56
|
+
@app.call(env)
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
private
|
|
61
|
+
|
|
62
|
+
def should_profile?(path)
|
|
63
|
+
return false if paths_match?(path, @skip_paths)
|
|
64
|
+
|
|
65
|
+
@only_paths ? paths_match?(path, @only_paths) : true
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def paths_match?(path, paths)
|
|
69
|
+
paths.any? { |skip_path| skip_path =~ path }
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def profiling_options
|
|
73
|
+
result = {}
|
|
74
|
+
result[:measure_mode] = @measure_mode
|
|
75
|
+
result[:track_allocations] = @track_allocations
|
|
76
|
+
result[:exclude_common] = @exclude_common
|
|
77
|
+
|
|
78
|
+
if @ignore_existing_threads
|
|
79
|
+
result[:exclude_threads] = Thread.list.select {|thread| thread != Thread.current}
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
if @request_thread_only
|
|
83
|
+
result[:include_threads] = [Thread.current]
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
result
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def print_options
|
|
90
|
+
{min_percent: @min_percent, sort_method: @sort_method}
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def print(profile, path)
|
|
94
|
+
@printer_klasses.each do |printer_klass, base_name|
|
|
95
|
+
printer = printer_klass.new(profile)
|
|
96
|
+
|
|
97
|
+
if base_name.respond_to?(:call)
|
|
98
|
+
base_name = base_name.call
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
if printer_klass == ::RubyProf::MultiPrinter
|
|
102
|
+
printer.print(profile: "#{path}-#{base_name}", **print_options)
|
|
103
|
+
elsif printer_klass == ::RubyProf::CallTreePrinter
|
|
104
|
+
printer.print(profile: "#{path}-#{base_name}", **print_options)
|
|
105
|
+
else
|
|
106
|
+
file_name = ::File.join(@tmpdir, "#{path}-#{base_name}")
|
|
107
|
+
::File.open(file_name, 'wb') do |file|
|
|
108
|
+
printer.print(file, **print_options)
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
end
|
data/lib/ruby-prof/task.rb
CHANGED
|
@@ -1,147 +1,147 @@
|
|
|
1
|
-
#!/usr/bin/env ruby
|
|
2
|
-
# encoding: utf-8
|
|
3
|
-
|
|
4
|
-
require 'rake'
|
|
5
|
-
require 'rake/testtask'
|
|
6
|
-
require 'fileutils'
|
|
7
|
-
|
|
8
|
-
module RubyProf
|
|
9
|
-
|
|
10
|
-
# Define a task library for profiling unit tests with ruby-prof.
|
|
11
|
-
#
|
|
12
|
-
# All of the options provided by
|
|
13
|
-
# the Rake:TestTask are supported except the loader
|
|
14
|
-
# which is set to ruby-prof. For detailed information
|
|
15
|
-
# please refer to the Rake:TestTask documentation.
|
|
16
|
-
#
|
|
17
|
-
# ruby-prof specific options include:
|
|
18
|
-
#
|
|
19
|
-
# output_dir - For each file specified an output
|
|
20
|
-
# file with profile information will be
|
|
21
|
-
# written to the output directory.
|
|
22
|
-
# By default, the output directory is
|
|
23
|
-
# called "profile" and is created underneath
|
|
24
|
-
# the current working directory.
|
|
25
|
-
#
|
|
26
|
-
# printer - Specifies the output printer. Valid values include
|
|
27
|
-
# :flat, :graph, :graph_html and :call_tree.
|
|
28
|
-
#
|
|
29
|
-
# min_percent - Methods that take less than the specified percent
|
|
30
|
-
# will not be written out.
|
|
31
|
-
#
|
|
32
|
-
# Example:
|
|
33
|
-
#
|
|
34
|
-
# require 'ruby-prof/task'
|
|
35
|
-
#
|
|
36
|
-
# RubyProf::ProfileTask.new do |t|
|
|
37
|
-
# t.test_files = FileList['test/test*.rb']
|
|
38
|
-
# t.output_dir = "c:/temp"
|
|
39
|
-
# t.printer = :graph
|
|
40
|
-
# t.min_percent = 10
|
|
41
|
-
# end
|
|
42
|
-
#
|
|
43
|
-
# If rake is invoked with a "TEST=filename" command line option,
|
|
44
|
-
# then the list of test files will be overridden to include only the
|
|
45
|
-
# filename specified on the command line. This provides an easy way
|
|
46
|
-
# to run just one test.
|
|
47
|
-
#
|
|
48
|
-
# If rake is invoked with a "TESTOPTS=options" command line option,
|
|
49
|
-
# then the given options are passed to the test process after a
|
|
50
|
-
# '--'. This allows Test::Unit options to be passed to the test
|
|
51
|
-
# suite.
|
|
52
|
-
#
|
|
53
|
-
# Examples:
|
|
54
|
-
#
|
|
55
|
-
# rake profile # run tests normally
|
|
56
|
-
# rake profile TEST=just_one_file.rb # run just one test file.
|
|
57
|
-
# rake profile TESTOPTS="-v" # run in verbose mode
|
|
58
|
-
# rake profile TESTOPTS="--runner=fox" # use the fox test runner
|
|
59
|
-
|
|
60
|
-
class ProfileTask < Rake::TestTask
|
|
61
|
-
attr_accessor :output_dir
|
|
62
|
-
attr_accessor :min_percent
|
|
63
|
-
attr_accessor :printer
|
|
64
|
-
|
|
65
|
-
def initialize(name = :profile)
|
|
66
|
-
super(name)
|
|
67
|
-
end
|
|
68
|
-
|
|
69
|
-
# Create the tasks defined by this task lib.
|
|
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
|
|
89
|
-
|
|
90
|
-
# Run script
|
|
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
|
|
125
|
-
|
|
126
|
-
def output_directory
|
|
127
|
-
File.expand_path(@output_dir)
|
|
128
|
-
end
|
|
129
|
-
|
|
130
|
-
def create_output_directory
|
|
131
|
-
if not File.exist?(output_directory)
|
|
132
|
-
Dir.mkdir(output_directory)
|
|
133
|
-
end
|
|
134
|
-
end
|
|
135
|
-
|
|
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
|
|
142
|
-
|
|
143
|
-
def option_list # :nodoc:
|
|
144
|
-
ENV['OPTIONS'] || @options.join(" ") || ""
|
|
145
|
-
end
|
|
146
|
-
end
|
|
147
|
-
end
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# encoding: utf-8
|
|
3
|
+
|
|
4
|
+
require 'rake'
|
|
5
|
+
require 'rake/testtask'
|
|
6
|
+
require 'fileutils'
|
|
7
|
+
|
|
8
|
+
module RubyProf
|
|
9
|
+
|
|
10
|
+
# Define a task library for profiling unit tests with ruby-prof.
|
|
11
|
+
#
|
|
12
|
+
# All of the options provided by
|
|
13
|
+
# the Rake:TestTask are supported except the loader
|
|
14
|
+
# which is set to ruby-prof. For detailed information
|
|
15
|
+
# please refer to the Rake:TestTask documentation.
|
|
16
|
+
#
|
|
17
|
+
# ruby-prof specific options include:
|
|
18
|
+
#
|
|
19
|
+
# output_dir - For each file specified an output
|
|
20
|
+
# file with profile information will be
|
|
21
|
+
# written to the output directory.
|
|
22
|
+
# By default, the output directory is
|
|
23
|
+
# called "profile" and is created underneath
|
|
24
|
+
# the current working directory.
|
|
25
|
+
#
|
|
26
|
+
# printer - Specifies the output printer. Valid values include
|
|
27
|
+
# :flat, :graph, :graph_html and :call_tree.
|
|
28
|
+
#
|
|
29
|
+
# min_percent - Methods that take less than the specified percent
|
|
30
|
+
# will not be written out.
|
|
31
|
+
#
|
|
32
|
+
# Example:
|
|
33
|
+
#
|
|
34
|
+
# require 'ruby-prof/task'
|
|
35
|
+
#
|
|
36
|
+
# RubyProf::ProfileTask.new do |t|
|
|
37
|
+
# t.test_files = FileList['test/test*.rb']
|
|
38
|
+
# t.output_dir = "c:/temp"
|
|
39
|
+
# t.printer = :graph
|
|
40
|
+
# t.min_percent = 10
|
|
41
|
+
# end
|
|
42
|
+
#
|
|
43
|
+
# If rake is invoked with a "TEST=filename" command line option,
|
|
44
|
+
# then the list of test files will be overridden to include only the
|
|
45
|
+
# filename specified on the command line. This provides an easy way
|
|
46
|
+
# to run just one test.
|
|
47
|
+
#
|
|
48
|
+
# If rake is invoked with a "TESTOPTS=options" command line option,
|
|
49
|
+
# then the given options are passed to the test process after a
|
|
50
|
+
# '--'. This allows Test::Unit options to be passed to the test
|
|
51
|
+
# suite.
|
|
52
|
+
#
|
|
53
|
+
# Examples:
|
|
54
|
+
#
|
|
55
|
+
# rake profile # run tests normally
|
|
56
|
+
# rake profile TEST=just_one_file.rb # run just one test file.
|
|
57
|
+
# rake profile TESTOPTS="-v" # run in verbose mode
|
|
58
|
+
# rake profile TESTOPTS="--runner=fox" # use the fox test runner
|
|
59
|
+
|
|
60
|
+
class ProfileTask < Rake::TestTask
|
|
61
|
+
attr_accessor :output_dir
|
|
62
|
+
attr_accessor :min_percent
|
|
63
|
+
attr_accessor :printer
|
|
64
|
+
|
|
65
|
+
def initialize(name = :profile)
|
|
66
|
+
super(name)
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# Create the tasks defined by this task lib.
|
|
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
|
|
89
|
+
|
|
90
|
+
# Run script
|
|
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
|
|
125
|
+
|
|
126
|
+
def output_directory
|
|
127
|
+
File.expand_path(@output_dir)
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def create_output_directory
|
|
131
|
+
if not File.exist?(output_directory)
|
|
132
|
+
Dir.mkdir(output_directory)
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
|
|
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
|
|
142
|
+
|
|
143
|
+
def option_list # :nodoc:
|
|
144
|
+
ENV['OPTIONS'] || @options.join(" ") || ""
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
end
|
data/lib/ruby-prof/thread.rb
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
module RubyProf
|
|
2
|
-
class Thread
|
|
3
|
-
# Returns the total time this thread was executed.
|
|
4
|
-
def total_time
|
|
5
|
-
self.call_tree.total_time
|
|
6
|
-
end
|
|
7
|
-
|
|
8
|
-
# Returns the amount of time this thread waited while other thread executed.
|
|
9
|
-
def wait_time
|
|
10
|
-
# wait_time, like self:time, is always method local
|
|
11
|
-
# thus we need to sum over all methods and call infos
|
|
12
|
-
self.methods.inject(0) do |sum, method_info|
|
|
13
|
-
method_info.callers.each do |call_tree|
|
|
14
|
-
sum += call_tree.wait_time
|
|
15
|
-
end
|
|
16
|
-
sum
|
|
17
|
-
end
|
|
18
|
-
end
|
|
19
|
-
end
|
|
20
|
-
end
|
|
1
|
+
module RubyProf
|
|
2
|
+
class Thread
|
|
3
|
+
# Returns the total time this thread was executed.
|
|
4
|
+
def total_time
|
|
5
|
+
self.call_tree.total_time
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
# Returns the amount of time this thread waited while other thread executed.
|
|
9
|
+
def wait_time
|
|
10
|
+
# wait_time, like self:time, is always method local
|
|
11
|
+
# thus we need to sum over all methods and call infos
|
|
12
|
+
self.methods.inject(0) do |sum, method_info|
|
|
13
|
+
method_info.callers.each do |call_tree|
|
|
14
|
+
sum += call_tree.wait_time
|
|
15
|
+
end
|
|
16
|
+
sum
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
data/lib/ruby-prof/version.rb
CHANGED
data/lib/ruby-prof.rb
CHANGED
|
@@ -1,52 +1,50 @@
|
|
|
1
|
-
# encoding: utf-8
|
|
2
|
-
require 'rubygems/version'
|
|
3
|
-
|
|
4
|
-
# Load the C-based binding.
|
|
5
|
-
begin
|
|
6
|
-
version = Gem::Version.new(RUBY_VERSION)
|
|
7
|
-
require "#{version.segments[0..1].join('.')}/ruby_prof.so"
|
|
8
|
-
rescue LoadError
|
|
9
|
-
require "ruby_prof.so"
|
|
10
|
-
end
|
|
11
|
-
|
|
12
|
-
require 'ruby-prof/version'
|
|
13
|
-
require 'ruby-prof/call_tree'
|
|
14
|
-
require 'ruby-prof/
|
|
15
|
-
require 'ruby-prof/
|
|
16
|
-
require 'ruby-prof/
|
|
17
|
-
require 'ruby-prof/
|
|
18
|
-
require 'ruby-prof/
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
autoload :
|
|
23
|
-
autoload :
|
|
24
|
-
autoload :
|
|
25
|
-
autoload :
|
|
26
|
-
autoload :
|
|
27
|
-
autoload :
|
|
28
|
-
autoload :FlatPrinter, 'ruby-prof/printers/flat_printer'
|
|
29
|
-
autoload :GraphHtmlPrinter, 'ruby-prof/printers/graph_html_printer'
|
|
30
|
-
autoload :GraphPrinter, 'ruby-prof/printers/graph_printer'
|
|
31
|
-
autoload :MultiPrinter, 'ruby-prof/printers/multi_printer'
|
|
32
|
-
|
|
33
|
-
# :nodoc:
|
|
34
|
-
# Checks if the user specified the clock mode via
|
|
35
|
-
# the RUBY_PROF_MEASURE_MODE environment variable
|
|
36
|
-
def self.figure_measure_mode
|
|
37
|
-
case ENV["RUBY_PROF_MEASURE_MODE"]
|
|
38
|
-
when "wall", "wall_time"
|
|
39
|
-
RubyProf.measure_mode = RubyProf::WALL_TIME
|
|
40
|
-
when "allocations"
|
|
41
|
-
RubyProf.measure_mode = RubyProf::ALLOCATIONS
|
|
42
|
-
when "
|
|
43
|
-
RubyProf.measure_mode = RubyProf::
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
RubyProf::figure_measure_mode
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
require 'rubygems/version'
|
|
3
|
+
|
|
4
|
+
# Load the C-based binding.
|
|
5
|
+
begin
|
|
6
|
+
version = Gem::Version.new(RUBY_VERSION)
|
|
7
|
+
require "#{version.segments[0..1].join('.')}/ruby_prof.so"
|
|
8
|
+
rescue LoadError
|
|
9
|
+
require "ruby_prof.so"
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
require 'ruby-prof/version'
|
|
13
|
+
require 'ruby-prof/call_tree'
|
|
14
|
+
require 'ruby-prof/measurement'
|
|
15
|
+
require 'ruby-prof/method_info'
|
|
16
|
+
require 'ruby-prof/profile'
|
|
17
|
+
require 'ruby-prof/rack'
|
|
18
|
+
require 'ruby-prof/thread'
|
|
19
|
+
|
|
20
|
+
module RubyProf
|
|
21
|
+
autoload :CallTreeVisitor, 'ruby-prof/call_tree_visitor'
|
|
22
|
+
autoload :AbstractPrinter, 'ruby-prof/printers/abstract_printer'
|
|
23
|
+
autoload :CallInfoPrinter, 'ruby-prof/printers/call_info_printer'
|
|
24
|
+
autoload :CallStackPrinter, 'ruby-prof/printers/call_stack_printer'
|
|
25
|
+
autoload :CallTreePrinter, 'ruby-prof/printers/call_tree_printer'
|
|
26
|
+
autoload :DotPrinter, 'ruby-prof/printers/dot_printer'
|
|
27
|
+
autoload :FlameGraphPrinter, 'ruby-prof/printers/flame_graph_printer'
|
|
28
|
+
autoload :FlatPrinter, 'ruby-prof/printers/flat_printer'
|
|
29
|
+
autoload :GraphHtmlPrinter, 'ruby-prof/printers/graph_html_printer'
|
|
30
|
+
autoload :GraphPrinter, 'ruby-prof/printers/graph_printer'
|
|
31
|
+
autoload :MultiPrinter, 'ruby-prof/printers/multi_printer'
|
|
32
|
+
|
|
33
|
+
# :nodoc:
|
|
34
|
+
# Checks if the user specified the clock mode via
|
|
35
|
+
# the RUBY_PROF_MEASURE_MODE environment variable
|
|
36
|
+
def self.figure_measure_mode
|
|
37
|
+
case ENV["RUBY_PROF_MEASURE_MODE"]
|
|
38
|
+
when "wall", "wall_time"
|
|
39
|
+
RubyProf.measure_mode = RubyProf::WALL_TIME
|
|
40
|
+
when "allocations"
|
|
41
|
+
RubyProf.measure_mode = RubyProf::ALLOCATIONS
|
|
42
|
+
when "process", "process_time"
|
|
43
|
+
RubyProf.measure_mode = RubyProf::PROCESS_TIME
|
|
44
|
+
else
|
|
45
|
+
# the default is defined in the measure_mode reader
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
RubyProf::figure_measure_mode
|