Skip to content

Commit 3be2b07

Browse files
committed
Updated Async implementation based on spike.
1 parent dffa56c commit 3be2b07

File tree

6 files changed

+148
-850
lines changed

6 files changed

+148
-850
lines changed

examples/benchmark_async.rb

Lines changed: 62 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,6 @@ def foo(latch = nil)
2222
end
2323
end
2424

25-
class AsyncAlternateClass
26-
include Concurrent::AsyncAlternate
27-
def foo(latch = nil)
28-
latch.count_down if latch
29-
end
30-
end
31-
3225
IPS_NUM = 100
3326
BMBM_NUM = 100_000
3427
SMALL_BMBM = 250
@@ -47,14 +40,7 @@ def foo(latch = nil)
4740
end
4841

4942
async = AsyncClass.new
50-
bm.report('async, thread per object') do
51-
latch = Concurrent::CountDownLatch.new(IPS_NUM)
52-
IPS_NUM.times { async.async.foo(latch) }
53-
latch.wait
54-
end
55-
56-
async = AsyncAlternateClass.new
57-
bm.report('async, global thread pool') do
43+
bm.report('async') do
5844
latch = Concurrent::CountDownLatch.new(IPS_NUM)
5945
IPS_NUM.times { async.async.foo(latch) }
6046
latch.wait
@@ -72,14 +58,7 @@ def foo(latch = nil)
7258
end
7359

7460
async = AsyncClass.new
75-
bm.report('async, thread per object') do
76-
latch = Concurrent::CountDownLatch.new(BMBM_NUM)
77-
BMBM_NUM.times { async.async.foo(latch) }
78-
latch.wait
79-
end
80-
81-
async = AsyncAlternateClass.new
82-
bm.report('async, global thread pool') do
61+
bm.report('async') do
8362
latch = Concurrent::CountDownLatch.new(BMBM_NUM)
8463
BMBM_NUM.times { async.async.foo(latch) }
8564
latch.wait
@@ -92,16 +71,35 @@ def foo(latch = nil)
9271
puts "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
9372
puts ""
9473

74+
Benchmark.ips do |bm|
75+
bm.report('future') do
76+
latch = Concurrent::CountDownLatch.new(IPS_NUM)
77+
IPS_NUM.times do
78+
Concurrent::Future.execute { latch.count_down }
79+
end
80+
latch.wait
81+
end
82+
83+
async = AsyncClass.new
84+
bm.report('async') do
85+
latch = Concurrent::CountDownLatch.new(IPS_NUM)
86+
IPS_NUM.times { AsyncClass.new.async.foo(latch) }
87+
latch.wait
88+
end
89+
90+
bm.compare!
91+
end
92+
9593
Benchmark.bmbm do |bm|
9694
bm.report('celluloid') do
9795
latch = Concurrent::CountDownLatch.new(SMALL_BMBM)
9896
SMALL_BMBM.times { CelluloidClass.new.async.foo(latch) }
9997
latch.wait
10098
end
10199

102-
bm.report('async, global thread pool') do
100+
bm.report('async') do
103101
latch = Concurrent::CountDownLatch.new(SMALL_BMBM)
104-
SMALL_BMBM.times { AsyncAlternateClass.new.async.foo(latch) }
102+
SMALL_BMBM.times { AsyncClass.new.async.foo(latch) }
105103
latch.wait
106104
end
107105
end
@@ -133,46 +131,37 @@ def foo(latch = nil)
133131
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
134132

135133
Calculating -------------------------------------
136-
celluloid 24.000 i/100ms
137-
async, thread per object
138-
36.000 i/100ms
139-
async, global thread pool
140-
36.000 i/100ms
134+
celluloid 21.000 i/100ms
135+
async 36.000 i/100ms
141136
-------------------------------------------------
142-
celluloid 270.238 (±10.4%) i/s - 1.344k
143-
async, thread per object
144-
366.529 (± 3.3%) i/s - 1.836k
145-
async, global thread pool
146-
365.264 (± 3.0%) i/s - 1.836k
137+
celluloid 218.207 (±17.0%) i/s - 1.071k
138+
async 375.318 (± 3.2%) i/s - 1.908k
147139

148140
Comparison:
149-
async, thread per object: 366.5 i/s
150-
async, global thread pool: 365.3 i/s - 1.00x slower
151-
celluloid: 270.2 i/s - 1.36x slower
141+
async: 375.3 i/s
142+
celluloid: 218.2 i/s - 1.72x slower
152143

153-
Rehearsal -------------------------------------------------------------
154-
celluloid 4.110000 0.670000 4.780000 ( 4.784982)
155-
async, thread per object 3.050000 0.090000 3.140000 ( 3.128709)
156-
async, global thread pool 2.960000 0.020000 2.980000 ( 2.981984)
157-
--------------------------------------------------- total: 10.900000sec
144+
Rehearsal ---------------------------------------------
145+
celluloid 4.150000 0.690000 4.840000 ( 4.826509)
146+
async 2.740000 0.010000 2.750000 ( 2.762197)
147+
------------------------------------ total: 7.590000sec
158148

159-
user system total real
160-
celluloid 4.220000 0.700000 4.920000 ( 4.955064)
161-
async, thread per object 3.000000 0.060000 3.060000 ( 3.055045)
162-
async, global thread pool 3.150000 0.060000 3.210000 ( 3.240574)
149+
user system total real
150+
celluloid 4.060000 0.680000 4.740000 ( 4.734005)
151+
async 2.720000 0.040000 2.760000 ( 2.745365)
163152

164153
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
165154
Short-lived objects
166155
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
167156

168-
Rehearsal -------------------------------------------------------------
169-
celluloid 0.180000 0.050000 0.230000 ( 0.220111)
170-
async, global thread pool 0.090000 0.020000 0.110000 ( 0.111569)
171-
---------------------------------------------------- total: 0.340000sec
157+
Rehearsal ---------------------------------------------
158+
celluloid 0.120000 0.030000 0.150000 ( 0.146426)
159+
async 0.080000 0.020000 0.100000 ( 0.091462)
160+
------------------------------------ total: 0.250000sec
172161

173-
user system total real
174-
celluloid 0.240000 0.120000 0.360000 ( 0.350697)
175-
async, global thread pool 0.010000 0.000000 0.010000 ( 0.013509)
162+
user system total real
163+
celluloid 0.160000 0.060000 0.220000 ( 0.216363)
164+
async 0.010000 0.010000 0.020000 ( 0.015761)
176165

177166
===========================================================
178167
jruby 1.7.19 (1.9.3p551) 2015-01-29 20786bd on Java HotSpot(TM) 64-Bit Server VM 1.8.0_45-b14 +jit [darwin-x86_64]
@@ -184,42 +173,33 @@ def foo(latch = nil)
184173

185174
Calculating -------------------------------------
186175
celluloid 2.000 i/100ms
187-
async, thread per object
188-
24.000 i/100ms
189-
async, global thread pool
190-
48.000 i/100ms
176+
async 20.000 i/100ms
191177
-------------------------------------------------
192-
celluloid 130.115 (±40.7%) i/s - 492.000
193-
async, thread per object
194-
896.257 (±17.6%) i/s - 3.984k
195-
async, global thread pool
196-
926.262 (±11.0%) i/s - 4.560k
178+
celluloid 141.910 (±40.9%) i/s - 508.000
179+
async 783.468 (±32.4%) i/s - 3.120k
197180

198181
Comparison:
199-
async, global thread pool: 926.3 i/s
200-
async, thread per object: 896.3 i/s - 1.03x slower
201-
celluloid: 130.1 i/s - 7.12x slower
182+
async: 783.5 i/s
183+
celluloid: 141.9 i/s - 5.52x slower
202184

203-
Rehearsal -------------------------------------------------------------
204-
celluloid 5.800000 1.590000 7.390000 ( 5.306000)
205-
async, thread per object 2.880000 0.190000 3.070000 ( 1.601000)
206-
async, global thread pool 2.150000 0.130000 2.280000 ( 1.172000)
207-
--------------------------------------------------- total: 12.740000sec
185+
Rehearsal ---------------------------------------------
186+
celluloid 5.880000 1.560000 7.440000 ( 5.464000)
187+
async 2.800000 0.230000 3.030000 ( 1.615000)
188+
----------------------------------- total: 10.470000sec
208189

209-
user system total real
210-
celluloid 5.590000 1.520000 7.110000 ( 5.391000)
211-
async, thread per object 2.480000 0.160000 2.640000 ( 1.364000)
212-
async, global thread pool 1.850000 0.130000 1.980000 ( 1.008000)
190+
user system total real
191+
celluloid 5.660000 1.500000 7.160000 ( 5.432000)
192+
async 3.040000 0.250000 3.290000 ( 1.749000)
213193

214194
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
215195
Short-lived objects
216196
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
217197

218-
Rehearsal -------------------------------------------------------------
219-
celluloid 1.530000 0.140000 1.670000 ( 0.597000)
220-
async, global thread pool 0.060000 0.000000 0.060000 ( 0.018000)
221-
---------------------------------------------------- total: 1.730000sec
198+
Rehearsal ---------------------------------------------
199+
celluloid 1.580000 0.120000 1.700000 ( 0.612000)
200+
async 0.060000 0.010000 0.070000 ( 0.018000)
201+
------------------------------------ total: 1.770000sec
222202

223-
user system total real
224-
celluloid 1.160000 0.160000 1.320000 ( 0.431000)
225-
async, global thread pool 0.020000 0.000000 0.020000 ( 0.009000)
203+
user system total real
204+
celluloid 0.670000 0.110000 0.780000 ( 0.295000)
205+
async 0.030000 0.000000 0.030000 ( 0.009000)

lib/concurrent.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
require 'concurrent/map'
1616
require 'concurrent/tuple'
1717
require 'concurrent/async'
18-
require 'concurrent/async_alternate'
1918
require 'concurrent/dataflow'
2019
require 'concurrent/delay'
2120
require 'concurrent/exchanger'

0 commit comments

Comments
 (0)