Skip to content

Commit 7624406

Browse files
authored
[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| +--------+---------+------+------+-----+-----+-------+--------------+
1 parent da919d9 commit 7624406

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

frameworks/Ruby/rack/config/auto_tune.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# MAX_THREADS, add threads per process to reach MAX_THREADS.
88
require 'etc'
99

10-
KB_PER_WORKER = 128 * 1_024 # average of peak PSS of single-threaded processes (watch smem -k)
10+
KB_PER_WORKER = 64 * 1_024 # average of peak PSS of single-threaded processes (watch smem -k)
1111
MIN_WORKERS = 2
1212
MAX_WORKERS_PER_VCPU = 1.25 # virtual/logical
1313
MIN_THREADS_PER_WORKER = 1

frameworks/Ruby/rack/config/puma.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
# FWBM only... use the puma_auto_tune gem in production!
88
num_workers, num_threads = auto_tune
9-
9+
num_threads = [num_threads, 32].min
1010

1111
before_fork do
1212
Sequel::DATABASES.each(&:disconnect)

frameworks/Ruby/rack/hello_world.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,13 @@ class HelloWorld
5454
</html>'
5555

5656
def initialize
57-
# auto_tune
58-
max_connections = 512
57+
if defined?(Puma)
58+
num_workers, num_threads = auto_tune
59+
num_threads = [num_threads, 32].min
60+
max_connections = num_workers * num_threads
61+
else
62+
max_connections = 512
63+
end
5964
@db = PgDb.new(DEFAULT_DATABASE_URL, max_connections)
6065
end
6166

0 commit comments

Comments
 (0)