2015-11-04 13:13:19 -05:00
|
|
|
module Gitlab
|
|
|
|
module Sherlock
|
2015-11-05 12:01:05 -05:00
|
|
|
# Class for profiling code on a per line basis.
|
|
|
|
#
|
|
|
|
# The LineProfiler class can be used to profile code on per line basis
|
|
|
|
# without littering your code with Ruby implementation specific profiling
|
|
|
|
# methods.
|
|
|
|
#
|
|
|
|
# This profiler only includes samples taking longer than a given threshold
|
|
|
|
# and those that occur in the actual application (e.g. files from Gems are
|
|
|
|
# ignored).
|
2015-11-04 13:13:19 -05:00
|
|
|
class LineProfiler
|
|
|
|
# The minimum amount of time that has to be spent in a file for it to be
|
|
|
|
# included in a list of samples.
|
|
|
|
MINIMUM_DURATION = 10.0
|
|
|
|
|
2015-11-05 12:01:05 -05:00
|
|
|
# Profiles the given block.
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# profiler = LineProfiler.new
|
|
|
|
#
|
|
|
|
# retval, samples = profiler.profile do
|
|
|
|
# "cats are amazing"
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# retval # => "cats are amazing"
|
|
|
|
# samples # => [#<Gitlab::Sherlock::FileSample ...>, ...]
|
|
|
|
#
|
|
|
|
# Returns an Array containing the block's return value and an Array of
|
|
|
|
# FileSample objects.
|
2015-11-04 13:13:19 -05:00
|
|
|
def profile(&block)
|
2015-11-05 12:01:05 -05:00
|
|
|
if mri?
|
2015-11-04 13:13:19 -05:00
|
|
|
profile_mri(&block)
|
|
|
|
else
|
|
|
|
raise NotImplementedError,
|
|
|
|
'Line profiling is not supported on this platform'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-11-05 12:01:05 -05:00
|
|
|
# Profiles the given block using rblineprof (MRI only).
|
2015-11-04 13:13:19 -05:00
|
|
|
def profile_mri
|
2015-11-09 09:54:13 -05:00
|
|
|
require 'rblineprof'
|
|
|
|
|
2015-11-04 13:13:19 -05:00
|
|
|
retval = nil
|
|
|
|
samples = lineprof(/^#{Rails.root.to_s}/) { retval = yield }
|
|
|
|
|
|
|
|
file_samples = aggregate_rblineprof(samples)
|
|
|
|
|
|
|
|
[retval, file_samples]
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns an Array of file samples based on the output of rblineprof.
|
2015-11-05 12:01:05 -05:00
|
|
|
#
|
|
|
|
# lineprof_stats - A Hash containing rblineprof statistics on a per file
|
|
|
|
# basis.
|
|
|
|
#
|
|
|
|
# Returns an Array of FileSample objects.
|
2015-11-04 13:13:19 -05:00
|
|
|
def aggregate_rblineprof(lineprof_stats)
|
|
|
|
samples = []
|
|
|
|
|
|
|
|
lineprof_stats.each do |(file, stats)|
|
|
|
|
source_lines = File.read(file).each_line.to_a
|
|
|
|
line_samples = []
|
|
|
|
|
|
|
|
total_duration = microsec_to_millisec(stats[0][0])
|
|
|
|
total_events = stats[0][2]
|
|
|
|
|
|
|
|
next if total_duration <= MINIMUM_DURATION
|
|
|
|
|
|
|
|
stats[1..-1].each_with_index do |data, index|
|
|
|
|
next unless source_lines[index]
|
|
|
|
|
|
|
|
duration = microsec_to_millisec(data[0])
|
|
|
|
events = data[2]
|
|
|
|
|
|
|
|
line_samples << LineSample.new(duration, events)
|
|
|
|
end
|
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
samples << FileSample
|
|
|
|
.new(file, line_samples, total_duration, total_events)
|
2015-11-04 13:13:19 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
samples
|
|
|
|
end
|
|
|
|
|
2015-11-05 12:01:05 -05:00
|
|
|
private
|
|
|
|
|
2015-11-04 13:13:19 -05:00
|
|
|
def microsec_to_millisec(microsec)
|
|
|
|
microsec / 1000.0
|
|
|
|
end
|
2015-11-05 12:01:05 -05:00
|
|
|
|
|
|
|
def mri?
|
|
|
|
RUBY_ENGINE == 'ruby'
|
|
|
|
end
|
2015-11-04 13:13:19 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|