2018-11-20 04:59:18 -05:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
|
|
|
|
require 'benchmark'
|
|
|
|
|
|
|
|
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 04:59:19 -05:00
|
|
|
|
2018-11-20 04:59:18 -05:00
|
|
|
time = Benchmark.realtime do
|
2018-11-20 05:09:53 -05:00
|
|
|
chain = make_chain(length) do
|
|
|
|
while true
|
|
|
|
Fiber.yield(message)
|
|
|
|
end
|
|
|
|
end
|
2018-11-20 04:59:18 -05:00
|
|
|
end
|
2018-11-20 04:59:19 -05:00
|
|
|
|
2018-11-20 04:59:18 -05:00
|
|
|
puts "Creating #{fibers} fibers took #{time}..."
|
2018-11-20 04:59:19 -05:00
|
|
|
|
2018-11-20 04:59:18 -05:00
|
|
|
time = Benchmark.realtime do
|
|
|
|
repeats.times do
|
|
|
|
abort "invalid result" unless chain.resume == message
|
|
|
|
end
|
|
|
|
end
|
2018-11-20 04:59:19 -05:00
|
|
|
|
2018-11-20 04:59:18 -05:00
|
|
|
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
|