forked from bytebeamio/rumqtt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rumqttsync.rs
66 lines (54 loc) · 1.97 KB
/
rumqttsync.rs
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
use rumqttc::{Client, Event, Incoming, MqttOptions, QoS};
use std::error::Error;
use std::thread;
use std::time::{Duration, Instant};
mod common;
#[cfg(not(target_env = "msvc"))]
#[global_allocator]
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;
fn main() {
pretty_env_logger::init();
let guard = pprof::ProfilerGuard::new(100).unwrap();
start("rumqtt-sync", 100, 1_000_000).unwrap();
common::profile("bench.pb", guard);
}
pub fn start(id: &str, payload_size: usize, count: usize) -> Result<(), Box<dyn Error>> {
let mut mqttoptions = MqttOptions::new(id, "localhost", 1883);
mqttoptions.set_keep_alive(Duration::from_secs(20));
mqttoptions.set_inflight(100);
let (mut client, mut connection) = Client::new(mqttoptions, 10);
thread::spawn(move || {
for _i in 0..count {
let payload = vec![0; payload_size];
let qos = QoS::AtLeastOnce;
let topic = "hello/benchmarks/world";
client.publish(topic, qos, false, payload).unwrap();
}
thread::sleep(Duration::from_secs(1));
});
let mut acks_count = 0;
let start = Instant::now();
// flatten isn't shorter because the if let is needed anyways, and
// flattening a result is not an entirely obvious operation either.
#[allow(clippy::manual_flatten)]
for event in connection.iter() {
if let Ok(Event::Incoming(Incoming::PubAck(_))) = event {
acks_count += 1;
if acks_count == count {
break;
}
}
}
let elapsed_micros = start.elapsed().as_micros();
let throughput = (acks_count * 1_000_000) / elapsed_micros as usize;
// --------------------------- results ---------------------------------------
let print = common::Print {
id: id.to_owned(),
messages: count,
payload_size,
throughput,
};
println!("{}", serde_json::to_string_pretty(&print).unwrap());
println!("@");
Ok(())
}