Class: CSVPlusPlus::BenchmarkedCompiler

Inherits:
Compiler
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/csv_plus_plus/benchmarked_compiler.rb

Overview

Extend a Compiler class and add benchmark timings

Instance Attribute Summary collapse

Attributes inherited from Compiler

#options, #runtime

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Compiler

#benchmark=, #compile_template, with_compiler

Constructor Details

#initialize(benchmark:, options:, runtime:) ⇒ BenchmarkedCompiler

Returns a new instance of BenchmarkedCompiler.

Parameters:

  • benchmark (::Benchmark::Report)


48
49
50
51
52
53
# File 'lib/csv_plus_plus/benchmarked_compiler.rb', line 48

def initialize(benchmark:, options:, runtime:)
  super(options:, runtime:)

  @benchmark = ::T.let(benchmark, ::Benchmark::Report)
  @timings = ::T.let([], ::T::Array[::Benchmark::Tms])
end

Instance Attribute Details

#benchmarkBenchmark::Report (readonly)

A Benchmark instance

Returns:

  • (Benchmark::Report)

    the current value of benchmark



10
11
12
# File 'lib/csv_plus_plus/benchmarked_compiler.rb', line 10

def benchmark
  @benchmark
end

#timingsArray<Benchmark::Tms> (readonly)

Benchmark timings that have been accumulated by each step of compilation

Returns:

  • (Array<Benchmark::Tms>)

    the current value of timings



10
11
12
# File 'lib/csv_plus_plus/benchmarked_compiler.rb', line 10

def timings
  @timings
end

Class Method Details

.with_benchmarks(options:, runtime:, &block) ⇒ Object

Instantiate a ::Compiler that can benchmark (time) it’s stages. For better or worse, the only way that they Benchmark library exposes it’s ::Benchmark::Report is via a block, so this code also has to wrap with one

Parameters:



31
32
33
34
35
36
37
38
# File 'lib/csv_plus_plus/benchmarked_compiler.rb', line 31

def self.with_benchmarks(options:, runtime:, &block)
  ::Benchmark.benchmark(::Benchmark::CAPTION, 25, ::Benchmark::FORMAT, '> Total') do |x|
    # compiler.extend(self)
    compiler = new(benchmark: x, options:, runtime:)
    block.call(compiler)
    [compiler.timings.reduce(:+)]
  end
end

Instance Method Details

#outputting!(&block) ⇒ Object

Time the Compiler#outputting! stage rubocop:disable Naming/BlockForwarding



58
59
60
# File 'lib/csv_plus_plus/benchmarked_compiler.rb', line 58

def outputting!(&block)
  time_stage('Writing the spreadsheet') { super(&block) }
end