boot.rb 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # frozen_string_literal: true
  2. require "bundler/setup"
  3. require "time"
  4. MAX_PK = 10_000
  5. QUERY_RANGE = (1..MAX_PK).freeze
  6. ALL_IDS = QUERY_RANGE.to_a
  7. QUERIES_MIN = 1
  8. QUERIES_MAX = 500
  9. SEQUEL_NO_ASSOCIATIONS = true
  10. SERVER_STRING =
  11. if defined?(Iodine)
  12. "Iodine"
  13. elsif defined?(Puma)
  14. "Puma"
  15. elsif defined?(Unicorn)
  16. "Unicorn"
  17. end
  18. Bundler.require(:default) # Load core modules
  19. CONTENT_TYPE = 'Content-Type'
  20. JSON_TYPE = 'application/json'
  21. HTML_TYPE = 'text/html; charset=utf-8'
  22. PLAINTEXT_TYPE = 'text/plain'
  23. DATE_HEADER = 'Date'
  24. SERVER_HEADER = 'Server'
  25. def connect(dbtype)
  26. Bundler.require(dbtype) # Load database-specific modules
  27. adapters = {
  28. mysql: {
  29. mri: "mysql2"
  30. },
  31. postgresql: {
  32. mri: "postgres"
  33. }
  34. }
  35. opts = {}
  36. # Determine threading/thread pool size and timeout
  37. if defined?(Puma) &&
  38. (threads = Puma.cli_config.options.fetch(:max_threads)) > 1
  39. opts[:max_connections] = (2 * Math.log(threads)).floor
  40. opts[:pool_timeout] = 10
  41. elsif defined?(Unicorn)
  42. Sequel.single_threaded = true
  43. end
  44. Sequel.connect "%{adapter}://%{host}/%{database}?user=%{user}&password=%{password}" %
  45. {
  46. adapter:
  47. adapters.fetch(dbtype).fetch(:mri),
  48. host: "tfb-database",
  49. database: "hello_world",
  50. user: "benchmarkdbuser",
  51. password: "benchmarkdbpass"
  52. },
  53. opts
  54. end
  55. DB = connect ENV.fetch("DBTYPE").to_sym
  56. # Define ORM models
  57. class World < Sequel.Model(:World)
  58. def_column_alias(:randomnumber, :randomNumber) if DB.database_type == :mysql
  59. def self.batch_update(worlds)
  60. if DB.database_type == :mysql
  61. worlds.map(&:save_changes)
  62. else
  63. ids = []
  64. sql = String.new("UPDATE world SET randomnumber = CASE id ")
  65. worlds.each do |world|
  66. sql << "when #{world.id} then #{world.randomnumber} "
  67. ids << world.id
  68. end
  69. sql << "ELSE randomnumber END WHERE id IN ( #{ids.join(',')})"
  70. DB.run(sql)
  71. end
  72. end
  73. end
  74. class Fortune < Sequel.Model(:Fortune)
  75. # Allow setting id to zero (0) per benchmark requirements
  76. unrestrict_primary_key
  77. end
  78. [World, Fortune].each(&:freeze)
  79. DB.freeze