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