Class: Binpacker::LptScheduler

Inherits:
Scheduler show all
Defined in:
lib/binpacker/scheduler.rb

Instance Method Summary collapse

Methods inherited from Scheduler

for

Instance Method Details

#partition(tests:, worker_count:, timings:) ⇒ Object

Longest Processing Time first. Sort tests by descending weight, assign each to the least-loaded worker.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/binpacker/scheduler.rb', line 22

def partition(tests:, worker_count:, timings:)
  queues = Array.new(worker_count) { |i| WorkerQueue.new(i) }
  loads = Array.new(worker_count, 0.0)

  # Sort by weight descending; unknown tests get default weight
  sorted = tests.sort_by { |t|
    -timings.fetch(t.key, Timing::DEFAULT_WEIGHT)
  }

  sorted.each do |test|
    min_idx = loads.each_with_index.min_by { |load, _| load }.last
    queues[min_idx].push(test)
    loads[min_idx] += timings.fetch(test.key, Timing::DEFAULT_WEIGHT)
  end

  queues
end