Created
October 14, 2019 06:22
-
-
Save firejox/11579afacf47701a4c9a5071ecdc7bce to your computer and use it in GitHub Desktop.
Channel ping-pong latency
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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