public
Rubygem
Description: Pedal to the metal manual profiler
Clone URL: git://github.com/nicksieger/stickshift.git
Search Repo:
Add :top_level => true option to only trigger/enable when called 
through top-level method
nicksieger (author)
Wed May 14 13:18:45 -0700 2008
commit  f545a18134a92788f94bb1d442c2bec4132a6429
tree    be2e6beeadd3ac5963f9fa282c5e5d49f5d49055
parent  4aa50f6c86bcc6034864fed60aabce5584e34ceb
...
1
2
3
 
4
5
6
7
8
...
22
23
24
 
 
 
 
25
26
27
28
29
 
 
 
 
 
 
 
 
 
 
 
30
31
32
33
34
35
36
37
...
73
74
75
 
76
77
78
...
1
2
3
4
5
6
7
8
9
...
23
24
25
26
27
28
29
30
 
 
 
 
31
32
33
34
35
36
37
38
39
40
41
42
 
 
 
 
43
44
45
...
81
82
83
84
85
86
87
0
@@ -1,6 +1,7 @@
0
 require 'benchmark'
0
 
0
 module Stickshift
0
+ class << self; attr_accessor :top_level_trigger; end
0
   class Timer
0
     def self.current; Thread.current['__stickshift']; end
0
     def self.current=(timer); Thread.current['__stickshift'] = timer; end
0
0
0
@@ -22,16 +23,23 @@
0
       end
0
     end
0
 
0
+ def enabled?
0
+ Timer.current || Stickshift.top_level_trigger.nil? || @options[:top_level]
0
+ end
0
+
0
     def invoke(&block)
0
- Timer.current = self
0
- result = nil
0
- @elapsed = Benchmark.realtime do
0
- result = block.call
0
+ return block.call unless enabled?
0
+ begin
0
+ Timer.current = self
0
+ result = nil
0
+ @elapsed = Benchmark.realtime do
0
+ result = block.call
0
+ end
0
+ result
0
+ ensure
0
+ Timer.current = @parent
0
+ report unless @parent
0
       end
0
- result
0
- ensure
0
- Timer.current = @parent
0
- report unless @parent
0
     end
0
 
0
     def add(child)
0
@@ -73,6 +81,7 @@
0
 class Module
0
   def instrument(*meths)
0
     options = Hash === meths.last ? meths.pop : {}
0
+ Stickshift.top_level_trigger = true if options[:top_level]
0
     @__stickshift ||= {}
0
     meths.each do |meth|
0
       unless instrumented?(meth)
...
33
34
35
 
36
37
38
...
42
43
44
 
45
46
47
...
55
56
57
58
59
60
 
 
 
61
62
63
...
101
102
103
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104
105
...
33
34
35
36
37
38
39
...
43
44
45
46
47
48
49
...
57
58
59
 
 
 
60
61
62
63
64
65
...
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
0
@@ -33,6 +33,7 @@
0
   def capture
0
     @save = $stdout
0
     @stdout = $stdout = StringIO.new
0
+ @out = nil
0
   end
0
   
0
   def teardown
0
@@ -42,6 +43,7 @@
0
     String.uninstrument_all
0
     $stdout = @save if @save
0
     puts @out if @out
0
+ Stickshift.top_level_trigger = nil
0
   end
0
 
0
   def test_formatting
0
@@ -55,9 +57,9 @@
0
     Foo.instrument :slow_method
0
     capture
0
     Bar.new.slow_method
0
- @out = @stdout.string
0
- assert @out =~ /Bar#slow_method/
0
- assert @out =~ /Foo#slow_method/
0
+ out = @stdout.string
0
+ assert out =~ /Bar#slow_method/
0
+ assert out =~ /Foo#slow_method/
0
   end
0
 
0
   def test_custom_label
0
@@ -101,6 +103,28 @@
0
     Array.instrument :length, :inspect_self => true
0
     %w(a b c).length
0
     assert @stdout.string =~ /"a", "b", "c"/
0
+ end
0
+
0
+ def test_output_only_when_triggered_by_top_level_method
0
+ capture
0
+ Foo.instrument :slow_method
0
+ Bar.instrument :slow_method
0
+ Bar.new.slow_method
0
+
0
+ assert !@stdout.string.empty?
0
+ expected = @stdout.string
0
+ @stdout.reopen(StringIO.new)
0
+ assert @stdout.string.empty?
0
+
0
+ Bar.uninstrument :slow_method
0
+ Bar.instrument :slow_method, :top_level => true
0
+ assert Stickshift.top_level_trigger
0
+
0
+ Foo.new.slow_method
0
+ assert @stdout.string.empty?
0
+
0
+ Bar.new.slow_method
0
+ assert_equal expected, @stdout.string
0
   end
0
 end

Comments

    No one has commented yet.