summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsamuel <samuel@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-11-20 09:59:18 +0000
committersamuel <samuel@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-11-20 09:59:18 +0000
commit748c497a6d8f3f4136a6bd4c1931c73f041aba0e (patch)
treef5b318d63f22014837e166ce3f3c9c88aae66e5b
parent990ddcfccf7b234960bf953ca8cf453d4b397d11 (diff)
Rewrite fiber benchmark
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65836 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rwxr-xr-xbenchmark/bm_vm2_fiber_chain.rb44
-rwxr-xr-xfiber_benchmark.rb103
2 files changed, 44 insertions, 103 deletions
diff --git a/benchmark/bm_vm2_fiber_chain.rb b/benchmark/bm_vm2_fiber_chain.rb
new file mode 100755
index 0000000..3227be0
--- /dev/null
+++ b/benchmark/bm_vm2_fiber_chain.rb
@@ -0,0 +1,44 @@
+#!/usr/bin/env ruby
+
+require 'benchmark'
+
+def make_link(previous)
+ Fiber.new do
+ while message = previous.resume
+ Fiber.yield(message)
+ end
+ end
+end
+
+def run_benchmark(fibers, repeats, message = :hello)
+ chain = nil
+
+ time = Benchmark.realtime do
+ chain = Fiber.new do
+ while true
+ Fiber.yield(message)
+ end
+ end
+
+ (fibers - 1).times do
+ chain = make_link(chain)
+ end
+ end
+
+ puts "Creating #{fibers} fibers took #{time}..."
+
+ time = Benchmark.realtime do
+ repeats.times do
+ abort "invalid result" unless chain.resume == message
+ end
+ end
+
+ puts "Passing #{repeats} messages took #{time}..."
+end
+
+n = (ARGV[0] || 1000).to_i
+m = (ARGV[1] || 1000).to_i
+
+5.times do
+ run_benchmark(n, m)
+end
diff --git a/fiber_benchmark.rb b/fiber_benchmark.rb
deleted file mode 100755
index 030c166..0000000
--- a/fiber_benchmark.rb
+++ /dev/null
@@ -1,103 +0,0 @@
-#!/usr/bin/env ruby
-
-require 'fiber'
-require 'benchmark'
-
-class Ring
- attr_reader :id
- attr_accessor :attach
-
- def initialize(id)
- @id = id
- #puts "Creating ring ... #{id}"
- @fiber = Fiber.new do
- pass_message
- end
- end
-
- def |(other)
- other.attach = self if !other.nil?
- #puts "attaching #{@id} to #{other.id}" if !other.nil?
- other
- end
-
- def resume
- @fiber.resume
- end
-
- def pass_message
- #puts "I'm fiber #{@id}"
- while message = message_in
- #puts "... #{@id} I received message #{message}"
- # do something with message
- message_out(message)
- end
- end
-
- def message_in
- #puts "Resuming #{@attach.id}" if !@attach.nil?
- @attach.resume if !@attach.nil?
- end
-
- def message_out(message)
- Fiber.yield(message)
- end
-
-end
-
-class RingStart < Ring
- attr_accessor :message
- def initialize(n, m, message)
- @m = m
- @message = message
- super(n)
- end
-
- def pass_message
- loop { message_out(@message) }
- end
-
-end
-
-
-def create_chain_r(i, chain)
- # recursive version
- return chain if i<=0
- r = chain.nil? ? Ring.new(i) : chain | Ring.new(i)
- create_chain(i-1, r)
-end
-
-def create_chain(n, chain)
- # loop version
- # needed to avoid stack overflow for high n
- n.downto(0) {
- chain = chain | Ring.new(n)
- }
- chain
-end
-
-def run_benchmark(n, m)
- mess = :hello
- ringu = nil
- chain = nil
-
- tm = Benchmark.measure {
- ringu = RingStart.new(0, m, mess)
- chain = create_chain(n, ringu)
- }.format("%10.6r\n").gsub!(/\(|\)/, "")
-
- puts "setup time for #{n} fibers: #{tm}"
-
- tm = Benchmark.measure {
- m.times { ringu.message = chain.resume }
- }.format("%10.6r\n").gsub!(/\(|\)/, "")
-
- puts "execution time for #{m} messages: #{tm}"
-end
-
-n = (ARGV[0] || 1000).to_i
-m = (ARGV[1] || 10000).to_i
-
-5.times do
- run_benchmark(n, m)
-end