Skip to content

Commit

Permalink
[ruby/rack] Use autotune for database connections
Browse files Browse the repository at this point in the history
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|
+--------+---------+------+------+-----+-----+-------+--------------+
  • Loading branch information
p8 committed May 4, 2024
1 parent 034d6fb commit 884b8d1
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion frameworks/Ruby/rack/config/auto_tune.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion frameworks/Ruby/rack/config/puma.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

# 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)
Expand Down
9 changes: 7 additions & 2 deletions frameworks/Ruby/rack/hello_world.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 884b8d1

Please sign in to comment.