Class: Dolphin::Experiment

Inherits:
Object
  • Object
show all
Defined in:
lib/dolphin/experiment.rb

Instance Method Summary collapse

Constructor Details

#initialize(name, logger = nil, &block) ⇒ Experiment


4
5
6
7
8
9
# File 'lib/dolphin/experiment.rb', line 4

def initialize(name, logger=nil, &block)
  @name = name
  @logger = logger
  block.call(self)
  @comparison = lambda {|x| x }
end

Instance Method Details

#compare_and_log_data_if(feature_name, &block) ⇒ Object


24
25
26
27
# File 'lib/dolphin/experiment.rb', line 24

def compare_and_log_data_if(feature_name, &block)
  @log_comparison_failures = Dolphin.feature_available?(feature_name)
  @comparison = block if block
end

#existing(&block) ⇒ Object


11
12
13
# File 'lib/dolphin/experiment.rb', line 11

def existing(&block)
  @existing = block
end

#experimental(feature_name, &block) ⇒ Object


15
16
17
18
# File 'lib/dolphin/experiment.rb', line 15

def experimental(feature_name, &block)
  @experimental_feature_name = feature_name
  @experimental = block
end

#runObject


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/dolphin/experiment.rb', line 29

def run
  raise "must define existing implementation" unless @existing
  existing_result = @existing.call
  if Dolphin.feature_available?(@experimental_feature_name)
    begin
      experimental_result           = @experimental.call
      canonical_existing_result     = @comparison.call(existing_result)
      canonical_experimental_result = @comparison.call(experimental_result)
      if @logger and canonical_existing_result != canonical_experimental_result
        extra = nil
        if (@log_comparison_failures)
          extra = ", expected #{canonical_existing_result.inspect} got #{canonical_experimental_result.inspect}"
        end
        @logger.warn("#{@name}: experimental value differs#{extra}")
      end
      @use_experimental_result ? experimental_result : existing_result
    rescue Object => e
      if @use_experimental_result
        raise e
      else
        @logger.error(e)
        existing_result
      end
    end
  else
    existing_result
  end
end

#use_experimental_result?(feature_name) ⇒ Boolean


20
21
22
# File 'lib/dolphin/experiment.rb', line 20

def use_experimental_result?(feature_name)
  @use_experimental_result = Dolphin.feature_available?(feature_name)
end