In Files

Parent

Files

Bullshit::Comparison

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

Attributes

benchmark_methods[R]

Return all benchmark methods for all the cached benchmark cases.

Public Class Methods

new(&block) click to toggle source

Return a comparison object configured by block.

# File lib/bullshit.rb, line 2101
    def initialize(&block)
      @cases = {}
      @benchmark_methods = []
      block and instance_eval(&block)
    end

Public Instance Methods

benchmark(bc_class, method, opts = {}) click to toggle source

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.
# File lib/bullshit.rb, line 2112
    def benchmark(bc_class, method, opts = {})
      opts = { :run => true, :combine => true }.merge opts
      if Case === bc_class
        bullshit_case, bc_class = bc_class, bc_class.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
      if file_path = opts[:load]
        success = if file_path != true
          bc_method.load(file_path)
        else
          bc_method.load
        end
        if success
          @benchmark_methods << bc_method
        else
          warn "Loading of #{bc_method} failed. Skipping to next."
        end
      else
        opts[:run] and bullshit_case.run false
        @benchmark_methods << bc_method
      end
      nil
    end
compare_methods(comparator) click to toggle source

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

# File lib/bullshit.rb, line 2166
    def compare_methods(comparator)
      benchmark_methods.sort_by { |m| m.clock.__send__(comparator) }
    end
display() click to toggle source

Output all speed comparisons between methods.

# File lib/bullshit.rb, line 2182
    def display
      output.puts Time.now.strftime(' %FT%T %Z ').center(COLUMNS, '=')
      for comparator in [ :call_time_mean, :call_time_median ]
        output.puts
        cmethods = compare_methods(comparator)
        cmethods.size < 2 and return
        max = cmethods.last.clock.__send__(comparator)
        output.puts "Comparing times (#{comparator}):"
        cmethods.each_with_index do |m, i|
          output.printf\
            "% 2u #{prefix_string(m)}\n   %17.9f"\
            " (%#{::Bullshit::Clock::TIMES_MAX}s) %s\n"\
            "   %17.9f %8.2f %17.9f\n",
            i + 1, m.clock.calls(comparator), m.case.class.compare_time,
            compute_covers(cmethods, m), m.clock.__send__(comparator),
            m.clock.sample_standard_deviation_percentage(m.case.class.compare_time),
            m.clock.sum(m.case.class.compare_time)
        end
        output.puts "   %17s (%#{::Bullshit::Clock::TIMES_MAX}s) %s\n"\
                    "   %17s %8s %17s\n"\
                    % %w[calls/sec time covers secs/call std% sum]
        display_speedup_matrix(cmethods, comparator)
      end
      output.puts '=' * COLUMNS
    end
longest_name_size() click to toggle source

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

# File lib/bullshit.rb, line 2171
    def longest_name_size
      benchmark_methods.map { |m| m.long_name.size }.max
    end
output_filename(name) click to toggle source

Output results to the file named name.

# File lib/bullshit.rb, line 2095
    def output_filename(name)
      path = File.expand_path(name, output_dir)
      output File.new(path, 'a+')
    end
prefix_string(method) click to toggle source

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

# File lib/bullshit.rb, line 2176
    def prefix_string(method)
      "% -#{longest_name_size}s %u repeats:" %
        [ method.long_name , method.clock.repeat ]
    end

Private Instance Methods

compute_covers(cmethods, m) click to toggle source

(Not documented)

# File lib/bullshit.rb, line 2227
    def compute_covers(cmethods, m)
      covers = []
      for x in cmethods
        if m != x and m.cover?(x)
          j = 0
          if cmethods.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 * ', '
    end
display_speedup_matrix(cmethods, comparator) click to toggle source

(Not documented)

# File lib/bullshit.rb, line 2210
    def display_speedup_matrix(cmethods, comparator)
      output.print "\n", " " * 3
      cmethods.size.times do |i|
        output.printf "%7d ", i + 1
      end
      output.puts
      cmethods.each_with_index do |x, i|
        output.printf "%2d ", i + 1
        cmethods.each do |y|
          ratio = x.clock.calls(comparator).to_f / y.clock.calls(comparator)
          ratio /= 0.0 if ratio >= 1000
          output.printf "%7.2f ", ratio
        end
        output.puts
      end
    end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.