|
@@ -16,53 +16,50 @@ class HelloWorldController < ApplicationController
|
|
end
|
|
end
|
|
|
|
|
|
def query
|
|
def query
|
|
- queries = params[:queries].to_i
|
|
|
|
- queries = 1 if queries < 1
|
|
|
|
- queries = 500 if queries > 500
|
|
|
|
-
|
|
|
|
- results = QUERY_RANGE.sample(queries).map do |id|
|
|
|
|
|
|
+ results = QUERY_RANGE.sample(query_count).map do |id|
|
|
World.find(id)
|
|
World.find(id)
|
|
end
|
|
end
|
|
|
|
|
|
render json: results
|
|
render json: results
|
|
end
|
|
end
|
|
|
|
|
|
|
|
+ def cached_query
|
|
|
|
+ results = QUERY_RANGE.sample(query_count).map do |id|
|
|
|
|
+ Rails.cache.fetch(id) do
|
|
|
|
+ World.find(id).as_json
|
|
|
|
+ end
|
|
|
|
+ end
|
|
|
|
+
|
|
|
|
+ render json: results
|
|
|
|
+ end
|
|
|
|
+
|
|
def fortune
|
|
def fortune
|
|
@fortunes = Fortune.all.to_a
|
|
@fortunes = Fortune.all.to_a
|
|
@fortunes << Fortune.new(id: 0, message: 'Additional fortune added at request time.')
|
|
@fortunes << Fortune.new(id: 0, message: 'Additional fortune added at request time.')
|
|
- @fortunes = @fortunes.sort_by!(&:message)
|
|
|
|
|
|
+ @fortunes.sort_by!(&:message)
|
|
end
|
|
end
|
|
|
|
|
|
def update
|
|
def update
|
|
- queries = (params[:queries] || 1).to_i
|
|
|
|
- queries = 1 if queries < 1
|
|
|
|
- queries = 500 if queries > 500
|
|
|
|
-
|
|
|
|
- worlds = queries.times.map{Random.rand(1..10_000)}.map do |id|
|
|
|
|
|
|
+ worlds = query_count.times.map { Random.rand(1..10_000) }.map do |id|
|
|
# get a random row from the database, which we know has 10000
|
|
# get a random row from the database, which we know has 10000
|
|
# rows with ids 1 - 10000
|
|
# rows with ids 1 - 10000
|
|
- world = World.select(:id, :randomNumber).find(id)
|
|
|
|
- begin
|
|
|
|
- rn = Random.rand(1..10_000)
|
|
|
|
- end while rn == world.randomNumber
|
|
|
|
- world.update_column(:randomNumber, rn)
|
|
|
|
|
|
+ world = World.find(id)
|
|
|
|
+ random = Random.rand(1..10_000)
|
|
|
|
+ random = Random.rand(1..10_000) until random != world.randomNumber
|
|
|
|
+ world.update_columns(randomNumber: random)
|
|
world
|
|
world
|
|
end
|
|
end
|
|
|
|
|
|
render json: worlds
|
|
render json: worlds
|
|
end
|
|
end
|
|
|
|
|
|
- def cached_query
|
|
|
|
- queries = params[:queries].to_i
|
|
|
|
- queries = 1 if queries < 1
|
|
|
|
- queries = 500 if queries > 500
|
|
|
|
|
|
+ private
|
|
|
|
|
|
- results = QUERY_RANGE.sample(queries).map do |id|
|
|
|
|
- Rails.cache.fetch("world-#{id}") do
|
|
|
|
- World.find(id)
|
|
|
|
- end
|
|
|
|
- end
|
|
|
|
|
|
+ def query_count
|
|
|
|
+ queries = params[:queries].to_i
|
|
|
|
+ return 1 if queries < 1
|
|
|
|
+ return 500 if queries > 500
|
|
|
|
|
|
- render json: results
|
|
|
|
|
|
+ queries
|
|
end
|
|
end
|
|
end
|
|
end
|