Class: RSpecTurbo::BatchPlanner

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec_turbo/batch_planner.rb

Overview

Runs ‘rspec –dry-run –format json` to count examples per file, then packs the files into N balanced batches using the Longest-Processing-Time first (LPT) greedy heuristic. Files heavier than a single batch’s fair share are split into slices of individual example IDs so one huge file can’t bottle- neck a worker.

If the dry-run fails for any reason it falls back to equal-weight packing.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(files, num_workers:, rspec_options: []) ⇒ BatchPlanner

Returns a new instance of BatchPlanner.



16
17
18
19
20
21
22
23
24
# File 'lib/rspec_turbo/batch_planner.rb', line 16

def initialize(files, num_workers:, rspec_options: [])
  @files = files
  @n = num_workers
  @rspec_options = rspec_options
  @counts = {}
  @batches = []
  @pending_count = 0
  @dry_run_elapsed = 0
end

Instance Attribute Details

#batchesObject (readonly)

Returns the value of attribute batches.



14
15
16
# File 'lib/rspec_turbo/batch_planner.rb', line 14

def batches
  @batches
end

#countsObject (readonly)

Returns the value of attribute counts.



14
15
16
# File 'lib/rspec_turbo/batch_planner.rb', line 14

def counts
  @counts
end

#dry_run_elapsedObject (readonly)

Returns the value of attribute dry_run_elapsed.



14
15
16
# File 'lib/rspec_turbo/batch_planner.rb', line 14

def dry_run_elapsed
  @dry_run_elapsed
end

#pending_countObject (readonly)

Returns the value of attribute pending_count.



14
15
16
# File 'lib/rspec_turbo/batch_planner.rb', line 14

def pending_count
  @pending_count
end

Instance Method Details

#example_count(units) ⇒ Object



36
# File 'lib/rspec_turbo/batch_planner.rb', line 36

def example_count(units) = units.sum { |unit| unit_weight(unit) }

#plan!Object



26
27
28
29
30
31
32
33
34
# File 'lib/rspec_turbo/batch_planner.rb', line 26

def plan!
  result = dry_run
  @counts = result[:counts]
  @pending_count = result[:pending_count]
  units = build_units(@files, @counts, result[:ids])
  @batches = bin_pack(units)

  self
end