|
@@ -1,62 +1,65 @@
|
|
-MAX_PK = 10_000
|
|
|
|
-QUERIES_MIN = 1
|
|
|
|
-QUERIES_MAX = 500
|
|
|
|
|
|
+QUERY_RANGE = (1..10_000).freeze
|
|
|
|
+ALL_IDS = QUERY_RANGE.to_a
|
|
|
|
|
|
HelloWorld::App.controllers do
|
|
HelloWorld::App.controllers do
|
|
|
|
+
|
|
|
|
+ after do
|
|
|
|
+ response['Server'] = 'padrino'
|
|
|
|
+ end
|
|
|
|
+
|
|
|
|
+ after do
|
|
|
|
+ response['Date'] = Time.now.httpdate
|
|
|
|
+ end if defined?(Puma)
|
|
|
|
+
|
|
get '/json', :provides => [:json] do
|
|
get '/json', :provides => [:json] do
|
|
- response.headers['Server'] = 'padrino'
|
|
|
|
- response.headers['Date'] = Time.now.httpdate
|
|
|
|
{message: "Hello, World!"}.to_json
|
|
{message: "Hello, World!"}.to_json
|
|
end
|
|
end
|
|
|
|
|
|
get '/db', :provides => [:json] do
|
|
get '/db', :provides => [:json] do
|
|
- response.headers['Server'] = 'padrino'
|
|
|
|
- response.headers['Date'] = Time.now.httpdate
|
|
|
|
- id = Random.rand(MAX_PK) + 1
|
|
|
|
- World.get(id).attributes.to_json
|
|
|
|
|
|
+ world = ActiveRecord::Base.with_connection do
|
|
|
|
+ World.find(rand1).attributes
|
|
|
|
+ end
|
|
|
|
+ world.to_json
|
|
end
|
|
end
|
|
|
|
|
|
get '/queries', :provides => [:json] do
|
|
get '/queries', :provides => [:json] do
|
|
- response.headers['Server'] = 'padrino'
|
|
|
|
- response.headers['Date'] = Time.now.httpdate
|
|
|
|
- queries = params['queries'].to_i.clamp(QUERIES_MIN, QUERIES_MAX)
|
|
|
|
-
|
|
|
|
- results = (1..queries).map do
|
|
|
|
- World.get(Random.rand(MAX_PK) + 1).attributes
|
|
|
|
- end.to_json
|
|
|
|
|
|
+ worlds = ActiveRecord::Base.with_connection do
|
|
|
|
+ ALL_IDS.sample(bounded_queries).map do |id|
|
|
|
|
+ World.find(id).attributes
|
|
|
|
+ end
|
|
|
|
+ end
|
|
|
|
+ worlds.to_json
|
|
end
|
|
end
|
|
|
|
|
|
get '/fortunes' do
|
|
get '/fortunes' do
|
|
- response.headers['Server'] = 'padrino'
|
|
|
|
- response.headers['Date'] = Time.now.httpdate
|
|
|
|
- @fortunes = Fortune.all
|
|
|
|
- @fortunes << Fortune.new(:id => 0, :message => "Additional fortune added at request time.")
|
|
|
|
- @fortunes = @fortunes.sort_by { |x| x.message }
|
|
|
|
|
|
+ @fortunes = Fortune.all.to_a
|
|
|
|
+ @fortunes << Fortune.new(
|
|
|
|
+ id: 0,
|
|
|
|
+ message: "Additional fortune added at request time."
|
|
|
|
+ )
|
|
|
|
+ @fortunes = @fortunes.sort_by(&:message)
|
|
|
|
|
|
render 'fortunes', layout: "layout"
|
|
render 'fortunes', layout: "layout"
|
|
end
|
|
end
|
|
|
|
|
|
get '/updates', :provides => [:json] do
|
|
get '/updates', :provides => [:json] do
|
|
- response.headers['Server'] = 'padrino'
|
|
|
|
- response.headers['Date'] = Time.now.httpdate
|
|
|
|
- queries = params['queries'].to_i.clamp(QUERIES_MIN, QUERIES_MAX)
|
|
|
|
-
|
|
|
|
- worlds = (1..queries).map do
|
|
|
|
- # get a random row from the database, which we know has 10000
|
|
|
|
- # rows with ids 1 - 10000
|
|
|
|
- world = World.get(Random.rand(MAX_PK) + 1)
|
|
|
|
- world.update(randomNumber: Random.rand(MAX_PK) + 1)
|
|
|
|
- world.attributes
|
|
|
|
|
|
+ worlds = []
|
|
|
|
+ ActiveRecord::Base.with_connection do
|
|
|
|
+ worlds = ALL_IDS.sample(bounded_queries).map do |id|
|
|
|
|
+ world = World.find(id)
|
|
|
|
+ new_value = rand1
|
|
|
|
+ new_value = rand1 while new_value == world.randomNumber
|
|
|
|
+ world.randomNumber = new_value
|
|
|
|
+ world
|
|
|
|
+ end
|
|
|
|
+ World.upsert_all(worlds)
|
|
end
|
|
end
|
|
|
|
|
|
worlds.to_json
|
|
worlds.to_json
|
|
end
|
|
end
|
|
|
|
|
|
get '/plaintext' do
|
|
get '/plaintext' do
|
|
- response.headers['Server'] = 'padrino'
|
|
|
|
- response.headers['Date'] = Time.now.httpdate
|
|
|
|
content_type 'text/plain'
|
|
content_type 'text/plain'
|
|
"Hello, World!"
|
|
"Hello, World!"
|
|
end
|
|
end
|
|
-
|
|
|
|
end
|
|
end
|