Class: SpeedGun::Profiler::Base
- Inherits:
-
Object
- Object
- SpeedGun::Profiler::Base
- Defined in:
- lib/speed_gun/profiler/base.rb
Direct Known Subclasses
ActionController, ActionView, ActiveRecord, Js, Manual, Rack
Instance Attribute Summary collapse
-
#backtrace ⇒ Object
readonly
Returns the value of attribute backtrace.
-
#elapsed_time ⇒ Object
readonly
Returns the value of attribute elapsed_time.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#parent_profile_id ⇒ Object
readonly
Returns the value of attribute parent_profile_id.
Class Method Summary collapse
- .hook_method(klass, method_name) ⇒ Object
- .inherited(klass) ⇒ Object
- .label ⇒ Object
- .load(data) ⇒ Object
- .profile(profiler, *args, &block) ⇒ Object
- .profiler_type ⇒ Object
Instance Method Summary collapse
- #as_msgpack(*args) ⇒ Object
- #html ⇒ Object
-
#initialize ⇒ Base
constructor
A new instance of Base.
- #label ⇒ Object
- #measure(&block) ⇒ Object
- #profile(*args, &block) ⇒ Object
- #title ⇒ Object
- #to_msgpack(*args) ⇒ Object
- #type ⇒ Object
Constructor Details
#initialize ⇒ Base
Returns a new instance of Base.
59 60 61 62 63 64 |
# File 'lib/speed_gun/profiler/base.rb', line 59 def initialize @id = SecureRandom.uuid @parent_profile_id = nil @elapsed_time = 0 @backtrace = [] end |
Instance Attribute Details
#backtrace ⇒ Object (readonly)
Returns the value of attribute backtrace.
65 66 67 |
# File 'lib/speed_gun/profiler/base.rb', line 65 def backtrace @backtrace end |
#elapsed_time ⇒ Object (readonly)
Returns the value of attribute elapsed_time.
65 66 67 |
# File 'lib/speed_gun/profiler/base.rb', line 65 def elapsed_time @elapsed_time end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
65 66 67 |
# File 'lib/speed_gun/profiler/base.rb', line 65 def id @id end |
#parent_profile_id ⇒ Object (readonly)
Returns the value of attribute parent_profile_id.
65 66 67 |
# File 'lib/speed_gun/profiler/base.rb', line 65 def parent_profile_id @parent_profile_id end |
Class Method Details
.hook_method(klass, method_name) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/speed_gun/profiler/base.rb', line 21 def self.hook_method(klass, method_name) without_profiling = "#{method_name}_withtout_profile".intern with_profiling = "#{method_name}_with_profile".intern return unless klass.send(:method_defined?, method_name) return if klass.send(:method_defined?, with_profiling) profiler = profiler_type klass.send(:alias_method, without_profiling, method_name) klass.send(:define_method, with_profiling) do |*args, &block| return send(without_profiling, *args, &block) unless SpeedGun.current profile_args = [self] + args SpeedGun.current.profile(profiler, *profile_args) do send(without_profiling, *args, &block) end end klass.send(:alias_method, method_name, with_profiling) end |
.inherited(klass) ⇒ Object
17 18 19 |
# File 'lib/speed_gun/profiler/base.rb', line 17 def self.inherited(klass) SpeedGun::Profiler::PROFILERS[klass.profiler_type] = klass end |
.label ⇒ Object
5 6 7 |
# File 'lib/speed_gun/profiler/base.rb', line 5 def self.label name.sub(/.*::/, '') end |
.load(data) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/speed_gun/profiler/base.rb', line 40 def self.load(data) data.delete('label') type = data.delete('type') profiler = SpeedGun::Profiler::PROFILERS[type.to_sym] profile = profiler.new data.each_pair do |key, val| profile.send(:instance_variable_set, :"@#{key}", val) end profile end |
.profile(profiler, *args, &block) ⇒ Object
53 54 55 56 57 |
# File 'lib/speed_gun/profiler/base.rb', line 53 def self.profile(profiler, *args, &block) profile = new profiler.profiles << profile profile.profile(*args, &block) end |
.profiler_type ⇒ Object
9 10 11 12 13 14 15 |
# File 'lib/speed_gun/profiler/base.rb', line 9 def self.profiler_type name\ .sub(/.*::/, '')\ .gsub(/(.)([A-Z])/) { |m| "#{$1}_#{$2.downcase}" }\ .downcase\ .to_sym end |
Instance Method Details
#as_msgpack(*args) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/speed_gun/profiler/base.rb', line 104 def as_msgpack(*args) hash = {} instance_variables.each do |key| unless key.to_s =~ /^\@_/ hash[key.to_s.sub(/^\@/, '')] = instance_variable_get(key) end end hash['type'] = type.to_s hash['label'] = label hash['title'] = title hash end |
#html ⇒ Object
71 72 73 |
# File 'lib/speed_gun/profiler/base.rb', line 71 def html '' end |
#label ⇒ Object
75 76 77 |
# File 'lib/speed_gun/profiler/base.rb', line 75 def label self.class.label end |
#measure(&block) ⇒ Object
96 97 98 99 100 101 102 |
# File 'lib/speed_gun/profiler/base.rb', line 96 def measure(&block) now = Time.now result = yield @elapsed_time = Time.now - now result end |
#profile(*args, &block) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/speed_gun/profiler/base.rb', line 83 def profile(*args, &block) @backtrace = cleanup_caller(caller(3)) parent_profile = SpeedGun.current.now_profile SpeedGun.current.now_profile = self call_without_defined(:before_profile, *args, &block) result = measure(&block) call_without_defined(:after_profile, *args, &block) return result ensure SpeedGun.current.now_profile = parent_profile @parent_profile_id = parent_profile.id if parent_profile end |
#title ⇒ Object
67 68 69 |
# File 'lib/speed_gun/profiler/base.rb', line 67 def title warn 'Override this method' end |
#to_msgpack(*args) ⇒ Object
117 118 119 |
# File 'lib/speed_gun/profiler/base.rb', line 117 def to_msgpack(*args) as_msgpack(*args).to_msgpack(*args) end |
#type ⇒ Object
79 80 81 |
# File 'lib/speed_gun/profiler/base.rb', line 79 def type self.class.profiler_type end |