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