-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcomposed_benchmarks.rb
89 lines (71 loc) · 3.25 KB
/
composed_benchmarks.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
require 'benchmark/ips'
require 'database_validations'
require_relative 'configurations'
require_relative 'gc_suite'
require_relative 'database_cleaner'
[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 :companies
create_table :countries
create_table :users_1 do |t|
t.string :email
t.string :full_name
t.belongs_to :country
t.belongs_to :company
t.index :email
t.index :full_name
end
create_table :users_2 do |t|
t.string :email
t.string :full_name
t.belongs_to :country, foreign_key: true
t.belongs_to :company, foreign_key: true
t.index :email, unique: true
t.index :full_name, unique: true
end
end
ActiveRecord::Schema.verbose = false
ActiveRecord::Base.logger = nil
class Company < ActiveRecord::Base
end
class Country < ActiveRecord::Base
end
class Users1 < ActiveRecord::Base
self.table_name = :users_1
validates_uniqueness_of :email
validates_uniqueness_of :full_name
belongs_to :company, optional: false
belongs_to :country, optional: false
end
class Users2 < ActiveRecord::Base
self.table_name = :users_2
validates_db_uniqueness_of :email
validates_db_uniqueness_of :full_name
db_belongs_to :company
db_belongs_to :country
end
# ===Benchmarks===
suite = GCSuite.new
company = Company.create!
country = Country.create!
field = 0
# ===Save only valid===
Benchmark.ips do |x|
x.config(suite: suite)
x.report("#{database_configuration[:adapter]} optimistic: without gem") { field += 1; Users1.create(company_id: company.id, country_id: country.id, full_name: field.to_s, email: field.to_s) }
x.report("#{database_configuration[:adapter]} optimistic: with gem") { field += 1; Users2.create(company_id: company.id, country_id: country.id, full_name: field.to_s, email: field.to_s) }
x.report("#{database_configuration[:adapter]} realistic: without gem") { field += 1; field % 100 == 0 ? Users1.create(company_id: -1, country_id: -1, full_name: 'invalid', email: 'invalid') : Users1.create(company_id: company.id, country_id: country.id, full_name: field.to_s, email: field.to_s) }
x.report("#{database_configuration[:adapter]} realistic: with gem") { field += 1; field % 100 == 0 ? Users2.create(company_id: -1, country_id: -1, full_name: 'invalid', email: 'invalid') : Users2.create(company_id: company.id, country_id: country.id, full_name: field.to_s, email: field.to_s) }
x.report("#{database_configuration[:adapter]} pessimistic: without gem") { Users1.create(company_id: -1, country_id: -1, full_name: 'invalid', email: 'invalid') }
x.report("#{database_configuration[:adapter]} pessimistic: with gem") { Users2.create(company_id: -1, country_id: -1, full_name: 'invalid', email: 'invalid') }
end
# Clear the DB
ActiveRecord::Schema.define(version: 2) do
drop_table :users_1, if_exists: true, force: :cascade
drop_table :users_2, if_exists: true, force: :cascade
drop_table :companies, if_exists: true, force: :cascade
drop_table :countries, if_exists: true, force: :cascade
end
end