boot.rb 1.7 KB

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