Skip to content
This repository was archived by the owner on Mar 24, 2026. It is now read-only.

Commit d17452e

Browse files
authored
[ruby/grape] Use postgres for the database (#10598)
For MySQL batch update isn't supported, so test it with PostgreSQL instead for better comparisont. Also rename `boot.rb` to `hello_world.rb` and extract database logic to `db.rb` like other Ruby tests.
1 parent c510928 commit d17452e

7 files changed

Lines changed: 51 additions & 35 deletions

File tree

frameworks/Ruby/grape/Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
source 'https://rubygems.org'
22

3-
gem 'trilogy', '~> 2.9'
43
gem 'activerecord', '~> 8.1.0', :require => 'active_record'
54
gem 'grape', '~> 3.0'
5+
gem 'pg'
66
gem 'json', '~> 2.9'
77

88
group :iodine, optional: true do

frameworks/Ruby/grape/Gemfile.lock

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,15 @@ GEM
6363
mustermann-grape (1.1.0)
6464
mustermann (>= 1.0.0)
6565
nio4r (2.7.4)
66+
pg (1.6.3)
67+
pg (1.6.3-x86_64-darwin)
68+
pg (1.6.3-x86_64-linux)
6669
puma (7.1.0)
6770
nio4r (~> 2.0)
6871
rack (3.2.4)
6972
ruby2_keywords (0.0.5)
7073
securerandom (0.4.1)
7174
timeout (0.4.4)
72-
trilogy (2.9.0)
7375
tzinfo (2.0.6)
7476
concurrent-ruby (~> 1.0)
7577
uri (1.1.1)
@@ -85,8 +87,8 @@ DEPENDENCIES
8587
grape (~> 3.0)
8688
iodine (~> 0.7)
8789
json (~> 2.9)
90+
pg
8891
puma (~> 7.1)
89-
trilogy (~> 2.9)
9092

9193
BUNDLED WITH
9294
4.0.3

frameworks/Ruby/grape/benchmark_config.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"port": 8080,
1111
"approach": "Realistic",
1212
"classification": "Micro",
13-
"database": "MySQL",
13+
"database": "Postgres",
1414
"framework": "grape",
1515
"language": "Ruby",
1616
"orm": "Full",
@@ -31,7 +31,7 @@
3131
"port": 8080,
3232
"approach": "Realistic",
3333
"classification": "Micro",
34-
"database": "MySQL",
34+
"database": "Postgres",
3535
"framework": "grape",
3636
"language": "Ruby",
3737
"orm": "Full",

frameworks/Ruby/grape/config.ru

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1-
require_relative 'boot'
1+
# frozen_string_literal: true
2+
3+
require_relative 'hello_world'
4+
25
run Acme::API
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
production:
2-
adapter: trilogy
2+
adapter: postgresql
33
encoding: utf8
44
host: tfb-database
55
database: hello_world
66
username: benchmarkdbuser
77
password: benchmarkdbpass
88
pool: <%= ENV.fetch('MAX_THREADS') %>
99
timeout: 5000
10-
ssl: true
11-
ssl_mode: 4 <%# Trilogy::SSL_PREFERRED_NOVERIFY %>
12-
tls_min_version: 3 <%# Trilogy::TLS_VERSION_12 %>
10+
encoding: unicode

frameworks/Ruby/grape/db.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# frozen_string_literal: true
2+
3+
require 'erb'
4+
require 'active_record'
5+
require 'yaml'
6+
7+
db_config = YAML.load(ERB.new(File.read('config/database.yml')).result)[ENV['RACK_ENV']]
8+
ActiveRecord::Base.establish_connection(db_config)
9+
10+
class World < ActiveRecord::Base
11+
self.table_name = 'World'
12+
13+
alias_attribute(:randomNumber, :randomnumber) \
14+
if connection.adapter_name.downcase.start_with?('postgres')
15+
end
16+
Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,15 @@
1-
require 'erb'
2-
require 'active_record'
3-
require 'yaml'
4-
5-
MAX_PK = 10_000
6-
ID_RANGE = (1..MAX_PK).freeze
7-
ALL_IDS = ID_RANGE.to_a
8-
QUERIES_MIN = 1
9-
QUERIES_MAX = 500
10-
1+
require 'bundler/setup'
112
Bundler.require :default
123

13-
db_config = YAML.load(ERB.new(File.read('config/database.yml')).result)[ENV['RACK_ENV']]
14-
ActiveRecord::Base.establish_connection(db_config)
15-
16-
class World < ActiveRecord::Base
17-
self.table_name = 'World'
18-
end
4+
require_relative 'db'
195

206
module Acme
7+
MAX_PK = 10_000
8+
ID_RANGE = (1..MAX_PK).freeze
9+
ALL_IDS = ID_RANGE.to_a
10+
QUERIES_MIN = 1
11+
QUERIES_MAX = 500
12+
2113
class HelloWorld < Grape::API
2214
get '/json' do
2315
{message:'Hello, World!'}
@@ -62,16 +54,21 @@ def rand1
6254
end
6355

6456
get '/updates' do
65-
worlds =
66-
ActiveRecord::Base.with_connection do
67-
ALL_IDS.sample(bounded_queries).map do |id|
68-
world = World.find(id)
69-
new_value = rand1
70-
new_value = rand1 while new_value == world.randomNumber
71-
world.update_columns(randomNumber: new_value)
72-
world
73-
end
57+
worlds = nil
58+
ids = ALL_IDS.sample(bounded_queries)
59+
ActiveRecord::Base.with_connection do
60+
worlds = ids.map do |id|
61+
world = World.find(id)
62+
new_value = rand1
63+
new_value = rand1 until new_value != world.randomNumber
64+
{ id: id, randomnumber: new_value }
7465
end
66+
end
67+
worlds.sort_by!{_1[:id]}
68+
ActiveRecord::Base.with_connection do
69+
World.upsert_all(worlds)
70+
end
71+
worlds
7572
end
7673
end
7774

0 commit comments

Comments
 (0)