Permalink
Browse files

improve spec and bench

  • Loading branch information...
1 parent 68d51b2 commit ced082bb47b2bff45dcea9d11b3a7ae73eb9cdc0 @the8472 committed Jan 4, 2013
Showing with 39 additions and 5 deletions.
  1. +34 −3 bench/core/basic_object/ivar_access_bench.rb
  2. +5 −2 spec/regression/GH-476_ivar_concurrency_spec.rb
@@ -10,13 +10,44 @@
disposable_objects = (0..10).map{Class.new.new}
-simple_reader = proc{|o| result = nil;SIMPLE_TIMES.times{ result = o.instance_variable_get(:@foo)};result}
-simple_writer = proc{|o| SIMPLE_TIMES.times{o.instance_variable_set(:@foo,1)}}
+simple_reader = proc do |o|
+ result = nil;
+ for i in (0..SIMPLE_TIMES)
+ result = o.instance_variable_get(:@foo)
+ result = o.instance_variable_get(:@foo)
+ result = o.instance_variable_get(:@foo)
+ result = o.instance_variable_get(:@foo)
+ result = o.instance_variable_get(:@foo)
+ result = o.instance_variable_get(:@foo)
+ result = o.instance_variable_get(:@foo)
+ result = o.instance_variable_get(:@foo)
+ result = o.instance_variable_get(:@foo)
+ result = o.instance_variable_get(:@foo)
+ result = o.instance_variable_get(:@foo)
+ end
+ result
+end
+simple_writer = proc do |o|
+ for i in (0..SIMPLE_TIMES)
+ o.instance_variable_set(:@foo,0)
+ o.instance_variable_set(:@foo,1)
+ o.instance_variable_set(:@foo,2)
+ o.instance_variable_set(:@foo,3)
+ o.instance_variable_set(:@foo,4)
+ o.instance_variable_set(:@foo,5)
+ o.instance_variable_set(:@foo,6)
+ o.instance_variable_set(:@foo,7)
+ o.instance_variable_set(:@foo,8)
+ o.instance_variable_set(:@foo,9)
+ end
+end
Benchmark.bmbm do |b|
b.report "baseline x#{SIMPLE_TIMES}" do
- SIMPLE_TIMES.times{1}
+ for i in (0..SIMPLE_TIMES)
+ 1
+ end
end
b.report "single threaded reads x#{SIMPLE_TIMES}" do
@@ -4,10 +4,12 @@
it 'should not lose concurrent writes under growth operations' do
lost_writes = false
- (0..1000).each do |i|
+ (0..100).each do |i|
clazz = Class.new
object = clazz.new
+ mutate = true
+
# probing thread
t1 = Thread.new do
(0..10000).each do |i|
@@ -18,9 +20,10 @@
end
# mutating thread
- t2 = Thread.new{(0..10000).each{object.instance_variable_set(:"@bar_#{rand(100000)}",1)}}
+ t2 = Thread.new{(0..10000).each{break unless mutate;object.instance_variable_set(:"@bar_#{rand(100000)}",1)}}
t1.join
+ mutate = false
t2.join
break if lost_writes

0 comments on commit ced082b

Please sign in to comment.