method_profiler 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (C) 2012 by Jimmy Cuadra
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
@@ -0,0 +1,19 @@
1
+ # MethodProfiler
2
+
3
+ **MethodProfiler** collects performance information about the methods in your objects and creates reports to help you identify slow methods.
4
+
5
+ ```ruby
6
+ profiler = MethodProfiler.new(MyClass)
7
+ MyClass.new.foo
8
+ puts profiler.report
9
+
10
+ # MethodProfiler results for:
11
+ # MyClass
12
+ # +--------+--------------+-------------+
13
+ # | Method | Average Time | Total Calls |
14
+ # +--------+--------------+-------------+
15
+ # | foo | 91.037000 ms | 1 |
16
+ # | bar | 15.016000 ms | 2 |
17
+ # | baz | 23.005000 ms | 1 |
18
+ # +--------+--------------+-------------+
19
+ ```
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ desc "Run specs"
5
+ RSpec::Core::RakeTask.new { |t| t.rspec_opts = "--color" }
6
+
7
+ task :default => :spec
@@ -0,0 +1,84 @@
1
+ require 'benchmark'
2
+ require 'hirb'
3
+
4
+ class MethodProfiler
5
+ attr_reader :observed_methods, :data
6
+
7
+ def initialize(obj)
8
+ @obj = obj
9
+ initialize_data
10
+ find_obj_methods
11
+ wrap_methods_with_profiling
12
+ end
13
+
14
+ def profile(method, &block)
15
+ result = nil
16
+ benchmark = Benchmark.measure { result = block.call }
17
+ elapsed_time = benchmark.to_s.match(/\(\s*([^\)]+)\)/)[1].to_f
18
+ @data[method.to_sym] << elapsed_time
19
+ result
20
+ end
21
+
22
+ def report
23
+ [
24
+ "MethodProfiler results for:",
25
+ @obj.to_s,
26
+ Hirb::Helpers::Table.render(
27
+ final_data,
28
+ headers: ["Method", "Average Time", "Total Calls"],
29
+ description: false
30
+ )
31
+ ].join("\n")
32
+ end
33
+
34
+ def reset!
35
+ initialize_data
36
+ @final_data = nil
37
+ end
38
+
39
+ private
40
+
41
+ def initialize_data
42
+ @data = Hash.new { |h, k| h[k] = [] }
43
+ end
44
+
45
+ def find_obj_methods
46
+ @observed_methods ||= begin
47
+ methods = @obj.instance_methods
48
+ @obj.ancestors.each do |a|
49
+ next if a == @obj
50
+ methods -= a.instance_methods
51
+ end
52
+ methods
53
+ end
54
+ end
55
+
56
+ def wrap_methods_with_profiling
57
+ profiler = self
58
+ observed = observed_methods
59
+
60
+ @obj.module_eval do
61
+ observed.each do |method|
62
+ define_method("#{method}_with_profiling") do |*args|
63
+ profiler.profile(method) { send("#{method}_without_profiling", *args) }
64
+ end
65
+
66
+ alias_method "#{method}_without_profiling", method
67
+ alias_method method, "#{method}_with_profiling"
68
+ end
69
+ end
70
+ end
71
+
72
+ def final_data
73
+ @final_data ||= begin
74
+ final_data = []
75
+ data.each do |method, records|
76
+ total_calls = records.size
77
+ average = records.reduce(:+) / total_calls
78
+ final_data << [method, '%f ms' % (average * 1000), total_calls]
79
+ end
80
+ final_data.sort! { |a, b| b[1] <=> a[1] }
81
+ final_data
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,19 @@
1
+ # encoding: utf-8
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "method_profiler"
5
+ s.version = "0.0.1"
6
+ s.authors = ["Jimmy Cuadra"]
7
+ s.email = ["jimmy@jimmycuadra.com"]
8
+ s.homepage = "https://github.com/change/method_profiler"
9
+ s.summary = %q{Find slow methods in your program.}
10
+ s.description = %q{MethodProfiler observes your code and generates reports about the methods that were run and how long they took.}
11
+
12
+ s.files = `git ls-files`.split("\n")
13
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
15
+ s.require_paths = ["lib"]
16
+
17
+ s.add_runtime_dependency "hirb", ">= 0.6.0"
18
+ s.add_development_dependency "rspec"
19
+ end
@@ -0,0 +1,60 @@
1
+ require 'spec_helper'
2
+
3
+ profiler = MethodProfiler.new(Petition)
4
+
5
+ describe MethodProfiler do
6
+ before { @petition = Petition.new }
7
+ after { profiler.reset! }
8
+
9
+ it "can be instantiated with an object to observe" do
10
+ profiler.should be_true
11
+ end
12
+
13
+ it "finds all the object's instance methods" do
14
+ profiler.observed_methods.sort.should == [:foo, :bar, :baz].sort
15
+ end
16
+
17
+ it "creates wrapper methods for each method in the object" do
18
+ @petition.should respond_to(:foo)
19
+ @petition.should respond_to(:foo_with_profiling)
20
+ @petition.should respond_to(:foo_without_profiling)
21
+ @petition.should_not respond_to(:foo_with_profiling_with_profiling)
22
+ end
23
+
24
+ describe "#profile" do
25
+ it "adds a new record for the method call" do
26
+ @petition.foo
27
+ profiler.data[:foo].size.should == 1
28
+ end
29
+
30
+ it "calls the real method" do
31
+ @petition.should_receive(:foo_without_profiling)
32
+ @petition.foo
33
+ end
34
+
35
+ it "returns the value of the real method" do
36
+ @petition.baz.should == "blah"
37
+ end
38
+ end
39
+
40
+ describe "#report" do
41
+ it "outputs a string of report data" do
42
+ profiler.report.should be_an_instance_of(String)
43
+ end
44
+
45
+ it "outputs one line for each method that was called" do
46
+ @petition.foo
47
+ @petition.bar
48
+ @petition.baz
49
+ profiler.report.scan(/foo/).size.should == 1
50
+ profiler.report.scan(/bar/).size.should == 1
51
+ profiler.report.scan(/baz/).size.should == 1
52
+ end
53
+
54
+ it "combines multiple calls to the same method into one line" do
55
+ @petition.foo
56
+ @petition.foo
57
+ profiler.report.scan(/foo/).size.should == 1
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,8 @@
1
+ $:.unshift(File.expand_path("../../lib", __FILE__))
2
+ require "method_profiler"
3
+
4
+ class Petition
5
+ def foo; end
6
+ def bar; end
7
+ def baz; "blah"; end
8
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: method_profiler
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jimmy Cuadra
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-01 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: hirb
16
+ requirement: &70119481195160 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 0.6.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70119481195160
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &70119481194080 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70119481194080
36
+ description: MethodProfiler observes your code and generates reports about the methods
37
+ that were run and how long they took.
38
+ email:
39
+ - jimmy@jimmycuadra.com
40
+ executables: []
41
+ extensions: []
42
+ extra_rdoc_files: []
43
+ files:
44
+ - .gitignore
45
+ - Gemfile
46
+ - LICENSE
47
+ - README.md
48
+ - Rakefile
49
+ - lib/method_profiler.rb
50
+ - method_profiler.gemspec
51
+ - spec/method_profiler_spec.rb
52
+ - spec/spec_helper.rb
53
+ homepage: https://github.com/change/method_profiler
54
+ licenses: []
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ! '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubyforge_project:
73
+ rubygems_version: 1.8.17
74
+ signing_key:
75
+ specification_version: 3
76
+ summary: Find slow methods in your program.
77
+ test_files:
78
+ - spec/method_profiler_spec.rb
79
+ - spec/spec_helper.rb