hello_world.rb 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. require 'json'
  7. if RUBY_PLATFORM == 'java'
  8. DEFAULT_DATABASE_URL = 'jdbc:postgresql://tfb-database/hello_world?user=benchmarkdbuser&password=benchmarkdbpass'
  9. else
  10. DEFAULT_DATABASE_URL = 'postgresql://tfb-database/hello_world?user=benchmarkdbuser&password=benchmarkdbpass'
  11. end
  12. class HelloWorld
  13. QUERY_RANGE = (1..10_000).freeze # range of IDs in the Fortune DB
  14. ALL_IDS = QUERY_RANGE.to_a # enumeration of all the IDs in fortune DB
  15. MIN_QUERIES = 1 # min number of records that can be retrieved
  16. MAX_QUERIES = 500 # max number of records that can be retrieved
  17. CONTENT_TYPE = 'Content-Type'
  18. CONTENT_LENGTH = 'Content-Length'
  19. JSON_TYPE = 'application/json'
  20. HTML_TYPE = 'text/html; charset=utf-8'
  21. PLAINTEXT_TYPE = 'text/plain'
  22. DATE = 'Date'
  23. SERVER = 'Server'
  24. SERVER_STRING = if defined?(PhusionPassenger)
  25. 'Passenger'
  26. elsif defined?(Puma)
  27. 'Puma'
  28. elsif defined?(Iodine)
  29. 'Iodine'
  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>Fortunes</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. if defined?(Puma)
  53. num_workers, num_threads = auto_tune
  54. num_threads = [num_threads, 32].min
  55. max_connections = num_workers * num_threads
  56. else
  57. max_connections = 512
  58. end
  59. @db = PgDb.new(DEFAULT_DATABASE_URL, max_connections)
  60. end
  61. def respond(content_type, body = '')
  62. headers = {
  63. CONTENT_TYPE => content_type,
  64. DATE => Time.now.utc.httpdate,
  65. SERVER => SERVER_STRING
  66. }
  67. headers[CONTENT_LENGTH] = body.bytesize.to_s if defined?(Unicorn)
  68. [
  69. 200,
  70. headers,
  71. [body]
  72. ]
  73. end
  74. def fortunes
  75. fortunes = @db.select_fortunes
  76. fortunes << { id: 0, message: 'Additional fortune added at request time.' }
  77. fortunes.sort_by! { |item| item[:message] }
  78. buffer = String.new
  79. buffer << TEMPLATE_PREFIX
  80. fortunes.each do |item|
  81. buffer << "<tr><td>#{item[:id]}</td><td>#{Rack::Utils.escape_html(item[:message])}</td></tr>"
  82. end
  83. buffer << TEMPLATE_POSTFIX
  84. end
  85. def call(env)
  86. case env['PATH_INFO']
  87. when '/json'
  88. # Test type 1: JSON serialization
  89. respond JSON_TYPE,
  90. { message: 'Hello, World!' }.to_json
  91. when '/db'
  92. # Test type 2: Single database query
  93. respond JSON_TYPE, @db.select_random_world.to_json
  94. when '/queries'
  95. # Test type 3: Multiple database queries
  96. params = Rack::Utils.parse_query(env['QUERY_STRING'])
  97. queries = params['queries']
  98. respond JSON_TYPE, @db.select_worlds(queries).to_json
  99. when '/fortunes'
  100. # Test type 4: Fortunes
  101. respond HTML_TYPE, fortunes
  102. when '/updates'
  103. # Test type 5: Database updates
  104. params = Rack::Utils.parse_query(env['QUERY_STRING'])
  105. queries = params['queries']
  106. respond JSON_TYPE, @db.update_worlds(queries).to_json
  107. when '/plaintext'
  108. # Test type 6: Plaintext
  109. respond PLAINTEXT_TYPE, 'Hello, World!'
  110. end
  111. end
  112. end