Browse Source

Add Pool to increase the throughput

Jesterovskiy 9 years ago
parent
commit
d48e856df8
2 changed files with 12 additions and 6 deletions
  1. 7 4
      frameworks/Crystal/kemal/server-postgres.cr
  2. 5 2
      frameworks/Crystal/kemal/shard.yml

+ 7 - 4
frameworks/Crystal/kemal/server-postgres.cr

@@ -1,11 +1,14 @@
 require "kemal"
 require "pg"
+require "pool/connection"
 require "html_builder"
 
 # Compose Objects (like Hash) to have a to_json method
 require "json/to_json"
 
-DB = PG.connect("postgres://benchmarkdbuser:benchmarkdbpass@#{ENV["DBHOST"]? || "127.0.0.1"}/hello_world")
+DB = ConnectionPool.new(capacity: 25, timeout: 0.01) do
+  PG.connect("postgres://benchmarkdbuser:benchmarkdbpass@#{ENV["DBHOST"]? || "127.0.0.1"}/hello_world")
+end
 
 class CONTENT
   UTF8 = "; charset=UTF-8"
@@ -18,19 +21,19 @@ ID_MAXIMUM = 10_000
 
 private def randomWorld
   id = rand(1..ID_MAXIMUM)
-  result = DB.exec({Int32, Int32}, "SELECT id, randomNumber FROM world WHERE id = $1", [id]).rows.first
+  result = DB.connection.exec({Int32, Int32}, "SELECT id, randomNumber FROM world WHERE id = $1", [id]).rows.first
   {:id => result[0], :randomNumber => result[1]}
 end
 
 private def setWorld(world)
-  DB.exec("UPDATE world set randomNumber = $1 where id = $2", [world[:randomNumber], world[:id]])
+  DB.connection.exec("UPDATE world set randomNumber = $1 where id = $2", [world[:randomNumber], world[:id]])
   world
 end
 
 private def fortunes
   data = [] of  Hash(Symbol, (String | Int32))
 
-  DB.exec({Int32, String}, "select id, message from Fortune").rows.each do |row|
+  DB.connection.exec({Int32, String}, "select id, message from Fortune").rows.each do |row|
     data.push({:id => row[0], :message => row[1]})
   end
   data

+ 5 - 2
frameworks/Crystal/kemal/shard.yml

@@ -3,8 +3,11 @@ version: "0.0.1"
 
 dependencies:
   pg:
-    github: "will/crystal-pg"
-    version: "~> 0.5.0"
+    github: will/crystal-pg
+    branch: master
+  pool:
+    github: ysbaddaden/pool
+    branch: master
   kemal:
     github: "sdogruyol/kemal"
     branch: "master"