Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/ExeBy/exe_root.../opt/alt/ruby22/lib64/ruby/2.2.0
File: profiler.rb
# Profile provides a way to Profile your Ruby application.
[0] Fix | Delete
#
[1] Fix | Delete
# Profiling your program is a way of determining which methods are called and
[2] Fix | Delete
# how long each method takes to complete. This way you can detect which
[3] Fix | Delete
# methods are possible bottlenecks.
[4] Fix | Delete
#
[5] Fix | Delete
# Profiling your program will slow down your execution time considerably,
[6] Fix | Delete
# so activate it only when you need it. Don't confuse benchmarking with
[7] Fix | Delete
# profiling.
[8] Fix | Delete
#
[9] Fix | Delete
# There are two ways to activate Profiling:
[10] Fix | Delete
#
[11] Fix | Delete
# == Command line
[12] Fix | Delete
#
[13] Fix | Delete
# Run your Ruby script with <code>-rprofile</code>:
[14] Fix | Delete
#
[15] Fix | Delete
# ruby -rprofile example.rb
[16] Fix | Delete
#
[17] Fix | Delete
# If you're profiling an executable in your <code>$PATH</code> you can use
[18] Fix | Delete
# <code>ruby -S</code>:
[19] Fix | Delete
#
[20] Fix | Delete
# ruby -rprofile -S some_executable
[21] Fix | Delete
#
[22] Fix | Delete
# == From code
[23] Fix | Delete
#
[24] Fix | Delete
# Just require 'profile':
[25] Fix | Delete
#
[26] Fix | Delete
# require 'profile'
[27] Fix | Delete
#
[28] Fix | Delete
# def slow_method
[29] Fix | Delete
# 5000.times do
[30] Fix | Delete
# 9999999999999999*999999999
[31] Fix | Delete
# end
[32] Fix | Delete
# end
[33] Fix | Delete
#
[34] Fix | Delete
# def fast_method
[35] Fix | Delete
# 5000.times do
[36] Fix | Delete
# 9999999999999999+999999999
[37] Fix | Delete
# end
[38] Fix | Delete
# end
[39] Fix | Delete
#
[40] Fix | Delete
# slow_method
[41] Fix | Delete
# fast_method
[42] Fix | Delete
#
[43] Fix | Delete
# The output in both cases is a report when the execution is over:
[44] Fix | Delete
#
[45] Fix | Delete
# ruby -rprofile example.rb
[46] Fix | Delete
#
[47] Fix | Delete
# % cumulative self self total
[48] Fix | Delete
# time seconds seconds calls ms/call ms/call name
[49] Fix | Delete
# 68.42 0.13 0.13 2 65.00 95.00 Integer#times
[50] Fix | Delete
# 15.79 0.16 0.03 5000 0.01 0.01 Fixnum#*
[51] Fix | Delete
# 15.79 0.19 0.03 5000 0.01 0.01 Fixnum#+
[52] Fix | Delete
# 0.00 0.19 0.00 2 0.00 0.00 IO#set_encoding
[53] Fix | Delete
# 0.00 0.19 0.00 1 0.00 100.00 Object#slow_method
[54] Fix | Delete
# 0.00 0.19 0.00 2 0.00 0.00 Module#method_added
[55] Fix | Delete
# 0.00 0.19 0.00 1 0.00 90.00 Object#fast_method
[56] Fix | Delete
# 0.00 0.19 0.00 1 0.00 190.00 #toplevel
[57] Fix | Delete
[58] Fix | Delete
module Profiler__
[59] Fix | Delete
class Wrapper < Struct.new(:defined_class, :method_id, :hash) # :nodoc:
[60] Fix | Delete
private :defined_class=, :method_id=, :hash=
[61] Fix | Delete
[62] Fix | Delete
def initialize(klass, mid)
[63] Fix | Delete
super(klass, mid, nil)
[64] Fix | Delete
self.hash = Struct.instance_method(:hash).bind(self).call
[65] Fix | Delete
end
[66] Fix | Delete
[67] Fix | Delete
def to_s
[68] Fix | Delete
"#{defined_class.inspect}#".sub(/\A\#<Class:(.*)>#\z/, '\1.') << method_id.to_s
[69] Fix | Delete
end
[70] Fix | Delete
alias inspect to_s
[71] Fix | Delete
end
[72] Fix | Delete
[73] Fix | Delete
# internal values
[74] Fix | Delete
@@start = nil # the start time that profiling began
[75] Fix | Delete
@@stacks = nil # the map of stacks keyed by thread
[76] Fix | Delete
@@maps = nil # the map of call data keyed by thread, class and id. Call data contains the call count, total time,
[77] Fix | Delete
PROFILE_CALL_PROC = TracePoint.new(*%i[call c_call b_call]) {|tp| # :nodoc:
[78] Fix | Delete
now = Process.times[0]
[79] Fix | Delete
stack = (@@stacks[Thread.current] ||= [])
[80] Fix | Delete
stack.push [now, 0.0]
[81] Fix | Delete
}
[82] Fix | Delete
PROFILE_RETURN_PROC = TracePoint.new(*%i[return c_return b_return]) {|tp| # :nodoc:
[83] Fix | Delete
now = Process.times[0]
[84] Fix | Delete
key = Wrapper.new(tp.defined_class, tp.method_id)
[85] Fix | Delete
stack = (@@stacks[Thread.current] ||= [])
[86] Fix | Delete
if tick = stack.pop
[87] Fix | Delete
threadmap = (@@maps[Thread.current] ||= {})
[88] Fix | Delete
data = (threadmap[key] ||= [0, 0.0, 0.0, key])
[89] Fix | Delete
data[0] += 1
[90] Fix | Delete
cost = now - tick[0]
[91] Fix | Delete
data[1] += cost
[92] Fix | Delete
data[2] += cost - tick[1]
[93] Fix | Delete
stack[-1][1] += cost if stack[-1]
[94] Fix | Delete
end
[95] Fix | Delete
}
[96] Fix | Delete
module_function
[97] Fix | Delete
# Starts the profiler.
[98] Fix | Delete
#
[99] Fix | Delete
# See Profiler__ for more information.
[100] Fix | Delete
def start_profile
[101] Fix | Delete
@@start = Process.times[0]
[102] Fix | Delete
@@stacks = {}
[103] Fix | Delete
@@maps = {}
[104] Fix | Delete
PROFILE_CALL_PROC.enable
[105] Fix | Delete
PROFILE_RETURN_PROC.enable
[106] Fix | Delete
end
[107] Fix | Delete
# Stops the profiler.
[108] Fix | Delete
#
[109] Fix | Delete
# See Profiler__ for more information.
[110] Fix | Delete
def stop_profile
[111] Fix | Delete
PROFILE_CALL_PROC.disable
[112] Fix | Delete
PROFILE_RETURN_PROC.disable
[113] Fix | Delete
end
[114] Fix | Delete
# Outputs the results from the profiler.
[115] Fix | Delete
#
[116] Fix | Delete
# See Profiler__ for more information.
[117] Fix | Delete
def print_profile(f)
[118] Fix | Delete
stop_profile
[119] Fix | Delete
total = Process.times[0] - @@start
[120] Fix | Delete
if total == 0 then total = 0.01 end
[121] Fix | Delete
totals = {}
[122] Fix | Delete
@@maps.values.each do |threadmap|
[123] Fix | Delete
threadmap.each do |key, data|
[124] Fix | Delete
total_data = (totals[key] ||= [0, 0.0, 0.0, key])
[125] Fix | Delete
total_data[0] += data[0]
[126] Fix | Delete
total_data[1] += data[1]
[127] Fix | Delete
total_data[2] += data[2]
[128] Fix | Delete
end
[129] Fix | Delete
end
[130] Fix | Delete
[131] Fix | Delete
# Maybe we should show a per thread output and a totals view?
[132] Fix | Delete
[133] Fix | Delete
data = totals.values
[134] Fix | Delete
data = data.sort_by{|x| -x[2]}
[135] Fix | Delete
sum = 0
[136] Fix | Delete
f.printf " %% cumulative self self total\n"
[137] Fix | Delete
f.printf " time seconds seconds calls ms/call ms/call name\n"
[138] Fix | Delete
for d in data
[139] Fix | Delete
sum += d[2]
[140] Fix | Delete
f.printf "%6.2f %8.2f %8.2f %8d ", d[2]/total*100, sum, d[2], d[0]
[141] Fix | Delete
f.printf "%8.2f %8.2f %s\n", d[2]*1000/d[0], d[1]*1000/d[0], d[3]
[142] Fix | Delete
end
[143] Fix | Delete
f.printf "%6.2f %8.2f %8.2f %8d ", 0.0, total, 0.0, 1 # ???
[144] Fix | Delete
f.printf "%8.2f %8.2f %s\n", 0.0, total*1000, "#toplevel" # ???
[145] Fix | Delete
end
[146] Fix | Delete
end
[147] Fix | Delete
[148] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function