$:.unshift File.dirname(__FILE__) + '/../lib'
%w( ambition rubygems active_record benchmark ).each { |f| require f }

class User < ActiveRecord::Base
  def self.reflections
    return @reflections if @reflections
    @reflections = {}
    @reflections[:ideas]    = Reflection.new(:has_many,   'user_id',     :ideas,   'ideas')
    @reflections[:invites]  = Reflection.new(:has_many,   'referrer_id', :invites, 'invites')
    @reflections[:profile]  = Reflection.new(:has_one,    'user_id',     :profile, 'profiles')
    @reflections[:account]  = Reflection.new(:belongs_to, 'account_id',  :account, 'accounts')
    @reflections
  end

  def self.table_name
    'users'
  end
end

class Reflection < Struct.new(:macro, :primary_key_name, :name, :table_name)
end

Times = 10000

Benchmark.bm(30) do |x|
  x.report 'simple select' do
    Times.times do
      User.select { |u| u.id == 20 }.to_hash
    end
  end

  x.report 'dual select' do
    Times.times do
      User.select { |u| u.id == 20 && u.age > 20 }.to_hash
    end
  end

  x.report 'join select' do
    Times.times do
      User.select { |u| u.id == 20 && u.ideas.name =~ /stuff/ }.to_hash
    end
  end

  x.report 'dual select w/ sort' do
    Times.times do
      User.select { |u| u.id == 20 && u.age > 20 }.sort_by { |u| u.id }.to_hash
    end
  end

  x.report 'dual select w/ stuff' do
    Times.times do
      User.select { |u| u.id == 20 && u.age > 20 }.sort_by { |u| u.id }.first(20).to_hash
    end
  end

  x.report "it's complicated" do
    Times.times do
      User.select { |u| (u.id == 20 && u.age > 20) || u.profile.name == 'Jon' }.sort_by { |u| [u.id, -u.name] }.first(20).to_hash
    end
  end
end


=begin
                            user     system      total        real
  1st instantiation:    0.000000   0.000000   0.000000 (  0.005466)
  2nd instantiation:    0.000000   0.000000   0.000000 (  0.000108)
  Local ordering:       0.000000   0.000000   0.000000 (  0.000265)
  Advanced ordering:    0.000000   0.000000   0.000000 (  0.000413)
  Local conditions:     0.000000   0.000000   0.000000 (  0.000241)
  Advanced conditions:  0.000000   0.000000   0.000000 (  0.000602)
  Its complicated:      0.000000   0.000000   0.000000 (  0.001017)
=end