benchmarks_controller.rb 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. worlds = DB.synchronize do
  13. requested_ids.map do |id|
  14. World.with_pk(id)
  15. end
  16. end
  17. render json: worlds.map!(&:values)
  18. end
  19. def fortunes
  20. records = Fortune.all
  21. records << Fortune.new(id: 0, message: "Additional fortune added at request time.")
  22. records.sort_by!(&:message)
  23. render plain: FORTUNES_TEMPLATE.result(binding)
  24. headers["content-type"] = "text/html; charset=utf-8"
  25. end
  26. def updates
  27. worlds = nil
  28. DB.synchronize do
  29. worlds = requested_ids.map do |id|
  30. world = World.with_pk(id)
  31. new_value = random_id
  32. new_value = random_id while new_value == world.randomnumber
  33. world.randomnumber = new_value
  34. world
  35. end
  36. World.batch_update(worlds)
  37. end
  38. render json: worlds.map!(&:values)
  39. end
  40. private
  41. def requested_ids
  42. num = params[:queries].to_i
  43. if num > 500
  44. num = 500
  45. elsif num < 1
  46. num = 1
  47. end
  48. ALL_DB_IDS.sample(num)
  49. end
  50. def random_id
  51. Random.rand(9_999) + 1
  52. end
  53. end