Permalink
Browse files

Significant optimization of new experiments (which only affects the f…

…irst request, but boy, does it affect it!): stopped the n+1 inserts required to start a new experiment.
  • Loading branch information...
1 parent 3af0511 commit 6bc698afd3ef388284fabad10e2719daa548fd36 @patio11 patio11 committed Aug 19, 2009
Showing with 12 additions and 9 deletions.
  1. +12 −9 lib/abingo/experiment.rb
@@ -49,16 +49,19 @@ def self.alternatives_for_test(test_name)
def self.start_experiment!(test_name, alternatives_array)
cloned_alternatives_array = alternatives_array.clone
- experiment = Abingo::Experiment.find_or_create_by_test_name(test_name)
- while (cloned_alternatives_array.size > 0)
- alt = cloned_alternatives_array[0]
- weight = cloned_alternatives_array.size - (cloned_alternatives_array - [alt]).size
- experiment.alternatives.create(:content => alt, :weight => weight,
- :lookup => Abingo::Alternative.calculate_lookup(test_name, alt))
- cloned_alternatives_array -= [alt]
+ ActiveRecord::Base.transaction do
+ experiment = Abingo::Experiment.new(:test_name => test_name)
+ while (cloned_alternatives_array.size > 0)
+ alt = cloned_alternatives_array[0]
+ weight = cloned_alternatives_array.size - (cloned_alternatives_array - [alt]).size
+ experiment.alternatives.build(:content => alt, :weight => weight,
+ :lookup => Abingo::Alternative.calculate_lookup(test_name, alt))
+ cloned_alternatives_array -= [alt]
+ end
+ experiment.save!
+ Abingo.cache.write("Abingo::Experiment::exists(#{test_name})".gsub(" ", ""), 1)
+ experiment
end
- Abingo.cache.write("Abingo::Experiment::exists(#{test_name})".gsub(" ", ""), 1)
- experiment
end
end

0 comments on commit 6bc698a

Please sign in to comment.