Browse Source

[ruby] Reduce `random_id` calls in queries and update (#9016)

* [ruby/rails] Update trilogy to 2.8.1 and use ssl for connection

This fixes connection errors:

      /usr/local/bundle/gems/activerecord-7.1.3.1/lib/active_record/connection_adapters/trilogy_adapter.rb:61:in
      `rescue in new_client': trilogy_auth_recv: caching_sha2_password
      requires either TCP with TLS or a unix socket: TRILOGY_UNSUPPORTED
      (ActiveRecord::ConnectionNotEstablished)

* [ruby] Reduce `random_id` calls in queries and update

Calling `Array#sample` with a size x is faster than calling `rand` x
times.
Petrik de Heus 1 year ago
parent
commit
2bbfa559d5

+ 6 - 4
frameworks/Ruby/grape/config.ru

@@ -4,6 +4,8 @@ require 'yaml'
 require_relative 'config/auto_tune'
 require_relative 'config/auto_tune'
 
 
 MAX_PK = 10_000
 MAX_PK = 10_000
+ID_RANGE = (1..MAX_PK).freeze
+ALL_IDS = ID_RANGE.to_a
 QUERIES_MIN = 1
 QUERIES_MIN = 1
 QUERIES_MAX = 500
 QUERIES_MAX = 500
 
 
@@ -53,8 +55,8 @@ module Acme
 
 
     get '/query' do
     get '/query' do
       ActiveRecord::Base.connection_pool.with_connection do
       ActiveRecord::Base.connection_pool.with_connection do
-        Array.new(bounded_queries) do
-          World.find(rand1)
+        ALL_IDS.sample(bounded_queries).map do |id|
+          World.find(id)
         end
         end
       end
       end
     end
     end
@@ -62,8 +64,8 @@ module Acme
     get '/updates' do
     get '/updates' do
       worlds =
       worlds =
         ActiveRecord::Base.connection_pool.with_connection do
         ActiveRecord::Base.connection_pool.with_connection do
-          Array.new(bounded_queries) do
-            world = World.find(rand1)
+          ALL_IDS.sample(bounded_queries).map do |id|
+            world = World.find(id)
             new_value = rand1
             new_value = rand1
             new_value = rand1 while new_value == world.randomNumber
             new_value = rand1 while new_value == world.randomNumber
             world.update_columns(randomNumber: new_value)
             world.update_columns(randomNumber: new_value)

+ 2 - 0
frameworks/Ruby/rack-sequel/boot.rb

@@ -3,6 +3,8 @@ require 'bundler/setup'
 require 'time'
 require 'time'
 
 
 MAX_PK = 10_000
 MAX_PK = 10_000
+ID_RANGE = (1..10_000).freeze
+ALL_IDS = ID_RANGE.to_a
 QUERIES_MIN = 1
 QUERIES_MIN = 1
 QUERIES_MAX = 500
 QUERIES_MAX = 500
 SEQUEL_NO_ASSOCIATIONS = true
 SEQUEL_NO_ASSOCIATIONS = true

+ 5 - 5
frameworks/Ruby/rack-sequel/hello_world.rb

@@ -29,8 +29,8 @@ class HelloWorld
 
 
   def queries(env)
   def queries(env)
     DB.synchronize do
     DB.synchronize do
-      Array.new(bounded_queries(env)) do
-        WORLD_BY_ID.(:id=>rand1)
+      ALL_IDS.sample(bounded_queries(env)).map do |id|
+        WORLD_BY_ID.(id: id)
       end
       end
     end
     end
   end
   end
@@ -78,9 +78,9 @@ class HelloWorld
 
 
   def updates(env)
   def updates(env)
     DB.synchronize do
     DB.synchronize do
-      Array.new(bounded_queries(env)) do
-        world = WORLD_BY_ID.(:id=>rand1)
-        WORLD_UPDATE.(:id=>world[:id], :randomnumber=>(world[:randomnumber] = rand1))
+      ALL_IDS.sample(bounded_queries(env)).map do |id|
+        world = WORLD_BY_ID.(id: id)
+        WORLD_UPDATE.(id: world[:id], randomnumber: (world[:randomnumber] = rand1))
         world
         world
       end
       end
     end
     end

+ 1 - 1
frameworks/Ruby/rails/app/controllers/hello_world_controller.rb

@@ -7,7 +7,7 @@ class HelloWorldController < ApplicationController
   MAX_QUERIES = 500          # max number of records that can be retrieved
   MAX_QUERIES = 500          # max number of records that can be retrieved
 
 
   def db
   def db
-    render json: World.find(random_id)
+    render json: World.find(random_id).attributes
   end
   end
 
 
   def query
   def query

+ 2 - 0
frameworks/Ruby/sinatra-sequel/boot.rb

@@ -3,6 +3,8 @@ require 'bundler/setup'
 require 'time'
 require 'time'
 
 
 MAX_PK = 10_000
 MAX_PK = 10_000
+ID_RANGE = (1..MAX_PK).freeze
+ALL_IDS = ID_RANGE.to_a
 QUERIES_MIN = 1
 QUERIES_MIN = 1
 QUERIES_MAX = 500
 QUERIES_MAX = 500
 SEQUEL_NO_ASSOCIATIONS = true
 SEQUEL_NO_ASSOCIATIONS = true

+ 4 - 4
frameworks/Ruby/sinatra-sequel/hello_world.rb

@@ -56,8 +56,8 @@ class HelloWorld < Sinatra::Base
   get '/queries' do
   get '/queries' do
     worlds =
     worlds =
       DB.synchronize do
       DB.synchronize do
-        Array.new(bounded_queries) do
-          World.with_pk(rand1)
+        ALL_IDS.sample(bounded_queries).map do |id|
+          World.with_pk(id)
         end
         end
       end
       end
 
 
@@ -80,8 +80,8 @@ class HelloWorld < Sinatra::Base
   get '/updates' do
   get '/updates' do
     worlds =
     worlds =
       DB.synchronize do
       DB.synchronize do
-        Array.new(bounded_queries) do
-          world = World.with_pk(rand1)
+        ALL_IDS.sample(bounded_queries).map do |id|
+          world = World.with_pk(id)
           new_value = rand1
           new_value = rand1
           new_value = rand1 while new_value == world.randomnumber
           new_value = rand1 while new_value == world.randomnumber
           world.update(randomnumber: new_value)
           world.update(randomnumber: new_value)

+ 2 - 0
frameworks/Ruby/sinatra/boot.rb

@@ -3,6 +3,8 @@ require 'bundler/setup'
 require 'time'
 require 'time'
 
 
 MAX_PK = 10_000
 MAX_PK = 10_000
+ID_RANGE = (1..MAX_PK).freeze
+ALL_IDS = ID_RANGE.to_a
 QUERIES_MIN = 1
 QUERIES_MIN = 1
 QUERIES_MAX = 500
 QUERIES_MAX = 500
 
 

+ 7 - 7
frameworks/Ruby/sinatra/hello_world.rb

@@ -62,12 +62,12 @@ class HelloWorld < Sinatra::Base
   get '/queries' do
   get '/queries' do
     worlds =
     worlds =
       ActiveRecord::Base.connection_pool.with_connection do
       ActiveRecord::Base.connection_pool.with_connection do
-        Array.new(bounded_queries) do
-          World.find(rand1)
+        ALL_IDS.sample(bounded_queries).map do |id|
+          World.find(id).attributes
         end
         end
       end
       end
 
 
-    json worlds.map!(&:attributes)
+    json worlds
   end
   end
 
 
   # Test type 4: Fortunes
   # Test type 4: Fortunes
@@ -88,16 +88,16 @@ class HelloWorld < Sinatra::Base
   get '/updates' do
   get '/updates' do
     worlds =
     worlds =
       ActiveRecord::Base.connection_pool.with_connection do
       ActiveRecord::Base.connection_pool.with_connection do
-        Array.new(bounded_queries) do
-          world = World.find(rand1)
+        ALL_IDS.sample(bounded_queries).map do |id|
+          world = World.find(id)
           new_value = rand1
           new_value = rand1
           new_value = rand1 while new_value == world.randomnumber
           new_value = rand1 while new_value == world.randomnumber
           world.update_columns(randomnumber: new_value)
           world.update_columns(randomnumber: new_value)
-          world
+          world.attributes
         end
         end
       end
       end
 
 
-    json worlds.map!(&:attributes)
+    json worlds
   end
   end
 
 
   # Test type 6: Plaintext
   # Test type 6: Plaintext