boot.rb 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # frozen_string_literal: true
  2. require 'bundler/setup'
  3. require 'time'
  4. MAX_PK = 10_000
  5. QUERIES_MIN = 1
  6. QUERIES_MAX = 500
  7. SEQUEL_NO_ASSOCIATIONS = true
  8. CONTENT_TYPE = 'Content-Type'
  9. JSON_TYPE = 'application/json'
  10. HTML_TYPE = 'text/html; charset=utf-8'
  11. PLAINTEXT_TYPE = 'text/plain'
  12. DATE_HEADER = 'Date'
  13. SERVER_HEADER = 'Server'
  14. SERVER_STRING =
  15. if defined?(PhusionPassenger)
  16. [
  17. PhusionPassenger::SharedConstants::SERVER_TOKEN_NAME,
  18. PhusionPassenger::VERSION_STRING
  19. ].join('/').freeze
  20. elsif defined?(Puma)
  21. Puma::Const::PUMA_SERVER_STRING
  22. elsif defined?(Unicorn)
  23. Unicorn::HttpParser::DEFAULTS['SERVER_SOFTWARE']
  24. end
  25. Bundler.require(:default) # Load core modules
  26. def connect(dbtype)
  27. Bundler.require(dbtype) # Load database-specific modules
  28. adapters = {
  29. :mysql=>{ :jruby=>'jdbc:mysql', :mri=>'mysql2' },
  30. :postgresql=>{ :jruby=>'jdbc:postgresql', :mri=>'postgres' }
  31. }
  32. opts = {}
  33. # Determine threading/thread pool size and timeout
  34. if defined?(JRUBY_VERSION)
  35. opts[:max_connections] = (2 * Math.log(Integer(ENV.fetch('MAX_CONCURRENCY')))).floor
  36. opts[:pool_timeout] = 10
  37. elsif defined?(Puma) && (threads = Puma.cli_config.options.fetch(:max_threads)) > 1
  38. opts[:max_connections] = (2 * Math.log(threads)).floor
  39. opts[:pool_timeout] = 10
  40. else
  41. Sequel.single_threaded = true
  42. end
  43. Sequel.connect \
  44. '%{adapter}://%{host}/%{database}?user=%{user}&password=%{password}' % {
  45. :adapter=>adapters.fetch(dbtype).fetch(defined?(JRUBY_VERSION) ? :jruby : :mri),
  46. :host=>'tfb-database',
  47. :database=>'hello_world',
  48. :user=>'benchmarkdbuser',
  49. :password=>'benchmarkdbpass'
  50. }, opts
  51. end
  52. DB = connect ENV.fetch('DBTYPE').to_sym
  53. # Define ORM models
  54. class World < Sequel::Model(:World)
  55. def_column_alias(:randomnumber, :randomNumber) if DB.database_type == :mysql
  56. end
  57. class Fortune < Sequel::Model(:Fortune)
  58. # Allow setting id to zero (0) per benchmark requirements
  59. unrestrict_primary_key
  60. end
  61. [World, Fortune].each(&:freeze)
  62. DB.freeze