Skip to content

Commit 8c2e651

Browse files
committed
Add query count test examples
1 parent 45109a6 commit 8c2e651

File tree

2 files changed

+106
-1
lines changed

2 files changed

+106
-1
lines changed

017_query_count.rb

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
require 'rubygems'
2+
require 'bundler/inline'
3+
4+
gemfile(true) do
5+
source 'https://rubygems.org'
6+
gem 'activesupport'
7+
gem 'activerecord'
8+
gem 'pg'
9+
end
10+
11+
require 'active_record'
12+
13+
puts "Loaded gems"
14+
15+
class Migration < ActiveRecord::Migration[5.0]
16+
def up
17+
create_table :things do |t|
18+
t.string :col0
19+
t.string :col1
20+
t.string :col2
21+
t.string :col3
22+
t.string :col4
23+
end
24+
25+
create_table :minions do |t|
26+
t.integer :thing_id
27+
t.string :name
28+
end
29+
30+
sql = <<-END
31+
INSERT INTO
32+
things(col0, col1, col2, col3, col4)
33+
(SELECT
34+
rpad('x', 100, 'x'),
35+
rpad('x', 100, 'x'),
36+
rpad('x', 100, 'x'),
37+
rpad('x', 100, 'x'),
38+
rpad('x', 100, 'x')
39+
FROM generate_series(1, 100)
40+
);
41+
END
42+
43+
ActiveRecord::Base.connection.execute sql
44+
end
45+
46+
def down
47+
drop_table :things
48+
drop_table :minions
49+
end
50+
end
51+
52+
class Thing < ActiveRecord::Base
53+
has_many :minions
54+
end
55+
56+
class Minion < ActiveRecord::Base
57+
belongs_to :thing
58+
end
59+
60+
def migrate(dir = :up)
61+
Migration.new.migrate(dir)
62+
end
63+
64+
def track_queries
65+
results = []
66+
ActiveSupport::Notifications.subscribe('sql.active_record') do |*args|
67+
event = ActiveSupport::Notifications::Event.new(*args)
68+
query_name = event.payload[:name]
69+
70+
next if %w(SCHEMA).include? query_name # skip schema lookups
71+
results << query_name
72+
end
73+
yield
74+
ActiveSupport::Notifications.unsubscribe('sql.active_record')
75+
results
76+
end
77+
# ------ main ------
78+
79+
ActiveRecord::Base.establish_connection(
80+
adapter: 'postgresql',
81+
database: '017_things',
82+
host: 'localhost',
83+
username: 'postgres'
84+
)
85+
86+
cmd = ARGV[0]
87+
88+
case cmd
89+
when '--migrate'
90+
migrate(:up)
91+
when '--rollback'
92+
migrate(:down)
93+
when '--each'
94+
puts "Queries:\n"
95+
puts (track_queries { Thing.limit(10).each { |thing| thing.minions.load } })
96+
when '--include'
97+
puts "Queries:\n"
98+
puts (track_queries { Thing.limit(10).includes(:minions).load })
99+
else
100+
puts "No command provided!"
101+
exit!(1)
102+
end
103+
104+
exit!(0)
105+

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,6 @@ Tips:
280280
* But don't forget to turn on caching and set log level to `:info`
281281
* Database size may affect your results, so rollback transactions or clear data
282282
* Write performance test for complex DB queries
283-
* Test DB queries count and try to reduce it
283+
* Test DB queries count and try to reduce it (may use *assert_value* gem) (see [017_query_count.rb](017_query_count.rb))
284284
* Generate enough data for performance test
285285

0 commit comments

Comments
 (0)