浏览代码

[ruby/agoo] Use prepared statements (#10193)

+------------------------+-----+-----+-------+
|             branch_name|   db|query|fortune|
+------------------------+-----+-----+-------+
|                  master|69028|68419|  60723|
|agoo/prepared-statements|98140|78085|  71027|
+------------------------+-----+-----+-------+
Petrik de Heus 4 天之前
父节点
当前提交
77f4e1e97f
共有 1 个文件被更改,包括 29 次插入33 次删除
  1. 29 33
      frameworks/Ruby/agoo/app.rb

+ 29 - 33
frameworks/Ruby/agoo/app.rb

@@ -7,13 +7,17 @@ require 'pg'
 require 'rack'
 require 'rack'
 
 
 $pool = ConnectionPool.new(size: 1, timeout: 5) do
 $pool = ConnectionPool.new(size: 1, timeout: 5) do
-          PG::Connection.new({
-                                 dbname:   'hello_world',
-                                 host:     'tfb-database',
-                                 user:     'benchmarkdbuser',
-                                 password: 'benchmarkdbpass'
-                             })
-        end
+  conn = PG::Connection.new({
+    dbname:   'hello_world',
+    host:     'tfb-database',
+    user:     'benchmarkdbuser',
+    password: 'benchmarkdbpass'
+  })
+  conn.set_error_verbosity(PG::PQERRORS_VERBOSE)
+  conn.prepare('select_world', 'SELECT * FROM world WHERE id = $1')
+  conn.prepare('select_fortune', 'SELECT id, message FROM fortune')
+  conn
+end
 
 
 QUERY_RANGE = (1..10_000).freeze
 QUERY_RANGE = (1..10_000).freeze
 ALL_IDS = QUERY_RANGE.to_a
 ALL_IDS = QUERY_RANGE.to_a
@@ -43,11 +47,7 @@ class BaseHandler
 
 
   def self.get_one_record(id = get_one_random_number)
   def self.get_one_record(id = get_one_random_number)
     $pool.with do |conn|
     $pool.with do |conn|
-      conn.exec_params(<<-SQL, [id]).first
-
-        SELECT * FROM world WHERE id = $1
-
-      SQL
+      conn.exec_prepared('select_world', [id]).first
     end
     end
   end
   end
 
 
@@ -114,15 +114,12 @@ class DbHandler < BaseHandler
   end
   end
 end
 end
 
 
+
 class FortunesHandler < BaseHandler
 class FortunesHandler < BaseHandler
   def self.call(_req)
   def self.call(_req)
     f_1 = $pool.with do |conn|
     f_1 = $pool.with do |conn|
-            conn.exec(<<-SQL)
-
-              SELECT id, message FROM fortune
-
-            SQL
-          end
+      conn.exec_prepared('select_fortune', [])
+    end
 
 
     f_2 = f_1.map(&:to_h).
     f_2 = f_1.map(&:to_h).
             append({ 'id' => '0', 'message' => 'Additional fortune added at request time.' }).
             append({ 'id' => '0', 'message' => 'Additional fortune added at request time.' }).
@@ -192,21 +189,20 @@ class UpdatesHandler < BaseHandler
 end
 end
 
 
 Agoo::Log.configure({
 Agoo::Log.configure({
-                        classic:  true,
-                        colorize: true,
-                        console:  true,
-                        dir:      '',
-                        states:   {
-                            DEBUG:    false,
-                            INFO:     false,
-
-                            connect:  false,
-                            eval:     false,
-                            push:     false,
-                            request:  false,
-                            response: false
-                        }
-                    })
+  classic:  true,
+  colorize: true,
+  console:  true,
+  dir:      '',
+  states:   {
+    DEBUG:    false,
+    INFO:     false,
+    connect:  false,
+    eval:     false,
+    push:     false,
+    request:  false,
+    response: false
+  }
+})
 
 
 worker_count = 4
 worker_count = 4
 worker_count = ENV['AGOO_WORKER_COUNT'].to_i if ENV.key?('AGOO_WORKER_COUNT')
 worker_count = ENV['AGOO_WORKER_COUNT'].to_i if ENV.key?('AGOO_WORKER_COUNT')