|
@@ -2,57 +2,61 @@
|
|
|
|
|
|
# Our Rack application to be executed by rackup
|
|
# Our Rack application to be executed by rackup
|
|
class HelloWorld < Roda
|
|
class HelloWorld < Roda
|
|
- plugin :default_headers, 'Content-Type'=>'text/html; charset=utf-8'
|
|
|
|
- plugin :default_headers, 'Server'=>SERVER_STRING if SERVER_STRING
|
|
|
|
plugin :hooks
|
|
plugin :hooks
|
|
- plugin :json
|
|
|
|
- plugin :render, :escape=>true, :layout_opts=>{ :cache_key=>'default_layout' }
|
|
|
|
|
|
+ plugin :render, escape: true, layout_opts: { cache_key: 'default_layout' }
|
|
plugin :static_routing
|
|
plugin :static_routing
|
|
|
|
|
|
def bounded_queries
|
|
def bounded_queries
|
|
queries = request['queries'].to_i
|
|
queries = request['queries'].to_i
|
|
return QUERIES_MIN if queries < QUERIES_MIN
|
|
return QUERIES_MIN if queries < QUERIES_MIN
|
|
return QUERIES_MAX if queries > QUERIES_MAX
|
|
return QUERIES_MAX if queries > QUERIES_MAX
|
|
|
|
+
|
|
queries
|
|
queries
|
|
end
|
|
end
|
|
|
|
|
|
# Return a random number between 1 and MAX_PK
|
|
# Return a random number between 1 and MAX_PK
|
|
def rand1
|
|
def rand1
|
|
- rand(MAX_PK).succ
|
|
|
|
|
|
+ rand(MAX_PK) + 1
|
|
end
|
|
end
|
|
|
|
|
|
after do
|
|
after do
|
|
response['Date'] = Time.now.httpdate
|
|
response['Date'] = Time.now.httpdate
|
|
|
|
+ response['Server'] = SERVER_STRING if SERVER_STRING
|
|
end
|
|
end
|
|
|
|
|
|
# Test type 1: JSON serialization
|
|
# Test type 1: JSON serialization
|
|
static_get '/json' do |_|
|
|
static_get '/json' do |_|
|
|
- { :message=>'Hello, World!' }
|
|
|
|
|
|
+ response['Content-Type'] = 'application/json'
|
|
|
|
+
|
|
|
|
+ { message: 'Hello, World!' }.to_json
|
|
end
|
|
end
|
|
|
|
|
|
# Test type 2: Single database query
|
|
# Test type 2: Single database query
|
|
static_get '/db' do |_|
|
|
static_get '/db' do |_|
|
|
- World.with_pk(rand1).values
|
|
|
|
|
|
+ response['Content-Type'] = 'application/json'
|
|
|
|
+
|
|
|
|
+ World.with_pk(rand1).values.to_json
|
|
end
|
|
end
|
|
|
|
|
|
# Test type 3: Multiple database queries
|
|
# Test type 3: Multiple database queries
|
|
static_get '/queries' do |_|
|
|
static_get '/queries' do |_|
|
|
- worlds =
|
|
|
|
- DB.synchronize do
|
|
|
|
- Array.new(bounded_queries) do
|
|
|
|
- World.with_pk(rand1)
|
|
|
|
- end
|
|
|
|
|
|
+ response['Content-Type'] = 'application/json'
|
|
|
|
+ worlds = DB.synchronize do
|
|
|
|
+ Array.new(bounded_queries) do
|
|
|
|
+ World.with_pk(rand1).values
|
|
end
|
|
end
|
|
|
|
+ end
|
|
|
|
|
|
- worlds.map!(&:values)
|
|
|
|
|
|
+ worlds.to_json
|
|
end
|
|
end
|
|
|
|
|
|
# Test type 4: Fortunes
|
|
# Test type 4: Fortunes
|
|
static_get '/fortunes' do |_|
|
|
static_get '/fortunes' do |_|
|
|
|
|
+ response['Content-Type'] = 'text/html; charset=utf-8'
|
|
@fortunes = Fortune.all
|
|
@fortunes = Fortune.all
|
|
@fortunes << Fortune.new(
|
|
@fortunes << Fortune.new(
|
|
- :id=>0,
|
|
|
|
- :message=>'Additional fortune added at request time.'
|
|
|
|
|
|
+ id: 0,
|
|
|
|
+ message: 'Additional fortune added at request time.'
|
|
)
|
|
)
|
|
@fortunes.sort_by!(&:message)
|
|
@fortunes.sort_by!(&:message)
|
|
|
|
|
|
@@ -61,16 +65,18 @@ class HelloWorld < Roda
|
|
|
|
|
|
# Test type 5: Database updates
|
|
# Test type 5: Database updates
|
|
static_get '/updates' do |_|
|
|
static_get '/updates' do |_|
|
|
- worlds =
|
|
|
|
- DB.synchronize do
|
|
|
|
- Array.new(bounded_queries) do
|
|
|
|
- world = World.with_pk(rand1)
|
|
|
|
- world.update(:randomnumber=>rand1)
|
|
|
|
- world
|
|
|
|
- end
|
|
|
|
|
|
+ response['Content-Type'] = 'application/json'
|
|
|
|
+ worlds = DB.synchronize do
|
|
|
|
+ Array.new(bounded_queries) do
|
|
|
|
+ world = World.with_pk(rand1)
|
|
|
|
+ new_value = rand1
|
|
|
|
+ new_value = rand1 while new_value == world.randomnumber
|
|
|
|
+ world.update(randomnumber: new_value)
|
|
|
|
+ world.values
|
|
end
|
|
end
|
|
|
|
+ end
|
|
|
|
|
|
- worlds.map!(&:values)
|
|
|
|
|
|
+ worlds.to_json
|
|
end
|
|
end
|
|
|
|
|
|
# Test type 6: Plaintext
|
|
# Test type 6: Plaintext
|