Class: Batchinator

Inherits:
Object show all
Defined in:
lib/ceedling/batchinator.rb

Instance Method Summary collapse

Instance Method Details

#build_step(msg, heading: true, &block) ⇒ Object

Neaten up a build step with progress message and some scope encapsulation



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ceedling/batchinator.rb', line 20

def build_step(msg, heading: true, &block)
  if heading
    msg = @reportinator.generate_heading( @loginator.decorate( msg, LogLabels::RUN ) )
  else # Progress message
    msg = "\n" + @reportinator.generate_progress( @loginator.decorate( msg, LogLabels::RUN ) )
  end

  @loginator.log( msg )

  yield # Execute build step block
end

#exec(workload:, things:, &job_block) ⇒ Object

Parallelize work to be done:

  • Enqueue things (thread-safe)
  • Spin up a number of worker threads within constraints of project file config and amount of work
  • Each worker thread consumes one item from queue and runs the block against its details
  • When the queue is empty, the worker threads wind down


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ceedling/batchinator.rb', line 37

def exec(workload:, things:, &job_block)

  batch_results = []
  sum_elapsed = 0.0

  all_elapsed = Benchmark.realtime do
    # Determine number of worker threads to run
    workers = 1
    case workload
    when :compile
      workers = @configurator.project_compile_threads
    when :test
      workers = @configurator.project_test_threads
    else
      raise NameError.new("Unrecognized batch workload type: #{workload}")
    end

    # Perform the actual parallelized work and collect the results and timing
    batch_results = Parallel.map(things, in_threads: workers) do |key, value| 
      this_results = ''
      this_elapsed = Benchmark.realtime { this_results = job_block.call(key, value) }
      [this_results, this_elapsed]
    end

    # Separate the elapsed time and results
    if batch_results.size > 0
      batch_results, batch_elapsed = batch_results.transpose
      sum_elapsed = batch_elapsed.sum()
    end
  end

  # Report the timing if requested
  @loginator.lazy(Verbosity::OBNOXIOUS) do 
    "\nBatch Elapsed: (All: %.3fsec Sum: %.3fsec)\n" % [all_elapsed, sum_elapsed]
  end

  batch_results
end

#setupObject



15
16
17
# File 'lib/ceedling/batchinator.rb', line 15

def setup
  @queue = Queue.new
end