-
Notifications
You must be signed in to change notification settings - Fork 13
/
uniqueness_validator_benchmark.rb
53 lines (43 loc) · 1.95 KB
/
uniqueness_validator_benchmark.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
require 'benchmark/ips'
require 'database_validations'
require_relative 'configurations'
require_relative 'gc_suite'
require_relative 'database_cleaner'
[sqlite_configuration, postgresql_configuration, mysql_configuration].each do |database_configuration|
ActiveRecord::Base.establish_connection(database_configuration)
clear_database!(database_configuration)
ActiveRecord::Schema.define(version: 1) do
create_table :entities do |t|
t.integer :field
t.index [:field], unique: true
end
end
ActiveRecord::Schema.verbose = false
ActiveRecord::Base.logger = nil
class Entity < ActiveRecord::Base
reset_column_information
end
class DbValidation < Entity
validates_db_uniqueness_of :field
end
class AppValidation < Entity
validates_uniqueness_of :field
end
# ===Benchmarks===
suite = GCSuite.new
field = 0
Entity.create(field: field)
Benchmark.ips do |x|
x.config(suite: suite)
x.report("#{database_configuration[:adapter]} only unique: validates_uniqueness_of") { field += 1; AppValidation.create(field: field) }
x.report("#{database_configuration[:adapter]} only unique: validates_db_uniqueness_of") { field += 1; DbValidation.create(field: field) }
x.report("#{database_configuration[:adapter]} each hundredth is a duplicate: validates_uniqueness_of") { field += 1; AppValidation.create(field: (field % 100 == 0 ? 0 : field)) }
x.report("#{database_configuration[:adapter]} each hundredth is a duplicate: validates_db_uniqueness_of") { field += 1; DbValidation.create(field: (field % 100 == 0 ? 0 : field)) }
x.report("#{database_configuration[:adapter]} only duplicates: validates_uniqueness_of") { AppValidation.create(field: 0) }
x.report("#{database_configuration[:adapter]} only duplicates: validates_db_uniqueness_of") { DbValidation.create(field: 0) }
end
# Clear the DB
ActiveRecord::Schema.define(version: 1) do
drop_table :entities, if_exists: true, force: :cascade
end
end