Skip to content

Commit 720d61e

Browse files
committed
[ruby/agoo] Use prepared statements
+------------------------+-----+-----+-------+ | branch_name| db|query|fortune| +------------------------+-----+-----+-------+ | master|69028|68419| 60723| |agoo/prepared-statements|98140|78085| 71027| +------------------------+-----+-----+-------+
1 parent 43edfc1 commit 720d61e

File tree

1 file changed

+29
-33
lines changed

1 file changed

+29
-33
lines changed

frameworks/Ruby/agoo/app.rb

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@
77
require 'rack'
88

99
$pool = ConnectionPool.new(size: 1, timeout: 5) do
10-
PG::Connection.new({
11-
dbname: 'hello_world',
12-
host: 'tfb-database',
13-
user: 'benchmarkdbuser',
14-
password: 'benchmarkdbpass'
15-
})
16-
end
10+
conn = PG::Connection.new({
11+
dbname: 'hello_world',
12+
host: 'tfb-database',
13+
user: 'benchmarkdbuser',
14+
password: 'benchmarkdbpass'
15+
})
16+
conn.set_error_verbosity(PG::PQERRORS_VERBOSE)
17+
conn.prepare('select_world', 'SELECT * FROM world WHERE id = $1')
18+
conn.prepare('select_fortune', 'SELECT id, message FROM fortune')
19+
conn
20+
end
1721

1822
QUERY_RANGE = (1..10_000).freeze
1923
ALL_IDS = QUERY_RANGE.to_a
@@ -43,11 +47,7 @@ def self.get_one_random_number
4347

4448
def self.get_one_record(id = get_one_random_number)
4549
$pool.with do |conn|
46-
conn.exec_params(<<-SQL, [id]).first
47-
48-
SELECT * FROM world WHERE id = $1
49-
50-
SQL
50+
conn.exec_prepared('select_world', [id]).first
5151
end
5252
end
5353

@@ -114,15 +114,12 @@ def self.call(_req)
114114
end
115115
end
116116

117+
117118
class FortunesHandler < BaseHandler
118119
def self.call(_req)
119120
f_1 = $pool.with do |conn|
120-
conn.exec(<<-SQL)
121-
122-
SELECT id, message FROM fortune
123-
124-
SQL
125-
end
121+
conn.exec_prepared('select_fortune', [])
122+
end
126123

127124
f_2 = f_1.map(&:to_h).
128125
append({ 'id' => '0', 'message' => 'Additional fortune added at request time.' }).
@@ -192,21 +189,20 @@ def self.call(req)
192189
end
193190

194191
Agoo::Log.configure({
195-
classic: true,
196-
colorize: true,
197-
console: true,
198-
dir: '',
199-
states: {
200-
DEBUG: false,
201-
INFO: false,
202-
203-
connect: false,
204-
eval: false,
205-
push: false,
206-
request: false,
207-
response: false
208-
}
209-
})
192+
classic: true,
193+
colorize: true,
194+
console: true,
195+
dir: '',
196+
states: {
197+
DEBUG: false,
198+
INFO: false,
199+
connect: false,
200+
eval: false,
201+
push: false,
202+
request: false,
203+
response: false
204+
}
205+
})
210206

211207
worker_count = 4
212208
worker_count = ENV['AGOO_WORKER_COUNT'].to_i if ENV.key?('AGOO_WORKER_COUNT')

0 commit comments

Comments
 (0)