Skip to content

Instantly share code, notes, and snippets.

@carlhoerberg
Forked from laser/rpc_benchmark.rb
Last active August 29, 2015 14:00
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 carlhoerberg/2c1fde3e5992092b2d81 to your computer and use it in GitHub Desktop.
Save carlhoerberg/2c1fde3e5992092b2d81 to your computer and use it in GitHub Desktop.
#!/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