Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(network): stress test utils #1995

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions crates/papyrus_network/src/bin/network_stress_test/converters.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
use std::mem::size_of;
use std::time::{Duration, SystemTime};

struct StressTestMessage {
id: u32,
payload: Vec<u8>,
time: SystemTime,
pub const METADATA_SIZE: usize = size_of::<u32>() + size_of::<u64>() + size_of::<u32>();

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

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

impl From<StressTestMessage> for Vec<u8> {
Expand All @@ -24,7 +34,7 @@ impl From<Vec<u8>> for StressTestMessage {
// This auto implements TryFrom<Vec<u8>> for StressTestMessage
fn from(mut value: Vec<u8>) -> Self {
let vec_size = value.len();
let payload_size = vec_size - 12;
let payload_size = vec_size - METADATA_SIZE;
let id_and_time = value.split_off(payload_size);
let id = u32::from_be_bytes(id_and_time[0..4].try_into().unwrap());
let seconds = u64::from_be_bytes(id_and_time[4..12].try_into().unwrap());
Expand Down
131 changes: 131 additions & 0 deletions crates/papyrus_network/src/bin/network_stress_test/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
use std::collections::{BTreeMap, HashSet};
use std::str::FromStr;
use std::time::{SystemTime, UNIX_EPOCH};
use std::vec;

use libp2p::identity::Keypair;
use libp2p::Multiaddr;
use papyrus_config::dumping::{append_sub_config_name, ser_param, SerializeConfig};
use papyrus_config::{ParamPath, ParamPrivacyInput, SerializedParam};
use papyrus_network::NetworkConfig;
use serde::{Deserialize, Serialize, Serializer};

pub const BOOTSTRAP_CONFIG_FILE_PATH: &str =
"crates/papyrus_network/src/bin/network_stress_test/bootstrap_test_config.json";
pub const BOOTSTRAP_OUTPUT_FILE_PATH: &str =
"crates/papyrus_network/src/bin/network_stress_test/bootstrap_output.csv";
pub const DEFAULT_CONFIG_FILE_PATH: &str =
"crates/papyrus_network/src/bin/network_stress_test/test_config.json";
pub const DEFAULT_OUTPUT_FILE_PATH: &str =
"crates/papyrus_network/src/bin/network_stress_test/output.csv";

#[derive(Debug, Deserialize, Serialize)]
pub struct TestConfig {
pub network_config: NetworkConfig,
pub buffer_size: usize,
pub message_size: usize,
pub num_messages: u32,
pub output_path: String,
}

impl SerializeConfig for TestConfig {
fn dump(&self) -> BTreeMap<ParamPath, SerializedParam> {
let mut config = BTreeMap::from_iter([
ser_param(
"buffer_size",
&self.buffer_size,
"The buffer size for the network receiver.",
ParamPrivacyInput::Public,
),
ser_param(
"message_size",
&self.message_size,
"The size of the payload for the test messages.",
ParamPrivacyInput::Public,
),
ser_param(
"num_messages",
&self.num_messages,
"The amount of messages to send and receive.",
ParamPrivacyInput::Public,
),
ser_param(
"output_path",
&self.output_path,
"The path of the output file.",
ParamPrivacyInput::Public,
),
]);
config.extend(append_sub_config_name(self.network_config.dump(), "network_config"));
config
}
}

impl Default for TestConfig {
fn default() -> Self {
Self {
network_config: NetworkConfig::default(),
buffer_size: 1000,
message_size: 1000,
num_messages: 100000,
output_path: BOOTSTRAP_OUTPUT_FILE_PATH.to_string(),
}
}
}

impl TestConfig {
#[allow(dead_code)]
pub fn create_config_files() {
let secret_key = vec![0; 32];
let keypair = Keypair::ed25519_from_bytes(secret_key.clone()).unwrap();
let peer_id = keypair.public().to_peer_id();

let _ = TestConfig {
network_config: NetworkConfig {
tcp_port: 10000,
quic_port: 10001,
secret_key: Some(secret_key),
..Default::default()
},
..Default::default()
}
.dump_to_file(&vec![], &HashSet::new(), BOOTSTRAP_CONFIG_FILE_PATH);
let _ = TestConfig {
network_config: NetworkConfig {
tcp_port: 10002,
quic_port: 10003,
bootstrap_peer_multiaddr: Some(
Multiaddr::from_str(&format!("/ip4/127.0.0.1/tcp/10000/p2p/{}", peer_id))
.unwrap(),
),
..Default::default()
},
output_path: DEFAULT_OUTPUT_FILE_PATH.to_string(),
..Default::default()
}
.dump_to_file(&vec![], &HashSet::new(), DEFAULT_CONFIG_FILE_PATH);
}
}

#[derive(Debug, Deserialize, Serialize)]
pub struct Record {
pub id: u32,
#[serde(serialize_with = "serialize_system_time_as_u128_millis")]
pub start_time: SystemTime,
#[serde(serialize_with = "serialize_system_time_as_u128_millis")]
pub end_time: SystemTime,
pub duration: u128,
}

pub fn serialize_system_time_as_u128_millis<S>(
time: &SystemTime,
serializer: S,
) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let duration_since_epoch =
time.duration_since(UNIX_EPOCH).map_err(serde::ser::Error::custom)?;
let millis = duration_since_epoch.as_millis();
serializer.serialize_u128(millis)
}
2 changes: 1 addition & 1 deletion crates/papyrus_network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl SerializeConfig for NetworkConfig {
ParamPrivacyInput::Private,
)]);
config.extend(ser_optional_param(
&self.bootstrap_peer_multiaddr,
&self.advertised_multiaddr,
Multiaddr::empty(),
"advertised_multiaddr",
"The external address other peers see this node. If this is set, the node will not \
Expand Down
Loading