Class: Bullshit::Comparison

Inherits:
Object
  • Object
show all
Extended by:
DSLKit::Constant, DSLKit::DSLAccessor
Includes:
CommonConstants
Defined in:
lib/bullshit.rb

Overview

A Comparison instance compares the benchmark results of different case methods and outputs the results.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Comparison

Return a comparison object configured by block.


2116
2117
2118
2119
2120
# File 'lib/bullshit.rb', line 2116

def initialize(&block)
  @cases = {}
  @benchmark_methods = []
  block and instance_eval(&block)
end

Instance Attribute Details

#benchmark_methodsObject (readonly)

Return all benchmark methods for all the cached benchmark cases.


2172
2173
2174
# File 'lib/bullshit.rb', line 2172

def benchmark_methods
  @benchmark_methods
end

Instance Method Details

#benchmark(bc_class, method, opts = {}) ⇒ Object

Benchmark case method method of bc_class. Options are:

  • :run to not run this bc_class if set to false (defaults to true),

  • :load to load the data of a previous run for this method, if set to true. If the true value is a file path string, load the data from the given file at the path.


2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
# File 'lib/bullshit.rb', line 2127

def benchmark(bc_class, method, opts = {})
  opts = { :run => true, :combine => true }.merge opts
  if Case === bc_class
    bullshit_case, bc_class = bc_class, bullshit_case.class
    @cases[bc_class] ||= []
    if opts[:combine]
      if @cases[bc_class].empty?
        @cases[bc_class] << bullshit_case
      else
        bullshit_case = @cases[bc_class].first
      end
    else
      @cases[bc_class] << bullshit_case
    end
  else
    @cases[bc_class] ||= []
    if opts[:combine]
      unless bullshit_case = @cases[bc_class].first
        bullshit_case = bc_class.new
        @cases[bc_class] << bullshit_case
      end
    else
      bullshit_case = bc_class.new
      @cases[bc_class] << bullshit_case
    end
  end
  bc_method = bullshit_case[method] or raise BullshitException,
    "unknown benchmark method #{bc_class}##{method}"
  if comment = opts[:comment]
    bc_method.comment = comment
  end
  @benchmark_methods << bc_method
  if file_path = opts[:load]
    if file_path != true
      bc_method.load file_path
    else
      bc_method.load
    end
  else
    opts[:run] and bullshit_case.run false
  end
  nil
end

#compare_methods(comparator) ⇒ Object

Return all benchmark methods ordered by the result of comparator call to their clock values.


2176
2177
2178
# File 'lib/bullshit.rb', line 2176

def compare_methods(comparator)
  benchmark_methods.sort_by { |m| m.clock.__send__(comparator) }
end

#displayObject

Output all speed comparisons between methods.


2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
# File 'lib/bullshit.rb', line 2192

def display
  output.puts Time.now.strftime(' %FT%T %Z ').center(COLUMNS, '=')
  for comparator in [ :call_time_mean, :call_time_median ]
    output.puts
    methods = compare_methods(comparator)
    methods.size < 2 and return
    max = methods.last.clock.__send__(comparator)
    output.puts "Comparing times (#{comparator}):"
    methods.each_with_index do |m, i|
      covers = []
      for x in methods
        if m != x and m.cover?(x)
          j = 0
          if methods.find { |y| j += 1; x == y }
            my_case = m.case.class
            iterations = m.clock.analysis[my_case.compare_time].suggested_sample_size(
              x.clock.analysis[my_case.compare_time], my_case.covering.alpha_level, my_case.covering.beta_level)
            if iterations.nan? or iterations.infinite?
              covers << "#{j} (?)"
            else
              min_iterations = iterations.ceil
              min_iterations >= 1E6 and min_iterations = "%f" % (1 / 0.0)
              covers << "#{j} (>=#{min_iterations})"
            end
          end
        end
      end
      covers *= ', '
      output.printf\
        "% 2u #{prefix_string(m)}\n   %17.9f"\
        " (%#{::Bullshit::Clock::TIMES_MAX}s) -> %8.3fx %s\n"\
        "   %17.9f\n",
        i + 1, m.clock.calls(comparator), m.case.class.compare_time,
        max / m.clock.__send__(comparator), covers, m.clock.__send__(comparator)
    end
    output.puts "   %17s (%#{::Bullshit::Clock::TIMES_MAX}s) -> %8s  %s\n"\
                "   %17s\n"\
                % %w[calls/sec time speed covers secs/call]
  end
  output.puts '=' * COLUMNS
end

#longest_name_sizeObject

Return the length of the longest name of all benchmark methods.


2181
2182
2183
# File 'lib/bullshit.rb', line 2181

def longest_name_size
  benchmark_methods.map { |m| m.long_name.size }.max
end

#output_filename(name) ⇒ Object

Output results to the file named name.


2110
2111
2112
2113
# File 'lib/bullshit.rb', line 2110

def output_filename(name)
  path = File.expand_path(name, output_dir)
  output File.new(path, 'a+')
end

#prefix_string(method) ⇒ Object

Returns the prefix_string for a method speed comparison in the output.


2186
2187
2188
2189
# File 'lib/bullshit.rb', line 2186

def prefix_string(method)
  "% -#{longest_name_size}s %u repeats:" %
    [ method.long_name , method.clock.repeat ]
end