Class: Benchmark::Memory::Measurement

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Comparable, Enumerable
Defined in:
lib/benchmark/memory/measurement.rb,
lib/benchmark/memory/measurement/metric.rb

Overview

Encapsulate the combined metrics of an action.

Defined Under Namespace

Classes: Metric

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(memory:, objects:, strings:) ⇒ Measurement

Instantiate a Measurement of memory usage.

Parameters:

  • memory (Metric)

    The memory usage of an action.

  • objects (Metric)

    The object allocations of an action.

  • strings (Metric)

    The string allocations of an action.



41
42
43
44
45
46
# File 'lib/benchmark/memory/measurement.rb', line 41

def initialize(memory:, objects:, strings:)
  @memory = memory
  @objects = objects
  @strings = strings
  @metrics = [@memory, @objects, @strings]
end

Instance Attribute Details

#memoryMetric (readonly)

Returns The memory allocation metric.

Returns:

  • (Metric)

    The memory allocation metric.



49
50
51
# File 'lib/benchmark/memory/measurement.rb', line 49

def memory
  @memory
end

#metricsArray<Metric> (readonly)

Returns The metrics for the measurement.

Returns:

  • (Array<Metric>)

    The metrics for the measurement.



52
53
54
# File 'lib/benchmark/memory/measurement.rb', line 52

def metrics
  @metrics
end

#objectsMetric (readonly)

Returns The object allocation metric.

Returns:

  • (Metric)

    The object allocation metric.



55
56
57
# File 'lib/benchmark/memory/measurement.rb', line 55

def objects
  @objects
end

#stringsMetric (readonly)

Returns The string allocation metric.

Returns:

  • (Metric)

    The string allocation metric.



58
59
60
# File 'lib/benchmark/memory/measurement.rb', line 58

def strings
  @strings
end

Class Method Details

.from_result(result) ⇒ Object

Create a Measurement from a MemoryProfiler::Results object.

Parameters:

  • result (MemoryProfiler::Results)

    The results of a MemoryProfiler report.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/benchmark/memory/measurement.rb', line 16

def self.from_result(result)
  memory = Metric.new(
    :memsize,
    result.total_allocated_memsize,
    result.total_retained_memsize
  )
  objects = Metric.new(
    :objects,
    result.total_allocated,
    result.total_retained
  )
  strings = Metric.new(
    :strings,
    result.strings_allocated.size,
    result.strings_retained.size
  )

  new(:memory => memory, :objects => objects, :strings => strings)
end

Instance Method Details

#<=>(other) ⇒ Integer

Compare two measurements for sorting purposes.

Parameters:

Returns:

  • (Integer)


68
69
70
# File 'lib/benchmark/memory/measurement.rb', line 68

def <=>(other)
  memory <=> other.memory
end

#allocated_memoryInteger

Total amount of allocated memory for the measurement.

Returns:

  • (Integer)


75
76
77
# File 'lib/benchmark/memory/measurement.rb', line 75

def allocated_memory
  memory.allocated
end