class ValueAggregate
Aggregates memory allocations by value. Groups allocations by their actual values (e.g., string contents) and creates sub-aggregates.
Definitions
def initialize(title, &block)
Initialize a new value aggregate.
Signature
-
parameter
titleString The title for this aggregate.
-
parameter
blockBlock A block that extracts the metric from an allocation.
Implementation
def initialize(title, &block)
@title = title
@metric = block
@aggregates = Hash.new{|h,k| h[k] = Aggregate.new(safe_key(k.inspect), &@metric)}
end
def <<(allocation)
Add an allocation to this value aggregate.
Signature
-
parameter
allocationAllocation The allocation to add.
Implementation
def << allocation
if value = allocation.value
aggregate = @aggregates[value]
aggregate << allocation
end
end
def aggregates_by(key)
Sort aggregates by a given key.
Signature
-
parameter
keySymbol The key to sort by (e.g., :memory or :count).
-
returns
Array Sorted array of [value, aggregate] pairs.
Implementation
def aggregates_by(key)
@aggregates.sort_by{|value, aggregate| [aggregate.total[key], value]}
end
def print(io = $stderr, limit: 10, level: 2)
Print this value aggregate to an IO stream.
Signature
-
parameter
ioIO The output stream to write to.
-
parameter
limitInteger Maximum number of items to display.
-
parameter
levelInteger Markdown heading level for output.
Implementation
def print(io = $stderr, limit: 10, level: 2)
io.puts "#{'#' * level} #{@title}", nil
aggregates_by(:count).last(limit).reverse_each do |value, aggregate|
aggregate.print(io, level: level+1)
end
end
def as_json(options = nil)
Convert this value aggregate to a JSON-compatible hash.
Signature
-
parameter
optionsHash | Nil Optional JSON serialization options.
-
returns
Hash JSON-compatible representation.
Implementation
def as_json(options = nil)
result = {
title: @title,
aggregates: @aggregates.map{|k, v| [safe_key(k), v.as_json]}
}
end