boot.rb 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # frozen_string_literal: true
  2. require 'bundler/setup'
  3. SEQUEL_NO_ASSOCIATIONS = true
  4. Bundler.require(:default) # Load core modules
  5. def connect(dbtype)
  6. Bundler.require(dbtype) # Load database-specific modules
  7. adapters = {
  8. mysql: 'mysql2',
  9. postgresql: 'postgres'
  10. }
  11. opts = {}
  12. # Determine threading/thread pool size and timeout
  13. if defined?(Puma) && (threads = Puma.cli_config.options.fetch(:max_threads)) > 1
  14. opts[:max_connections] = (2 * Math.log(threads)).floor
  15. opts[:pool_timeout] = 10
  16. end
  17. Sequel.connect \
  18. '%{adapter}://%{host}/%{database}?user=%{user}&password=%{password}' % {
  19. adapter: (dbtype == :mysql ? 'mysql2' : 'postgresql'),
  20. host: 'tfb-database',
  21. database: 'hello_world',
  22. user: 'benchmarkdbuser',
  23. password: 'benchmarkdbpass'
  24. }, opts
  25. end
  26. DB = connect ENV.fetch('DBTYPE').to_sym
  27. # Define ORM models
  28. class World < Sequel::Model(:World)
  29. BY_ID = naked.where(id: :$id).prepare(:first, :world_by_id)
  30. UPDATE = where(id: :$id).prepare(:update, :world_update, randomnumber: :$randomnumber)
  31. def_column_alias(:randomnumber, :randomNumber) if DB.database_type == :mysql
  32. def self.batch_update(worlds)
  33. if DB.database_type == :mysql
  34. worlds.each do |world|
  35. UPDATE.(id: world[:id], randomnumber: world[:randomnumber])
  36. end
  37. else
  38. ids = []
  39. sql = String.new("UPDATE world SET randomnumber = CASE id ")
  40. worlds.each do |world|
  41. sql << "when #{world[:id]} then #{world[:randomnumber]} "
  42. ids << world[:id]
  43. end
  44. sql << "ELSE randomnumber END WHERE id IN ( #{ids.join(',')})"
  45. DB.run(sql)
  46. end
  47. end
  48. end
  49. class Fortune < Sequel::Model(:Fortune)
  50. # Allow setting id to zero (0) per benchmark requirements
  51. unrestrict_primary_key
  52. end
  53. [World, Fortune].each(&:freeze)
  54. DB.freeze