eclipse / paho.mqtt.python

paho.mqtt.python
Other
2.19k stars 723 forks source link

python client messages per second? #48

Closed anirudh-chhangani closed 8 years ago

anirudh-chhangani commented 8 years ago

Has anyone done any benchmark for the python client, how many MPS are you guys able to achieve?

ralight commented 8 years ago

It seems as though nobody has - it might be better asking this on the mailing list.

yoch commented 8 years ago

This is very dependent on things like messages size, network latency and broker speed (and CPU, of course).

Here a very basic benchmark :

import paho.mqtt.client as mqtt
from time import time, sleep
import random

BROKER = "localhost"
NB_MESSAGES = 100000
PAYLOAD_LEN = 128
TOPIC = 'a/b/c/d'

rcpt_counter = 0

def on_disconnect(client, userdata, rc):
    elapsed = time() - T0
    print('sending', NB_MESSAGES / elapsed, 'messages per sec')

def on_message(client, userdata, msg):
    global T1, rcpt_counter
    rcpt_counter += 1
    if rcpt_counter % 1000 == 0:
        T2 = time()
        print('  receiveing', 1000 / (T2 - T1), 'messages per sec')
        T1 = T2

p = mqtt.Client()
p.on_disconnect = on_disconnect
p.connect(BROKER)
p.loop_start()

c = mqtt.Client()
c.connect(BROKER)
c.on_message = on_message
c.subscribe(TOPIC)
c.loop_start()

#prepare some random data
data = [''.join(chr(random.getrandbits(8)) for _ in range(PAYLOAD_LEN))
        for _ in range(NB_MESSAGES)]

T0 = T1 = time()

for i in range(NB_MESSAGES):
    p.publish(TOPIC, data[i])

p.disconnect()

sleep(20)
c.disconnect()