Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

Commit

Permalink
Significant optimization of new experiments (which only affects the f…
Browse files Browse the repository at this point in the history
…irst request, but boy, does it affect it!): stopped the n+1 inserts required to start a new experiment.
  • Loading branch information
patio11 committed Aug 19, 2009
1 parent 3af0511 commit 6bc698a
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions lib/abingo/experiment.rb
Expand Up @@ -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.