class ProfileTools
ProfileTools
is used to instrument specific methods. Provides feedback about method execution
time and number of objects created.
Constants
- EVENT
Attributes
profiled_methods[R]
Public Class Methods
add_method(display_name)
click to toggle source
# File lib/profile-tools.rb, line 17 def self.add_method(display_name) profiled_methods << display_name end
delete_method(display_name)
click to toggle source
# File lib/profile-tools.rb, line 21 def self.delete_method(display_name) profiled_methods.delete_if { |method| method == display_name } end
instrument() { || ... }
click to toggle source
# File lib/profile-tools.rb, line 86 def self.instrument profiler.instrument do yield end end
load(yaml_file)
click to toggle source
# File lib/profile-tools.rb, line 45 def self.load(yaml_file) require 'yaml' profile(YAML.load_file(yaml_file)) end
new()
click to toggle source
# File lib/profile-tools.rb, line 25 def initialize ObjectSpace.count_objects end
profile(classes)
click to toggle source
# File lib/profile-tools.rb, line 50 def self.profile(classes) profile_tools = new classes.each do |class_name, methods| methods.each do |method_name| if method_name =~ /\A\./ profile_tools.profile_class_method(class_name, method_name[1, method_name.size]) else profile_tools.profile_instance_method(class_name, method_name) end end end profile_tools end
profiler()
click to toggle source
# File lib/profile-tools.rb, line 82 def self.profiler Thread.current[:profile_tools_profiler] ||= Profiler.new end
stop_profiling(methods)
click to toggle source
# File lib/profile-tools.rb, line 66 def self.stop_profiling(methods) profile_tools = new methods.each do |method| if method =~ /#/ profile_tools.remove_profiled_instance_method(*method.split('#', 2)) elsif method =~ /\./ profile_tools.remove_profiled_class_method(*method.split('.', 2)) end end end
stop_profiling!()
click to toggle source
# File lib/profile-tools.rb, line 78 def self.stop_profiling! stop_profiling(profiled_methods.dup) end
Public Instance Methods
profile_class_method(class_name, method_name)
click to toggle source
# File lib/profile-tools.rb, line 33 def profile_class_method(class_name, method_name) profile_method(Object.const_get(class_name).singleton_class, method_name, "#{class_name}.#{method_name}") end
profile_instance_method(class_name, method_name)
click to toggle source
# File lib/profile-tools.rb, line 29 def profile_instance_method(class_name, method_name) profile_method(Object.const_get(class_name), method_name, "#{class_name}##{method_name}") end
remove_profiled_class_method(class_name, method_name)
click to toggle source
# File lib/profile-tools.rb, line 41 def remove_profiled_class_method(class_name, method_name) remove_profiling(Object.const_get(class_name).singleton_class, method_name, "#{class_name}.#{method_name}") end
remove_profiled_instance_method(class_name, method_name)
click to toggle source
# File lib/profile-tools.rb, line 37 def remove_profiled_instance_method(class_name, method_name) remove_profiling(Object.const_get(class_name), method_name, "#{class_name}##{method_name}") end
Private Instance Methods
generate_method_name(method_name, suffix)
click to toggle source
# File lib/profile-tools.rb, line 114 def generate_method_name(method_name, suffix) punctuation = if method_name =~ /(\?|!)$/ $1 end method_name = method_name.sub(punctuation, '') if punctuation "#{method_name}_#{suffix}#{punctuation}" end
profile_method(kls, method_name, display_name)
click to toggle source
# File lib/profile-tools.rb, line 94 def profile_method(kls, method_name, display_name) self.class.add_method(display_name) method_name_without_profiling = generate_method_name(method_name.to_s, 'without_profiling') method_name_with_profiling = generate_method_name(method_name.to_s, 'with_profiling') kls.class_eval( <<-STR, __FILE__, __LINE__ + 1 def #{method_name_with_profiling}(*args) ::ProfileTools.profiler.instrument('#{display_name}') do #{method_name_without_profiling}(*args) end end STR ) kls.alias_method(method_name_without_profiling, method_name) kls.alias_method(method_name, method_name_with_profiling) end
remove_profiling(kls, method_name, display_name)
click to toggle source
# File lib/profile-tools.rb, line 125 def remove_profiling(kls, method_name, display_name) self.class.delete_method(display_name) method_name_without_profiling = generate_method_name(method_name.to_s, 'without_profiling') method_name_with_profiling = generate_method_name(method_name.to_s, 'with_profiling') kls.alias_method(method_name, method_name_without_profiling) kls.send(:remove_method, method_name_with_profiling) kls.send(:remove_method, method_name_without_profiling) end