forked from ruby-prof/ruby-prof
/
flat_printer.rb
77 lines (68 loc) · 2.38 KB
/
flat_printer.rb
1
2
3
4
5
6
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# encoding: utf-8
module RubyProf
# Generates flat[link:files/examples/flat_txt.html] profile reports as text.
# To use the flat printer:
#
# result = RubyProf.profile do
# [code to profile]
# end
#
# printer = RubyProf::FlatPrinter.new(result)
# printer.print(STDOUT, {})
#
class FlatPrinter < AbstractPrinter
# Print a flat profile report to the provided output.
#
# output - Any IO object, including STDOUT or a file.
# The default value is STDOUT.
#
# options - Hash of print options. See #setup_options
# for more information.
#
def print(output = STDOUT, options = {})
@output = output
# Now sort methods by largest self time by default,
# not total time like in other printouts
options[:sort_method] ||= :self_time
setup_options(options)
print_threads
end
private
def print_threads
@result.threads.each do |thread_id, methods|
print_methods(thread_id, methods)
@output << "\n" * 2
end
end
def print_methods(thread_id, methods)
# Get total time
toplevel = methods.max
total_time = toplevel.total_time
if total_time == 0
total_time = 0.01
end
methods = methods.sort_by(&sort_method).reverse
@output << "Thread ID: %d\n" % thread_id
@output << "Total: %0.6f\n" % total_time
@output << "\n"
@output << " %self total self wait child calls name\n"
sum = 0
methods.each do |method|
self_percent = (method.self_time / total_time) * 100
next if self_percent < min_percent
sum += method.self_time
#self_time_called = method.called > 0 ? method.self_time/method.called : 0
#total_time_called = method.called > 0? method.total_time/method.called : 0
@output << "%6.2f %8.2f %8.2f %8.2f %8.2f %8d %s\n" % [
method.self_time / total_time * 100, # %self
method.total_time, # total
method.self_time, # self
method.wait_time, # wait
method.children_time, # children
method.called, # calls
method_name(method) # name
]
end
end
end
end