New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Profile private methods in addition to public and protected. #6

Closed
spilth opened this Issue Jan 2, 2013 · 6 comments

Comments

Projects
None yet
2 participants
@spilth
Contributor

spilth commented Jan 2, 2013

It would be nice if this profiled private methods as well. Did you make a conscious choice not to for a particular reason?

@jimmycuadra

This comment has been minimized.

Show comment
Hide comment
@jimmycuadra

jimmycuadra Jan 9, 2013

Contributor

Private methods were not left out for any philosophical reason. I'd be open to having them profiled. The gold star solution would be to allow you to control which methods to profile among public, protected, and private.

Contributor

jimmycuadra commented Jan 9, 2013

Private methods were not left out for any philosophical reason. I'd be open to having them profiled. The gold star solution would be to allow you to control which methods to profile among public, protected, and private.

@spilth

This comment has been minimized.

Show comment
Hide comment
@spilth

spilth Jan 9, 2013

Contributor

I forked the project and am attempting to get private methods being profiled as well but no luck yet.

Contributor

spilth commented Jan 9, 2013

I forked the project and am attempting to get private methods being profiled as well but no luck yet.

@spilth

This comment has been minimized.

Show comment
Hide comment
@spilth

spilth Jan 9, 2013

Contributor

I got this working and submitted a pull request so this is a duplicate now.

Contributor

spilth commented Jan 9, 2013

I got this working and submitted a pull request so this is a duplicate now.

@spilth spilth closed this Jan 9, 2013

@spilth

This comment has been minimized.

Show comment
Hide comment
@spilth

spilth Jan 10, 2013

Contributor

"The gold star solution would be to allow you to control which methods to profile among public, protected, and private."

What do you think the default for this should be? Profile all by default? Turn off what you don't want?

Contributor

spilth commented Jan 10, 2013

"The gold star solution would be to allow you to control which methods to profile among public, protected, and private."

What do you think the default for this should be? Profile all by default? Turn off what you don't want?

@jimmycuadra

This comment has been minimized.

Show comment
Hide comment
@jimmycuadra

jimmycuadra Jan 10, 2013

Contributor

On second thought, I'm not sure it's particularly important or useful to have that be configurable. If you can think of a use case where it would matter whether certain methods are profiled or not, I'm all ears.

Contributor

jimmycuadra commented Jan 10, 2013

On second thought, I'm not sure it's particularly important or useful to have that be configurable. If you can think of a use case where it would matter whether certain methods are profiled or not, I'm all ears.

@spilth

This comment has been minimized.

Show comment
Hide comment
@spilth

spilth Jan 10, 2013

Contributor

This crossed my mind as well and I couldn't think of a good use case either.

It did result in me refactoring wrap_methods_with_profiling for a bit more for clarity/smaller method:

def wrap_methods_with_profiling(options = {})
  options = {:singleton => true, :public => true, :private => true}.merge(options)

  wrap_singleton_methods        if options[:singleton] 
  wrap_instance_methods         if options[:public]
  wrap_private_instance_methods if options[:private]
end

def wrap_singleton_methods
  profiler = self
  singleton_methods_to_wrap = @obj.methods(false)

  @obj.singleton_class.module_eval do
    singleton_methods_to_wrap.each do |method|
      define_method("#{method}_with_profiling") do |*args, &block|
        profiler.send(:profile, method, true) { send("#{method}_without_profiling", *args, &block) }
      end

      alias_method "#{method}_without_profiling", method
      alias_method method, "#{method}_with_profiling"
    end
  end
end

def wrap_instance_methods
  profiler = self
  instance_methods_to_wrap = @obj.instance_methods(false)

  @obj.module_eval do
    instance_methods_to_wrap.each do |method|
      define_method("#{method}_with_profiling") do |*args, &block|
        profiler.send(:profile, method) { send("#{method}_without_profiling", *args, &block) }
      end

      alias_method "#{method}_without_profiling", method
      alias_method method, "#{method}_with_profiling"
    end
  end
end

def wrap_private_instance_methods
  profiler = self
  private_instance_methods_to_wrap = @obj.private_instance_methods(false) 

  @obj.module_eval do
    private_instance_methods_to_wrap.each do |method|
      define_method("#{method}_with_profiling") do |*args, &block|
        profiler.send(:profile, method) { send("#{method}_without_profiling", *args, &block) }
      end

      alias_method "#{method}_without_profiling", method
      alias_method method, "#{method}_with_profiling"

      private "#{method}_with_profiling"
      private "#{method}_without_profiling"

    end
  end
end
Contributor

spilth commented Jan 10, 2013

This crossed my mind as well and I couldn't think of a good use case either.

It did result in me refactoring wrap_methods_with_profiling for a bit more for clarity/smaller method:

def wrap_methods_with_profiling(options = {})
  options = {:singleton => true, :public => true, :private => true}.merge(options)

  wrap_singleton_methods        if options[:singleton] 
  wrap_instance_methods         if options[:public]
  wrap_private_instance_methods if options[:private]
end

def wrap_singleton_methods
  profiler = self
  singleton_methods_to_wrap = @obj.methods(false)

  @obj.singleton_class.module_eval do
    singleton_methods_to_wrap.each do |method|
      define_method("#{method}_with_profiling") do |*args, &block|
        profiler.send(:profile, method, true) { send("#{method}_without_profiling", *args, &block) }
      end

      alias_method "#{method}_without_profiling", method
      alias_method method, "#{method}_with_profiling"
    end
  end
end

def wrap_instance_methods
  profiler = self
  instance_methods_to_wrap = @obj.instance_methods(false)

  @obj.module_eval do
    instance_methods_to_wrap.each do |method|
      define_method("#{method}_with_profiling") do |*args, &block|
        profiler.send(:profile, method) { send("#{method}_without_profiling", *args, &block) }
      end

      alias_method "#{method}_without_profiling", method
      alias_method method, "#{method}_with_profiling"
    end
  end
end

def wrap_private_instance_methods
  profiler = self
  private_instance_methods_to_wrap = @obj.private_instance_methods(false) 

  @obj.module_eval do
    private_instance_methods_to_wrap.each do |method|
      define_method("#{method}_with_profiling") do |*args, &block|
        profiler.send(:profile, method) { send("#{method}_without_profiling", *args, &block) }
      end

      alias_method "#{method}_without_profiling", method
      alias_method method, "#{method}_with_profiling"

      private "#{method}_with_profiling"
      private "#{method}_without_profiling"

    end
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment