Class: Benchmark::Driver::RubyDslParser

Inherits:
Object
  • Object
show all
Defined in:
lib/benchmark/driver/ruby_dsl_parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(runner: :call, output: :ips) ⇒ RubyDslParser

Returns a new instance of RubyDslParser.



4
5
6
7
8
9
# File 'lib/benchmark/driver/ruby_dsl_parser.rb', line 4

def initialize(runner: :call, output: :ips)
  @prelude = nil
  @jobs = []
  @runner_options = Benchmark::Driver::Configuration::RunnerOptions.new(runner)
  @output_options = Benchmark::Driver::Configuration::OutputOptions.new(output)
end

Instance Method Details

#compare!Object



54
55
56
# File 'lib/benchmark/driver/ruby_dsl_parser.rb', line 54

def compare!
  @output_options.compare = true
end

#configurationBenchmark::Driver::Configuration

API to fetch configuration parsed from DSL



13
14
15
16
17
18
19
20
21
# File 'lib/benchmark/driver/ruby_dsl_parser.rb', line 13

def configuration
  @jobs.each do |job|
    job.prelude = @prelude
  end
  Benchmark::Driver::Configuration.new(@jobs).tap do |c|
    c.runner_options = @runner_options
    c.output_options = @output_options
  end
end

#prelude=(prelude) ⇒ Object

Parameters:

  • prelude (String)
    • Script required for benchmark whose execution time is not measured.



24
25
26
27
28
29
30
31
32
33
# File 'lib/benchmark/driver/ruby_dsl_parser.rb', line 24

def prelude=(prelude)
  unless prelude.is_a?(String)
    raise ArgumentError.new("prelude must be String but got #{prelude.inspect}")
  end
  unless @prelude.nil?
    raise ArgumentError.new("prelude is already set:\n#{@prelude}")
  end

  @prelude = prelude
end

#report(name = nil, script: nil, &block) ⇒ Object

Parameters:

  • name (String, nil) (defaults to: nil)
    • Name shown on result output. This must be provided if block is given.

  • script (String, nil) (defaults to: nil)
    • Benchmarked script in String. Only either of script or block must be provided.

  • block (Proc, nil)
    • Benchmarked Proc object.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/benchmark/driver/ruby_dsl_parser.rb', line 38

def report(name = nil, script: nil, &block)
  if script.nil? && !block_given?
    raise ArgumentError.new('script or block must be provided')
  elsif !script.nil? && block_given?
    raise ArgumentError.new('script and block cannot be specified at the same time')
  elsif name.nil? && block_given?
    raise ArgumentError.new('name must be specified if block is given')
  elsif !name.nil? && !name.is_a?(String)
    raise ArgumentError.new("name must be String but got #{name.inspect}")
  elsif !script.nil? && !script.is_a?(String)
    raise ArgumentError.new("script must be String but got #{script.inspect}")
  end

  @jobs << Benchmark::Driver::Configuration::Job.new(name || script, script || block)
end