Skip to content

Commit

Permalink
feat: complete metrics + benchmark code
Browse files Browse the repository at this point in the history
  • Loading branch information
BastienFaivre committed Jan 14, 2025
1 parent 845456e commit ceb2f0a
Show file tree
Hide file tree
Showing 15 changed files with 687 additions and 47 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
benchmark/*.json
benchmark/config/*
!benchmark/config/README.md
target
147 changes: 145 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,24 @@ libp2p-dog = { version = "0.1.0", path = "dog" }
async-channel = "2.3.1"
asynchronous-codec = "0.7.0"
bytes = "1.6"
clap = "4.5.16"
either = "1.13.0"
futures = "0.3.30"
futures-timer = "3.0.3"
hex_fmt = "0.3.0"
libp2p = "0.54.1"
lru = "0.12.5"
prometheus-client = "0.23.0"
prometheus-client = "0.22.3"
quick-protobuf = "0.8.1"
quick-protobuf-codec = "0.3.1"
rand = "0.8"
serde = "1"
serde_json = "1.0.135"
thiserror = "2"
tokio = "1.42.0"
toml = "0.8.19"
tracing = "0.1.37"
tracing-subscriber = "0.3.18"
void = "1.0.2"

[workspace.lints]
Expand Down
10 changes: 7 additions & 3 deletions benchmark/code/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ license = "Apache-2.0"
release = false

[dependencies]
libp2p = { workspace = true, features = ["noise", "tcp", "yamux", "tokio", "macros"] }
clap = { workspace = true, features = ["derive"] }
libp2p = { workspace = true, features = ["noise", "tcp", "yamux", "tokio", "macros", "gossipsub"] }
libp2p-dog = { path = "../../dog" }
prometheus-client = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
tokio = { workspace = true, features = ["full"] }
clap = { version = "4.5.16", features = ["derive"] }
toml = { workspace = true }
tracing = { workspace = true }
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
tracing-subscriber = { workspace = true, features = ["env-filter"] }

[lints]
workspace = true
8 changes: 4 additions & 4 deletions benchmark/code/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use clap::Parser;

#[derive(Parser, Debug)]
pub(crate) struct Args {
#[arg(short, long, default_value_t = 0)]
pub port: u16,
#[arg(short, long)]
pub dir: String,

#[arg(long, value_delimiter = ',', default_value = "")]
pub peers: Vec<String>,
#[arg(short, long)]
pub config: String,
}

impl Args {
Expand Down
73 changes: 73 additions & 0 deletions benchmark/code/src/behaviour.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use libp2p::{
gossipsub::{self, IdentTopic},
identity::Keypair,
swarm::{behaviour::toggle::Toggle, NetworkBehaviour},
};
use prometheus_client::registry::Registry;

use crate::config::{Config, Protocol};

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

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

impl From<libp2p_dog::Event> for NetworkEvent {
fn from(event: libp2p_dog::Event) -> Self {
Self::Dog(event)
}
}

impl From<gossipsub::Event> for NetworkEvent {
fn from(event: gossipsub::Event) -> Self {
Self::Gossipsub(event)
}
}

#[derive(NetworkBehaviour)]
#[behaviour(to_swarm = "NetworkEvent")]
pub(crate) struct Behaviour {
pub dog: Toggle<libp2p_dog::Behaviour>,
pub gossipsub: Toggle<gossipsub::Behaviour>,
}

impl Behaviour {
pub(crate) fn new(config: &Config, key: &Keypair, registry: &mut Registry) -> Self {
let dog = if let Protocol::Dog = config.benchmark.protocol {
Toggle::from(Some(
libp2p_dog::Behaviour::new_with_metrics(
libp2p_dog::TransactionAuthenticity::Signed(key.clone()),
libp2p_dog::Config::default(),
registry,
)
.expect("Failed to create dog behaviour"),
))
} else {
Toggle::from(None)
};

let gossipsub = if let Protocol::Gossipsub = config.benchmark.protocol {
Toggle::from({
let mut behaviour = gossipsub::Behaviour::new_with_metrics(
gossipsub::MessageAuthenticity::Signed(key.clone()),
gossipsub::Config::default(),
registry,
Default::default(),
)
.expect("Failed to create gossipsub behaviour");
let topic = IdentTopic::new(GOSSIPSUB_TOPIC_STR);
behaviour
.subscribe(&topic)
.expect("Failed to subscribe to gossipsub topic");
Some(behaviour)
})
} else {
Toggle::from(None)
};

Self { dog, gossipsub }
}
}
Loading

0 comments on commit ceb2f0a

Please sign in to comment.