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.



1168
1169
1170
1171
1172
# File 'lib/bullshit.rb', line 1168

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.



1229
1230
1231
# File 'lib/bullshit.rb', line 1229

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.



1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
# File 'lib/bullshit.rb', line 1179

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) ⇒ Object

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



1233
1234
1235
# File 'lib/bullshit.rb', line 1233

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

#displayObject

Output all speed comparisons between methods.



1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
# File 'lib/bullshit.rb', line 1249

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_sizeObject

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



1238
1239
1240
# File 'lib/bullshit.rb', line 1238

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.



1162
1163
1164
1165
# File 'lib/bullshit.rb', line 1162

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.



1243
1244
1245
1246
# File 'lib/bullshit.rb', line 1243

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