Permalink
Browse files

+ object pool

  • Loading branch information...
Florian René Hanke
Florian René Hanke committed Oct 22, 2012
1 parent b8a1a87 commit b087b93921ccbb1082ec71258a10ed8ede7ebd31
Showing with 75 additions and 52 deletions.
  1. +2 −1 server/lib/picky/pool.rb
  2. +56 −34 server/spec/functional/pool_spec.rb
  3. +17 −17 server/spec/lib/pool_spec.rb
View
@@ -90,7 +90,8 @@ def release instance
# anymore.
#
def release_all
- @__free__ += @__used__
+ @__used__.each { |used| @__free__ << used } # +0 Array per release_all
+ # @__used__ += @__free__ # +1 Array per release_all
@__used__.clear
end
@@ -6,55 +6,77 @@
describe 'GC stats: searching' do
- PoolSpecThing = Struct.new :id, :first, :last
+ PoolSpecThing = Struct.new :id, :first, :last, :other
before(:each) do
Picky::Indexes.clear_indexes
- # GC.stress = true
- end
- after(:each) do
- # GC.stress = false
end
+ let(:amount) { 5_000 }
let(:data) do
index = Picky::Index.new :sorted do
category :first
category :last
+ category :other
+ end
+ 3.times do |i|
+ index.add PoolSpecThing.new(i+1, 'Abracadabra', 'Mirgel', 'whatever it')
+ index.add PoolSpecThing.new(i+2, 'Abraham', 'Minder', 'is not too')
+ index.add PoolSpecThing.new(i+3, 'Azzie', 'Mueller', 'unimportant')
end
- index.add PoolSpecThing.new(1, 'Abracadabra', 'Mirgel')
- index.add PoolSpecThing.new(2, 'Abraham', 'Minder')
- index.add PoolSpecThing.new(3, 'Azzie', 'Mueller')
index
end
let(:search) { Picky::Search.new data }
# TODO Study what the problem is.
#
- # context 'without pool' do
- # it 'runs the GC more' do
- # try = search
- # query = 'abracadabra mirgel'
- # gc_runs_of do
- # 10000.times { try.search query }
- # end.should <= 0
- # end
- # end
- # context 'with pool' do
- # before(:each) do
- # Picky::Pool.install
- # end
- # after(:each) do
- # # Reload since installing the Pool taints the classes.
- # #
- # Picky::Loader.load_framework
- # end
- # it 'runs the GC less' do
- # try = search
- # query = 'abracadabra mirgel'
- # gc_runs_of do
- # 10000.times { try.search query; Picky::Pool.release_all }
- # end.should <= 0 # Definitely less GC runs.
- # end
- # end
+ context 'without pool' do
+ it 'runs the GC more' do
+ # Quickly check if the pool is removed.
+ #
+ fail 'object pool still installed' if Picky::Query::Token.respond_to? :release_all
+
+ try = search
+ query = 'abracadabra mirgel'
+ gc_runs_of do
+ amount.times { try.search query }
+ end.should <= 13
+ end
+ it 'is less (?) performant' do
+ try = search
+ query = 'abracadabra mirgel'
+ performance_of do
+ amount.times { try.search query }
+ end.should <= 0.42
+ end
+ end
+ context 'with pool' do
+ before(:each) do
+ Picky::Pool.install
+ end
+ after(:each) do
+ # Reload since installing the Pool taints the classes.
+ #
+ Picky::Loader.load_framework
+ end
+ it 'runs the GC less' do
+ # Quickly check that the pool is added.
+ #
+ fail 'object pool not installed' unless Picky::Query::Token.respond_to? :release_all
+
+ try = search
+ query = 'abracadabra mirgel'
+ gc_runs_of do
+ amount.times { try.search query; Picky::Pool.release_all }
+ end.should <= 1 # Definitely less GC runs.
+ end
+ it 'is more (?) performant' do
+ try = search
+ query = 'abracadabra mirgel'
+ performance_of do
+ amount.times { try.search query }
+ end.should <= 0.42
+ end
+ end
end
@@ -4,7 +4,7 @@
describe Picky::Pool do
- let(:pool) do
+ let(:pool_klass) do
Class.new do
extend Picky::Pool
@@ -30,50 +30,50 @@ def initialize number
context 'functional' do
before(:each) do
described_class.clear
- pool.clear
+ pool_klass.clear
other.clear
end
it 'lets me get an instance' do
- pool.new(1).should be_kind_of(pool)
+ pool_klass.new(1).should be_kind_of(pool_klass)
end
it 'does not create a new reference if it has free ones' do
- p1 = pool.new 1
- p2 = pool.new 2
+ p1 = pool_klass.new 1
+ p2 = pool_klass.new 2
p1.release
- pool.free_size.should == 1
+ pool_klass.free_size.should == 1
end
it 'gives me the released reference if I try to new' do
- p1 = pool.new 1
- p2 = pool.new 2
+ p1 = pool_klass.new 1
+ p2 = pool_klass.new 2
p1.release
- pool.new(3).number.should == 3
+ pool_klass.new(3).number.should == 3
end
it 'releases all PoolTests if called on PoolTest' do
- p1 = pool.new 1
- pool.new 2
+ p1 = pool_klass.new 1
+ pool_klass.new 2
other.new 1
other.new 2
other.free_size.should == 0
- pool.release_all
+ pool_klass.release_all
- pool.new(3).should == p1
+ pool_klass.new(3).should == p1
other.free_size.should == 0
end
it 'releases all if called on Pool' do
- pool.new 1
- pool.new 2
+ pool_klass.new 1
+ pool_klass.new 2
other.new 1
other.new 2
- pool.free_size.should == 0
+ pool_klass.free_size.should == 0
described_class.release_all
- pool.free_size.should == 2
+ pool_klass.free_size.should == 2
end
end

0 comments on commit b087b93

Please sign in to comment.