-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmassive_async.rb
executable file
·94 lines (72 loc) · 1.92 KB
/
massive_async.rb
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#! /usr/bin/env ruby
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'async'
gem 'cztop', path: '../../'
gem 'benchmark'
end
require 'async/barrier'
ENDPOINTS = [
'inproc://req_rep_example0',
'inproc://req_rep_example1',
'inproc://req_rep_example2',
'ipc:///tmp/req_rep_example0',
# 'ipc:///tmp/req_rep_example1',
# 'ipc:///tmp/req_rep_example2',
'tcp://localhost:5556',
# 'tcp://localhost:5557',
# 'tcp://localhost:5558',
]
REQ_SOCKETS = 200
REQUESTS = 1000 # per REQ socket
PROGRESS_STEP = 1000 # requests per '.' printed
puts "#{ENDPOINTS.size} REP sockets serving requests from #{REQ_SOCKETS} REQ sockets."
puts "Each REQ socket will send #{REQUESTS} requests."
puts ". = #{PROGRESS_STEP} requests"
Async do |task|
rep_task = Async do |task|
ENDPOINTS.each do |endpoint|
task.async do
socket = CZTop::Socket::REP.new endpoint
n = 0
while true
msg = socket.receive.to_a
# puts "<<< #{msg.to_a.inspect}"
socket << msg.map(&:upcase)
n += 1
end
ensure
puts "REP at #{endpoint} served #{n} requests."
end
end
end
n = 0
realtime = Benchmark.realtime do
req_task = Async do
barrier = Async::Barrier.new
REQ_SOCKETS.times.map do |i|
barrier.async do
socket = CZTop::Socket::REQ.new
# socket.options.rcvtimeo = 1000 # ms
ENDPOINTS.each do |endpoint|
socket.connect endpoint
end
REQUESTS.times do |i|
socket << "foobar ##{i}"
msg = socket.receive
n += 1
print '.' if n % PROGRESS_STEP == 0
# puts ">>> #{msg.to_a.inspect}"
end
end
end
barrier.wait
end
req_task.wait
puts
rep_task.stop
end
rps = n / realtime / 1000
puts "#{n} requests in %.2fs = %.1fk r/s" % [realtime, rps]
end