|
| 1 | +#!/usr/bin/env ruby |
| 2 | +require 'bunny' |
| 3 | + |
| 4 | +MESSAGE_COUNT = 50_000 |
| 5 | + |
| 6 | +def publish_messages_individually(connection) |
| 7 | + channel = connection.create_channel |
| 8 | + queue = channel.queue('', exclusive: true) |
| 9 | + |
| 10 | + channel.confirm_select(tracking: true) |
| 11 | + |
| 12 | + start_time = Time.now |
| 13 | + MESSAGE_COUNT.times do |i| |
| 14 | + body = i.to_s |
| 15 | + channel.basic_publish(body, '', queue.name) |
| 16 | + end |
| 17 | + |
| 18 | + # Wait for any remaining confirmations |
| 19 | + channel.wait_for_confirms |
| 20 | + end_time = Time.now |
| 21 | + |
| 22 | + puts "Published #{MESSAGE_COUNT} messages individually in #{((end_time - start_time) * 1000).to_i} ms" |
| 23 | +ensure |
| 24 | + channel.close if channel && channel.open? |
| 25 | +end |
| 26 | + |
| 27 | +def publish_messages_in_batch(connection) |
| 28 | + channel = connection.create_channel |
| 29 | + queue = channel.queue('', exclusive: true) |
| 30 | + |
| 31 | + channel.confirm_select(tracking: true) |
| 32 | + |
| 33 | + batch_size = 1000 |
| 34 | + start_time = Time.now |
| 35 | + |
| 36 | + (0...MESSAGE_COUNT).each_slice(batch_size) do |batch| |
| 37 | + messages = batch.map { |i| i.to_s } |
| 38 | + channel.basic_publish_batch(messages, '', queue.name) |
| 39 | + end |
| 40 | + |
| 41 | + # Wait for any remaining confirmations |
| 42 | + channel.wait_for_confirms |
| 43 | + end_time = Time.now |
| 44 | + |
| 45 | + puts "Published #{MESSAGE_COUNT} messages in batch in #{((end_time - start_time) * 1000).to_i} ms" |
| 46 | +ensure |
| 47 | + channel.close if channel && channel.open? |
| 48 | +end |
| 49 | + |
| 50 | +def handle_publish_confirms_asynchronously(connection) |
| 51 | + channel = connection.create_channel |
| 52 | + queue = channel.queue('', exclusive: true) |
| 53 | + |
| 54 | + outstanding_confirms = {} |
| 55 | + # A mutex is necessary because the confirm callbacks are executed in a separate thread |
| 56 | + confirms_mutex = Mutex.new |
| 57 | + |
| 58 | + channel.confirm_select do |delivery_tag, multiple, nack| |
| 59 | + confirms_mutex.synchronize do |
| 60 | + if multiple |
| 61 | + outstanding_confirms.reject! { |k, _| k <= delivery_tag } |
| 62 | + else |
| 63 | + outstanding_confirms.delete(delivery_tag) |
| 64 | + end |
| 65 | + end |
| 66 | + if nack |
| 67 | + puts "Message with delivery tag #{delivery_tag} was nacked!" |
| 68 | + end |
| 69 | + end |
| 70 | + |
| 71 | + start_time = Time.now |
| 72 | + MESSAGE_COUNT.times do |i| |
| 73 | + body = i.to_s |
| 74 | + seq_no = channel.next_publish_seq_no |
| 75 | + confirms_mutex.synchronize do |
| 76 | + outstanding_confirms[seq_no] = body |
| 77 | + end |
| 78 | + channel.basic_publish(body, '', queue.name) |
| 79 | + end |
| 80 | + |
| 81 | + # Wait for any remaining confirmations |
| 82 | + channel.wait_for_confirms |
| 83 | + end_time = Time.now |
| 84 | + |
| 85 | + puts "Published #{MESSAGE_COUNT} messages and handled confirms asynchronously in #{((end_time - start_time) * 1000).to_i} ms" |
| 86 | +ensure |
| 87 | + channel.close if channel && channel.open? |
| 88 | +end |
| 89 | + |
| 90 | +begin |
| 91 | + connection = Bunny.new |
| 92 | + connection.start |
| 93 | + |
| 94 | + publish_messages_individually(connection) |
| 95 | + publish_messages_in_batch(connection) |
| 96 | + handle_publish_confirms_asynchronously(connection) |
| 97 | +rescue Interrupt => _ |
| 98 | + connection.close |
| 99 | + exit(0) |
| 100 | +ensure |
| 101 | + connection.close if connection |
| 102 | +end |
0 commit comments