File: method_encoding.rb

package info (click to toggle)
ruby-amq-protocol 2.5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 572 kB
  • sloc: ruby: 5,975; python: 248; makefile: 4
file content (198 lines) | stat: -rw-r--r-- 4,845 bytes parent folder | download
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/env ruby
# encoding: utf-8
# frozen_string_literal: true

$LOAD_PATH << File.expand_path(File.join(File.dirname(__FILE__), "..", "lib"))

require "amq/protocol/client"
require "benchmark/ips"

puts
puts "-" * 80
puts "AMQP Method Encoding/Decoding Benchmarks on #{RUBY_DESCRIPTION}"
puts "-" * 80

FRAME_SIZE = 131072  # 128KB, typical default

# Common message properties
BASIC_PROPERTIES = {
  content_type: "application/json",
  delivery_mode: 2,
  priority: 0,
  headers: { "x-custom" => "value" }
}.freeze

MINIMAL_PROPERTIES = {
  delivery_mode: 2
}.freeze

# Payloads
SMALL_BODY = '{"id":1}'.freeze
MEDIUM_BODY = ('x' * 1024).freeze
LARGE_BODY = ('x' * 65536).freeze

puts "=== Basic.Publish (Full Message Encoding) ==="
puts "This is the critical hot path for publishing messages"
puts

Benchmark.ips do |x|
  x.config(time: 5, warmup: 2)

  x.report("Publish small (8B) + minimal props") do
    AMQ::Protocol::Basic::Publish.encode(
      1,              # channel
      SMALL_BODY,     # payload
      MINIMAL_PROPERTIES,
      "",             # exchange
      "test.queue",   # routing_key
      false,          # mandatory
      false,          # immediate
      FRAME_SIZE
    )
  end

  x.report("Publish small (8B) + full props") do
    AMQ::Protocol::Basic::Publish.encode(
      1,
      SMALL_BODY,
      BASIC_PROPERTIES,
      "",
      "test.queue",
      false,
      false,
      FRAME_SIZE
    )
  end

  x.report("Publish medium (1KB) + full props") do
    AMQ::Protocol::Basic::Publish.encode(
      1,
      MEDIUM_BODY,
      BASIC_PROPERTIES,
      "",
      "test.queue",
      false,
      false,
      FRAME_SIZE
    )
  end

  x.report("Publish large (64KB) + full props") do
    AMQ::Protocol::Basic::Publish.encode(
      1,
      LARGE_BODY,
      BASIC_PROPERTIES,
      "",
      "test.queue",
      false,
      false,
      FRAME_SIZE
    )
  end

  x.compare!
end

# Create sample encoded methods for decoding benchmarks
puts
puts "=== Method Decoding ==="

# Simulate encoded Basic.Deliver frame payload (after class/method ID)
# Basic.Deliver: consumer_tag(shortstr), delivery_tag(longlong), redelivered(bit), exchange(shortstr), routing_key(shortstr)
def make_deliver_payload(consumer_tag, delivery_tag, exchange, routing_key)
  buffer = String.new(encoding: 'BINARY')
  buffer << consumer_tag.bytesize.chr
  buffer << consumer_tag
  buffer << AMQ::Pack.pack_uint64_big_endian(delivery_tag)
  buffer << "\x00"  # redelivered = false
  buffer << exchange.bytesize.chr
  buffer << exchange
  buffer << routing_key.bytesize.chr
  buffer << routing_key
  buffer
end

DELIVER_PAYLOAD_SHORT = make_deliver_payload("ctag", 1, "", "q")
DELIVER_PAYLOAD_TYPICAL = make_deliver_payload("bunny-consumer-12345", 999999, "amq.topic", "events.user.created")

Benchmark.ips do |x|
  x.config(time: 5, warmup: 2)

  x.report("Basic.Deliver.decode (short)") do
    AMQ::Protocol::Basic::Deliver.decode(DELIVER_PAYLOAD_SHORT)
  end

  x.report("Basic.Deliver.decode (typical)") do
    AMQ::Protocol::Basic::Deliver.decode(DELIVER_PAYLOAD_TYPICAL)
  end

  x.compare!
end

puts
puts "=== Properties Encoding/Decoding ==="

Benchmark.ips do |x|
  x.config(time: 5, warmup: 2)

  x.report("encode_properties (minimal)") do
    AMQ::Protocol::Basic.encode_properties(100, MINIMAL_PROPERTIES)
  end

  x.report("encode_properties (full)") do
    AMQ::Protocol::Basic.encode_properties(1024, BASIC_PROPERTIES)
  end

  x.compare!
end

# Create encoded properties for decode benchmark
ENCODED_MINIMAL_PROPS = AMQ::Protocol::Basic.encode_properties(100, MINIMAL_PROPERTIES)
ENCODED_FULL_PROPS = AMQ::Protocol::Basic.encode_properties(1024, BASIC_PROPERTIES)

# Skip the first 12 bytes (class_id, weight, body_size)
PROPS_DATA_MINIMAL = ENCODED_MINIMAL_PROPS[12..-1]
PROPS_DATA_FULL = ENCODED_FULL_PROPS[12..-1]

Benchmark.ips do |x|
  x.config(time: 5, warmup: 2)

  x.report("decode_properties (minimal)") do
    AMQ::Protocol::Basic.decode_properties(PROPS_DATA_MINIMAL)
  end

  x.report("decode_properties (full)") do
    AMQ::Protocol::Basic.decode_properties(PROPS_DATA_FULL)
  end

  x.compare!
end

puts
puts "=== Other Common Methods ==="

Benchmark.ips do |x|
  x.config(time: 5, warmup: 2)

  x.report("Basic.Ack.encode") do
    AMQ::Protocol::Basic::Ack.encode(1, 12345, false)
  end

  x.report("Basic.Nack.encode") do
    AMQ::Protocol::Basic::Nack.encode(1, 12345, false, true)
  end

  x.report("Basic.Reject.encode") do
    AMQ::Protocol::Basic::Reject.encode(1, 12345, true)
  end

  x.report("Queue.Declare.encode") do
    AMQ::Protocol::Queue::Declare.encode(1, "test.queue", false, true, false, false, false, {})
  end

  x.report("Exchange.Declare.encode") do
    AMQ::Protocol::Exchange::Declare.encode(1, "test.exchange", "topic", false, true, false, false, false, {})
  end

  x.compare!
end