Skip to content

Commit 860dcae

Browse files
committed
Update benchmark to new promises
1 parent ba0ec3b commit 860dcae

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

examples/benchmark_new_futures.rb

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,42 @@
22

33
require 'benchmark/ips'
44
require 'concurrent'
5+
require 'concurrent-edge'
56

7+
raise 'concurrent-ext not loaded' if Concurrent.on_cruby? && Concurrent.c_extensions_loaded?
68

79
scale = 1
810
time = 10 * scale
911
warmup = 2 * scale
1012
warmup *= 10 if Concurrent.on_jruby?
1113

1214
Benchmark.ips(time, warmup) do |x|
13-
x.report('flat-old') { Concurrent::Promise.execute { 1 }.flat_map { |v| Concurrent::Promise.execute { v + 2 } }.value! }
14-
x.report('flat-new') { Concurrent::Promises.future(:fast) { 1 }.then { |v| Concurrent::Promises.future(:fast) { v + 2 } }.flat.value! }
15+
x.report('flat-old') do
16+
Concurrent::Promise.execute { 1 }.flat_map { |v| Concurrent::Promise.execute { v + 2 } }.value!
17+
end
18+
x.report('flat-new') do
19+
Concurrent::Promises.future(:fast) { 1 }.then { |v| Concurrent::Promises.future(:fast) { v + 2 } }.flat.value!
20+
end
1521
x.compare!
1622
end
1723

1824
Benchmark.ips(time, warmup) do |x|
1925
x.report('status-old') { f = Concurrent::Promise.execute { nil }; 100.times { f.complete? } }
20-
x.report('status-new') { f = Concurrent::Promises.future(:fast) { nil }; 100.times { f.completed? } }
26+
x.report('status-new') { f = Concurrent::Promises.future(:fast) { nil }; 100.times { f.resolved? } }
2127
x.compare!
2228
end
2329

2430
Benchmark.ips(time, warmup) do |x|
2531
of = Concurrent::Promise.execute { 1 }
26-
nf = Concurrent::Promises.succeeded_future(1, :fast)
32+
nf = Concurrent::Promises.fulfilled_future(1, :fast)
2733
x.report('value-old') { of.value! }
2834
x.report('value-new') { nf.value! }
2935
x.compare!
3036
end
3137

3238
Benchmark.ips(time, warmup) do |x|
3339
x.report('graph-old') do
34-
head = Concurrent::Promise.execute { 1 }
40+
head = Concurrent::Promise.fulfill(1)
3541
10.times do
3642
branch1 = head.then(&:succ)
3743
branch2 = head.then(&:succ).then(&:succ)
@@ -40,7 +46,7 @@
4046
head.value!
4147
end
4248
x.report('graph-new') do
43-
head = Concurrent::Promises.succeeded_future(1, :fast)
49+
head = Concurrent::Promises.fulfilled_future(1, :fast)
4450
10.times do
4551
branch1 = head.then(&:succ)
4652
branch2 = head.then(&:succ).then(&:succ)
@@ -52,15 +58,15 @@
5258
end
5359

5460
Benchmark.ips(time, warmup) do |x|
55-
x.report('immediate-old') { Concurrent::Promise.execute { nil }.value! }
56-
x.report('immediate-new') { Concurrent::Promises.succeeded_future(nil, :fast).value! }
61+
x.report('immediate-old') { Concurrent::Promise.fulfill(nil).value! }
62+
x.report('immediate-new') { Concurrent::Promises.fulfilled_future(nil, :fast).value! }
5763
x.compare!
5864
end
5965

6066
Benchmark.ips(time, warmup) do |x|
6167
of = Concurrent::Promise.execute { 1 }
62-
nf = Concurrent::Promises.succeeded_future(1, :fast)
63-
x.report('then-old') { 100.times.reduce(of) { |nf, _| nf.then(&:succ) }.value! }
64-
x.report('then-new') { 100.times.reduce(nf) { |nf, _| nf.then(&:succ) }.value! }
68+
nf = Concurrent::Promises.fulfilled_future(1, :fast)
69+
x.report('then-old') { 50.times.reduce(of) { |nf, _| nf.then(&:succ) }.value! }
70+
x.report('then-new') { 50.times.reduce(nf) { |nf, _| nf.then(&:succ) }.value! }
6571
x.compare!
6672
end

0 commit comments

Comments
 (0)