Class: AllocationStats::AllocationsProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/allocation_stats/allocations_proxy.rb

Overview

AllocationsProxy acts as a proxy for an array of Allocation objects. The idea behind this class is merely to provide some domain-specific methods for transforming (filtering, sorting, and grouping) allocation information. This class uses the Command pattern heavily, in order to build and maintain the list of transforms it will ultimately perform, before retrieving the transformed collection of Allocations.

Chaining

Use of the Command pattern and Procs allows for transform-chaining in any order. Apply methods such as #from and #group_by to build the internal list of transforms. The transforms will not be applied to the collection of Allocations until a call to #to_a (#all) resolves them.

Filtering Transforms

Methods that filter the collection of Allocations will add a transform to an Array, @wheres. When the result set is finally retrieved, each where is applied serially, so that @wheres represents a logical conjunction ("and") of of filtering transforms. Presently there is no way to "or" filtering transforms together with a logical disjunction.

Mapping Transforms

Grouping Transform

Only one method will allow a grouping transform: #group_by. Only one grouping transform is allowed; subsequent calls to #group_by will only replace the previous grouping transform.

Constant Summary collapse

DEFAULT_COLUMNS =

default columns for the tabular output

[:sourcefile, :sourceline, :class_path, :method_id, :memsize, :class]
NUMERIC_COLUMNS =

columns that should be right-aligned for the tabular output

[:sourceline, :memsize]

Instance Method Summary collapse

Constructor Details

#initialize(allocations, alias_paths: false) ⇒ AllocationsProxy

Instantiate an AllocationStats::AllocationsProxy with an array of Allocations. AllocationProxy's view of pwd is set at instantiation.

Parameters:

  • allocations (Array<Allocation>)

    array of Allocation objects



44
45
46
47
48
49
50
51
# File 'lib/allocation_stats/allocations_proxy.rb', line 44

def initialize(allocations, alias_paths: false)
  @allocations = allocations
  @pwd = Dir.pwd
  @wheres = []
  @group_by = nil
  @mappers  = []
  @alias_paths = alias_paths
end

Instance Method Details

#alias_paths(value = nil) ⇒ Object

If a value is passed in, @alias_paths will be set to this value, and the AllocationStats object will be returned. If no value is passed in, this will return the @alias_paths.



77
78
79
80
81
82
83
84
85
# File 'lib/allocation_stats/allocations_proxy.rb', line 77

def alias_paths(value = nil)
  # reader
  return @alias_paths if value.nil?

  # writer
  @alias_paths = value

  return self
end

#at_least(count) ⇒ Object

Select allocation groups which have at least count allocations.

be selected.

Parameters:

  • count (Fixnum)

    the minimum number of Allocations for each group to



101
102
103
104
105
106
107
# File 'lib/allocation_stats/allocations_proxy.rb', line 101

def at_least(count)
  @mappers << Proc.new do |allocations|
    allocations.delete_if { |key,value| value.size < count }
  end

  self
end

#bytesObject

Map to bytes via #memsize. This is done in one of two ways:

  • If the current result set is an Array, then this transform just maps each Allocation to its #memsize.
  • If the current result set is a Hash (meaning it has been grouped), then this transform maps each value in the Hash (which is an Array of Allocations) to the sum of the Allocation #memsizes within.


215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/allocation_stats/allocations_proxy.rb', line 215

def bytes
  @mappers << Proc.new do |allocations|
    if allocations.is_a? Array
      allocations.map(&:memsize)
    elsif allocations.is_a? Hash
      bytes_h = {}
      allocations.each do |key, allocations|
        bytes_h[key] = allocations.inject(0) { |sum, allocation| sum + allocation.memsize }
      end
      bytes_h
    end
  end

  self
end

#from(pattern) ⇒ Object

Select allocations for which the sourcefile includes pattern.

#from can be called multiple times, adding to @wheres. See documentation for AllocationStats::AllocationsProxy for more information about chaining.

Parameters:



117
118
119
120
121
122
123
# File 'lib/allocation_stats/allocations_proxy.rb', line 117

def from(pattern)
  @wheres << Proc.new do |allocations|
    allocations.select { |allocation| allocation.sourcefile[pattern] }
  end

  self
end

#from_pwdObject

Select allocations for which the sourcefile includes the present working directory.

#from_pwd can be called multiple times, adding to @wheres. See documentation for AllocationStats::AllocationsProxy for more information about chaining.



146
147
148
149
150
151
152
# File 'lib/allocation_stats/allocations_proxy.rb', line 146

def from_pwd
  @wheres << Proc.new do |allocations|
    allocations.select { |allocation| allocation.sourcefile[@pwd] }
  end

  self
end

#group_by(*args) ⇒ Object

Group allocations by one or more attributes, that is, a list of symbols. Commonly, you might want to group allocations by:

  • :sourcefile, :sourceline, :class
  • :sourcefile, :method_id, :class
  • :classpath, :method_id, :class

In this case, :class is the class of the allocated object (as opposed to :classpath, the classpath where the allocation occured).



163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/allocation_stats/allocations_proxy.rb', line 163

def group_by(*args)
  @group_keys = args

  @group_by = Proc.new do |allocations|
    getters = attribute_getters(@group_keys)

    allocations.group_by do |allocation|
      getters.map { |getter| getter.call(allocation) }
    end
  end

  self
end

#not_from(pattern) ⇒ Object

Select allocations for which the sourcefile does not include pattern.

#not_from can be called multiple times, adding to @wheres. See documentation for AllocationStats::AllocationsProxy for more information about chaining.

Parameters:



133
134
135
136
137
138
139
# File 'lib/allocation_stats/allocations_proxy.rb', line 133

def not_from(pattern)
  @wheres << Proc.new do |allocations|
    allocations.reject { |allocation| allocation.sourcefile[pattern] }
  end

  self
end

#sort_by_sizeObject Also known as: sort_by_count



87
88
89
90
91
92
93
94
# File 'lib/allocation_stats/allocations_proxy.rb', line 87

def sort_by_size
  @mappers << Proc.new do |allocations|
    allocations.sort_by { |key, value| -value.size }
               .inject({}) { |hash, pair| hash[pair[0]] = pair[1]; hash }
  end

  self
end

#to_aObject Also known as: all

Apply all transformations to the contained list of Allocations. This is aliased as :all.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/allocation_stats/allocations_proxy.rb', line 55

def to_a
  results = @allocations

  @wheres.each do |where|
    results = where.call(results)
  end

  # First apply group_by
  results = @group_by.call(results) if @group_by

  # Apply each mapper
  @mappers.each do |mapper|
    results = mapper.call(results)
  end

  results
end

#to_jsonObject

Resolve all transformations, and convert the resultant Array to JSON.



257
258
259
# File 'lib/allocation_stats/allocations_proxy.rb', line 257

def to_json
  to_a.to_json
end

#to_text(columns: DEFAULT_COLUMNS) ⇒ String

Resolve the AllocationsProxy (by calling #to_a) and return tabular information about the Allocations as a String.

Parameters:

  • columns (Array<Symbol>) (defaults to: DEFAULT_COLUMNS)

    a list of columns to print out

Returns:

  • (String)

    information about the Allocations, in a tabular format



243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/allocation_stats/allocations_proxy.rb', line 243

def to_text(columns: DEFAULT_COLUMNS)
  resolved = to_a

  # if resolved is an Array of Allocations
  if resolved.is_a?(Array) && resolved.first.is_a?(Allocation)
    to_text_from_plain(resolved, columns: columns)

  # if resolved is a Hash (was grouped)
  elsif resolved.is_a?(Hash)
    to_text_from_groups(resolved)
  end
end

#where(hash) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/allocation_stats/allocations_proxy.rb', line 177

def where(hash)
  @wheres << Proc.new do |allocations|
    conditions = hash.inject({}) do |h, pair|
      faux, value = *pair
      getter = attribute_getters([faux]).first
      h.merge(getter => value)
    end

    allocations.select do |allocation|
      conditions.all? { |getter, value| getter.call(allocation) == value }
    end
  end

  self
end