Class: SpeedGun::Profiler

Inherits:
Object
  • Object
show all
Defined in:
lib/speed_gun/profiler.rb

Defined Under Namespace

Classes: ActionController, ActionView, ActiveRecord, Base, Js, Manual, Rack

Constant Summary collapse

PROFILERS =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Profiler

Returns a new instance of Profiler.



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/speed_gun/profiler.rb', line 29

def initialize(env)
  @id = SecureRandom.uuid
  @path = env['PATH_INFO']
  @query = env['QUERY_STRING']
  @env = env
  @requested_at = Time.now
  @profiles = []
  @browser = nil
  @active = true
  @now_profile = nil
end

Instance Attribute Details

#browserObject

Returns the value of attribute browser.



40
41
42
# File 'lib/speed_gun/profiler.rb', line 40

def browser
  @browser
end

#envObject (readonly)

Returns the value of attribute env.



40
41
42
# File 'lib/speed_gun/profiler.rb', line 40

def env
  @env
end

#idObject (readonly)

Returns the value of attribute id.



40
41
42
# File 'lib/speed_gun/profiler.rb', line 40

def id
  @id
end

#now_profileObject

Returns the value of attribute now_profile.



41
42
43
# File 'lib/speed_gun/profiler.rb', line 41

def now_profile
  @now_profile
end

#pathObject (readonly)

Returns the value of attribute path.



40
41
42
# File 'lib/speed_gun/profiler.rb', line 40

def path
  @path
end

#profilesObject (readonly)

Returns the value of attribute profiles.



40
41
42
# File 'lib/speed_gun/profiler.rb', line 40

def profiles
  @profiles
end

#queryObject (readonly)

Returns the value of attribute query.



40
41
42
# File 'lib/speed_gun/profiler.rb', line 40

def query
  @query
end

#requested_atObject (readonly)

Returns the value of attribute requested_at.



40
41
42
# File 'lib/speed_gun/profiler.rb', line 40

def requested_at
  @requested_at
end

Class Method Details

.load(id) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/speed_gun/profiler.rb', line 12

def self.load(id)
  src = SpeedGun.store[id]

  return nil unless src

  restore(src)
end

.restore(src) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/speed_gun/profiler.rb', line 20

def self.restore(src)
  data = src.kind_of?(String) ? MessagePack.unpack(src) : src

  profiler = new({})
  profiler.restore_by_hash(data)

  profiler
end

Instance Method Details

#activate!Object



61
62
63
# File 'lib/speed_gun/profiler.rb', line 61

def activate!
  @active = true
end

#active?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/speed_gun/profiler.rb', line 57

def active?
  @active && !skip?
end

#as_msgpack(*args) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/speed_gun/profiler.rb', line 79

def as_msgpack(*args)
  {
    id: @id,
    path: @path,
    query: @query,
    env: msgpackable_env,
    requested_at: @requested_at.to_i,
    profiles: @profiles.map { |profile| profile.as_msgpack(*args) },
    browser: @browser ? @browser.as_msgpack(*args) : nil,
  }
end

#deactivate!Object



65
66
67
# File 'lib/speed_gun/profiler.rb', line 65

def deactivate!
  @active = false
end

#dumpObject



69
70
71
72
73
# File 'lib/speed_gun/profiler.rb', line 69

def dump
  SpeedGun.store[id] = to_msgpack

  SpeedGun::Hook.invoke_all(self)
end

#profile(type, *args, &block) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/speed_gun/profiler.rb', line 43

def profile(type, *args, &block)
  profiler = PROFILERS[type]

  if profiler
    profiler.profile(self, *args, &block)
  else
    yield
  end
end

#restore_attribute(key, val) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/speed_gun/profiler.rb', line 105

def restore_attribute(key, val)
  case key
  when 'requested_at'
    Time.at(val)
  when 'profiles'
    val.map { |profile| SpeedGun::Profiler::Base.load(profile) }
  when 'browser'
    val ? SpeedGun::Browser.new(val) : val
  else
    val
  end
end

#restore_by_hash(hash) ⇒ Object



99
100
101
102
103
# File 'lib/speed_gun/profiler.rb', line 99

def restore_by_hash(hash)
  hash.each_pair do |key, val|
    instance_variable_set(:"@#{key}", restore_attribute(key, val))
  end
end

#skip?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/speed_gun/profiler.rb', line 53

def skip?
  SpeedGun.config.skip_paths.any? { |prefix| prefix.match(@path) }
end

#to_json(*args) ⇒ Object



95
96
97
# File 'lib/speed_gun/profiler.rb', line 95

def to_json(*args)
  MultiJson.dump(as_msgpack(*args))
end

#to_msgpack(*args) ⇒ Object



91
92
93
# File 'lib/speed_gun/profiler.rb', line 91

def to_msgpack(*args)
  as_msgpack(*args).to_msgpack(*args)
end