Browse Source

[ruby/grape] Upgrade Ruby to 3.3-rc and enable YJIT (#8465)

This also removes some warning by copying the database call
implementations from sinatra and roda.
Petrik de Heus 1 year ago
parent
commit
6d05fbab35

+ 1 - 1
frameworks/Ruby/grape/README.md

@@ -11,7 +11,7 @@ comparing a variety of web servers.
 ## Infrastructure Software Versions
 ## Infrastructure Software Versions
 The tests were run with:
 The tests were run with:
 
 
-* [Ruby 3.1](http://www.ruby-lang.org/)
+* [Ruby 3.3-rc1](http://www.ruby-lang.org/)
 * [Grape 1.6.2](http://www.ruby-grape.org/)
 * [Grape 1.6.2](http://www.ruby-grape.org/)
 * [Rack 2.2.3.1](https://rack.github.io/)
 * [Rack 2.2.3.1](https://rack.github.io/)
 * [Unicorn 6.1.0](https://yhbt.net/unicorn/)
 * [Unicorn 6.1.0](https://yhbt.net/unicorn/)

+ 30 - 20
frameworks/Ruby/grape/config.ru

@@ -2,6 +2,10 @@ require 'erb'
 require 'active_record'
 require 'active_record'
 require 'yaml'
 require 'yaml'
 
 
+MAX_PK = 10_000
+QUERIES_MIN = 1
+QUERIES_MAX = 500
+
 Bundler.require :default
 Bundler.require :default
 
 
 db_config = YAML.load(ERB.new(File.read('config/database.yml')).result)[ENV['RACK_ENV']]
 db_config = YAML.load(ERB.new(File.read('config/database.yml')).result)[ENV['RACK_ENV']]
@@ -28,38 +32,45 @@ module Acme
   end
   end
 
 
   class DatabaseQueries < Grape::API
   class DatabaseQueries < Grape::API
+    helpers do
+      def bounded_queries
+        queries = params[:queries].to_i
+        return QUERIES_MIN if queries < QUERIES_MIN
+        return QUERIES_MAX if queries > QUERIES_MAX
+        queries
+      end
+
+      # Return a random number between 1 and MAX_PK
+      def rand1
+        rand(MAX_PK).succ
+      end
+    end
+
     get '/db' do
     get '/db' do
       ActiveRecord::Base.connection_pool.with_connection do
       ActiveRecord::Base.connection_pool.with_connection do
-        World.find(Random.rand(10000) + 1)
+        World.find(rand1).attributes
       end
       end
     end
     end
 
 
     get '/query' do
     get '/query' do
-      queries = params[:queries].to_i
-      queries = 1 if queries < 1
-      queries = 500 if queries > 500
-
       ActiveRecord::Base.connection_pool.with_connection do
       ActiveRecord::Base.connection_pool.with_connection do
-        (1..queries).map do
-          World.find(Random.rand(10000) + 1)
+        Array.new(bounded_queries) do
+          World.find(rand1)
         end
         end
       end
       end
     end
     end
 
 
     get '/updates' do
     get '/updates' do
-      queries = params[:queries].to_i
-      queries = 1 if queries < 1
-      queries = 500 if queries > 500
-
-      ActiveRecord::Base.connection_pool.with_connection do
-        worlds = (1..queries).map do
-          world = World.find(Random.rand(10000) + 1)
-          world.randomNumber = Random.rand(10000) + 1
-          World.update(world.id, :randomNumber => world.randomNumber)
-          world
+      worlds =
+        ActiveRecord::Base.connection_pool.with_connection do
+          Array.new(bounded_queries) do
+            world = World.find(rand1)
+            new_value = rand1
+            new_value = rand1 while new_value == world.randomNumber
+            world.update(randomNumber: new_value)
+            world
+          end
         end
         end
-        worlds
-      end
     end
     end
   end
   end
 
 
@@ -68,7 +79,6 @@ module Acme
       header 'Date', Time.now.httpdate
       header 'Date', Time.now.httpdate
       header 'Server', 'WebServer'
       header 'Server', 'WebServer'
     end
     end
-    
     content_type :json, 'application/json'
     content_type :json, 'application/json'
     format :json
     format :json
 
 

+ 3 - 1
frameworks/Ruby/grape/grape-unicorn.dockerfile

@@ -1,4 +1,6 @@
-FROM ruby:3.1
+FROM ruby:3.3-rc
+
+ENV RUBY_YJIT_ENABLE=1
 
 
 RUN apt-get update -yqq && apt-get install -yqq nginx
 RUN apt-get update -yqq && apt-get install -yqq nginx
 
 

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

@@ -1,4 +1,6 @@
-FROM ruby:3.1
+FROM ruby:3.3-rc
+
+ENV RUBY_YJIT_ENABLE=1
 
 
 ADD ./ /grape
 ADD ./ /grape