Skip to content

Instantly share code, notes, and snippets.

@firejox
Created October 14, 2019 06:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save firejox/11579afacf47701a4c9a5071ecdc7bce to your computer and use it in GitHub Desktop.
Save firejox/11579afacf47701a4c9a5071ecdc7bce to your computer and use it in GitHub Desktop.
Channel ping-pong latency
ch = Channel(Int32).new
done = Channel(Time::Span).new
count = (ARGV[0]? || 1000).to_i
4.times do
spawn do
delay = Time::Span::ZERO
count.times do |i|
delay += Time.measure do
ch.send i
end
end
done.send delay
end
end
4.times do
spawn do
delay = Time::Span::ZERO
count.times do |i|
delay += Time.measure do
ch.receive?
end
end
done.send delay
end
end
total_delay = Time::Span::ZERO
8.times do
total_delay += done.receive
end
puts total_delay
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment