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