#!/usr/bin/env node /** MQTT QoS0 publish rate against nats-server MQTT gateway. */ import mqtt from "mqtt"; const url = process.argv[2] || "mqtt://10.10.10.21:1883"; const count = Number(process.argv[3] || 5000); const size = Number(process.argv[4] || 128); const payload = Buffer.alloc(size, 9); const topic = `bench/mqtt/${process.pid}`; const c = mqtt.connect(url, { reconnectPeriod: 0, connectTimeout: 5000 }); await new Promise((res, rej) => { c.on("connect", res); c.on("error", rej); }); const t0 = process.hrtime.bigint(); for (let i = 0; i < count; i++) { await new Promise((res, rej) => c.publish(topic, payload, { qos: 0 }, (err) => (err ? rej(err) : res()))); } const ns = Number(process.hrtime.bigint() - t0); c.end(true); const sec = ns / 1e9; console.log( JSON.stringify({ mode: "mqtt-qos0", count, size, url, secs: Number(sec.toFixed(3)), pubs_per_sec: Math.round(count / sec), mb_per_sec: Number(((count * size) / sec / 1e6).toFixed(2)), }), );