Class: B::ConsoleWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/b/output_plugins.rb

Overview

print results in human friendly tabular format

usage hints:

  • set :multiply=>1 if you want output in seconds instead of milliseconds

  • increase :column_width if you see indendation issues with wide values

  • increase :max_label_width if you want to see more of your labels

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ ConsoleWriter

Returns a new instance of ConsoleWriter.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/b/output_plugins.rb', line 60

def initialize(opts={})
  @opts = opts = { :out => $stderr, :multiply => 1000, :round => 2, 
                   :column_width => 11, :max_label_width => 20 }.merge(opts)

  @out = opts[:out]
  @max_label_width = opts[:max_label_width]

  @columns = [
    # C_ID    C_LABEL          C_WIDTH              C_ROUND       C_MUL
    [:id,     '',                               -1,          nil,             nil],
    [:rounds, 'rounds',                         -1,            0,               0],
    [:rate,   'r/s',           opts[:column_width], opts[:round],               1],
    [:mean,   'mean',          opts[:column_width], opts[:round], opts[:multiply]],
    [:max,    'max',           opts[:column_width], opts[:round], opts[:multiply]],
    [:min,    'min',           opts[:column_width], opts[:round], opts[:multiply]],
    [:stddev, "\u00b1 stddev", opts[:column_width], opts[:round], opts[:multiply]]
  ]

end

Instance Method Details

#finish(job) ⇒ Object



111
112
113
114
115
116
117
118
119
# File 'lib/b/output_plugins.rb', line 111

def finish(job)
  @columns.each_with_index do |col, i|
    next if 2 > i # first two columns were already printed in start()
    value = job.send(col[C_ID]) * col[C_MUL]

    printf "%#{col[C_WIDTH]}.#{col[C_ROUND]}f ", value
  end
  printf "\n"
end

#register(job) ⇒ Object



80
81
82
83
84
85
# File 'lib/b/output_plugins.rb', line 80

def register(job)
  # adjust width of first column (label) to fit longest label
  @columns[0][C_WIDTH] = [@columns[0][C_WIDTH], [job.id.length+2,@max_label_width].min].max
  # adjust width of second column (rounds) to fit widest value
  @columns[1][C_WIDTH] = [@columns[1][C_WIDTH], @columns[1][C_LABEL].length, job.rounds.to_s.length].max
end

#start(job) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/b/output_plugins.rb', line 87

def start(job)
  if @header_printed.nil?
    @header_printed = true

    # add :x column if this is a comparison
    if job.compare
      @columns << [:x, "x #{job.compare}", @opts[:column_width], @opts[:round], 1]
    end

    # print header
    header = '-' * (@columns.transpose[C_WIDTH].reduce(&:+) + @columns.length - 1)
    header[2..3+job.group.length] = " #{job.group} "
    printf header + "\n"
    @columns.each_with_index do |col, i|
      printf col[C_LABEL].rjust(col[C_WIDTH]) + ' '
    end
    printf "\n"
  end
  # print job.label
  printf "#{job.id[0..@columns[0][C_WIDTH]-1].ljust(@columns[0][C_WIDTH])} "
  # print rounds
  printf "%#{@columns[1][C_WIDTH]}d ", job.send(:rounds)
end