Browse Source

[ruby/rack] Use autotune for Puma database connections (#8536)

Alos decrease the KB_PER_WORKER as this is a small application.

+--------+---------+------+------+-----+-----+-------+--------------+
|threads |plaintext|update|  json|   db|query|fortune|weighted_score|
+--------+---------+------+------+-----+-----+-------+--------------+
|      32|   145031| 11936|122017|27001|18156|  23712|          1500|
|       2|   126527| 11878|141483|33525|20515|  27725|          1591|
|       1|   126701| 12923|133573|31402|19468|  24894|          1617|
|       5|    34445| 15098|103356|29189|23860|  22518|          1802|
|       5|    34440| 16363|114751|29820|20933|  24349|          1845|
+--------+---------+------+------+-----+-----+-------+--------------+
Petrik de Heus 1 year ago
parent
commit
7624406e06

+ 1 - 1
frameworks/Ruby/rack/config/auto_tune.rb

@@ -7,7 +7,7 @@
 # MAX_THREADS, add threads per process to reach MAX_THREADS.
 require 'etc'
 
-KB_PER_WORKER = 128 * 1_024 # average of peak PSS of single-threaded processes (watch smem -k)
+KB_PER_WORKER = 64 * 1_024 # average of peak PSS of single-threaded processes (watch smem -k)
 MIN_WORKERS = 2
 MAX_WORKERS_PER_VCPU = 1.25 # virtual/logical
 MIN_THREADS_PER_WORKER = 1

+ 1 - 1
frameworks/Ruby/rack/config/puma.rb

@@ -6,7 +6,7 @@ require 'sequel'
 
 # FWBM only... use the puma_auto_tune gem in production!
 num_workers, num_threads = auto_tune
-
+num_threads = [num_threads, 32].min
 
 before_fork do
   Sequel::DATABASES.each(&:disconnect)

+ 7 - 2
frameworks/Ruby/rack/hello_world.rb

@@ -54,8 +54,13 @@ class HelloWorld
   </html>'
 
   def initialize
-    # auto_tune
-    max_connections = 512
+    if defined?(Puma)
+      num_workers, num_threads = auto_tune
+      num_threads = [num_threads, 32].min
+      max_connections = num_workers * num_threads
+    else
+      max_connections = 512
+    end
     @db = PgDb.new(DEFAULT_DATABASE_URL, max_connections)
   end