forked from TechEmpower/FrameworkBenchmarks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhello_world.rb
More file actions
151 lines (135 loc) · 3.63 KB
/
hello_world.rb
File metadata and controls
151 lines (135 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# frozen_string_literal: true
# Our Rack application to be executed by rackup
if RUBY_PLATFORM == 'java'
require_relative 'db_jruby'
else
require_relative 'db'
end
require_relative 'config/auto_tune'
require 'rack'
require 'json'
require 'time'
require 'erb'
DATABASE_URL = 'postgresql://tfb-database/hello_world?user=benchmarkdbuser&password=benchmarkdbpass'
$db = connect(DATABASE_URL)
class HelloWorld
QUERY_RANGE = (1..10_000).freeze # range of IDs in the Fortune DB
ALL_IDS = QUERY_RANGE.to_a # enumeration of all the IDs in fortune DB
MIN_QUERIES = 1 # min number of records that can be retrieved
MAX_QUERIES = 500 # max number of records that can be retrieved
CONTENT_TYPE = 'Content-Type'
JSON_TYPE = 'application/json'
HTML_TYPE = 'text/html; charset=utf-8'
PLAINTEXT_TYPE = 'text/plain'
DATE = 'Date'
SERVER = 'Server'
SERVER_STRING = 'Rack'
TEMPLATE_PREFIX = <<~HTML
<!DOCTYPE html>
<html>
<head>
<title>Fortunes</title>
</head>
<body>
<table>
<tr>
<th>id</th>
<th>message</th>
</tr>
HTML
TEMPLATE_POSTFIX = <<~HTML
</table>
</body>
</html>
HTML
def call(env)
case env['PATH_INFO']
when '/json'
# Test type 1: JSON serialization
respond JSON_TYPE,
JSON.generate({ message: 'Hello, World!' })
when '/db'
# Test type 2: Single database query
id = random_id
respond JSON_TYPE, JSON.generate($db.with{ _1.select_world(id) })
when '/queries'
# Test type 3: Multiple database queries
queries = bounded_queries(env)
respond JSON_TYPE, JSON.generate(select_worlds(queries))
when '/fortunes'
# Test type 4: Fortunes
respond HTML_TYPE, fortunes
when '/updates'
# Test type 5: Database updates
queries = bounded_queries(env)
respond JSON_TYPE, JSON.generate(update_worlds(queries))
when '/plaintext'
# Test type 6: Plaintext
respond PLAINTEXT_TYPE, 'Hello, World!'
end
end
private
def respond(content_type, body)
[
200,
headers(content_type),
[body]
]
end
if defined?(Puma) || defined?(Falcon)
def headers(content_type)
{
CONTENT_TYPE => content_type,
SERVER => SERVER_STRING,
DATE => Time.now.httpdate
}
end
else
def headers(content_type)
{
CONTENT_TYPE => content_type,
SERVER => SERVER_STRING
}
end
end
def fortunes
fortunes = $db.with(&:select_fortunes).map(&:to_h)
fortunes << { 'id' => 0, 'message' => 'Additional fortune added at request time.' }
fortunes.sort_by! { |item| item['message'] }
buffer = String.new
buffer << TEMPLATE_PREFIX
fortunes.each do |item|
buffer << "<tr><td>#{item['id']}</td><td>#{ERB::Escape.html_escape(item['message'])}</td></tr>"
end
buffer << TEMPLATE_POSTFIX
end
def update_worlds(count)
results = select_worlds(count)
ids = []
sql = String.new("UPDATE world SET randomnumber = CASE id ")
results.each do |r|
r['randomnumber'] = random_id
ids << r['id']
sql << "when #{r['id']} then #{r['randomnumber']} "
end
sql << "ELSE randomnumber END WHERE id IN ( #{ids.join(',')})"
$db.with{ _1.exec(sql) }
results
end
def select_worlds(count)
ids = ALL_IDS.sample(count)
$db.with do |conn|
ids.map do |id|
conn.select_world(id)
end
end
end
def random_id
Random.rand(QUERY_RANGE)
end
def bounded_queries(env)
params = Rack::Utils.parse_query(env['QUERY_STRING'])
queries = params['queries'].to_i
queries.clamp(MIN_QUERIES, MAX_QUERIES)
end
end