diff options
author | Sean McGivern <sean@gitlab.com> | 2018-07-19 15:38:57 +0300 |
---|---|---|
committer | Sean McGivern <sean@gitlab.com> | 2018-07-19 18:00:27 +0300 |
commit | ef973f6f28f5b98ba126985b01d1c463e1cd8761 (patch) | |
tree | f5a4a2f1cccfe3946aa65cc5627a86340fe0177f /lib/gitlab/profiler | |
parent | 928c81e2598da2a114011549b7aa68fce85fd077 (diff) |
Add a Gitlab::Profiler.print_by_total_time convenience method
This method uses Gitlab::Profiler::TotalTimeFlatPrinter internally, which is the
same as RubyProf::FlatPrinter, but the min_percent option (and new max_percent
option) applies to the _total_ time, not the _self_ time. This helps us figure
out if we're calling a library inefficiently, for instance.
Diffstat (limited to 'lib/gitlab/profiler')
-rw-r--r-- | lib/gitlab/profiler/total_time_flat_printer.rb | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/lib/gitlab/profiler/total_time_flat_printer.rb b/lib/gitlab/profiler/total_time_flat_printer.rb new file mode 100644 index 00000000000..2fd0ec10ba8 --- /dev/null +++ b/lib/gitlab/profiler/total_time_flat_printer.rb @@ -0,0 +1,39 @@ +module Gitlab + module Profiler + class TotalTimeFlatPrinter < RubyProf::FlatPrinter + def max_percent + @options[:max_percent] || 100 + end + + # Copied from: + # <https://github.com/ruby-prof/ruby-prof/blob/master/lib/ruby-prof/printers/flat_printer.rb> + # + # The changes are just to filter by total time, not self time, and add a + # max_percent option as well. + def print_methods(thread) + total_time = thread.total_time + methods = thread.methods.sort_by(&sort_method).reverse + + sum = 0 + methods.each do |method| + total_percent = (method.total_time / total_time) * 100 + next if total_percent < min_percent + next if total_percent > max_percent + + sum += method.self_time + + @output << "%6.2f %9.3f %9.3f %9.3f %9.3f %8d %s%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.recursive? ? "*" : " ", # cycle + method_name(method) # name + ] + end + end + end + end +end |