performance/cache_runner.rb in identity_cache-0.5.1 vs performance/cache_runner.rb in identity_cache-1.0.0

- old
+ new

@@ -1,32 +1,27 @@ -$LOAD_PATH.unshift File.expand_path("../../lib", __FILE__) +# frozen_string_literal: true +$LOAD_PATH.unshift(File.expand_path("../../lib", __FILE__)) require 'active_record' require 'active_support/core_ext' require 'active_support/cache' require 'identity_cache' require 'memcached_store' require 'active_support/cache/memcached_store' -$memcached_port = 11211 -$mysql_port = 3306 - require File.dirname(__FILE__) + '/../test/helpers/active_record_objects' require File.dirname(__FILE__) + '/../test/helpers/database_connection' +require File.dirname(__FILE__) + '/../test/helpers/cache_connection' IdentityCache.logger = Logger.new(nil) -IdentityCache.cache_backend = ActiveSupport::Cache::MemcachedStore.new("localhost:#{$memcached_port}", :support_cas => true) +CacheConnection.setup -if ActiveRecord.gem_version < Gem::Version.new('5') && ActiveRecord::Base.respond_to?(:raise_in_transactional_callbacks=) - ActiveRecord::Base.raise_in_transactional_callbacks = true -end - def create_record(id) Item.new(id) end def database_ready(count) - Item.where(:id => (1..count)).count == count + Item.where(id: (1..count)).count == count rescue false end def create_database(count) @@ -40,30 +35,29 @@ DatabaseConnection.drop_tables DatabaseConnection.create_tables existing = Item.all (1..count).to_a.each do |i| - unless existing.any? { |e| e.id == i } - a = Item.new - a.id = i - a.associated = AssociatedRecord.new(name: "Associated for #{i}") - a.associated_records - (1..5).each do |j| - a.associated_records << AssociatedRecord.new(name: "Has Many #{j} for #{i}") - a.normalized_associated_records << NormalizedAssociatedRecord.new(name: "Normalized Has Many #{j} for #{i}") - end - a.save + next if existing.any? { |e| e.id == i } + a = Item.new + a.id = i + a.associated = AssociatedRecord.new(name: "Associated for #{i}") + a.associated_records + (1..5).each do |j| + a.associated_records << AssociatedRecord.new(name: "Has Many #{j} for #{i}") + a.normalized_associated_records << NormalizedAssociatedRecord.new(name: "Normalized Has Many #{j} for #{i}") end + a.save end ensure helper.teardown_models end def setup_embedded_associations - Item.cache_has_one :associated - Item.cache_has_many :associated_records, :embed => true - AssociatedRecord.cache_has_many :deeply_associated_records, :embed => true + Item.cache_has_one(:associated, embed: true) + Item.cache_has_many(:associated_records, embed: true) + AssociatedRecord.cache_has_many(:deeply_associated_records, embed: true) end class CacheRunner include ActiveRecordObjects include DatabaseConnection @@ -84,11 +78,11 @@ CACHE_RUNNERS = [] class FindRunner < CacheRunner def run (1..@count).each do |i| - ::Item.includes(:associated, {:associated_records => :deeply_associated_records}).find(i) + ::Item.includes(:associated, associated_records: :deeply_associated_records).find(i) end end end CACHE_RUNNERS << FindRunner @@ -107,42 +101,42 @@ end module DeletedRunner def prepare super - (1..@count).each {|i| ::Item.find(i).send(:expire_cache) } + (1..@count).each { |i| ::Item.find(i).expire_cache } end end module ConflictRunner def prepare super - records = (1..@count).map {|id| ::Item.find(id) } + records = (1..@count).map { |id| ::Item.find(id) } orig_resolve_cache_miss = ::Item.method(:resolve_cache_miss) ::Item.define_singleton_method(:resolve_cache_miss) do |id| - records[id-1].send(:expire_cache) + records[id - 1].expire_cache orig_resolve_cache_miss.call(id) end IdentityCache.cache.clear end end module DeletedConflictRunner include ConflictRunner def prepare super - (1..@count).each {|i| ::Item.find(i).send(:expire_cache) } + (1..@count).each { |i| ::Item.find(i).expire_cache } end end class EmbedRunner < CacheRunner def setup_models super - Item.cache_has_one :associated - Item.cache_has_many :associated_records, :embed => true - AssociatedRecord.cache_has_many :deeply_associated_records, :embed => true + Item.cache_has_one(:associated, embed: true) + Item.cache_has_many(:associated_records, embed: true) + AssociatedRecord.cache_has_many(:deeply_associated_records, embed: true) end def run (1..@count).each do |i| rec = ::Item.fetch(i) @@ -178,12 +172,12 @@ CACHE_RUNNERS << FetchEmbedDeletedConflictRunner class NormalizedRunner < CacheRunner def setup_models super - Item.cache_has_one :associated # :embed => false isn't supported - Item.cache_has_many :associated_records, :embed => :ids - AssociatedRecord.cache_has_many :deeply_associated_records, :embed => :ids + Item.cache_has_one(:associated, embed: :id) + Item.cache_has_many(:associated_records, embed: :ids) + AssociatedRecord.cache_has_many(:deeply_associated_records, embed: :ids) end def run (1..@count).each do |i| rec = ::Item.fetch(i)