raze.cr 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. require "raze"
  2. require "pg"
  3. BENCH_DB = DB.open("postgres://benchmarkdbuser:benchmarkdbpass@tfb-database:5432/hello_world")
  4. class CONTENT
  5. ID_MAX = 10_000
  6. JSON = "application/json"
  7. PLAIN = "text/plain"
  8. HTML = "text/html; charset=UTF-8"
  9. end
  10. private def get_world
  11. id = Random.rand(CONTENT::ID_MAX).succ
  12. random_number = BENCH_DB.query_one("SELECT randomNumber FROM world WHERE id = $1", id, as: Int32)
  13. { id: id, randomNumber: random_number }
  14. end
  15. private def set_world(world)
  16. BENCH_DB.exec("UPDATE world SET randomNumber = $1 WHERE id = $2", world[:randomNumber], world[:id])
  17. world
  18. end
  19. private def fortunes
  20. data = Array(NamedTuple(id: Int32, message: String)).new
  21. BENCH_DB.query_each("SELECT id, message FROM Fortune") do |rs|
  22. data.push({id: rs.read(Int32), message: rs.read(String)})
  23. end
  24. data
  25. end
  26. private def sanitized_query_count(ctx)
  27. queries = ctx.query["queries"].as(String)
  28. queries = queries.to_i? || 1
  29. queries.clamp(1..500)
  30. end
  31. # Test 1: JSON Serialization
  32. get "/json" do |ctx|
  33. ctx.response.headers["Server"] = "Raze"
  34. ctx.response.headers["Date"] = HTTP.format_time(Time.now)
  35. ctx.response.content_type = CONTENT::JSON
  36. { message: "Hello, World!" }.to_json
  37. end
  38. # Postgres Test 2: Single database query
  39. get "/db" do |ctx|
  40. ctx.response.headers["Server"] = "Raze"
  41. ctx.response.headers["Date"] = HTTP.format_time(Time.now)
  42. ctx.response.content_type = CONTENT::JSON
  43. get_world.to_json
  44. end
  45. # Postgres Test 3: Multiple database query
  46. get "/queries" do |ctx|
  47. results = (1..sanitized_query_count(ctx)).map do
  48. get_world
  49. end
  50. ctx.response.headers["Server"] = "Raze"
  51. ctx.response.headers["Date"] = HTTP.format_time(Time.now)
  52. ctx.response.content_type = CONTENT::JSON
  53. results.to_json
  54. end
  55. # Postgres Test 4: Fortunes
  56. get "/fortunes" do |ctx|
  57. ctx.response.headers["Server"] = "Raze"
  58. ctx.response.headers["Date"] = HTTP.format_time(Time.now)
  59. ctx.response.content_type = CONTENT::HTML
  60. data = fortunes
  61. additional_fortune = {
  62. id: 0,
  63. message: "Additional fortune added at request time.",
  64. }
  65. data.push(additional_fortune)
  66. data.sort_by! { |fortune| fortune[:message] }
  67. render "views/fortunes.ecr"
  68. end
  69. # Postgres Test 5: Database Updates
  70. get "/updates" do |ctx|
  71. updated = (1..sanitized_query_count(ctx)).map do
  72. set_world({id: get_world[:id], randomNumber: Random.rand(CONTENT::ID_MAX).succ})
  73. end
  74. ctx.response.headers["Server"] = "Raze"
  75. ctx.response.headers["Date"] = HTTP.format_time(Time.now)
  76. ctx.response.content_type = CONTENT::JSON
  77. updated.to_json
  78. end
  79. # Test 6: Plaintext
  80. get "/plaintext" do |ctx|
  81. ctx.response.headers["Server"] = "Raze"
  82. ctx.response.headers["Date"] = HTTP.format_time(Time.now)
  83. ctx.response.content_type = CONTENT::PLAIN
  84. "Hello, World!"
  85. end
  86. Raze.config.logging = false
  87. Raze.config.port = 8080
  88. Raze.config.env = "production"
  89. Raze.config.reuse_port = true
  90. Raze.run