Skip to content

Commit

Permalink
algorithm tests
Browse files Browse the repository at this point in the history
  • Loading branch information
davidsiaw committed Jan 28, 2021
1 parent c05ae69 commit 31baba0
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
7 changes: 5 additions & 2 deletions app/models/district.rb
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,12 @@ def total_demanded(resource)
def instance_count_demanded(resource)
per_instance = total_registered(resource) / container_instances.count

# naively determine the number of instances needed for each service
# naively determine the number of instances needed for each service.
# this algo gives at worst n + 2 servers where n is the number of types
# of service memory requirements and at best the exact number of instances.
# please see tests for details.
demand_structure(resource).map do |k, v|
(k / 1699.to_f * v).ceil
(k / per_instance.to_f * v).ceil + 1
end.sum
end

Expand Down
48 changes: 48 additions & 0 deletions spec/models/district_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,52 @@
district.publish_sns("message")
end
end

describe '#instances_recommended' do
it 'gives the maximum from cpu and memory requirements' do
allow(district).to receive(:instance_count_demanded).with(:cpu) { 100 }
allow(district).to receive(:instance_count_demanded).with(:memory) { 10 }

expect(district.send(:instances_recommended)).to eq 100
end
end

describe '#instance_count_demanded' do
before do
# set some constants
allow(district).to receive(:container_instances) { [1] }
allow(district).to receive(:total_registered) { 1000 }
end

it 'gives 1 more server than required if we have only 1 service type with exact occupancy' do
allow(district).to receive(:demand_structure) { { 1000 => 3 } }

expect(district.send(:instance_count_demanded, :something)).to eq 4
end

it 'gives one more server required if we have 1 service type with less than half occupancy' do
allow(district).to receive(:demand_structure) { { 400 => 3 } }

expect(district.send(:instance_count_demanded, :something)).to eq 3
end

it 'gives exactly the number of servers required if we have 1 service type with more than half occupancy' do
allow(district).to receive(:demand_structure) { { 600 => 3 } }

expect(district.send(:instance_count_demanded, :something)).to eq 3
end

it 'gives two more servers than required if we have 1 service type with less than half occupancy and a minor type' do
allow(district).to receive(:demand_structure) { { 400 => 3, 100 => 3 } }

expect(district.send(:instance_count_demanded, :something)).to eq 5
end

it 'gives two more servers than required if we have 1 service type with more than half occupancy and a minor type' do
allow(district).to receive(:demand_structure) { { 600 => 3, 100 => 3 } }

expect(district.send(:instance_count_demanded, :something)).to eq 5
end

end
end

0 comments on commit 31baba0

Please sign in to comment.