-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathlocal_lat_poll.cr
60 lines (44 loc) · 1.3 KB
/
local_lat_poll.cr
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
require "../src/zeromq"
if ARGV.size < 2
puts "usage: crystal local_lat_poll.cr <message-size> <roundtrip-count>"
exit
end
link = "tcp://127.0.0.1:5555"
message_size = ARGV[0].to_i
roundtrip_count = ARGV[1].to_i
begin
ctx = ZMQ::Context.new
s1 = ctx.socket(ZMQ::REQ)
s2 = ctx.socket(ZMQ::REP)
rescue e : ZMQ::ContextError
STDERR.puts "Failed to allocate context or socket!"
raise e
end
s1.set_socket_option(ZMQ::LINGER, 100)
s2.set_socket_option(ZMQ::LINGER, 100)
s2.bind(link) || puts "binding failed #{link}"
s1.connect(link) || puts "connect failed"
poller = ZMQ::Poller.new
poller.register_readable(s2)
poller.register_readable(s1)
start_time = Time.monotonic
# kick it off
message = ZMQ::Message.new("a" * message_size)
s1.send_message(message, ZMQ::DONTWAIT)
i = roundtrip_count
until i == 0
i -= 1
poller.poll(-1)
poller.readables.each do |socket|
received_message = socket.receive_message(ZMQ::DONTWAIT)
socket.send_message(received_message, ZMQ::DONTWAIT)
end
end
span = (Time.monotonic - start_time)
latency = span.total_microseconds / roundtrip_count / 2.0
puts "mean latency: %.3f [us]" % latency
puts "messages per second: %.3f " % (roundtrip_count / span.total_seconds)
puts "received all messages in %.3f ms" % (span.total_milliseconds)
s1.close
s2.close
ctx.terminate