boot.rb 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. adapter = 'postgresql'
  16. # Determine threading/thread pool size and timeout
  17. if defined?(Puma) && (threads = Puma.cli_config.options.fetch(:max_threads)) > 1
  18. opts[:max_connections] = threads
  19. opts[:pool_timeout] = 10
  20. else
  21. opts[:max_connections] = 512
  22. end
  23. Sequel.connect \
  24. '%{adapter}://%{host}/%{database}?user=%{user}&password=%{password}' % {
  25. adapter: adapter,
  26. host: 'tfb-database',
  27. database: 'hello_world',
  28. user: 'benchmarkdbuser',
  29. password: 'benchmarkdbpass'
  30. }, opts
  31. end
  32. DB = connect 'postgres'
  33. # Define ORM models
  34. class World < Sequel::Model(:World)
  35. def_column_alias(:randomnumber, :randomNumber) if DB.database_type == :mysql
  36. def self.batch_update(worlds)
  37. if DB.database_type == :mysql
  38. worlds.map(&:save_changes)
  39. else
  40. ids = []
  41. sql = String.new("UPDATE world SET randomnumber = CASE id ")
  42. worlds.each do |world|
  43. sql << "when #{world.id} then #{world.randomnumber} "
  44. ids << world.id
  45. end
  46. sql << "ELSE randomnumber END WHERE id IN ( #{ids.join(',')})"
  47. DB.run(sql)
  48. end
  49. end
  50. end
  51. class Fortune < Sequel::Model(:Fortune)
  52. # Allow setting id to zero (0) per benchmark requirements
  53. unrestrict_primary_key
  54. end
  55. [World, Fortune].each(&:freeze)
  56. DB.freeze