Class: Corkscrews::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/corkscrews/controller.rb

Defined Under Namespace

Classes: Round

Constant Summary collapse

SPEEDUPS =
Analysis::SPEEDUPS.freeze
DEFAULT_RANDOM =
Random.new(12_345)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(targets:, random: DEFAULT_RANDOM) ⇒ Controller

Returns a new instance of Controller.



23
24
25
26
27
# File 'lib/corkscrews/controller.rb', line 23

def initialize(targets:, random: DEFAULT_RANDOM)
  @targets = targets
  @random = random
  @round_index = 0
end

Class Method Details

.plan_for(analysis, rounds: 32, random: DEFAULT_RANDOM) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/corkscrews/controller.rb', line 47

def self.plan_for(analysis, rounds: 32, random: DEFAULT_RANDOM)
  aggregate = analysis.aggregate
  controller = new(targets: aggregate[:targets].first(32), random: random)
  progress_count = aggregate[:progress].values.sum { |entry| entry[:count].to_i }
  duration_ns = aggregate[:duration_ns]

  Array.new(rounds) do
    controller.next_round(progress_visits: progress_count, duration_ns: duration_ns)
  end
end

Instance Method Details

#next_round(progress_visits:, duration_ns:, targets: nil, history: []) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/corkscrews/controller.rb', line 29

def next_round(progress_visits:, duration_ns:, targets: nil, history: [])
  active_targets = targets || @targets
  target = pick_target(active_targets, history)
  speedup_pct = pick_speedup(target, history)
  share = target_share(target)
  reduced = share * (speedup_pct.to_f / 100.0)

  Round.new(
    id: next_round_id,
    target: target,
    speedup_pct: speedup_pct,
    baseline: speedup_pct.zero?,
    visits: progress_visits,
    physical_duration_ns: duration_ns,
    virtual_duration_ns: (duration_ns * (1.0 - reduced)).round
  )
end