11require 'benchmark'
2- require 'concurrent '
2+ require 'rbconfig '
33require 'thread'
4+ require 'concurrent'
45Thread . abort_on_exception = true
56
67$go = false # for synchronizing parallel threads
1112# number of threads for parallel test
1213M = ARGV [ 0 ] ? ARGV [ 0 ] . to_i : 100
1314
15+ # list of platform-specific implementations
16+ ATOMICS = [
17+ 'MutexAtomic' ,
18+ 'CAtomic' ,
19+ 'JavaAtomic' ,
20+ 'RbxAtomic' ,
21+ ]
22+
23+ puts "Testing with #{ RbConfig ::CONFIG [ 'ruby_install_name' ] } #{ RUBY_VERSION } "
1424
15- puts "*** Sequential updates ***"
25+ puts
26+ puts '*** Sequential updates ***'
1627Benchmark . bm ( 10 ) do |x |
1728 value = 0
18- x . report " no lock" do
29+ x . report ' no lock' do
1930 N . times do
2031 value += 1
2132 end
2233 end
2334
2435 @lock = Mutex . new
25- x . report " mutex" do
36+ x . report ' mutex' do
2637 value = 0
2738 N . times do
2839 @lock . synchronize do
3142 end
3243 end
3344
34- @atom = Concurrent ::Atomic . new ( 0 )
35- x . report "atomic" do
36- N . times do
37- @atom . update { |x | x += 1 }
45+ ATOMICS . each do |clazz |
46+ if Concurrent . const_defined? clazz
47+ @atom = Concurrent . const_get ( clazz ) . new ( 0 )
48+ x . report clazz do
49+ N . times do
50+ @atom . update { |x | x += 1 }
51+ end
52+ end
3853 end
3954 end
4055end
4156
4257def para_setup ( num_threads , count , &block )
4358 if num_threads % 2 > 0
44- raise ArgumentError , " num_threads must be a multiple of two"
59+ raise ArgumentError , ' num_threads must be a multiple of two'
4560 end
46- raise ArgumentError , " need block" unless block_given?
61+ raise ArgumentError , ' need block' unless block_given?
4762
4863 # Keep those threads together
4964 tg = ThreadGroup . new
@@ -62,7 +77,7 @@ def para_setup(num_threads, count, &block)
6277 end
6378
6479 # Make sure all threads are started
65- while tg . list . find { |t | t . status != " run" }
80+ while tg . list . find { |t | t . status != ' run' }
6681 Thread . pass
6782 end
6883
@@ -78,15 +93,15 @@ def para_run(tg)
7893 $go = false
7994end
8095
81- puts "*** Parallel updates ***"
96+ puts
97+ puts '*** Parallel updates ***'
8298Benchmark . bm ( 10 ) do |bm |
8399 # This is not secure
84100 value = 0
85101 tg = para_setup ( M , N /M ) do |diff |
86102 value += diff
87103 end
88- bm . report ( "no lock" ) { para_run ( tg ) }
89-
104+ bm . report ( 'no lock' ) { para_run ( tg ) }
90105
91106 value = 0
92107 @lock = Mutex . new
@@ -95,15 +110,17 @@ def para_run(tg)
95110 value += diff
96111 end
97112 end
98- bm . report ( " mutex" ) { para_run ( tg ) }
113+ bm . report ( ' mutex' ) { para_run ( tg ) }
99114 raise unless value == 0
100115
101-
102- @atom = Concurrent ::Atomic . new ( 0 )
103- tg = para_setup ( M , N /M ) do |diff |
104- @atom . update { |x | x + diff }
116+ ATOMICS . each do |clazz |
117+ if Concurrent . const_defined? clazz
118+ @atom = Concurrent . const_get ( clazz ) . new ( 0 )
119+ tg = para_setup ( M , N /M ) do |diff |
120+ @atom . update { |x | x + diff }
121+ end
122+ bm . report ( clazz ) { para_run ( tg ) }
123+ raise unless @atom . value == 0
124+ end
105125 end
106- bm . report ( "atomic" ) { para_run ( tg ) }
107- raise unless @atom . value == 0
108-
109126end
0 commit comments