Sha256: 63957747d82b55559faccd1d19c2acc49dbab98e7365240351e2e4285e7e91ef

Contents?: true

Size: 1007 Bytes

Versions: 25

Compression:

Stored size: 1007 Bytes

Contents

require 'benchmark/ips'

require 'active_model'
require 'dry-validation'

I18n.locale = :en
I18n.backend.load_translations

COUNT = ENV['COUNT'].to_i
FIELDS = COUNT.times.map { |i| :"field_#{i}" }

class User
  include ActiveModel::Validations

  attr_reader(*FIELDS)
  validates(*FIELDS, presence: true, numericality: { greater_than: FIELDS.size / 2 })

  def initialize(attrs)
    attrs.each do |field, value|
      instance_variable_set(:"@#{field}", value)
    end
  end
end

schema = Dry::Validation.Schema do
  configure do
    config.messages = :i18n
  end

  FIELDS.each do |field|
    required(field).value(:int?, gt?: FIELDS.size / 2)
  end
end

data = FIELDS.reduce({}) { |h, f| h.update(f => FIELDS.index(f) + 1) }

puts schema.(data).inspect
puts User.new(data).validate

Benchmark.ips do |x|
  x.report('ActiveModel::Validations') do
    user = User.new(data)
    user.validate
    user.errors
  end

  x.report('dry-validation / schema') do
    schema.(data).messages
  end

  x.compare!
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
dry-validation-0.13.3 benchmarks/benchmark_schema_invalid_huge.rb
dry-validation-0.13.2 benchmarks/benchmark_schema_invalid_huge.rb
dry-validation-0.13.1 benchmarks/benchmark_schema_invalid_huge.rb
dry-validation-0.11.2 benchmarks/benchmark_schema_invalid_huge.rb
dry-validation-0.12.3 benchmarks/benchmark_schema_invalid_huge.rb
dry-validation-0.13.0 benchmarks/benchmark_schema_invalid_huge.rb
dry-validation-0.12.2 benchmarks/benchmark_schema_invalid_huge.rb
dry-validation-0.12.1 benchmarks/benchmark_schema_invalid_huge.rb
dry-validation-0.12.0 benchmarks/benchmark_schema_invalid_huge.rb
dry-validation-0.11.1 benchmarks/benchmark_schema_invalid_huge.rb
dry-validation-0.11.0 benchmarks/benchmark_schema_invalid_huge.rb
dry-validation-0.10.7 benchmarks/benchmark_schema_invalid_huge.rb
dry-validation-0.10.6 benchmarks/benchmark_schema_invalid_huge.rb
dry-validation-0.10.5 benchmarks/benchmark_schema_invalid_huge.rb
dry-validation-0.10.4 benchmarks/benchmark_schema_invalid_huge.rb
dry-validation-0.10.3 benchmarks/benchmark_schema_invalid_huge.rb
dry-validation-0.10.2 benchmarks/benchmark_schema_invalid_huge.rb
dry-validation-0.10.1 benchmarks/benchmark_schema_invalid_huge.rb
dry-validation-0.10.0 benchmarks/benchmark_schema_invalid_huge.rb
dry-validation-0.9.5 benchmarks/benchmark_schema_invalid_huge.rb