Module: Benchmark::Sweet

Included in:
Benchmark
Defined in:
lib/benchmark/sweet.rb,
lib/benchmark/sweet/ips.rb,
lib/benchmark/sweet/job.rb,
lib/benchmark/sweet/item.rb,
lib/benchmark/sweet/memory.rb,
lib/benchmark/sweet/queries.rb,
lib/benchmark/sweet/version.rb,
lib/benchmark/sweet/comparison.rb

Defined Under Namespace

Modules: IPS, Memory, Queries Classes: Comparison, Item, Job

Constant Summary collapse

VERSION =
"0.3.0"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.group(base, grouping, sort: false, &block) ⇒ Object

Parameters:

  • base (Array<Comparison>)

    ] array of comparisons

  • grouping (Symbol|Array<Symbol>|Proc)

    Proc passed to group_by to partition records. Accepts Comparison, returns an object to partition. returns nil to filter from the list



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/benchmark/sweet.rb', line 35

def self.group(base, grouping, sort: false, &block)
  if grouping.nil?
    yield nil, base
    return
  end

  grouping = symbol_to_proc(grouping)

  label_records = base.group_by(&grouping).select { |value, _comparisons| !value.nil? }
  label_records = label_records.sort_by(&:first) if sort

  label_records.each(&block)
end


86
87
88
89
# File 'lib/benchmark/sweet.rb', line 86

def self.print_table(header_name, header_value, table_rows)
  puts "", "#{header_name} #{header_value}", "" if header_value
  to_table(table_rows)
end

.symbol_to_proc(field, join: "_") ⇒ Object

proc produced: -> (comparison) { comparison.method }



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/benchmark/sweet.rb', line 70

def self.symbol_to_proc(field, join: "_")
  if field.kind_of?(Symbol) || field.kind_of?(String)
    field_name = field
    -> v { v[field_name] }
  elsif field.kind_of?(Array)
    field_names = field
    if join
      -> v { field_names.map { |gn| v[gn].to_s }.join(join) }
    else
      -> v { field_names.map { |gn| v[gn] } }
    end
  else
    field
  end
end

.table(base, grouping: nil, sort: false, row: :label, column: :metric, value: :comp_short) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/benchmark/sweet.rb', line 49

def self.table(base, grouping: nil, sort: false, row: :label, column: :metric, value: :comp_short)
  header_name = grouping.respond_to?(:call) ? "grouping" : grouping
  column = symbol_to_proc(column)
  value = symbol_to_proc(value)

  group(base, grouping, sort: true) do |header_value, table_comparisons|
    row_key = row.kind_of?(Symbol) || row.kind_of?(String) ? row : "label"
    table_rows = group(table_comparisons, row, sort: sort).map do |row_header, row_comparisons|
      row_comparisons.each_with_object({row_key => row_header}) do |comparison, row_data|
        row_data[column.call(comparison)] = value.call(comparison)
      end
    end
    if block_given?
      yield header_value, table_rows
    else
      print_table(header_name, header_value, table_rows)
    end
  end
end

.to_table(arr) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/benchmark/sweet.rb', line 91

def self.to_table(arr)
  return if arr.empty?

  strip_ansi = ->(s) { s.to_s.gsub(/\e\[[^m]*m/, '') }

  # collect headers from the first row's keys
  headers = arr[0].keys

  # compute visible width for each column (max of header and all values)
  column_sizes = headers.each_with_index.map do |header, i|
    values_max = arr.map { |row| strip_ansi.call(row.values[i]).length }.max
    [strip_ansi.call(header).length, values_max].max
  end

  # right-align with ANSI-aware padding
  pad = ->(str, width) {
    visible = strip_ansi.call(str).length
    " " * [width - visible, 0].max + str.to_s
  }

  puts headers.each_with_index.map { |h, i| pad.call(h.to_s, column_sizes[i]) }.join(" | ")
  puts column_sizes.map { |w| "-" * w }.join("-|-")

  arr.each do |row|
    puts row.values.each_with_index.map { |v, i| pad.call(v.to_s, column_sizes[i]) }.join(" | ")
  end
end

Instance Method Details

#items(options = {memory: true, ips: true}) {|job| ... } ⇒ Object

Yields:

  • (job)


12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/benchmark/sweet.rb', line 12

def items(options = {memory: true, ips: true})
  job = ::Benchmark::Sweet::Job.new(options)

  yield job

  job.load_entries
  job.run
  job.save_entries

  job.run_report

  job # both items and entries are useful
end