Selaa lähdekoodia

[ruby/grape] Hardcode Puma to 5 threads (#10389)

Also extract most logic to a boot.rb, similarly to other Ruby
frameworks.
Petrik de Heus 1 viikko sitten
vanhempi
sitoutus
a417041e2a

+ 91 - 0
frameworks/Ruby/grape/boot.rb

@@ -0,0 +1,91 @@
+require 'erb'
+require 'active_record'
+require 'yaml'
+
+MAX_PK = 10_000
+ID_RANGE = (1..MAX_PK).freeze
+ALL_IDS = ID_RANGE.to_a
+QUERIES_MIN = 1
+QUERIES_MAX = 500
+
+Bundler.require :default
+
+db_config = YAML.load(ERB.new(File.read('config/database.yml')).result)[ENV['RACK_ENV']]
+ActiveRecord::Base.establish_connection(db_config)
+
+class World < ActiveRecord::Base
+  self.table_name = 'World'
+end
+
+module Acme
+  class HelloWorld < Grape::API
+    get '/json' do
+      {message:'Hello, World!'}
+    end
+  end
+
+  class PlainText < Grape::API
+    content_type :plain, 'text/plain'
+    format :plain
+
+    get '/plaintext' do
+      'Hello, World!'
+    end
+  end
+
+  class DatabaseQueries < Grape::API
+    logger nil
+    helpers do
+      def bounded_queries
+        queries = params[:queries].to_i
+        queries.clamp(QUERIES_MIN, QUERIES_MAX)
+      end
+
+      # Return a random number between 1 and MAX_PK
+      def rand1
+        rand(MAX_PK).succ
+      end
+    end
+
+    get '/db' do
+      ActiveRecord::Base.with_connection do
+        World.find(rand1).attributes
+      end
+    end
+
+    get '/query' do
+      ActiveRecord::Base.with_connection do
+        ALL_IDS.sample(bounded_queries).map do |id|
+          World.find(id)
+        end
+      end
+    end
+
+    get '/updates' do
+      worlds =
+        ActiveRecord::Base.with_connection do
+          ALL_IDS.sample(bounded_queries).map do |id|
+            world = World.find(id)
+            new_value = rand1
+            new_value = rand1 while new_value == world.randomNumber
+            world.update_columns(randomNumber: new_value)
+            world
+          end
+        end
+    end
+  end
+
+  class API < Grape::API
+    before do
+      header 'Date', Time.now.httpdate if defined?(Puma)
+      header 'Server', 'grape'
+    end
+    logger nil
+    content_type :json, 'application/json'
+    format :json
+
+    mount ::Acme::HelloWorld
+    mount ::Acme::PlainText
+    mount ::Acme::DatabaseQueries
+  end
+end

+ 1 - 93
frameworks/Ruby/grape/config.ru

@@ -1,94 +1,2 @@
-require 'erb'
-require 'active_record'
-require 'yaml'
-require_relative 'config/auto_tune'
-
-MAX_PK = 10_000
-ID_RANGE = (1..MAX_PK).freeze
-ALL_IDS = ID_RANGE.to_a
-QUERIES_MIN = 1
-QUERIES_MAX = 500
-
-Bundler.require :default
-
-db_config = YAML.load(ERB.new(File.read('config/database.yml')).result)[ENV['RACK_ENV']]
-ActiveRecord::Base.establish_connection(db_config)
-
-class World < ActiveRecord::Base
-  self.table_name = 'World'
-end
-
-module Acme
-  class HelloWorld < Grape::API
-    get '/json' do
-      {message:'Hello, World!'}
-    end
-  end
-
-  class PlainText < Grape::API
-    content_type :plain, 'text/plain'
-    format :plain
-
-    get '/plaintext' do
-      'Hello, World!'
-    end
-  end
-
-  class DatabaseQueries < Grape::API
-    logger nil
-    helpers do
-      def bounded_queries
-        queries = params[:queries].to_i
-        queries.clamp(QUERIES_MIN, QUERIES_MAX)
-      end
-
-      # Return a random number between 1 and MAX_PK
-      def rand1
-        rand(MAX_PK).succ
-      end
-    end
-
-    get '/db' do
-      ActiveRecord::Base.with_connection do
-        World.find(rand1).attributes
-      end
-    end
-
-    get '/query' do
-      ActiveRecord::Base.with_connection do
-        ALL_IDS.sample(bounded_queries).map do |id|
-          World.find(id)
-        end
-      end
-    end
-
-    get '/updates' do
-      worlds =
-        ActiveRecord::Base.with_connection do
-          ALL_IDS.sample(bounded_queries).map do |id|
-            world = World.find(id)
-            new_value = rand1
-            new_value = rand1 while new_value == world.randomNumber
-            world.update_columns(randomNumber: new_value)
-            world
-          end
-        end
-    end
-  end
-
-  class API < Grape::API
-    before do
-      header 'Date', Time.now.httpdate if defined?(Puma)
-      header 'Server', 'grape'
-    end
-    logger nil
-    content_type :json, 'application/json'
-    format :json
-
-    mount ::Acme::HelloWorld
-    mount ::Acme::PlainText
-    mount ::Acme::DatabaseQueries
-  end
-end
-
+require_relative 'boot'
 run Acme::API
 run Acme::API

+ 1 - 1
frameworks/Ruby/grape/config/database.yml

@@ -5,5 +5,5 @@ production:
   database: hello_world
   database: hello_world
   username: benchmarkdbuser
   username: benchmarkdbuser
   password: benchmarkdbpass
   password: benchmarkdbpass
-  pool: 3
+  pool: <%= ENV.fetch('MAX_THREADS') %>
   timeout: 5000
   timeout: 5000

+ 0 - 13
frameworks/Ruby/grape/config/puma.rb

@@ -1,13 +0,0 @@
-require_relative 'auto_tune'
-
-# FWBM only... use the puma_auto_tune gem in production!
-_, num_threads = auto_tune
-
-threads num_threads
-
-# Use the `preload_app!` method when specifying a `workers` number.
-# This directive tells Puma to first boot the application and load code
-# before forking the application. This takes advantage of Copy On Write
-# process behavior so workers use less memory.
-#
-preload_app!

+ 2 - 1
frameworks/Ruby/grape/grape-iodine.dockerfile

@@ -14,9 +14,10 @@ ADD ./ /grape
 WORKDIR /grape
 WORKDIR /grape
 
 
 RUN bundle config set with 'iodine'
 RUN bundle config set with 'iodine'
-RUN bundle install --jobs=4 --gemfile=/grape/Gemfile
+RUN bundle install --jobs=8 --gemfile=/grape/Gemfile
 
 
 ENV RACK_ENV=production
 ENV RACK_ENV=production
+ENV MAX_THREADS=1
 
 
 EXPOSE 8080
 EXPOSE 8080
 
 

+ 4 - 2
frameworks/Ruby/grape/grape.dockerfile

@@ -12,9 +12,11 @@ ADD ./ /grape
 WORKDIR /grape
 WORKDIR /grape
 
 
 RUN bundle config set with 'puma'
 RUN bundle config set with 'puma'
-RUN bundle install --jobs=4 --gemfile=/grape/Gemfile
+RUN bundle install --jobs=8 --gemfile=/grape/Gemfile
 
 
 ENV WEB_CONCURRENCY=auto
 ENV WEB_CONCURRENCY=auto
+ENV MAX_THREADS=5
+
 EXPOSE 8080
 EXPOSE 8080
 
 
-CMD bundle exec puma -C config/puma.rb -b tcp://0.0.0.0:8080 -e production
+CMD bundle exec puma -b tcp://0.0.0.0:8080 -e production