Skip to content

Commit

Permalink
feat: updated benchmark code and added plot script
Browse files Browse the repository at this point in the history
  • Loading branch information
BastienFaivre committed Jan 17, 2025
1 parent e8d97dd commit 496589c
Show file tree
Hide file tree
Showing 13 changed files with 275 additions and 27 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ benchmark/machines.txt
benchmark/benchmark.conf
benchmark/inventory.ini
benchmark/*.json
benchmark/output
benchmark/output/output-*
benchmark/output/*.html
benchmark/config/*
!benchmark/config/README.md
target
3 changes: 3 additions & 0 deletions benchmark/code/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,8 @@ toml = { workspace = true }
tracing = { workspace = true }
tracing-subscriber = { workspace = true, features = ["env-filter"] }

[features]
debug = []

[lints]
workspace = true
35 changes: 34 additions & 1 deletion benchmark/code/src/behaviour.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::time::Duration;

use libp2p::{
gossipsub::{self, IdentTopic},
identity::Keypair,
Expand All @@ -9,24 +11,48 @@ use crate::config::{Config, Protocol};

pub(crate) const GOSSIPSUB_TOPIC_STR: &str = "benchmark";

#[cfg(feature = "debug")]
#[derive(Debug)]
pub(crate) enum NetworkEvent {
Dog(libp2p_dog::Event),
Gossipsub(gossipsub::Event),
}

#[cfg(not(feature = "debug"))]
#[derive(Debug)]
pub(crate) enum NetworkEvent {
Dog,
Gossipsub,
}

#[cfg(feature = "debug")]
impl From<libp2p_dog::Event> for NetworkEvent {
fn from(event: libp2p_dog::Event) -> Self {
Self::Dog(event)
}
}

#[cfg(not(feature = "debug"))]
impl From<libp2p_dog::Event> for NetworkEvent {
fn from(_: libp2p_dog::Event) -> Self {
Self::Dog
}
}

#[cfg(feature = "debug")]
impl From<gossipsub::Event> for NetworkEvent {
fn from(event: gossipsub::Event) -> Self {
Self::Gossipsub(event)
}
}

#[cfg(not(feature = "debug"))]
impl From<gossipsub::Event> for NetworkEvent {
fn from(_: gossipsub::Event) -> Self {
Self::Gossipsub
}
}

#[derive(NetworkBehaviour)]
#[behaviour(to_swarm = "NetworkEvent")]
pub(crate) struct Behaviour {
Expand All @@ -40,7 +66,14 @@ impl Behaviour {
Toggle::from(Some(
libp2p_dog::Behaviour::new_with_metrics(
libp2p_dog::TransactionAuthenticity::Signed(key.clone()),
libp2p_dog::Config::default(),
libp2p_dog::ConfigBuilder::default()
.target_redundancy(config.benchmark.redundancy)
.redundancy_delta_percent(config.benchmark.redundancy_delta)
.redundancy_interval(Duration::from_millis(
config.benchmark.redundancy_interval_in_ms,
))
.build()
.expect("Failed to build dog config"),
registry,
)
.expect("Failed to create dog behaviour"),
Expand Down
3 changes: 3 additions & 0 deletions benchmark/code/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ pub(crate) struct Benchmark {
pub tx_size_in_bytes: usize,
pub dump_interval_in_ms: u64,
pub registry_prefix: String,
pub redundancy: f64,
pub redundancy_delta: u8,
pub redundancy_interval_in_ms: u64,
}

impl Config {
Expand Down
21 changes: 7 additions & 14 deletions benchmark/code/src/handler.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,25 @@
#[cfg(feature = "debug")]
use libp2p::{
gossipsub,
swarm::{self, SwarmEvent},
};

#[cfg(feature = "debug")]
use crate::{
behaviour::{self, NetworkEvent},
config::Config,
};

#[cfg(feature = "debug")]
async fn handle_dog_event(
event: libp2p_dog::Event,
_swarm: &mut swarm::Swarm<behaviour::Behaviour>,
_config: &Config,
) {
match event {
libp2p_dog::Event::Transaction { transaction, .. } => {
tracing::info!(
"Received transaction: {}",
match String::from_utf8(transaction.data.clone()) {
Ok(data) => data,
Err(_) => "Invalid UTF-8".to_string(),
}
);
}
libp2p_dog::Event::RoutingUpdated { disabled_routes } => {
tracing::info!("Updated routing table: {:?}", disabled_routes);
}
}
tracing::info!("Dog event: {:?}", event);
}

#[cfg(feature = "debug")]
async fn handle_gossipsub_event(
event: gossipsub::Event,
_swarm: &mut swarm::Swarm<behaviour::Behaviour>,
Expand All @@ -37,6 +28,7 @@ async fn handle_gossipsub_event(
tracing::info!("Gossipsub event: {:?}", event);
}

#[cfg(feature = "debug")]
async fn handle_swarm_specific_event(
event: SwarmEvent<behaviour::NetworkEvent>,
_swarm: &mut swarm::Swarm<behaviour::Behaviour>,
Expand All @@ -45,6 +37,7 @@ async fn handle_swarm_specific_event(
tracing::info!("Swarm event: {:?}", event);
}

#[cfg(feature = "debug")]
pub(crate) async fn handle_swarm_event(
event: SwarmEvent<behaviour::NetworkEvent>,
swarm: &mut swarm::Swarm<behaviour::Behaviour>,
Expand Down
2 changes: 1 addition & 1 deletion benchmark/code/src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use tracing_subscriber::fmt;

pub(crate) fn init() {
let filter = EnvFilter::builder()
.with_default_directive(LevelFilter::DEBUG.into())
.with_default_directive(LevelFilter::INFO.into())
.from_env_lossy();

fmt().with_env_filter(filter).init();
Expand Down
22 changes: 16 additions & 6 deletions benchmark/code/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ async fn main() -> Result<(), Box<dyn Error>> {

swarm.listen_on(config.node.addr.clone())?;

time::sleep(Duration::from_secs(5)).await; // Make sure all other nodes are ready

for peer_addr in &config.node.peers {
swarm.dial(
DialOpts::unknown_peer_id()
Expand Down Expand Up @@ -73,8 +75,12 @@ async fn main() -> Result<(), Box<dyn Error>> {

loop {
select! {
event = swarm.select_next_some() => {
handler::handle_swarm_event(event, &mut swarm, &config).await;
_event = swarm.select_next_some() => {
// We do not process event when benchmarking to avoid unnecessary overhead
#[cfg(feature = "debug")]
{
handler::handle_swarm_event(_event, &mut swarm, &config).await;
}
}

_ = time::sleep_until(start_instant), if start_instant > time::Instant::now() => {
Expand All @@ -84,7 +90,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
}

_ = &mut transaction_timer => {
tracing::info!("Sending a transaction");
tracing::debug!("Sending a transaction");
match config.benchmark.protocol {
config::Protocol::Dog => {
match swarm.behaviour_mut()
Expand All @@ -93,7 +99,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
.expect("Dog behaviour should be enabled")
.publish(vec![0 as u8; config.benchmark.tx_size_in_bytes] as Vec<u8>) {
Ok(tx_id) => {
tracing::info!("Transaction sent with id {}", tx_id);
tracing::debug!("Transaction sent with id {}", tx_id);
}
Err(e) => {
tracing::error!("Failed to send transaction: {:?}", e);
Expand All @@ -107,7 +113,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
.expect("Gossipsub behaviour should be enabled")
.publish(gossipsub_topic.clone(), vec![0 as u8; config.benchmark.tx_size_in_bytes] as Vec<u8>) {
Ok(msg_id) => {
tracing::info!("Message sent with id {}", msg_id);
tracing::debug!("Message sent with id {}", msg_id);
}
Err(e) => {
tracing::error!("Failed to send message: {:?}", e);
Expand All @@ -120,7 +126,10 @@ async fn main() -> Result<(), Box<dyn Error>> {
}

_ = &mut dump_timer => {
tracing::info!("Dumping metrics");
#[cfg(feature = "debug")]
{
tracing::info!("Dumping metrics");
}

if let Err(e) = dump_metrics(&mut writer, &registry) {
tracing::error!("Failed to dump metrics: {:?}", e);
Expand Down Expand Up @@ -171,6 +180,7 @@ fn dump_metrics(mut writer: impl Write, registry: &Registry) -> Result<(), Box<d
let metrics = serde_json::Value::Object(metrics);
serde_json::to_writer(&mut writer, &metrics)?;
writer.write_all(b",")?;
writer.flush()?;
}
Err(e) => Err(e)?,
}
Expand Down
Loading

0 comments on commit 496589c

Please sign in to comment.