boot.rb 1.9 KB

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