Class: TestRunner::ResourcePool::CapacitySource

Inherits:
Object
  • Object
show all
Defined in:
lib/test-runner/resource_pool.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_value:, reserve:, detector:, overcommit:, available_detector: nil, monotonic: false) ⇒ CapacitySource

Returns a new instance of CapacitySource.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/test-runner/resource_pool.rb', line 38

def initialize(
  max_value:,
  reserve:,
  detector:,
  overcommit:,
  available_detector: nil,
  monotonic: false
)
  @max_value = max_value
  @reserve = reserve
  @detector = detector
  @overcommit = overcommit
  @initial_available = available_detector&.call
  @monotonic = monotonic
  @initialized = false
  @current_value = nil
end

Instance Attribute Details

#current_value ⇒ Object (readonly)

Returns the value of attribute current_value.



36
37
38
# File 'lib/test-runner/resource_pool.rb', line 36

def current_value
  @current_value
end

#detected ⇒ Object (readonly)

Returns the value of attribute detected.



36
37
38
# File 'lib/test-runner/resource_pool.rb', line 36

def detected
  @detected
end

#initial_available ⇒ Object (readonly)

Returns the value of attribute initial_available.



36
37
38
# File 'lib/test-runner/resource_pool.rb', line 36

def initial_available
  @initial_available
end

#max_value ⇒ Object (readonly)

Returns the value of attribute max_value.



36
37
38
# File 'lib/test-runner/resource_pool.rb', line 36

def max_value
  @max_value
end

#reserve ⇒ Object (readonly)

Returns the value of attribute reserve.



36
37
38
# File 'lib/test-runner/resource_pool.rb', line 36

def reserve
  @reserve
end

Instance Method Details

#current ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/test-runner/resource_pool.rb', line 56

def current
  @detected = @detector.call
  value = ResourcePool.capacity(limit(overcommit(usable_detected)), reserve)

  if @monotonic && @initialized
    value = monotonic_value(value)
  end

  @initialized = true
  @current_value = value
end

#limit(detected) ⇒ Object (protected)



98
99
100
101
102
103
104
105
106
# File 'lib/test-runner/resource_pool.rb', line 98

def limit(detected)
  if detected && max_value
    [detected, max_value].min
  elsif detected
    detected
  else
    max_value
  end
end

#monotonic_value(value) ⇒ Object (protected)



85
86
87
88
89
90
# File 'lib/test-runner/resource_pool.rb', line 85

def monotonic_value(value)
  return current_value if value.nil?
  return value if current_value.nil?

  [current_value, value].min
end

#overcommit(detected) ⇒ Object (protected)



92
93
94
95
96
# File 'lib/test-runner/resource_pool.rb', line 92

def overcommit(detected)
  return nil if detected.nil?

  (detected * @overcommit).floor
end

#preparation_capacity ⇒ Object

Preparation uses the startup sample without runtime overcommit.



69
70
71
72
73
# File 'lib/test-runner/resource_pool.rb', line 69

def preparation_capacity
  return nil if initial_available.nil? && max_value.nil?

  ResourcePool.capacity(limit(usable_detected), reserve)
end

#usable_detected ⇒ Object (protected)



77
78
79
80
81
82
83
# File 'lib/test-runner/resource_pool.rb', line 77

def usable_detected
  if detected && initial_available
    [detected, initial_available].min
  else
    detected || initial_available
  end
end