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.2.2"

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
# File 'lib/benchmark/sweet.rb', line 91

def self.to_table(arr)
  field_sizes = Hash.new
  arr.each { |row| field_sizes.merge!(row => row.map { |iterand| iterand[1].to_s.gsub(/\e\[[^m]+m/, '').length } ) }

  column_sizes = arr.reduce([]) do |lengths, row|
    row.each_with_index.map { |iterand, index| [lengths[index] || 0, field_sizes[row][index]].max }
  end

  format = column_sizes.collect {|n| "%#{n}s" }.join(" | ")
  format += "\n"

  printf format, *arr[0].each_with_index.map { |el, i| " "*(column_sizes[i] - field_sizes[arr[0]][i] ) + el[0].to_s }

  printf format, *column_sizes.collect { |w| "-" * w }

  arr[0..arr.count].each do |line|
    printf format, *line.each_with_index.map { |el, i| " "*(column_sizes[i] - field_sizes[line][i] ) + el[1].to_s }
  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