summaryrefslogtreecommitdiff
path: root/fiber_benchmark.rb
blob: 030c1666c20c8d5272ba9ff18f810c733e8d85bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/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