hello_world.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # frozen_string_literal: true
  2. # Our Rack application to be executed by rackup
  3. require_relative 'pg_db'
  4. require_relative 'config/auto_tune'
  5. require 'rack'
  6. if RUBY_PLATFORM == 'java'
  7. require 'json'
  8. DEFAULT_DATABASE_URL = 'jdbc:postgresql://tfb-database/hello_world?user=benchmarkdbuser&password=benchmarkdbpass'
  9. else
  10. require 'oj'
  11. Oj.mimic_JSON
  12. DEFAULT_DATABASE_URL = 'postgresql://tfb-database/hello_world?user=benchmarkdbuser&password=benchmarkdbpass'
  13. end
  14. class HelloWorld
  15. QUERY_RANGE = (1..10_000).freeze # range of IDs in the Fortune DB
  16. ALL_IDS = QUERY_RANGE.to_a # enumeration of all the IDs in fortune DB
  17. MIN_QUERIES = 1 # min number of records that can be retrieved
  18. MAX_QUERIES = 500 # max number of records that can be retrieved
  19. CONTENT_TYPE = 'Content-Type'
  20. CONTENT_LENGTH = 'Content-Length'
  21. JSON_TYPE = 'application/json'
  22. HTML_TYPE = 'text/html; charset=utf-8'
  23. PLAINTEXT_TYPE = 'text/plain'
  24. DATE = 'Date'
  25. SERVER = 'Server'
  26. SERVER_STRING = if defined?(PhusionPassenger)
  27. 'Passenger'
  28. elsif defined?(Puma)
  29. Puma::Const::PUMA_SERVER_STRING
  30. elsif defined?(Unicorn)
  31. 'Unicorn'
  32. elsif defined?(Falcon)
  33. 'Falcon'
  34. else
  35. ' Ruby Rack'
  36. end
  37. TEMPLATE_PREFIX = '<!DOCTYPE html>
  38. <html>
  39. <head>
  40. <title>Fortune</title>
  41. </head>
  42. <body>
  43. <table>
  44. <tr>
  45. <th>id</th>
  46. <th>message</th>
  47. </tr>'
  48. TEMPLATE_POSTFIX = '</table>
  49. </body
  50. </html>'
  51. def initialize
  52. # auto_tune
  53. max_connections = 512
  54. @db = PgDb.new(DEFAULT_DATABASE_URL, max_connections)
  55. end
  56. def respond(content_type, body = '')
  57. [
  58. 200,
  59. {
  60. CONTENT_TYPE => content_type,
  61. DATE => Time.now.utc.httpdate,
  62. SERVER => SERVER_STRING,
  63. CONTENT_LENGTH => body.length.to_s
  64. },
  65. [body]
  66. ]
  67. end
  68. def fortunes
  69. fortunes = @db.select_fortunes
  70. fortunes << { id: 0, message: 'Additional fortune added at request time.' }
  71. fortunes.sort_by! { |item| item[:message] }
  72. buffer = String.new
  73. buffer << TEMPLATE_PREFIX
  74. fortunes.each do |item|
  75. buffer << "<tr><td> #{item[:id]} </td> <td>#{Rack::Utils.escape_html(item[:message])}</td></tr>"
  76. end
  77. buffer << TEMPLATE_POSTFIX
  78. end
  79. def call(env)
  80. case env['PATH_INFO']
  81. when '/json'
  82. # Test type 1: JSON serialization
  83. respond JSON_TYPE,
  84. { message: 'Hello, World!' }.to_json
  85. when '/db'
  86. # Test type 2: Single database query
  87. respond JSON_TYPE, @db.select_random_world.to_json
  88. when '/queries'
  89. # Test type 3: Multiple database queries
  90. params = Rack::Utils.parse_query(env['QUERY_STRING'])
  91. queries = params['queries']
  92. respond JSON_TYPE, @db.select_worlds(queries).to_json
  93. when '/fortunes'
  94. # Test type 4: Fortunes
  95. respond HTML_TYPE, fortunes
  96. when '/updates'
  97. # Test type 5: Database updates
  98. params = Rack::Utils.parse_query(env['QUERY_STRING'])
  99. queries = params['queries']
  100. respond JSON_TYPE, @db.update_worlds(queries).to_json
  101. when '/plaintext'
  102. # Test type 6: Plaintext
  103. respond PLAINTEXT_TYPE, 'Hello, World!'
  104. end
  105. end
  106. end