Browse Source

[ruby/rack] Hardcode Puma to 5 threads (#10388)

This should also improve the performance of JRuby where previously the
threads were hardcoded to 512:

+---------------------+-----+-----+-----+------+-------+---------+--------------+
|          branch_name| json|   db|query|update|fortune|plaintext|weighted_score|
+---------------------+-----+-----+-----+------+-------+---------+--------------+
|               master|15415|14528|12212| 11690|  13936|   144433|          1185|
| threads = processors|30345|45011|32726| 22101|  39920|   112966|          2512|
|rack/puma-max-threads|29204|50077|45459| 25358|  41560|   117562|          3026|
+---------------------+-----+-----+-----+------+-------+---------+--------------+
Petrik de Heus 6 days ago
parent
commit
b3ccafd058

+ 7 - 13
frameworks/Ruby/rack/config/puma.rb

@@ -1,17 +1,11 @@
-require_relative 'auto_tune'
-
-# FWBM only... use the puma_auto_tune gem in production!
-num_workers, num_threads = auto_tune
-
-if RUBY_PLATFORM == 'java'
-  num_threads = 512
-  num_workers = 0
-end
-
-threads num_threads
-
-if num_workers > 0
+if ENV.fetch('WEB_CONCURRENCY') == 'auto'
   before_fork do
   before_fork do
     Sequel::DATABASES.each(&:disconnect)
     Sequel::DATABASES.each(&:disconnect)
   end
   end
+else
+  workers ENV.fetch('WEB_CONCURRENCY')
+  require 'concurrent/utility/processor_counter'
+  threads = (::Concurrent.available_processor_count * 1.5).to_i
+  threads threads
+  ENV['MAX_THREADS'] = threads.to_s
 end
 end

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

@@ -43,8 +43,8 @@ class HelloWorld
   </html>'
   </html>'
 
 
   def initialize
   def initialize
-    if defined?(Puma) && (threads = Puma.cli_config.options.fetch(:max_threads)) > 1
-      max_connections = threads
+    if defined?(Puma)
+      max_connections = ENV.fetch('MAX_THREADS')
     elsif defined?(Itsi)
     elsif defined?(Itsi)
       require_relative 'config/auto_tune'
       require_relative 'config/auto_tune'
       _num_workers, num_threads = auto_tune
       _num_workers, num_threads = auto_tune

+ 0 - 2
frameworks/Ruby/rack/pg_db.rb

@@ -7,8 +7,6 @@ if RUBY_PLATFORM == 'java'
   Jdbc::Postgres.load_driver
   Jdbc::Postgres.load_driver
 end
 end
 
 
-
-
 class PgDb
 class PgDb
   QUERY_RANGE = (1..10_000).freeze # range of IDs in the Fortune DB
   QUERY_RANGE = (1..10_000).freeze # range of IDs in the Fortune DB
   ALL_IDS = QUERY_RANGE.to_a # enumeration of all the IDs in fortune DB
   ALL_IDS = QUERY_RANGE.to_a # enumeration of all the IDs in fortune DB

+ 4 - 2
frameworks/Ruby/rack/rack-jruby.dockerfile

@@ -6,13 +6,15 @@ WORKDIR /rack
 
 
 COPY Gemfile*  ./
 COPY Gemfile*  ./
 
 
+#RUN echo $(ruby config/auto_tune.rb | grep -Eo '[0-9]+' | head -n 1)
+
 RUN bundle config set with 'puma'
 RUN bundle config set with 'puma'
 RUN bundle install --jobs=8
 RUN bundle install --jobs=8
 
 
 COPY . .
 COPY . .
 
 
-EXPOSE 8080
+ENV WEB_CONCURRENCY=0
 
 
-CMD config/java_tune.sh
+EXPOSE 8080
 
 
 CMD bundle exec puma -C config/puma.rb -b tcp://0.0.0.0:8080 -e production
 CMD bundle exec puma -C config/puma.rb -b tcp://0.0.0.0:8080 -e production

+ 3 - 1
frameworks/Ruby/rack/rack.dockerfile

@@ -17,7 +17,9 @@ RUN bundle install --jobs=8
 
 
 COPY . .
 COPY . .
 
 
-EXPOSE 8080
 ENV WEB_CONCURRENCY=auto
 ENV WEB_CONCURRENCY=auto
+ENV MAX_THREADS=5
+
+EXPOSE 8080
 
 
 CMD bundle exec puma -C config/puma.rb -b tcp://0.0.0.0:8080 -e production
 CMD bundle exec puma -C config/puma.rb -b tcp://0.0.0.0:8080 -e production