Sha256: 906eb29ddf146cbe4a1382dd5927f5dab939c3de1836189d1ad355a3845ff506

Contents?: true

Size: 1.62 KB

Versions: 20

Compression:

Stored size: 1.62 KB

Contents

require 'benchmark_driver/runner'

module BenchmarkDriver
  class << JobParser = Module.new
    # @param [Hash] config
    # @param [Hash] default_params - Special default values not written in job definition
    def parse(config, default_params: {})
      config = symbolize_keys(config)
      type = config.fetch(:type)
      if !type.is_a?(String)
        raise ArgumentError.new("Invalid type: #{config[:type].inspect} (expected String)")
      elsif !type.match(/\A[A-Za-z0-9_]+\z/)
        raise ArgumentError.new("Invalid type: #{config[:type].inspect} (expected to include only [A-Za-z0-9_])")
      end
      config.delete(:type)

      # Dynamic dispatch for plugin support
      require "benchmark_driver/runner/#{type}"
      job = ::BenchmarkDriver.const_get("Runner::#{camelize(type)}::JobParser", false).parse(config)
      default_params.each do |key, value|
        if job.respond_to?(key) && job.respond_to?("#{key}=") && job.public_send(key).nil?
          job.public_send("#{key}=", value)
        end
      end
      job
    end

    private

    def camelize(str)
      str.split('_').map(&:capitalize).join
    end

    # @param [Object] config
    def symbolize_keys(config)
      case config
      when Hash
        config.dup.tap do |hash|
          hash.keys.each do |key|
            case key
            when String, Symbol
              hash[key.to_sym] = symbolize_keys(hash.delete(key))
            else # Struct
              hash[key] = symbolize_keys(hash.delete(key))
            end
          end
        end
      when Array
        config.map { |c| symbolize_keys(c) }
      else
        config
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 2 rubygems

Version Path
benchmark_driver-0.14.21 lib/benchmark_driver/job_parser.rb
benchmark_driver-0.14.20 lib/benchmark_driver/job_parser.rb
benchmark_driver-0.14.19 lib/benchmark_driver/job_parser.rb
benchmark_driver-0.14.18 lib/benchmark_driver/job_parser.rb
benchmark_driver-0.14.17 lib/benchmark_driver/job_parser.rb
benchmark_driver-0.14.16 lib/benchmark_driver/job_parser.rb
benchmark_driver-0.14.15 lib/benchmark_driver/job_parser.rb
benchmark_driver-0.14.14 lib/benchmark_driver/job_parser.rb
benchmark_driver_monotonic_raw-0.14.13 lib/benchmark_driver/job_parser.rb
benchmark_driver-0.14.13 lib/benchmark_driver/job_parser.rb
benchmark_driver-0.14.12 lib/benchmark_driver/job_parser.rb
benchmark_driver-0.14.11 lib/benchmark_driver/job_parser.rb
benchmark_driver-0.14.10 lib/benchmark_driver/job_parser.rb
benchmark_driver-0.14.9 lib/benchmark_driver/job_parser.rb
benchmark_driver-0.14.8 lib/benchmark_driver/job_parser.rb
benchmark_driver-0.14.7 lib/benchmark_driver/job_parser.rb
benchmark_driver-0.14.6 lib/benchmark_driver/job_parser.rb
benchmark_driver-0.14.5 lib/benchmark_driver/job_parser.rb
benchmark_driver-0.14.4 lib/benchmark_driver/job_parser.rb
benchmark_driver-0.14.3 lib/benchmark_driver/job_parser.rb