benchmarks_controller.rb 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # frozen_string_literal: true
  2. class BenchmarksController < ApplicationController
  3. ALL_DB_IDS = (1..10_000).to_a
  4. FORTUNES_TEMPLATE = ERB.new(Rage.root.join("app/views/fortunes.html.erb").read)
  5. before_action do
  6. headers["server"] = "rage"
  7. end
  8. def db
  9. render json: World.with_pk(random_id).values
  10. end
  11. def queries
  12. ids = requested_ids
  13. worlds = DB.synchronize do
  14. ids.map do |id|
  15. World.with_pk(id)
  16. end
  17. end
  18. render json: worlds.map!(&:values)
  19. end
  20. def fortunes
  21. records = Fortune.all
  22. records << Fortune.new(id: 0, message: "Additional fortune added at request time.")
  23. records.sort_by!(&:message)
  24. render plain: FORTUNES_TEMPLATE.result(binding)
  25. headers["content-type"] = "text/html; charset=utf-8"
  26. end
  27. def updates
  28. worlds = nil
  29. ids = requested_ids
  30. DB.synchronize do
  31. worlds = ids.map do |id|
  32. world = World.with_pk(id)
  33. new_value = random_id
  34. new_value = random_id while new_value == world.randomnumber
  35. world.randomnumber = new_value
  36. world
  37. end
  38. World.batch_update(worlds)
  39. end
  40. render json: worlds.map!(&:values)
  41. end
  42. private
  43. def requested_ids
  44. num = params[:queries].to_i
  45. if num > 500
  46. num = 500
  47. elsif num < 1
  48. num = 1
  49. end
  50. ALL_DB_IDS.sample(num)
  51. end
  52. def random_id
  53. Random.rand(9_999) + 1
  54. end
  55. end