boot.rb 2.0 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. SERVER_STRING =
  10. if defined?(PhusionPassenger)
  11. 'passenger'
  12. elsif defined?(Puma)
  13. 'puma'
  14. elsif defined?(Unicorn)
  15. 'unicorn'
  16. elsif defined?(Agoo)
  17. 'agoo'
  18. end
  19. Bundler.require(:default) # Load core modules
  20. def connect(dbtype)
  21. Bundler.require(dbtype) # Load database-specific modules
  22. opts = {
  23. :adapter=>(dbtype == :mysql ? 'mysql2' : 'postgresql'),
  24. :username=>'benchmarkdbuser',
  25. :password=>'benchmarkdbpass',
  26. :host=>'tfb-database',
  27. :database=>'hello_world'
  28. }
  29. # Determine threading/thread pool size and timeout
  30. if defined?(JRUBY_VERSION)
  31. opts[:pool] = (2 * Math.log(Integer(ENV.fetch('MAX_CONCURRENCY')))).floor
  32. opts[:checkout_timeout] = 10
  33. elsif defined?(Puma) && (threads = Puma.cli_config.options.fetch(:max_threads)) > 1
  34. opts[:pool] = (2 * Math.log(threads)).floor
  35. opts[:checkout_timeout] = 10
  36. else
  37. # TODO: ActiveRecord doesn't have a single-threaded mode?
  38. opts[:pool] = 1
  39. opts[:checkout_timeout] = 0
  40. end
  41. ActiveRecord::Base.establish_connection(opts)
  42. end
  43. connect ENV.fetch('DBTYPE').to_sym
  44. # Define ORM models
  45. class World < ActiveRecord::Base
  46. self.table_name = name
  47. alias_attribute(:randomNumber, :randomnumber) \
  48. if connection.adapter_name.downcase.start_with?('postgres')
  49. if connection.adapter_name.downcase.start_with?('mysql')
  50. def self.upsert_all(attributes, on_duplicate: :update, update_only: nil, returning: nil, unique_by: nil, record_timestamps: nil)
  51. # On MySQL Batch updates verification isn't supported yet by TechEmpower.
  52. # https://github.com/TechEmpower/FrameworkBenchmarks/issues/5983
  53. attributes.each do |attrs|
  54. where(id: attrs[:id]).update_all(randomNumber: attrs[:randomNumber])
  55. end
  56. end
  57. end
  58. end
  59. class Fortune < ActiveRecord::Base
  60. self.table_name = name
  61. end
  62. ActiveRecord::Base.connection_handler.clear_active_connections!