boot.rb 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # frozen_string_literal: true
  2. require 'bundler/setup'
  3. require 'time'
  4. MAX_PK = 10_000
  5. ID_RANGE = (1..MAX_PK).freeze
  6. ALL_IDS = ID_RANGE.to_a
  7. QUERIES_MIN = 1
  8. QUERIES_MAX = 500
  9. SEQUEL_NO_ASSOCIATIONS = true
  10. SERVER_STRING = "Sinatra"
  11. Bundler.require(:default) # Load core modules
  12. def connect(dbtype)
  13. Bundler.require(dbtype) # Load database-specific modules
  14. opts = {}
  15. if dbtype == :mysql
  16. adapter = 'trilogy'
  17. opts[:ssl] = true
  18. opts[:ssl_mode] = 4 # Trilogy::SSL_PREFERRED_NOVERIFY
  19. opts[:tls_min_version] = 3 # Trilogy::TLS_VERSION_12
  20. else
  21. adapter = 'postgresql'
  22. end
  23. # Determine threading/thread pool size and timeout
  24. if defined?(Puma) && (threads = Puma.cli_config.options.fetch(:max_threads)) > 1
  25. opts[:max_connections] = threads
  26. opts[:pool_timeout] = 10
  27. else
  28. opts[:max_connections] = 512
  29. end
  30. Sequel.connect \
  31. '%{adapter}://%{host}/%{database}?user=%{user}&password=%{password}' % {
  32. adapter: adapter,
  33. host: 'tfb-database',
  34. database: 'hello_world',
  35. user: 'benchmarkdbuser',
  36. password: 'benchmarkdbpass'
  37. }, opts
  38. end
  39. DB = connect ENV.fetch('DBTYPE').to_sym
  40. # Define ORM models
  41. class World < Sequel::Model(:World)
  42. def_column_alias(:randomnumber, :randomNumber) if DB.database_type == :mysql
  43. def self.batch_update(worlds)
  44. if DB.database_type == :mysql
  45. worlds.map(&:save_changes)
  46. else
  47. ids = []
  48. sql = String.new("UPDATE world SET randomnumber = CASE id ")
  49. worlds.each do |world|
  50. sql << "when #{world.id} then #{world.randomnumber} "
  51. ids << world.id
  52. end
  53. sql << "ELSE randomnumber END WHERE id IN ( #{ids.join(',')})"
  54. DB.run(sql)
  55. end
  56. end
  57. end
  58. class Fortune < Sequel::Model(:Fortune)
  59. # Allow setting id to zero (0) per benchmark requirements
  60. unrestrict_primary_key
  61. end
  62. [World, Fortune].each(&:freeze)
  63. DB.freeze