boot.rb 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. SERVER_STRING =
  8. if defined?(PhusionPassenger)
  9. [
  10. PhusionPassenger::SharedConstants::SERVER_TOKEN_NAME,
  11. PhusionPassenger::VERSION_STRING
  12. ].join('/').freeze
  13. elsif defined?(Puma)
  14. Puma::Const::PUMA_SERVER_STRING
  15. elsif defined?(Unicorn)
  16. Unicorn::HttpParser::DEFAULTS['SERVER_SOFTWARE']
  17. end
  18. Bundler.require(:default) # Load core modules
  19. def connect(dbtype)
  20. Bundler.require(dbtype) # Load database-specific modules
  21. opts = {
  22. :adapter=>(dbtype == :mysql ? 'mysql2' : 'postgresql'),
  23. :username=>'benchmarkdbuser',
  24. :password=>'benchmarkdbpass',
  25. :host=>'tfb-database',
  26. :database=>'hello_world'
  27. }
  28. # Determine threading/thread pool size and timeout
  29. if defined?(JRUBY_VERSION)
  30. opts[:pool] = (2 * Math.log(Integer(ENV.fetch('MAX_CONCURRENCY')))).floor
  31. opts[:checkout_timeout] = 10
  32. elsif defined?(Puma) && (threads = Puma.cli_config.options.fetch(:max_threads)) > 1
  33. opts[:pool] = (2 * Math.log(threads)).floor
  34. opts[:checkout_timeout] = 10
  35. else
  36. # TODO: ActiveRecord doesn't have a single-threaded mode?
  37. opts[:pool] = 1
  38. opts[:checkout_timeout] = 0
  39. end
  40. ActiveRecord::Base.establish_connection(opts)
  41. end
  42. connect ENV.fetch('DBTYPE').to_sym
  43. # Define ORM models
  44. class World < ActiveRecord::Base
  45. self.table_name = name
  46. alias_attribute(:randomnumber, :randomNumber) \
  47. if connection.adapter_name.downcase.start_with?('mysql')
  48. end
  49. class Fortune < ActiveRecord::Base
  50. self.table_name = name
  51. end
  52. ActiveRecord::Base.clear_active_connections!