hello_world.rb 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. require 'active_record'
  2. Bundler.require :default
  3. set :logging, false
  4. ActiveRecord::Base.logger = nil
  5. set :activerecord_logger, nil
  6. set :static, false
  7. set :template_engine, :slim
  8. Slim::Engine.set_default_options format: :html5, sort_attrs: false
  9. # Specify the encoder - otherwise, sinatra/json inefficiently
  10. # attempts to load one of several on each request
  11. set :json_encoder => :to_json
  12. # Don't prefix JSON results with { "world": {...} }
  13. ActiveRecord::Base.include_root_in_json = false
  14. db_config = { :database => 'hello_world', :username => 'benchmarkdbuser', :password => 'benchmarkdbpass', :pool => 256, :timeout => 5000 }
  15. adapter = RUBY_PLATFORM == 'java' ? 'jdbcmysql' : 'mysql2'
  16. set :database, db_config.merge(:adapter => adapter, :host => ENV['DB_HOST'])
  17. # The sinatra-activerecord gem registers before and after filters that
  18. # call expensive synchronized ActiveRecord methods on every request to
  19. # verify every connection in the pool, even for routes that don't use
  20. # the database. Clear those filters and handle connection management
  21. # ourselves, which is what applications seeking high throughput with
  22. # ActiveRecord need to do anyway.
  23. settings.filters[:before].clear
  24. settings.filters[:after].clear
  25. class World < ActiveRecord::Base
  26. self.table_name = "World"
  27. end
  28. class Fortune < ActiveRecord::Base
  29. self.table_name = "Fortune"
  30. end
  31. get '/json' do
  32. json :message => 'Hello, World!'
  33. end
  34. get '/plaintext' do
  35. content_type 'text/plain'
  36. 'Hello, World!'
  37. end
  38. get '/db' do
  39. worlds = ActiveRecord::Base.connection_pool.with_connection do
  40. World.find(Random.rand(10000) + 1)
  41. end
  42. json(worlds)
  43. end
  44. get '/queries' do
  45. queries = (params[:queries] || 1).to_i
  46. queries = 1 if queries < 1
  47. queries = 500 if queries > 500
  48. worlds = ActiveRecord::Base.connection_pool.with_connection do
  49. (1..queries).map do
  50. World.find(Random.rand(10000) + 1)
  51. end
  52. end
  53. json(worlds)
  54. end
  55. get '/fortunes' do
  56. @fortunes = Fortune.all
  57. @fortunes << Fortune.new(:id => 0, :message => "Additional fortune added at request time.")
  58. @fortunes = @fortunes.sort_by { |x| x.message }
  59. slim :fortunes
  60. end
  61. get '/updates' do
  62. queries = (params[:queries] || 1).to_i
  63. queries = 1 if queries < 1
  64. queries = 500 if queries > 500
  65. worlds = ActiveRecord::Base.connection_pool.with_connection do
  66. worlds = (1..queries).map do
  67. world = World.find(Random.rand(10000) + 1)
  68. world.randomNumber = Random.rand(10000) + 1
  69. world
  70. end
  71. World.import worlds, :on_duplicate_key_update => [:randomNumber]
  72. worlds
  73. end
  74. json(worlds)
  75. end