Sha256: a0ccb06104b00bcaa050384914265ee1fa13f4e2814e93ef8ab1c7989261b3c1

Contents?: true

Size: 1.32 KB

Versions: 1

Compression:

Stored size: 1.32 KB

Contents

# frozen_string_literal: true

require 'benchmark/ips'
require 'switch_point'
require 'active_record'

SwitchPoint.configure do |config|
  config.define_switch_point :proxy,
    readonly: :proxy_readonly,
    writable: :proxy_writable
end

class Plain < ActiveRecord::Base
end

class Proxy1 < ActiveRecord::Base
  use_switch_point :proxy
end

class ProxyBase < ActiveRecord::Base
  self.abstract_class = true
  use_switch_point :proxy
end

class Proxy2 < ProxyBase
end

database_config = { adapter: 'sqlite3', database: ':memory:' }
ActiveRecord::Base.configurations = {
  'default' => database_config.dup,
  'proxy_readonly' => database_config.dup,
  'proxy_writable' => database_config.dup,
}
ActiveRecord::Base.establish_connection(:default)

Plain.connection.execute('CREATE TABLE plains (id integer primary key autoincrement)')
%i[readonly writable].each do |mode|
  ProxyBase.public_send("with_#{mode}") do
    %w[proxy1s proxy2s].each do |table|
      ProxyBase.connection.execute("CREATE TABLE #{table} (id integer primary key autoincrement)")
    end
  end
end

Benchmark.ips do |x|
  x.report('plain') do
    Plain.create
    Plain.first
  end

  x.report('proxy1') do
    Proxy1.with_writable { Proxy1.create }
    Proxy1.first
  end

  x.report('proxy2') do
    Proxy2.with_writable { Proxy2.create }
    Proxy2.first
  end

  x.compare!
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
switch_point-0.9.0 benchmark/proxy.rb