ruby-mqtt
ruby-mqtt copied to clipboard
ruby-mqtt 2x slower than paho-mqtt
Hi there,
I'm comparing different ruby mqtt clients, I have this code:
client = MQTT::Client.new(username: 'testuser', password: 'testpasswd', client_id: "client_#{rand(1..1_000_000)}", host: '127.0.0.1', )
client.connect()
count = 0
loop do
count += 1
i = rand(1..10)
topic = "to/timebox#{i}/cameras"
msg = "message #{count} #{Time.now} for timebox#{i}"
client.publish(topic, msg, true, 1)
end
Here is a version using paho-mqtt
$client = PahoMqtt::Client.new(username: 'testuser', password: 'testpasswd', client_id: "client_#{rand(1..1_000_000)}")
$client.connect('127.0.0.1', 1883)
$count = 0
def send_message
$count += 1
i = rand(1..$total)
topic = "to/timebox#{i}/cameras"
msg = "message #{$count} #{Time.now} for timebox#{i}"
$client.publish(topic, msg, true, 1)
end
$client.on_puback { send_message } # QOS1
send_message
loop { sleep 1 } # to keep process running
I used pv to calculate how many messages are being received per second and I'm getting:
- ruby-mqtt: 92 / sec
- paho-mqtt: 180 /sec
I prefer the syntax of ruby-mqtt and it appears to be better maintained, but any ideas why ruby-mqtt is nearly 2x slower?
No sorry, I am not certain why there is such a different in publish speed - it must have something to do with ruby-mqtt being synchronous/using threads. paho-mqtt borrowed the packet parsing code from ruby-mqtt, so it is unlikely to be that.
@rgaufman please can you test to see if @tenderlove's improvements fix your performance problems?