2018-11-20 05:16:34 -05:00
|
|
|
# Check performance of fiber creation and transfer.
|
2018-11-20 04:59:18 -05:00
|
|
|
|
|
|
|
def make_link(previous)
|
|
|
|
Fiber.new do
|
|
|
|
while message = previous.resume
|
|
|
|
Fiber.yield(message)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-20 05:09:53 -05:00
|
|
|
def make_chain(length, &block)
|
|
|
|
chain = Fiber.new(&block)
|
2018-11-20 05:09:55 -05:00
|
|
|
|
2018-11-20 05:09:53 -05:00
|
|
|
(length - 1).times do
|
2018-11-20 05:06:58 -05:00
|
|
|
chain = make_link(chain)
|
|
|
|
end
|
2018-11-20 05:06:59 -05:00
|
|
|
|
2018-11-20 05:06:58 -05:00
|
|
|
return chain
|
|
|
|
end
|
|
|
|
|
2018-11-20 05:09:53 -05:00
|
|
|
def run_benchmark(length, repeats, message = :hello)
|
2018-11-20 04:59:18 -05:00
|
|
|
chain = nil
|
2018-11-20 05:16:35 -05:00
|
|
|
|
2018-11-20 05:16:34 -05:00
|
|
|
chain = make_chain(length) do
|
|
|
|
while true
|
|
|
|
Fiber.yield(message)
|
2018-11-20 05:09:53 -05:00
|
|
|
end
|
2018-11-20 04:59:18 -05:00
|
|
|
end
|
2018-11-20 05:16:35 -05:00
|
|
|
|
2018-11-20 05:16:34 -05:00
|
|
|
repeats.times do
|
|
|
|
abort "invalid result" unless chain.resume == message
|
2018-11-20 04:59:18 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
n = (ARGV[0] || 1000).to_i
|
|
|
|
m = (ARGV[1] || 1000).to_i
|
|
|
|
|
|
|
|
5.times do
|
|
|
|
run_benchmark(n, m)
|
|
|
|
end
|