File: measurement.rb

package info (click to toggle)
ruby-benchmark-memory 0.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 300 kB
  • sloc: ruby: 1,121; makefile: 7; sh: 4
file content (53 lines) | stat: -rw-r--r-- 1,639 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
# frozen_string_literal: true

require 'forwardable'
require 'benchmark/memory/measurement/metric_extractor'

module Benchmark
  module Memory
    # Encapsulate the combined metrics of an action.
    class Measurement
      include Enumerable
      extend Forwardable

      # Create a Measurement from a MemoryProfiler::Results object.
      #
      # @param result [MemoryProfiler::Results]
      #   The results of a MemoryProfiler report.
      def self.from_result(result)
        memory = MetricExtractor.extract_memory(result)
        objects = MetricExtractor.extract_objects(result)
        strings = MetricExtractor.extract_strings(result)

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

      # Instantiate a Measurement of memory usage.
      #
      # @param memory [Metric] The memory usage of an action.
      # @param objects [Metric] The object allocations of an action.
      # @param strings [Metric] The string allocations of an action.
      def initialize(memory:, objects:, strings:)
        @memory = memory
        @objects = objects
        @strings = strings
        @metrics = [@memory, @objects, @strings]
      end

      # @return [Metric] The memory allocation metric.
      attr_reader :memory

      # @return [Array<Metric>] The metrics for the measurement.
      attr_reader :metrics

      # @return [Metric] The object allocation metric.
      attr_reader :objects

      # @return [Metric] The string allocation metric.
      attr_reader :strings

      # Enumerate through the metrics when enumerating a measurement.
      def_delegator :metrics, :each
    end
  end
end