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

Benchmark flow #1370

Closed
wants to merge 9 commits into from
Closed
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
16 changes: 16 additions & 0 deletions contrib/dev-tools/benches/run-benches.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

# This script is only intended to be used for local development or testing environments.

# Generate benches directory if it does not exist
mkdir -p ./benches

cargo bench --package torrust-tracker-torrent-repository
mv -b ./target/criterion/add_one_torrent/report/ ./benches/torrust-tracker-torrent-repository/add_one_torrent
mv -b ./target/criterion/add_multiple_torrents_in_parallel/report/ ./benches/torrust-tracker-torrent-repository/add_multiple_torrents_in_parallel
mv -b ./target/criterion/update_multiple_torrents_in_parallel/report/ ./benches/torrust-tracker-torrent-repository/update_multiple_torrents_in_parallel
mv -b ./target/criterion/update_one_torrent_in_parallel/report/ ./benches/torrust-tracker-torrent-repository/update_one_torrent_in_parallel
cargo bench --package bittorrent-http-tracker-core
mv -b target/criterion/http_tracker_handle_announce_once/ ./benches/http_tracker_handle_announce_once
cargo bench --package bittorrent-udp-tracker-core
mv -b target/criterion/udp_tracker_connect_once/ ./benches/udp_tracker_connect_once
6 changes: 6 additions & 0 deletions packages/http-tracker-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ aquatic_udp_protocol = "0"
bittorrent-http-tracker-protocol = { version = "3.0.0-develop", path = "../http-protocol" }
bittorrent-primitives = "0.1.0"
bittorrent-tracker-core = { version = "3.0.0-develop", path = "../tracker-core" }
criterion = { version = "0.5.1", features = ["async_tokio"] }
futures = "0"
thiserror = "2"
tokio = { version = "1", features = ["macros", "net", "rt-multi-thread", "signal", "sync"] }
Expand All @@ -28,3 +29,8 @@ tracing = "0"
[dev-dependencies]
mockall = "0"
torrust-tracker-test-helpers = { version = "3.0.0-develop", path = "../test-helpers" }

[[bench]]
harness = false
name = "http_tracker_core_benchmark"

3 changes: 3 additions & 0 deletions packages/http-tracker-core/benches/helpers/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod util;
pub mod sync;

38 changes: 38 additions & 0 deletions packages/http-tracker-core/benches/helpers/sync.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//use std::sync::Arc;
use std::time::{Duration, Instant};

use bittorrent_http_tracker_core::{
services::announce::AnnounceService
};
use crate::helpers::util::{initialize_core_tracker_services, sample_announce_request_for_peer, sample_peer};




#[must_use]
pub async fn return_announce_data_once(samples: u64) -> Duration {
let (core_tracker_services, core_http_tracker_services) = initialize_core_tracker_services();

let peer = sample_peer();

let (announce_request, client_ip_sources) = sample_announce_request_for_peer(peer);

let announce_service = AnnounceService::new(
core_tracker_services.core_config.clone(),
core_tracker_services.announce_handler.clone(),
core_tracker_services.authentication_service.clone(),
core_tracker_services.whitelist_authorization.clone(),
core_http_tracker_services.http_stats_event_sender.clone(),
);

let start = Instant::now();
for _ in 0..samples {
let _announce_data = announce_service
.handle_announce(&announce_request, &client_ip_sources, None)
.await
.unwrap();
}
start.elapsed()

}

133 changes: 133 additions & 0 deletions packages/http-tracker-core/benches/helpers/util.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::sync::Arc;

use aquatic_udp_protocol::{AnnounceEvent, NumberOfBytes, PeerId};
use bittorrent_http_tracker_protocol::v1::requests::announce::Announce;
use bittorrent_http_tracker_protocol::v1::services::peer_ip_resolver::ClientIpSources;
use bittorrent_tracker_core::announce_handler::AnnounceHandler;
use bittorrent_tracker_core::authentication::key::repository::in_memory::InMemoryKeyRepository;
use bittorrent_tracker_core::authentication::service::AuthenticationService;
use bittorrent_tracker_core::databases::setup::initialize_database;
use bittorrent_tracker_core::torrent::repository::in_memory::InMemoryTorrentRepository;
use bittorrent_tracker_core::torrent::repository::persisted::DatabasePersistentTorrentRepository;
use bittorrent_tracker_core::whitelist::authorization::WhitelistAuthorization;
use bittorrent_tracker_core::whitelist::repository::in_memory::InMemoryWhitelist;
use torrust_tracker_configuration::{Configuration, Core};
use torrust_tracker_primitives::peer::Peer;
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch};
use torrust_tracker_test_helpers::configuration;
use bittorrent_primitives::info_hash::InfoHash;

pub struct CoreTrackerServices {
pub core_config: Arc<Core>,
pub announce_handler: Arc<AnnounceHandler>,
pub authentication_service: Arc<AuthenticationService>,
pub whitelist_authorization: Arc<WhitelistAuthorization>,
}

pub struct CoreHttpTrackerServices {
pub http_stats_event_sender: Arc<Option<Box<dyn statistics::event::sender::Sender>>>,
}

pub fn initialize_core_tracker_services() -> (CoreTrackerServices, CoreHttpTrackerServices) {
initialize_core_tracker_services_with_config(&configuration::ephemeral_public())
}

pub fn initialize_core_tracker_services_with_config(config: &Configuration) -> (CoreTrackerServices, CoreHttpTrackerServices) {
let core_config = Arc::new(config.core.clone());
let database = initialize_database(&config.core);
let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default());
let db_torrent_repository = Arc::new(DatabasePersistentTorrentRepository::new(&database));
let in_memory_whitelist = Arc::new(InMemoryWhitelist::default());
let whitelist_authorization = Arc::new(WhitelistAuthorization::new(&config.core, &in_memory_whitelist.clone()));
let in_memory_key_repository = Arc::new(InMemoryKeyRepository::default());
let authentication_service = Arc::new(AuthenticationService::new(&core_config, &in_memory_key_repository));

let announce_handler = Arc::new(AnnounceHandler::new(
&config.core,
&whitelist_authorization,
&in_memory_torrent_repository,
&db_torrent_repository,
));

// HTTP stats
let (http_stats_event_sender, http_stats_repository) = statistics::setup::factory(config.core.tracker_usage_statistics);
let http_stats_event_sender = Arc::new(http_stats_event_sender);
let _http_stats_repository = Arc::new(http_stats_repository);

(
CoreTrackerServices {
core_config,
announce_handler,
authentication_service,
whitelist_authorization,
},
CoreHttpTrackerServices { http_stats_event_sender },
)
}
/*
pub fn sample_peer_using_ipv4() -> peer::Peer {
sample_peer()
}

pub fn sample_peer_using_ipv6() -> peer::Peer {
let mut peer = sample_peer();
peer.peer_addr = SocketAddr::new(
IpAddr::V6(Ipv6Addr::new(0x6969, 0x6969, 0x6969, 0x6969, 0x6969, 0x6969, 0x6969, 0x6969)),
8080,
);
peer
}
*/
pub fn sample_peer() -> peer::Peer {
peer::Peer {
peer_id: PeerId(*b"-qB00000000000000000"),
peer_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(126, 0, 0, 1)), 8080),
updated: DurationSinceUnixEpoch::new(1_669_397_478_934, 0),
uploaded: NumberOfBytes::new(0),
downloaded: NumberOfBytes::new(0),
left: NumberOfBytes::new(0),
event: AnnounceEvent::Started,
}
}

pub fn sample_announce_request_for_peer(peer: Peer) -> (Announce, ClientIpSources) {
let announce_request = Announce {
info_hash: sample_info_hash(),
peer_id: peer.peer_id,
port: peer.peer_addr.port(),
uploaded: Some(peer.uploaded),
downloaded: Some(peer.downloaded),
left: Some(peer.left),
event: Some(peer.event.into()),
compact: None,
numwant: None,
};

let client_ip_sources = ClientIpSources {
right_most_x_forwarded_for: None,
connection_info_ip: Some(peer.peer_addr.ip()),
};

(announce_request, client_ip_sources)
}
#[must_use]
pub fn sample_info_hash() -> InfoHash {
"3b245504cf5f11bbdbe1201cea6a6bf45aee1bc0" // DevSkim: ignore DS173237
.parse::<InfoHash>()
.expect("String should be a valid info hash")
}

use futures::future::BoxFuture;
use mockall::mock;
use tokio::sync::mpsc::error::SendError;

use bittorrent_http_tracker_core::statistics;

mock! {
HttpStatsEventSender {}
impl statistics::event::sender::Sender for HttpStatsEventSender {
fn send_event(&self, event: statistics::event::Event) -> BoxFuture<'static,Option<Result<(),SendError<statistics::event::Event> > > > ;
}
}

24 changes: 24 additions & 0 deletions packages/http-tracker-core/benches/http_tracker_core_benchmark.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use std::time::Duration;

use criterion::{criterion_group, criterion_main, Criterion};


mod helpers;
use crate::helpers::{sync, };
fn announce_once(c: &mut Criterion) {
let _rt = tokio::runtime::Builder::new_multi_thread().worker_threads(4).build().unwrap();

let mut group = c.benchmark_group("http_tracker_handle_announce_once");

group.warm_up_time(Duration::from_millis(500));
group.measurement_time(Duration::from_millis(1000));

group.bench_function("handle_announce_data", |b| {
b.iter(|| {sync::return_announce_data_once(100)});
});


}

criterion_group!(benches, announce_once);
criterion_main!(benches);
6 changes: 6 additions & 0 deletions packages/udp-tracker-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ bittorrent-udp-tracker-protocol = { version = "3.0.0-develop", path = "../udp-pr
bloom = "0.3.2"
blowfish = "0"
cipher = "0"
criterion = { version = "0.5.1", features = ["async_tokio"] }
futures = "0"
lazy_static = "1"
rand = "0"
Expand All @@ -34,3 +35,8 @@ zerocopy = "0.7"
[dev-dependencies]
mockall = "0"
torrust-tracker-test-helpers = { version = "3.0.0-develop", path = "../test-helpers" }

[[bench]]
harness = false
name = "udp_tracker_core_benchmark"

2 changes: 2 additions & 0 deletions packages/udp-tracker-core/benches/helpers/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod utils;
pub mod sync;
21 changes: 21 additions & 0 deletions packages/udp-tracker-core/benches/helpers/sync.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use std::time::{Instant, Duration};
use std::sync::Arc;
use bittorrent_udp_tracker_core::statistics;
use bittorrent_udp_tracker_core::services::connect::ConnectService;

use crate::helpers::utils::{sample_ipv4_remote_addr, sample_issue_time};
pub async fn connect_once(samples: u64) -> Duration {
let (udp_core_stats_event_sender, _udp_core_stats_repository) = statistics::setup::factory(false);
let udp_core_stats_event_sender = Arc::new(udp_core_stats_event_sender);
let connect_service = Arc::new(ConnectService::new(udp_core_stats_event_sender));
let start = Instant::now();

for _ in 0..samples {
let _response = connect_service
.handle_connect(sample_ipv4_remote_addr(), sample_issue_time())
.await;

}
start.elapsed()

}
45 changes: 45 additions & 0 deletions packages/udp-tracker-core/benches/helpers/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use std::net::{IpAddr, Ipv4Addr, SocketAddr};

use futures::future::BoxFuture;
use mockall::mock;
use tokio::sync::mpsc::error::SendError;


use bittorrent_udp_tracker_core::statistics;

pub(crate) fn sample_ipv4_remote_addr() -> SocketAddr {
sample_ipv4_socket_address()
}

/* fn sample_ipv4_remote_addr_fingerprint() -> u64 {
gen_remote_fingerprint(&sample_ipv4_socket_address())
}

fn sample_ipv6_remote_addr() -> SocketAddr {
sample_ipv6_socket_address()
}

fn sample_ipv6_remote_addr_fingerprint() -> u64 {
gen_remote_fingerprint(&sample_ipv6_socket_address())
}
*/

pub(crate) fn sample_ipv4_socket_address() -> SocketAddr {
SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080)
}

/* fn sample_ipv6_socket_address() -> SocketAddr {
SocketAddr::new(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)), 8080)
}
*/
pub(crate) fn sample_issue_time() -> f64 {
1_000_000_000_f64
}

mock! {
pub(crate) UdpCoreStatsEventSender {}
impl statistics::event::sender::Sender for UdpCoreStatsEventSender {
fn send_event(&self, event: statistics::event::Event) -> BoxFuture<'static,Option<Result<(),SendError<statistics::event::Event> > > > ;
}
}

21 changes: 21 additions & 0 deletions packages/udp-tracker-core/benches/udp_tracker_core_benchmark.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use std::time::Duration;

use criterion::{criterion_group, criterion_main, Criterion};


mod helpers;
use crate::helpers::{sync, };


fn bench_connect_once(c: &mut Criterion) {
let mut group = c.benchmark_group("udp_tracker/connect_once");
group.warm_up_time(Duration::from_millis(500));
group.measurement_time(Duration::from_millis(1000));

group.bench_function("connect_once", |b| {
b.iter(|| {sync::connect_once(100)});
});

}
criterion_group!(benches, bench_connect_once);
criterion_main!(benches);
Loading
Loading