File: measurement_serializer.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 (39 lines) | stat: -rw-r--r-- 1,149 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
# frozen_string_literal: true

require 'benchmark/memory/held_results/serializer'
require 'benchmark/memory/held_results/metric_serializer'
require 'benchmark/memory/measurement'

module Benchmark
  module Memory
    class HeldResults
      # Serialize measurements for holding between runs.
      class MeasurementSerializer < Serializer
        # Convert a JSON hash into a Measurement.
        #
        # @param hash [Hash] A JSON document hash.
        #
        # @return [Measurement]
        def load(hash)
          @object = Measurement.new(
            memory: MetricSerializer.load(hash['memory']),
            objects: MetricSerializer.load(hash['objects']),
            strings: MetricSerializer.load(hash['strings'])
          )
          self
        end

        # Convert the measurement to a Hash.
        #
        # @return [Hash] The measurement as a Hash.
        def to_h
          {
            memory: MetricSerializer.new(object.memory).to_h,
            objects: MetricSerializer.new(object.objects).to_h,
            strings: MetricSerializer.new(object.strings).to_h
          }
        end
      end
    end
  end
end