boot.rb 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 =
  11. if defined?(PhusionPassenger)
  12. 'passenger'
  13. elsif defined?(Puma)
  14. 'puma'
  15. elsif defined?(Unicorn)
  16. 'unicorn'
  17. elsif defined?(Agoo)
  18. 'agoo'
  19. end
  20. Bundler.require(:default) # Load core modules
  21. def connect(dbtype)
  22. Bundler.require(dbtype) # Load database-specific modules
  23. adapters = {
  24. :mysql=>{ :jruby=>'jdbc:mysql', :mri=>'mysql2' },
  25. :postgresql=>{ :jruby=>'jdbc:postgresql', :mri=>'postgres' }
  26. }
  27. opts = {}
  28. # Determine threading/thread pool size and timeout
  29. if defined?(JRUBY_VERSION)
  30. opts[:max_connections] = (2 * Math.log(Integer(ENV.fetch('MAX_CONCURRENCY')))).floor
  31. opts[:pool_timeout] = 10
  32. elsif defined?(Puma) && (threads = Puma.cli_config.options.fetch(:max_threads)) > 1
  33. opts[:max_connections] = (2 * Math.log(threads)).floor
  34. opts[:pool_timeout] = 10
  35. else
  36. Sequel.single_threaded = true
  37. end
  38. Sequel.connect \
  39. '%{adapter}://%{host}/%{database}?user=%{user}&password=%{password}' % {
  40. :adapter=>adapters.fetch(dbtype).fetch(defined?(JRUBY_VERSION) ? :jruby : :mri),
  41. :host=>'tfb-database',
  42. :database=>'hello_world',
  43. :user=>'benchmarkdbuser',
  44. :password=>'benchmarkdbpass'
  45. }, opts
  46. end
  47. DB = connect ENV.fetch('DBTYPE').to_sym
  48. # Define ORM models
  49. class World < Sequel::Model(:World)
  50. def_column_alias(:randomnumber, :randomNumber) if DB.database_type == :mysql
  51. def self.batch_update(worlds)
  52. if DB.database_type == :mysql
  53. worlds.map(&:save_changes)
  54. else
  55. ids = []
  56. sql = String.new("UPDATE world SET randomnumber = CASE id ")
  57. worlds.each do |world|
  58. sql << "when #{world.id} then #{world.randomnumber} "
  59. ids << world.id
  60. end
  61. sql << "ELSE randomnumber END WHERE id IN ( #{ids.join(',')})"
  62. DB.run(sql)
  63. end
  64. end
  65. end
  66. class Fortune < Sequel::Model(:Fortune)
  67. # Allow setting id to zero (0) per benchmark requirements
  68. unrestrict_primary_key
  69. end
  70. [World, Fortune].each(&:freeze)
  71. DB.freeze