#!/usr/bin/env ruby
#
# Copyright 2013-present Greg Hurrell. All rights reserved.
# Licensed under the terms of the BSD 2-clause license.

lib  = File.expand_path('../../ruby', File.dirname(__FILE__))
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)

require 'command-t/ext'
require 'command-t/util'
require 'benchmark'
require 'ostruct'
require 'yaml'

yaml    = File.expand_path('../../data/benchmark.yml', File.dirname(__FILE__))
data    = YAML.load_file(yaml)
threads = CommandT::Util.processor_count

puts "Starting benchmark run (PID: #{Process.pid})"

Benchmark.bmbm do |b|
  data['tests'].each do |test|
    scanner = OpenStruct.new(:paths => test['paths'])
    matcher = CommandT::Matcher.new(scanner)
    b.report(test['name']) do
      test['times'].times do
        test['queries'].each do |query|
          matcher.sorted_matches_for(
            query,
            :threads => threads,
            :recurse => ENV.fetch('RECURSE', '1') == '1'
          )
        end
      end
    end
  end
end

#!/usr/bin/env ruby
#
# Copyright 2013-present Greg Hurrell. All rights reserved.
# Licensed under the terms of the BSD 2-clause license.

lib  = File.expand_path('../../ruby', File.dirname(__FILE__))
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)

require 'command-t/ext'
require 'command-t/util'
require 'benchmark'
require 'ostruct'
require 'yaml'

yaml    = File.expand_path('../../data/benchmark.yml', File.dirname(__FILE__))
data    = YAML.load_file(yaml)
threads = CommandT::Util.processor_count

puts "Starting benchmark run (PID: #{Process.pid})"

Benchmark.bmbm do |b|
  data['tests'].each do |test|
    scanner = OpenStruct.new(:paths => test['paths'])
    matcher = CommandT::Matcher.new(scanner)
    b.report(test['name']) do
      test['times'].times do
        test['queries'].each do |query|
          query.split(//).reduce('') do |acc, char|
            query = acc + char
            matcher.sorted_matches_for(
              query,
              :threads => threads,
              :recurse => ENV.fetch('RECURSE', '1') == '1'
            )
            query
          end
        end
      end
    end
  end
end

Set new baseline for benchmark metrics
Set new baseline for benchmark metrics

Make the benchmarks more closely resemble real world usage. Users don't
search for "foobar" but rather "f", then "fo", then "foo" etc (unless
they have debouncing turned on).

I have some optimizations planned to handle exactly this case, so I'm
adjusting the benchmark so that I can measure the impact of the change.

I could have tweaked the `times` value of any of the benchmarks except
for "chromium (whole)" (which is already at 1), but I figured they're
all shorter anyway so they may as well just stay as they are.

New baseline:

                          user     system      total        real
  pathological        3.640000   0.090000   3.730000 (  3.805231)
  command-t           2.170000   0.030000   2.200000 (  2.209098)
  chromium (subset)   2.600000   0.220000   2.820000 (  1.626648)
  chromium (whole)   12.340000   0.060000  12.400000 (  7.209598)
