File: entry_formatter.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 (45 lines) | stat: -rw-r--r-- 1,105 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
# frozen_string_literal: true

require 'stringio'
require 'benchmark/memory/helpers'
require 'benchmark/memory/job/io_output/metric_formatter'

module Benchmark
  module Memory
    class Job
      class IOOutput
        # Format entries for use with the IOOutput.
        class EntryFormatter
          include Helpers

          # Instantiate a formatter to output an entry into an IO.
          #
          # @param entry [Entry] The entry to format.
          def initialize(entry)
            @entry = entry
          end

          # @return [Entry] The entry to format.
          attr_reader :entry

          # Format entry to a string to put on the output.
          #
          # @return [String]
          def to_s
            output = StringIO.new
            output << rjust(entry.label)

            first, *rest = *entry.measurement

            output << "#{MetricFormatter.new(first)}\n"
            rest.each do |metric|
              output << "#{' ' * 20}#{MetricFormatter.new(metric)}\n"
            end

            output.string
          end
        end
      end
    end
  end
end