Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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