Class: Rack::AllocationStats::Tracer

Inherits:
Action
  • Object
show all
Includes:
Utils
Defined in:
lib/rack/allocation_stats/tracer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Tracer

Returns a new instance of Tracer.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rack/allocation_stats/tracer.rb', line 12

def initialize(*args)
  super
  request = Rack::Request.new(@env)

  # Once we're here, GET["ras"] better exist
  @scope = request.GET["ras"]["scope"]
  @times = (request.GET["ras"]["times"] || 1).to_i
  @gc_report = request.GET["ras"]["gc_report"]
  @output = (request.GET["ras"]["output"] || :columnar).to_sym
  @alias_paths = request.GET["ras"]["alias_paths"] || false
  @new_env = delete_custom_params(@env)
end

Instance Attribute Details

#gc_reportObject (readonly)

Returns the value of attribute gc_report.



10
11
12
# File 'lib/rack/allocation_stats/tracer.rb', line 10

def gc_report
  @gc_report
end

#statsObject (readonly)

Returns the value of attribute stats.



10
11
12
# File 'lib/rack/allocation_stats/tracer.rb', line 10

def stats
  @stats
end

Instance Method Details

#actObject



25
26
27
28
29
30
31
# File 'lib/rack/allocation_stats/tracer.rb', line 25

def act
  @stats = AllocationStats.trace do
    @times.times do
      @middleware.call_app(@new_env)
    end
  end
end

#default_groups_and_sort(allocations) ⇒ Object



60
61
62
63
64
65
# File 'lib/rack/allocation_stats/tracer.rb', line 60

def default_groups_and_sort(allocations)
  allocations.
    group_by(:sourcefile, :sourceline, :class_plus).
    sort_by_size.
    all
end

#delete_custom_params(env) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/rack/allocation_stats/tracer.rb', line 67

def delete_custom_params(env)
  new_env = env

  get_params = Rack::Request.new(new_env).GET
  get_params.delete("ras")

  new_env.delete("rack.request.query_string")
  new_env.delete("rack.request.query_hash")

  new_env["QUERY_STRING"] = build_query(get_params)
  new_env
end

#responseObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rack/allocation_stats/tracer.rb', line 33

def response
  allocations = scoped_allocations

  if @output == :interactive
    allocations = allocations.all
    @middleware.allocation_stats_response(Formatters::HTML.new(self, allocations).format)
  elsif @output == :json
    allocations = default_groups_and_sort(allocations)
    @middleware.allocation_stats_response(Formatters::JSON.new(self, allocations).format)
  else
    allocations = default_groups_and_sort(allocations)
    @middleware.allocation_stats_response(Formatters::Text.new(self, allocations).format)
  end
end

#scoped_allocationsObject



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rack/allocation_stats/tracer.rb', line 48

def scoped_allocations
  allocations = @stats.allocations(alias_paths: @alias_paths)

  return allocations if @scope.nil?

  if @scope == "."
    return allocations.from_pwd
  else
    return allocations.from(@scope)
  end
end