Class: Rack::MiniProfiler::ClientTimerStruct

Inherits:
TimerStruct
  • Object
show all
Defined in:
lib/mini_profiler/client_timer_struct.rb

Overview

This class holds the client timings

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from TimerStruct

#[], #[]=, #attributes, #to_json

Constructor Details

#initialize(env = {}) ⇒ ClientTimerStruct

Returns a new instance of ClientTimerStruct.



22
23
24
# File 'lib/mini_profiler/client_timer_struct.rb', line 22

def initialize(env={})
  super
end

Class Method Details

.init_instrumentationObject



9
10
11
# File 'lib/mini_profiler/client_timer_struct.rb', line 9

def self.init_instrumentation
  "<script type=\"text/javascript\">mPt=function(){var t=[];return{t:t,probe:function(n){t.push({d:new Date(),n:n})}}}()</script>"
end

.instrument(name, orig) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/mini_profiler/client_timer_struct.rb', line 13

def self.instrument(name,orig)
  probe = "<script>mPt.probe('#{name}')</script>"
  wrapped = probe
  wrapped << orig 
  wrapped << probe 
  wrapped
end

Instance Method Details

#init_from_form_data(env, page_struct) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/mini_profiler/client_timer_struct.rb', line 26

def init_from_form_data(env, page_struct)
  timings = []
  clientTimes, clientPerf, baseTime = nil 
  form = env['rack.request.form_hash']

  clientPerf = form['clientPerformance'] if form 
  clientTimes = clientPerf['timing'] if clientPerf 

  baseTime = clientTimes['navigationStart'].to_i if clientTimes
  return unless clientTimes && baseTime 

  probes = form['clientProbes']
  translated = {}
  if probes && probes != "null"
    probes.each do |id, val|
      name = val["n"]
      translated[name] ||= {} 
      if translated[name][:start]
        translated[name][:finish] = val["d"]
      else 
        translated[name][:start] = val["d"]
      end
    end
  end

  translated.each do |name, data|
    h = {"Name" => name, "Start" => data[:start].to_i - baseTime}
    h["Duration"] = data[:finish].to_i - data[:start].to_i if data[:finish]
    timings.push(h)
  end

  clientTimes.keys.find_all{|k| k =~ /Start$/ }.each do |k|
    start = clientTimes[k].to_i - baseTime 
    finish = clientTimes[k.sub(/Start$/, "End")].to_i - baseTime
    duration = 0 
    duration = finish - start if finish > start 
    name = k.sub(/Start$/, "").split(/(?=[A-Z])/).map{|s| s.capitalize}.join(' ')
    timings.push({"Name" => name, "Start" => start, "Duration" => duration}) if start >= 0
  end

  clientTimes.keys.find_all{|k| !(k =~ /(End|Start)$/)}.each do |k|
    timings.push("Name" => k, "Start" => clientTimes[k].to_i - baseTime, "Duration" => -1)
  end

  self['RedirectCount'] = env['rack.request.form_hash']['clientPerformance']['navigation']['redirectCount']
  self['Timings'] = timings
end