boot.rb 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # frozen_string_literal: true
  2. require "bundler/setup"
  3. require "time"
  4. require "oj"
  5. MAX_PK = 10_000
  6. QUERIES_MIN = 1
  7. QUERIES_MAX = 500
  8. SEQUEL_NO_ASSOCIATIONS = true
  9. # Use the OJ gem instead of the JSON one
  10. Oj.mimic_JSON()
  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. def connect(dbtype)
  24. Bundler.require(dbtype) # Load database-specific modules
  25. adapters = {
  26. mysql: {
  27. jruby: "jdbc:mysql",
  28. mri: "mysql2"
  29. },
  30. postgresql: {
  31. jruby: "jdbc:postgresql",
  32. mri: "postgres"
  33. }
  34. }
  35. opts = {}
  36. # Determine threading/thread pool size and timeout
  37. if defined?(JRUBY_VERSION)
  38. opts[:max_connections] = (
  39. 2 * Math.log(Integer(ENV.fetch("MAX_CONCURRENCY")))
  40. ).floor
  41. opts[:pool_timeout] = 10
  42. elsif defined?(Puma) &&
  43. (threads = Puma.cli_config.options.fetch(:max_threads)) > 1
  44. opts[:max_connections] = (2 * Math.log(threads)).floor
  45. opts[:pool_timeout] = 10
  46. else
  47. Sequel.single_threaded = true
  48. end
  49. Sequel.connect "%{adapter}://%{host}/%{database}?user=%{user}&password=%{password}" %
  50. {
  51. adapter:
  52. adapters.fetch(dbtype).fetch(
  53. defined?(JRUBY_VERSION) ? :jruby : :mri
  54. ),
  55. host: "tfb-database",
  56. database: "hello_world",
  57. user: "benchmarkdbuser",
  58. password: "benchmarkdbpass"
  59. },
  60. opts
  61. end
  62. DB = connect ENV.fetch("DBTYPE").to_sym
  63. # Define ORM models
  64. class World < Sequel.Model(:World)
  65. def_column_alias(:randomnumber, :randomNumber) if DB.database_type == :mysql
  66. end
  67. class Fortune < Sequel.Model(:Fortune)
  68. # Allow setting id to zero (0) per benchmark requirements
  69. unrestrict_primary_key
  70. end
  71. [World, Fortune].each(&:freeze)
  72. DB.freeze