Skip to content

Commit

Permalink
fix(network): add peer id to test
Browse files Browse the repository at this point in the history
  • Loading branch information
eitanm-starkware committed Nov 14, 2024
1 parent fca05b6 commit d30d447
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
17 changes: 12 additions & 5 deletions crates/papyrus_network/src/bin/network_stress_test/converters.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,37 @@
use std::mem::size_of;
use std::str::FromStr;
use std::time::{Duration, SystemTime};

pub const METADATA_SIZE: usize = size_of::<u32>() + size_of::<u64>() + size_of::<u32>();
use libp2p::PeerId;

pub const METADATA_SIZE: usize = size_of::<u32>() + size_of::<u64>() + size_of::<u32>() + 38;

#[derive(Debug, Clone)]
pub struct StressTestMessage {
pub id: u32,
pub payload: Vec<u8>,
pub time: SystemTime,
pub peer_id: String,
}

impl StressTestMessage {
pub fn new(id: u32, payload: Vec<u8>) -> Self {
StressTestMessage { id, payload, time: SystemTime::now() }
pub fn new(id: u32, payload: Vec<u8>, peer_id: String) -> Self {
StressTestMessage { id, payload, time: SystemTime::now(), peer_id }
}
}

impl From<StressTestMessage> for Vec<u8> {
fn from(value: StressTestMessage) -> Self {
let StressTestMessage { id, mut payload, time } = value;
let StressTestMessage { id, mut payload, time, peer_id } = value;
let id = id.to_be_bytes().to_vec();
let time = time.duration_since(SystemTime::UNIX_EPOCH).unwrap();
let seconds = time.as_secs().to_be_bytes().to_vec();
let nanos = time.subsec_nanos().to_be_bytes().to_vec();
let peer_id = PeerId::from_str(&peer_id).unwrap().to_bytes();
payload.extend(id);
payload.extend(seconds);
payload.extend(nanos);
payload.extend(peer_id);
payload
}
}
Expand All @@ -40,6 +46,7 @@ impl From<Vec<u8>> for StressTestMessage {
let seconds = u64::from_be_bytes(id_and_time[4..12].try_into().unwrap());
let nanos = u32::from_be_bytes(id_and_time[12..16].try_into().unwrap());
let time = SystemTime::UNIX_EPOCH + Duration::new(seconds, nanos);
StressTestMessage { id, payload: value, time }
let peer_id = PeerId::from_bytes(&id_and_time[16..]).unwrap().to_string();
StressTestMessage { id, payload: value, time, peer_id }
}
}
8 changes: 4 additions & 4 deletions crates/papyrus_network/src/bin/network_stress_test/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ async fn main() {
let mut i = 0;
tokio::time::sleep(std::time::Duration::from_secs(5)).await;
loop {
let message = StressTestMessage::new(i, vec![0; message_size - METADATA_SIZE]);
let message = StressTestMessage::new(i, vec![0; message_size - METADATA_SIZE], peer_id.clone());
network_channels.broadcast_topic_client.broadcast_message(message).await.unwrap();
i += 1;
if i == num_messages {
Expand All @@ -61,7 +61,7 @@ async fn main() {
Ok(Some((received_message, _report_callback))) => {
let received_message = received_message.unwrap();
output_vector.push(Record {
peer_id: peer_id.clone(),
peer_id: received_message.peer_id,
id: received_message.id,
start_time: received_message.time,
end_time: SystemTime::now(),
Expand All @@ -71,7 +71,8 @@ async fn main() {
.as_micros(),
});
i += 1;
if i == num_messages * 4 {
if i == num_messages * 4{
tokio::time::sleep(std::time::Duration::from_secs(90)).await;
break;
}
}
Expand All @@ -83,7 +84,6 @@ async fn main() {
for record in output_vector {
wtr.serialize(record).unwrap();
}
tokio::time::sleep(std::time::Duration::from_secs(120)).await;
}
}
}

0 comments on commit d30d447

Please sign in to comment.