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: nil, output: :ips) ⇒ RubyDslParser

Returns a new instance of RubyDslParser.

Parameters:

  • runner (Symbol, nil) (defaults to: nil)
    • If this is nil, this is automatically decided by Benchmark::Driver#runner_type_for

  • output (Symbol) (defaults to: :ips)


6
7
8
9
10
11
12
13
14
15
# File 'lib/benchmark/driver/ruby_dsl_parser.rb', line 6

def initialize(runner: nil, output: :ips)
  @prelude = nil
  @loop_count = nil
  @jobs = []
  @runner = runner
  @execs = nil
  @bundler = false
  @output = output
  @compare = false
end

Instance Method Details

#bundlerObject



50
51
52
# File 'lib/benchmark/driver/ruby_dsl_parser.rb', line 50

def bundler
  @bundler = true
end

#compare!Object



71
72
73
# File 'lib/benchmark/driver/ruby_dsl_parser.rb', line 71

def compare!
  @compare = true
end

#configurationBenchmark::Driver::Configuration

API to fetch configuration parsed from DSL



19
20
21
22
23
24
25
26
27
28
# File 'lib/benchmark/driver/ruby_dsl_parser.rb', line 19

def configuration
  @jobs.each do |job|
    job.prelude = @prelude
    job.loop_count = @loop_count
  end
  Benchmark::Driver::Configuration.new(@jobs).tap do |c|
    c.runner_options = Benchmark::Driver::Configuration::RunnerOptions.new(@runner, @execs, nil, @bundler)
    c.output_options = Benchmark::Driver::Configuration::OutputOptions.new(@output, @compare)
  end
end

#loop_count(count) ⇒ Object



75
76
77
# File 'lib/benchmark/driver/ruby_dsl_parser.rb', line 75

def loop_count(count)
  @loop_count = count
end

#prelude(prelude_script) ⇒ Object

Parameters:

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



31
32
33
34
35
36
37
38
39
40
# File 'lib/benchmark/driver/ruby_dsl_parser.rb', line 31

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

  @prelude = prelude_script
end

#rbenv(*specs) ⇒ Object

Parameters:

  • specs (Array<String>)


43
44
45
46
47
48
# File 'lib/benchmark/driver/ruby_dsl_parser.rb', line 43

def rbenv(*specs)
  @execs ||= []
  specs.each do |spec|
    @execs << Benchmark::Driver::Configuration::Executable.parse_rbenv(spec)
  end
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.



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/benchmark/driver/ruby_dsl_parser.rb', line 57

def report(name = nil, script = nil, &block)
  if !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 || block || name)
end