Skip to main content

sequential_recv/
sequential-recv.rs

1//! send/recv 10,000,000 messages in sequence:
2//!
3//! ~25ns per send, ~35ns per recv
4
5extern crate unbounded_spsc;
6
7const MESSAGE_COUNT : u64 = 10_000_000;
8
9#[derive(Debug,PartialEq)]
10struct Mystruct {
11  x : f64,
12  y : f64,
13  z : f64
14}
15
16fn sendfun (sender : unbounded_spsc::Sender <Mystruct>) {
17  let mut counter = 0;
18  let start_time = std::time::SystemTime::now();
19  while counter < MESSAGE_COUNT {
20    sender.send (Mystruct { x: counter as f64, y: 1.5, z: 2.0 }).unwrap();
21    counter += 1;
22  }
23  let duration = start_time.elapsed().unwrap();
24  let duration_ns
25    = (duration.as_secs() * 1_000_000_000) + duration.subsec_nanos() as u64;
26  println!("sendfun duration ns: {duration_ns}");
27  println!("sendfun ns per message: {}", duration_ns / MESSAGE_COUNT);
28}
29
30fn recvfun (receiver : unbounded_spsc::Receiver <Mystruct>) {
31  let start_time = std::time::SystemTime::now();
32  while let Ok (_m) = receiver.recv() { }
33  let duration = start_time.elapsed().unwrap();
34  let duration_ns = (duration.as_secs() * 1_000_000_000) + duration.subsec_nanos()
35    as u64;
36  println!("recvfun duration ns: {duration_ns}");
37  println!("recvfun ns per message: {}", duration_ns / MESSAGE_COUNT);
38  println!("buffer ending capacity: {}", receiver.capacity());
39}
40
41fn main() {
42  println!("main...");
43  let (sender, receiver) = unbounded_spsc::channel();
44  let join_sender = std::thread::spawn (move || sendfun (sender));
45  join_sender.join().unwrap();
46  let join_receiver = std::thread::spawn (move || recvfun (receiver));
47  join_receiver.join().unwrap();
48  println!("...main");
49}