hello_world.rb 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. require "sinatra"
  2. require "sinatra/json"
  3. require "sinatra/activerecord"
  4. set :logging, false
  5. set :activerecord_logger, nil
  6. set :static, false
  7. # Specify the encoder - otherwise, sinatra/json inefficiently
  8. # attempts to load one of several on each request
  9. set :json_encoder => :to_json
  10. # Don't prefix JSON results with { "world": {...} }
  11. ActiveRecord::Base.include_root_in_json = false
  12. if RUBY_PLATFORM == 'java'
  13. set :database, { :adapter => 'jdbcmysql', :database => 'hello_world', :username => 'benchmarkdbuser', :password => 'benchmarkdbpass', :host => 'localhost', :pool => 256, :timeout => 5000 }
  14. else
  15. set :database, { :adapter => 'mysql2', :database => 'hello_world', :username => 'benchmarkdbuser', :password => 'benchmarkdbpass', :host => 'localhost', :pool => 256, :timeout => 5000 }
  16. end
  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. attr_accessible :randomNumber
  28. end
  29. get '/json' do
  30. json :message => 'Hello, World!'
  31. end
  32. get '/plaintext' do
  33. content_type 'text/plain'
  34. 'Hello, World!'
  35. end
  36. get '/db' do
  37. ActiveRecord::Base.connection_pool.with_connection do
  38. json(World.find(Random.rand(10000) + 1))
  39. end
  40. end
  41. get '/queries' do
  42. queries = (params[:queries] || 1).to_i
  43. queries = 1 if queries < 1
  44. queries = 500 if queries > 500
  45. ActiveRecord::Base.connection_pool.with_connection do
  46. results = (1..queries).map do
  47. World.find(Random.rand(10000) + 1)
  48. end
  49. json(results)
  50. end
  51. end