Last active
August 29, 2015 14:07
-
-
Save marcgg/bb10ba6d80bf598ccd38 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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