Skip to content

Instantly share code, notes, and snippets.

@marcgg
Last active August 29, 2015 14:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcgg/bb10ba6d80bf598ccd38 to your computer and use it in GitHub Desktop.
Save marcgg/bb10ba6d80bf598ccd38 to your computer and use it in GitHub Desktop.
require 'benchmark'
ActiveRecord::Base.logger = nil
ActiveRecord::Schema.verbose = false
ActiveRecord::Migration.verbose = false
# PARAMS
sql = ""
attributes = []
table = ""
min_permutations = 1
max_permutations = 3
repeat_for_bench = 100
# SETTING ALL COMBINATIONS
possibilities = []
(min_permutations..max_permutations).each do |i|
possibilities += attributes.repeated_permutation(i).to_a
end
possibilities.select! do |perm|
perm.map{|v| perm.grep(v).size}.inject(&:+) == perm.size
end
# BENCHMARK
puts "We have #{possibilities.count} possibilities"
possibilities.each_with_index do |perm, i|
index_name = "madness_test_#{i}"
begin
ActiveRecord::Migration.add_index(table, perm, name: index_name)
r = Benchmark.measure {
repeat_for_bench.times{ ActiveRecord::Base.connection.execute(sql) }
}
ActiveRecord::Migration.remove_index(table, name: index_name)
puts [i, perm.inspect, r].join(",")
rescue Exception => e
puts [i, perm.inspect, "#{e.class} - #{e.message}"].join(",")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment