2020-04-02 18:02:49 -07:00
|
|
|
#![cfg(target_os = "linux")]
|
|
|
|
|
2021-12-03 09:00:31 -08:00
|
|
|
use {
|
|
|
|
solana_streamer::{
|
2022-01-04 06:37:44 -08:00
|
|
|
packet::{Meta, Packet, PACKET_DATA_SIZE},
|
2021-12-03 09:00:31 -08:00
|
|
|
recvmmsg::*,
|
|
|
|
},
|
|
|
|
std::{net::UdpSocket, time::Instant},
|
|
|
|
};
|
2020-03-24 11:39:36 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn test_recv_mmsg_batch_size() {
|
|
|
|
let reader = UdpSocket::bind("127.0.0.1:0").expect("bind");
|
|
|
|
let addr = reader.local_addr().unwrap();
|
|
|
|
let sender = UdpSocket::bind("127.0.0.1:0").expect("bind");
|
|
|
|
|
|
|
|
const TEST_BATCH_SIZE: usize = 64;
|
|
|
|
let sent = TEST_BATCH_SIZE;
|
|
|
|
|
|
|
|
let mut elapsed_in_max_batch = 0;
|
2022-05-19 09:54:39 -07:00
|
|
|
let mut num_max_batches = 0;
|
2020-03-24 11:39:36 -07:00
|
|
|
(0..1000).for_each(|_| {
|
|
|
|
for _ in 0..sent {
|
|
|
|
let data = [0; PACKET_DATA_SIZE];
|
2022-09-22 15:23:03 -07:00
|
|
|
sender.send_to(&data[..], addr).unwrap();
|
2020-03-24 11:39:36 -07:00
|
|
|
}
|
|
|
|
let mut packets = vec![Packet::default(); TEST_BATCH_SIZE];
|
|
|
|
let now = Instant::now();
|
2022-01-04 06:08:15 -08:00
|
|
|
let recv = recv_mmsg(&reader, &mut packets[..]).unwrap();
|
2020-03-24 11:39:36 -07:00
|
|
|
elapsed_in_max_batch += now.elapsed().as_nanos();
|
2022-05-19 09:54:39 -07:00
|
|
|
if recv == TEST_BATCH_SIZE {
|
|
|
|
num_max_batches += 1;
|
|
|
|
}
|
2020-03-24 11:39:36 -07:00
|
|
|
});
|
2022-05-19 09:54:39 -07:00
|
|
|
assert!(num_max_batches > 990);
|
2020-03-24 11:39:36 -07:00
|
|
|
|
|
|
|
let mut elapsed_in_small_batch = 0;
|
|
|
|
(0..1000).for_each(|_| {
|
|
|
|
for _ in 0..sent {
|
|
|
|
let data = [0; PACKET_DATA_SIZE];
|
2022-09-22 15:23:03 -07:00
|
|
|
sender.send_to(&data[..], addr).unwrap();
|
2020-03-24 11:39:36 -07:00
|
|
|
}
|
|
|
|
let mut packets = vec![Packet::default(); 4];
|
|
|
|
let mut recv = 0;
|
|
|
|
let now = Instant::now();
|
|
|
|
while let Ok(num) = recv_mmsg(&reader, &mut packets[..]) {
|
2022-01-04 06:08:15 -08:00
|
|
|
recv += num;
|
2020-03-24 11:39:36 -07:00
|
|
|
if recv >= TEST_BATCH_SIZE {
|
|
|
|
break;
|
|
|
|
}
|
2022-01-04 06:37:44 -08:00
|
|
|
packets
|
|
|
|
.iter_mut()
|
2022-12-06 03:54:49 -08:00
|
|
|
.for_each(|pkt| *pkt.meta_mut() = Meta::default());
|
2020-03-24 11:39:36 -07:00
|
|
|
}
|
|
|
|
elapsed_in_small_batch += now.elapsed().as_nanos();
|
|
|
|
assert_eq!(TEST_BATCH_SIZE, recv);
|
|
|
|
});
|
|
|
|
|
|
|
|
assert!(elapsed_in_max_batch <= elapsed_in_small_batch);
|
|
|
|
}
|