Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env ruby
require 'securerandom'
require 'json'
require 'benchmark'
require 'bunny'
conn = Bunny.new
conn.start
ch = conn.create_channel
x = ch.default_exchange
q = ch.queue('calc')
results = Hash.new { |h,k| h[k] = Queue.new }
reply_q2 = ch.queue('', exclusive: true)
reply_q2.subscribe(block: false) do |delivery_info, properties, payload|
results[properties[:correlation_id]].push payload
end
n = 1000
Benchmark.bmbm do |b|
b.report("RabbitMQ") do
n.times do
# request id will be used as the name of the return queue
req_message = {
'id' => SecureRandom.hex,
'jsonrpc' => '2.0',
'method' => 'add',
'params' => [1, 5.1]
}
# send out our request, serialized as JSON
x.publish(req_message.to_json, {
correlation_id: req_message['id'],
reply_to: reply_q2.name,
routing_key: q.name
})
response = results[req_message['id']].pop
results.delete req_message['id'] # prevent memory leak
# subscribe to the return queue in a blocking fashion
"1+5.1=%.1f" % JSON.parse(response)['result'] # 1+5.1=6.1
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.