Skip to content

Commit 8501d7e

Browse files
authored
[ruby/hanami] Add support for queries and updates tests (#9657)
1 parent 6bebca6 commit 8501d7e

File tree

10 files changed

+259
-13
lines changed

10 files changed

+259
-13
lines changed

frameworks/Ruby/hanami/.env

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
HANAMI_ENV=production
2+
HANAMI_PORT=8080
3+
DATABASE_URL=postgres://benchmarkdbuser:benchmarkdbpass@tfb-database:5432/hello_world

frameworks/Ruby/hanami/Gemfile.lock

+166
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
GEM
2+
remote: https://rubygems.org/
3+
specs:
4+
bigdecimal (3.1.9)
5+
concurrent-ruby (1.3.4)
6+
dry-auto_inject (1.1.0)
7+
dry-core (~> 1.1)
8+
zeitwerk (~> 2.6)
9+
dry-cli (1.2.0)
10+
dry-configurable (1.3.0)
11+
dry-core (~> 1.1)
12+
zeitwerk (~> 2.6)
13+
dry-core (1.1.0)
14+
concurrent-ruby (~> 1.0)
15+
logger
16+
zeitwerk (~> 2.6)
17+
dry-events (1.1.0)
18+
concurrent-ruby (~> 1.0)
19+
dry-core (~> 1.1)
20+
dry-files (1.1.0)
21+
dry-inflector (1.2.0)
22+
dry-initializer (3.2.0)
23+
dry-logger (1.0.4)
24+
dry-logic (1.6.0)
25+
bigdecimal
26+
concurrent-ruby (~> 1.0)
27+
dry-core (~> 1.1)
28+
zeitwerk (~> 2.6)
29+
dry-monitor (1.0.1)
30+
dry-configurable (~> 1.0, < 2)
31+
dry-core (~> 1.0, < 2)
32+
dry-events (~> 1.0, < 2)
33+
dry-schema (1.13.4)
34+
concurrent-ruby (~> 1.0)
35+
dry-configurable (~> 1.0, >= 1.0.1)
36+
dry-core (~> 1.0, < 2)
37+
dry-initializer (~> 3.0)
38+
dry-logic (>= 1.4, < 2)
39+
dry-types (>= 1.7, < 2)
40+
zeitwerk (~> 2.6)
41+
dry-struct (1.7.0)
42+
dry-core (~> 1.1)
43+
dry-types (~> 1.8)
44+
ice_nine (~> 0.11)
45+
zeitwerk (~> 2.6)
46+
dry-system (1.2.0)
47+
dry-auto_inject (~> 1.1)
48+
dry-configurable (~> 1.3)
49+
dry-core (~> 1.1)
50+
dry-inflector (~> 1.1)
51+
dry-transformer (1.0.1)
52+
zeitwerk (~> 2.6)
53+
dry-types (1.8.0)
54+
bigdecimal (~> 3.0)
55+
concurrent-ruby (~> 1.0)
56+
dry-core (~> 1.0)
57+
dry-inflector (~> 1.0)
58+
dry-logic (~> 1.4)
59+
zeitwerk (~> 2.6)
60+
dry-validation (1.10.0)
61+
concurrent-ruby (~> 1.0)
62+
dry-core (~> 1.0, < 2)
63+
dry-initializer (~> 3.0)
64+
dry-schema (>= 1.12, < 2)
65+
zeitwerk (~> 2.6)
66+
hanami (2.2.1)
67+
bundler (>= 1.16, < 3)
68+
dry-configurable (~> 1.0, >= 1.2.0, < 2)
69+
dry-core (~> 1.0, < 2)
70+
dry-inflector (~> 1.0, >= 1.1.0, < 2)
71+
dry-logger (~> 1.0, < 2)
72+
dry-monitor (~> 1.0, >= 1.0.1, < 2)
73+
dry-system (~> 1.1)
74+
hanami-cli (~> 2.2.1)
75+
hanami-utils (~> 2.2)
76+
json (>= 2.7.2)
77+
zeitwerk (~> 2.6)
78+
hanami-cli (2.2.1)
79+
bundler (~> 2.1)
80+
dry-cli (~> 1.0, >= 1.1.0)
81+
dry-files (~> 1.0, >= 1.0.2, < 2)
82+
dry-inflector (~> 1.0, < 2)
83+
rake (~> 13.0)
84+
zeitwerk (~> 2.6)
85+
hanami-controller (2.1.0)
86+
dry-configurable (~> 1.0, < 2)
87+
dry-core (~> 1.0)
88+
hanami-utils (~> 2.1)
89+
rack (~> 2.0)
90+
zeitwerk (~> 2.6)
91+
hanami-router (2.1.0)
92+
mustermann (~> 3.0)
93+
mustermann-contrib (~> 3.0)
94+
rack (~> 2.0)
95+
hanami-utils (2.2.0)
96+
concurrent-ruby (~> 1.0)
97+
dry-core (~> 1.0, < 2)
98+
dry-transformer (~> 1.0, < 2)
99+
hanami-validations (2.1.0)
100+
dry-validation (>= 1.10, < 2)
101+
zeitwerk (~> 2.6.0)
102+
hansi (0.2.1)
103+
ice_nine (0.11.2)
104+
json (2.9.1)
105+
logger (1.6.4)
106+
mustermann (3.0.3)
107+
ruby2_keywords (~> 0.0.1)
108+
mustermann-contrib (3.0.3)
109+
hansi (~> 0.2.0)
110+
mustermann (= 3.0.3)
111+
nio4r (2.7.4)
112+
pg (1.5.9)
113+
puma (6.5.0)
114+
nio4r (~> 2.0)
115+
rack (2.2.9)
116+
rake (13.2.1)
117+
rom (5.3.2)
118+
rom-changeset (~> 5.3, >= 5.3.0)
119+
rom-core (~> 5.3, >= 5.3.2)
120+
rom-repository (~> 5.3, >= 5.3.0)
121+
rom-changeset (5.3.0)
122+
dry-core (~> 1.0)
123+
rom-core (~> 5.3)
124+
transproc (~> 1.0, >= 1.1.0)
125+
rom-core (5.3.2)
126+
concurrent-ruby (~> 1.1)
127+
dry-configurable (~> 1.0)
128+
dry-core (~> 1.0)
129+
dry-inflector (~> 1.0)
130+
dry-initializer (~> 3.0, >= 3.0.1)
131+
dry-struct (~> 1.0)
132+
dry-types (~> 1.6)
133+
transproc (~> 1.0, >= 1.1.0)
134+
rom-repository (5.3.0)
135+
dry-core (~> 1.0)
136+
dry-initializer (~> 3.0, >= 3.0.1)
137+
rom-core (~> 5.3, >= 5.3.0)
138+
rom-sql (3.6.5)
139+
dry-core (~> 1.0)
140+
dry-types (~> 1.0)
141+
rom (~> 5.2, >= 5.2.1)
142+
sequel (>= 4.49)
143+
ruby2_keywords (0.0.5)
144+
sequel (5.88.0)
145+
bigdecimal
146+
transproc (1.1.1)
147+
zeitwerk (2.6.18)
148+
149+
PLATFORMS
150+
ruby
151+
x86_64-darwin-23
152+
153+
DEPENDENCIES
154+
dry-types (~> 1.0, >= 1.6.1)
155+
hanami (~> 2.0)
156+
hanami-controller (~> 2.0)
157+
hanami-router (~> 2.0)
158+
hanami-validations (~> 2.0)
159+
pg
160+
puma
161+
rake
162+
rom (~> 5.3)
163+
rom-sql (~> 3.6)
164+
165+
BUNDLED WITH
166+
2.5.16

frameworks/Ruby/hanami/app/actions/db/index.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Index < HelloWorld::Action
88
include Deps["persistence.rom"]
99

1010
def handle(*, response)
11-
world = rom.relations[:World].where(id: random_id).one
11+
world = rom.relations[:World].by_pk(random_id).one
1212
response.headers['Server'] = 'hanami'
1313
response.headers['Date'] = Time.now.httpdate
1414
response.format = :json
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# frozen_string_literal: true
2+
3+
module HelloWorld
4+
module Actions
5+
module Queries
6+
class Index < HelloWorld::Action
7+
QUERY_RANGE = 1..10_000 # range of IDs in the Fortune DB
8+
ALL_IDS = QUERY_RANGE.to_a # enumeration of all the IDs in fortune DB
9+
MIN_QUERIES = 1 # min number of records that can be retrieved
10+
MAX_QUERIES = 500 # max number of records that can be retrieved
11+
12+
include Deps["persistence.rom"]
13+
14+
def handle(request, response)
15+
worlds = ALL_IDS.sample(queries(request)).map do |id|
16+
rom.relations[:World].by_pk(id).one
17+
end
18+
response.headers['Server'] = 'hanami'
19+
response.headers['Date'] = Time.now.httpdate
20+
response.format = :json
21+
response.body = worlds.to_json
22+
end
23+
24+
private
25+
26+
def queries(request)
27+
request.params[:queries].to_i.clamp(MIN_QUERIES, MAX_QUERIES)
28+
end
29+
end
30+
end
31+
end
32+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# frozen_string_literal: true
2+
3+
module HelloWorld
4+
module Actions
5+
module Updates
6+
class Index < HelloWorld::Action
7+
QUERY_RANGE = 1..10_000 # range of IDs in the Fortune DB
8+
ALL_IDS = QUERY_RANGE.to_a # enumeration of all the IDs in fortune DB
9+
MIN_QUERIES = 1 # min number of records that can be retrieved
10+
MAX_QUERIES = 500 # max number of records that can be retrieved
11+
12+
include Deps["persistence.rom"]
13+
14+
def handle(request, response)
15+
worlds = ALL_IDS.sample(queries(request)).map do |id|
16+
world = rom.relations[:World].by_pk(id)
17+
world_struct = world.one
18+
new_value = random_id
19+
new_value = random_id while new_value == world_struct[:randomnumber]
20+
world_struct[:randomnumber] = new_value
21+
world.command(:update).call(randomnumber: world_struct[:randomnumber])
22+
world_struct
23+
end
24+
response.headers['Server'] = 'hanami'
25+
response.headers['Date'] = Time.now.httpdate
26+
response.format = :json
27+
response.body = worlds.to_json
28+
end
29+
30+
private
31+
32+
def queries(request)
33+
request.params[:queries].to_i.clamp(MIN_QUERIES, MAX_QUERIES)
34+
end
35+
36+
def random_id
37+
Random.rand(QUERY_RANGE)
38+
end
39+
end
40+
end
41+
end
42+
end

frameworks/Ruby/hanami/benchmark_config.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
"framework": "hanami",
33
"tests": [{
44
"default": {
5-
"db_url": "/db",
65
"json_url": "/json",
6+
"db_url": "/db",
7+
"query_url": "/queries?queries=",
8+
"update_url": "/updates?queries=",
79
"plaintext_url": "/plaintext",
810
"port": 8080,
911
"approach": "Realistic",

frameworks/Ruby/hanami/config/providers/persistence.rb

+8-6
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@
55
require_relative '../auto_tune'
66
num_workers, num_threads = auto_tune
77

8-
opts = {}
9-
10-
if (threads = num_threads) > 1
11-
opts[:max_connections] = (2 * Math.log(threads)).floor
12-
opts[:pool_timeout] = 10
13-
end
8+
opts = {
9+
max_connections: 3,
10+
pool_timeout: 10
11+
}
1412
config = ROM::Configuration.new(:sql, target["settings"].database_url, opts)
1513

1614
register "config", config
@@ -27,4 +25,8 @@
2725

2826
register "rom", ROM.container(config)
2927
end
28+
29+
stop do
30+
target["persistence.rom"].disconnect
31+
end
3032
end

frameworks/Ruby/hanami/config/puma.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
num_workers, num_threads = auto_tune
66

77
workers num_workers
8-
threads num_threads, num_threads
8+
threads 3, 3
99

1010
port ENV.fetch("HANAMI_PORT", 2300)
1111
environment ENV.fetch("HANAMI_ENV", "development")

frameworks/Ruby/hanami/config/routes.rb

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ module HelloWorld
44
class Routes < Hanami::Routes
55
get "/db", to: "db.index"
66
get "/json", to: "json.index"
7+
get "/updates", to: "updates.index"
78
get "/plaintext", to: "plaintext.index"
9+
get "/queries", to: "queries.index"
810
end
911
end

frameworks/Ruby/hanami/hanami.dockerfile

+1-4
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,12 @@ RUN apt-get update && \
77
apt-get install -y --no-install-recommends libjemalloc2
88
ENV LD_PRELOAD=libjemalloc.so.2
99

10+
ADD ./ /hanami
1011
WORKDIR /hanami
1112

12-
COPY Gemfile ./
13-
1413
ENV BUNDLE_FORCE_RUBY_PLATFORM=true
1514
RUN bundle install --jobs=8
1615

17-
COPY . .
18-
1916
EXPOSE 8080
2017

2118
ENV HANAMI_ENV=production

0 commit comments

Comments
 (0)