Skip to content

Commit 153aa96

Browse files
committed
Skip per-key metric lookups in active_experiments when no metrics exist
1 parent c44a8ab commit 153aa96

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

lib/split/user.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def cleanup_old_versions!(experiment)
5656

5757
def active_experiments
5858
experiments_by_key = keys_without_finished(user.keys).each_with_object({}) do |key, memo|
59-
memo[key] = Metric.possible_experiments(key_without_version(key))
59+
memo[key] = experiments_for(key_without_version(key))
6060
end
6161

6262
names = experiments_by_key.values.flatten.map(&:name).uniq
@@ -95,5 +95,19 @@ def keys_without_finished(keys)
9595
def key_without_version(key)
9696
key.split(/\:\d(?!\:)/)[0]
9797
end
98+
99+
def experiments_for(name)
100+
if metrics_defined?
101+
Metric.possible_experiments(name)
102+
else
103+
Array(Experiment.find(name))
104+
end
105+
end
106+
107+
def metrics_defined?
108+
return @metrics_defined if defined?(@metrics_defined)
109+
@metrics_defined =
110+
Split.configuration.metrics.any? || Split.redis.exists?(:metrics)
111+
end
98112
end
99113
end

spec/user_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,18 @@
137137
expect(Split.redis).to receive(:hmget).with(:experiment_winner, any_args).once.and_call_original
138138
@subject.active_experiments
139139
end
140+
141+
it "resolves experiments directly, without per-key metric lookups, when no metrics are defined" do
142+
expect(Split::Metric).not_to receive(:possible_experiments)
143+
expect(@subject.active_experiments).to eq("active" => "red")
144+
end
145+
146+
it "still honors a metric persisted only in Redis" do
147+
Split::Metric.new(name: :conversion, experiments: [Split::ExperimentCatalog.find("active")]).save
148+
149+
expect(Split::Metric).to receive(:possible_experiments).at_least(:once).and_call_original
150+
expect(@subject.active_experiments).to eq("active" => "red")
151+
end
140152
end
141153

142154
context "allows user to be loaded from adapter" do

0 commit comments

Comments
 (0)