Skip to content

Commit 2bbfa55

Browse files
authored
[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.
1 parent 1ed28bf commit 2bbfa55

File tree

8 files changed

+29
-21
lines changed

8 files changed

+29
-21
lines changed

frameworks/Ruby/grape/config.ru

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ require 'yaml'
44
require_relative 'config/auto_tune'
55

66
MAX_PK = 10_000
7+
ID_RANGE = (1..MAX_PK).freeze
8+
ALL_IDS = ID_RANGE.to_a
79
QUERIES_MIN = 1
810
QUERIES_MAX = 500
911

@@ -53,17 +55,17 @@ module Acme
5355

5456
get '/query' do
5557
ActiveRecord::Base.connection_pool.with_connection do
56-
Array.new(bounded_queries) do
57-
World.find(rand1)
58+
ALL_IDS.sample(bounded_queries).map do |id|
59+
World.find(id)
5860
end
5961
end
6062
end
6163

6264
get '/updates' do
6365
worlds =
6466
ActiveRecord::Base.connection_pool.with_connection do
65-
Array.new(bounded_queries) do
66-
world = World.find(rand1)
67+
ALL_IDS.sample(bounded_queries).map do |id|
68+
world = World.find(id)
6769
new_value = rand1
6870
new_value = rand1 while new_value == world.randomNumber
6971
world.update_columns(randomNumber: new_value)

frameworks/Ruby/rack-sequel/boot.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
require 'time'
44

55
MAX_PK = 10_000
6+
ID_RANGE = (1..10_000).freeze
7+
ALL_IDS = ID_RANGE.to_a
68
QUERIES_MIN = 1
79
QUERIES_MAX = 500
810
SEQUEL_NO_ASSOCIATIONS = true

frameworks/Ruby/rack-sequel/hello_world.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ def db
2929

3030
def queries(env)
3131
DB.synchronize do
32-
Array.new(bounded_queries(env)) do
33-
WORLD_BY_ID.(:id=>rand1)
32+
ALL_IDS.sample(bounded_queries(env)).map do |id|
33+
WORLD_BY_ID.(id: id)
3434
end
3535
end
3636
end
@@ -78,9 +78,9 @@ def fortunes
7878

7979
def updates(env)
8080
DB.synchronize do
81-
Array.new(bounded_queries(env)) do
82-
world = WORLD_BY_ID.(:id=>rand1)
83-
WORLD_UPDATE.(:id=>world[:id], :randomnumber=>(world[:randomnumber] = rand1))
81+
ALL_IDS.sample(bounded_queries(env)).map do |id|
82+
world = WORLD_BY_ID.(id: id)
83+
WORLD_UPDATE.(id: world[:id], randomnumber: (world[:randomnumber] = rand1))
8484
world
8585
end
8686
end

frameworks/Ruby/rails/app/controllers/hello_world_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class HelloWorldController < ApplicationController
77
MAX_QUERIES = 500 # max number of records that can be retrieved
88

99
def db
10-
render json: World.find(random_id)
10+
render json: World.find(random_id).attributes
1111
end
1212

1313
def query

frameworks/Ruby/sinatra-sequel/boot.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
require 'time'
44

55
MAX_PK = 10_000
6+
ID_RANGE = (1..MAX_PK).freeze
7+
ALL_IDS = ID_RANGE.to_a
68
QUERIES_MIN = 1
79
QUERIES_MAX = 500
810
SEQUEL_NO_ASSOCIATIONS = true

frameworks/Ruby/sinatra-sequel/hello_world.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ def rand1
5656
get '/queries' do
5757
worlds =
5858
DB.synchronize do
59-
Array.new(bounded_queries) do
60-
World.with_pk(rand1)
59+
ALL_IDS.sample(bounded_queries).map do |id|
60+
World.with_pk(id)
6161
end
6262
end
6363

@@ -80,8 +80,8 @@ def rand1
8080
get '/updates' do
8181
worlds =
8282
DB.synchronize do
83-
Array.new(bounded_queries) do
84-
world = World.with_pk(rand1)
83+
ALL_IDS.sample(bounded_queries).map do |id|
84+
world = World.with_pk(id)
8585
new_value = rand1
8686
new_value = rand1 while new_value == world.randomnumber
8787
world.update(randomnumber: new_value)

frameworks/Ruby/sinatra/boot.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
require 'time'
44

55
MAX_PK = 10_000
6+
ID_RANGE = (1..MAX_PK).freeze
7+
ALL_IDS = ID_RANGE.to_a
68
QUERIES_MIN = 1
79
QUERIES_MAX = 500
810

frameworks/Ruby/sinatra/hello_world.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,12 @@ def rand1
6262
get '/queries' do
6363
worlds =
6464
ActiveRecord::Base.connection_pool.with_connection do
65-
Array.new(bounded_queries) do
66-
World.find(rand1)
65+
ALL_IDS.sample(bounded_queries).map do |id|
66+
World.find(id).attributes
6767
end
6868
end
6969

70-
json worlds.map!(&:attributes)
70+
json worlds
7171
end
7272

7373
# Test type 4: Fortunes
@@ -88,16 +88,16 @@ def rand1
8888
get '/updates' do
8989
worlds =
9090
ActiveRecord::Base.connection_pool.with_connection do
91-
Array.new(bounded_queries) do
92-
world = World.find(rand1)
91+
ALL_IDS.sample(bounded_queries).map do |id|
92+
world = World.find(id)
9393
new_value = rand1
9494
new_value = rand1 while new_value == world.randomnumber
9595
world.update_columns(randomnumber: new_value)
96-
world
96+
world.attributes
9797
end
9898
end
9999

100-
json worlds.map!(&:attributes)
100+
json worlds
101101
end
102102

103103
# Test type 6: Plaintext

0 commit comments

Comments
 (0)