Skip to content

Commit c155732

Browse files
nuts-ricejosecelano
authored andcommitted
chore: barebones benchmarks for UDP and HTTP core packages
- http + udp tracker core bench mods - bench sh script
1 parent 13e9aa8 commit c155732

File tree

13 files changed

+515
-113
lines changed

13 files changed

+515
-113
lines changed

Cargo.lock

+133-107
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash
2+
3+
# This script is only intended to be used for local development or testing environments.
4+
5+
cargo bench --package torrust-tracker-torrent-repository
6+
7+
cargo bench --package bittorrent-http-tracker-core
8+
9+
cargo bench --package bittorrent-udp-tracker-core

packages/http-tracker-core/Cargo.toml

+6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ aquatic_udp_protocol = "0"
1818
bittorrent-http-tracker-protocol = { version = "3.0.0-develop", path = "../http-protocol" }
1919
bittorrent-primitives = "0.1.0"
2020
bittorrent-tracker-core = { version = "3.0.0-develop", path = "../tracker-core" }
21+
criterion = { version = "0.5.1", features = ["async_tokio"] }
2122
futures = "0"
2223
thiserror = "2"
2324
tokio = { version = "1", features = ["macros", "net", "rt-multi-thread", "signal", "sync"] }
@@ -28,3 +29,8 @@ tracing = "0"
2829
[dev-dependencies]
2930
mockall = "0"
3031
torrust-tracker-test-helpers = { version = "3.0.0-develop", path = "../test-helpers" }
32+
33+
[[bench]]
34+
harness = false
35+
name = "http_tracker_core_benchmark"
36+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod sync;
2+
pub mod util;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use std::time::{Duration, Instant};
2+
3+
use bittorrent_http_tracker_core::services::announce::AnnounceService;
4+
5+
use crate::helpers::util::{initialize_core_tracker_services, sample_announce_request_for_peer, sample_peer};
6+
7+
#[must_use]
8+
pub async fn return_announce_data_once(samples: u64) -> Duration {
9+
let (core_tracker_services, core_http_tracker_services) = initialize_core_tracker_services();
10+
11+
let peer = sample_peer();
12+
13+
let (announce_request, client_ip_sources) = sample_announce_request_for_peer(peer);
14+
15+
let announce_service = AnnounceService::new(
16+
core_tracker_services.core_config.clone(),
17+
core_tracker_services.announce_handler.clone(),
18+
core_tracker_services.authentication_service.clone(),
19+
core_tracker_services.whitelist_authorization.clone(),
20+
core_http_tracker_services.http_stats_event_sender.clone(),
21+
);
22+
23+
let start = Instant::now();
24+
for _ in 0..samples {
25+
let _announce_data = announce_service
26+
.handle_announce(&announce_request, &client_ip_sources, None)
27+
.await
28+
.unwrap();
29+
}
30+
start.elapsed()
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
2+
use std::sync::Arc;
3+
4+
use aquatic_udp_protocol::{AnnounceEvent, NumberOfBytes, PeerId};
5+
use bittorrent_http_tracker_protocol::v1::requests::announce::Announce;
6+
use bittorrent_http_tracker_protocol::v1::services::peer_ip_resolver::ClientIpSources;
7+
use bittorrent_primitives::info_hash::InfoHash;
8+
use bittorrent_tracker_core::announce_handler::AnnounceHandler;
9+
use bittorrent_tracker_core::authentication::key::repository::in_memory::InMemoryKeyRepository;
10+
use bittorrent_tracker_core::authentication::service::AuthenticationService;
11+
use bittorrent_tracker_core::databases::setup::initialize_database;
12+
use bittorrent_tracker_core::torrent::repository::in_memory::InMemoryTorrentRepository;
13+
use bittorrent_tracker_core::torrent::repository::persisted::DatabasePersistentTorrentRepository;
14+
use bittorrent_tracker_core::whitelist::authorization::WhitelistAuthorization;
15+
use bittorrent_tracker_core::whitelist::repository::in_memory::InMemoryWhitelist;
16+
use torrust_tracker_configuration::{Configuration, Core};
17+
use torrust_tracker_primitives::peer::Peer;
18+
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch};
19+
use torrust_tracker_test_helpers::configuration;
20+
21+
pub struct CoreTrackerServices {
22+
pub core_config: Arc<Core>,
23+
pub announce_handler: Arc<AnnounceHandler>,
24+
pub authentication_service: Arc<AuthenticationService>,
25+
pub whitelist_authorization: Arc<WhitelistAuthorization>,
26+
}
27+
28+
pub struct CoreHttpTrackerServices {
29+
pub http_stats_event_sender: Arc<Option<Box<dyn statistics::event::sender::Sender>>>,
30+
}
31+
32+
pub fn initialize_core_tracker_services() -> (CoreTrackerServices, CoreHttpTrackerServices) {
33+
initialize_core_tracker_services_with_config(&configuration::ephemeral_public())
34+
}
35+
36+
pub fn initialize_core_tracker_services_with_config(config: &Configuration) -> (CoreTrackerServices, CoreHttpTrackerServices) {
37+
let core_config = Arc::new(config.core.clone());
38+
let database = initialize_database(&config.core);
39+
let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default());
40+
let db_torrent_repository = Arc::new(DatabasePersistentTorrentRepository::new(&database));
41+
let in_memory_whitelist = Arc::new(InMemoryWhitelist::default());
42+
let whitelist_authorization = Arc::new(WhitelistAuthorization::new(&config.core, &in_memory_whitelist.clone()));
43+
let in_memory_key_repository = Arc::new(InMemoryKeyRepository::default());
44+
let authentication_service = Arc::new(AuthenticationService::new(&core_config, &in_memory_key_repository));
45+
46+
let announce_handler = Arc::new(AnnounceHandler::new(
47+
&config.core,
48+
&whitelist_authorization,
49+
&in_memory_torrent_repository,
50+
&db_torrent_repository,
51+
));
52+
53+
// HTTP stats
54+
let (http_stats_event_sender, http_stats_repository) = statistics::setup::factory(config.core.tracker_usage_statistics);
55+
let http_stats_event_sender = Arc::new(http_stats_event_sender);
56+
let _http_stats_repository = Arc::new(http_stats_repository);
57+
58+
(
59+
CoreTrackerServices {
60+
core_config,
61+
announce_handler,
62+
authentication_service,
63+
whitelist_authorization,
64+
},
65+
CoreHttpTrackerServices { http_stats_event_sender },
66+
)
67+
}
68+
69+
pub fn sample_peer() -> peer::Peer {
70+
peer::Peer {
71+
peer_id: PeerId(*b"-qB00000000000000000"),
72+
peer_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(126, 0, 0, 1)), 8080),
73+
updated: DurationSinceUnixEpoch::new(1_669_397_478_934, 0),
74+
uploaded: NumberOfBytes::new(0),
75+
downloaded: NumberOfBytes::new(0),
76+
left: NumberOfBytes::new(0),
77+
event: AnnounceEvent::Started,
78+
}
79+
}
80+
81+
pub fn sample_announce_request_for_peer(peer: Peer) -> (Announce, ClientIpSources) {
82+
let announce_request = Announce {
83+
info_hash: sample_info_hash(),
84+
peer_id: peer.peer_id,
85+
port: peer.peer_addr.port(),
86+
uploaded: Some(peer.uploaded),
87+
downloaded: Some(peer.downloaded),
88+
left: Some(peer.left),
89+
event: Some(peer.event.into()),
90+
compact: None,
91+
numwant: None,
92+
};
93+
94+
let client_ip_sources = ClientIpSources {
95+
right_most_x_forwarded_for: None,
96+
connection_info_ip: Some(peer.peer_addr.ip()),
97+
};
98+
99+
(announce_request, client_ip_sources)
100+
}
101+
#[must_use]
102+
pub fn sample_info_hash() -> InfoHash {
103+
"3b245504cf5f11bbdbe1201cea6a6bf45aee1bc0" // DevSkim: ignore DS173237
104+
.parse::<InfoHash>()
105+
.expect("String should be a valid info hash")
106+
}
107+
108+
use bittorrent_http_tracker_core::statistics;
109+
use futures::future::BoxFuture;
110+
use mockall::mock;
111+
use tokio::sync::mpsc::error::SendError;
112+
113+
mock! {
114+
HttpStatsEventSender {}
115+
impl statistics::event::sender::Sender for HttpStatsEventSender {
116+
fn send_event(&self, event: statistics::event::Event) -> BoxFuture<'static,Option<Result<(),SendError<statistics::event::Event> > > > ;
117+
}
118+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
mod helpers;
2+
3+
use std::time::Duration;
4+
5+
use criterion::{criterion_group, criterion_main, Criterion};
6+
7+
use crate::helpers::sync;
8+
9+
fn announce_once(c: &mut Criterion) {
10+
let _rt = tokio::runtime::Builder::new_multi_thread().worker_threads(4).build().unwrap();
11+
12+
let mut group = c.benchmark_group("http_tracker_handle_announce_once");
13+
14+
group.warm_up_time(Duration::from_millis(500));
15+
group.measurement_time(Duration::from_millis(1000));
16+
17+
group.bench_function("handle_announce_data", |b| {
18+
b.iter(|| sync::return_announce_data_once(100));
19+
});
20+
}
21+
22+
criterion_group!(benches, announce_once);
23+
criterion_main!(benches);

packages/udp-tracker-core/Cargo.toml

+6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ bittorrent-udp-tracker-protocol = { version = "3.0.0-develop", path = "../udp-pr
2121
bloom = "0.3.2"
2222
blowfish = "0"
2323
cipher = "0"
24+
criterion = { version = "0.5.1", features = ["async_tokio"] }
2425
futures = "0"
2526
lazy_static = "1"
2627
rand = "0"
@@ -34,3 +35,8 @@ zerocopy = "0.7"
3435
[dev-dependencies]
3536
mockall = "0"
3637
torrust-tracker-test-helpers = { version = "3.0.0-develop", path = "../test-helpers" }
38+
39+
[[bench]]
40+
harness = false
41+
name = "udp_tracker_core_benchmark"
42+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod sync;
2+
mod utils;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use std::sync::Arc;
2+
use std::time::{Duration, Instant};
3+
4+
use bittorrent_udp_tracker_core::services::connect::ConnectService;
5+
use bittorrent_udp_tracker_core::statistics;
6+
7+
use crate::helpers::utils::{sample_ipv4_remote_addr, sample_issue_time};
8+
9+
#[allow(clippy::unused_async)]
10+
pub async fn connect_once(samples: u64) -> Duration {
11+
let (udp_core_stats_event_sender, _udp_core_stats_repository) = statistics::setup::factory(false);
12+
let udp_core_stats_event_sender = Arc::new(udp_core_stats_event_sender);
13+
let connect_service = Arc::new(ConnectService::new(udp_core_stats_event_sender));
14+
let start = Instant::now();
15+
16+
for _ in 0..samples {
17+
let _response = connect_service.handle_connect(sample_ipv4_remote_addr(), sample_issue_time());
18+
}
19+
20+
start.elapsed()
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
2+
3+
use bittorrent_udp_tracker_core::statistics;
4+
use futures::future::BoxFuture;
5+
use mockall::mock;
6+
use tokio::sync::mpsc::error::SendError;
7+
8+
pub(crate) fn sample_ipv4_remote_addr() -> SocketAddr {
9+
sample_ipv4_socket_address()
10+
}
11+
12+
pub(crate) fn sample_ipv4_socket_address() -> SocketAddr {
13+
SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080)
14+
}
15+
16+
pub(crate) fn sample_issue_time() -> f64 {
17+
1_000_000_000_f64
18+
}
19+
20+
mock! {
21+
pub(crate) UdpCoreStatsEventSender {}
22+
impl statistics::event::sender::Sender for UdpCoreStatsEventSender {
23+
fn send_event(&self, event: statistics::event::Event) -> BoxFuture<'static,Option<Result<(),SendError<statistics::event::Event> > > > ;
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
mod helpers;
2+
3+
use std::time::Duration;
4+
5+
use criterion::{criterion_group, criterion_main, Criterion};
6+
7+
use crate::helpers::sync;
8+
9+
fn bench_connect_once(c: &mut Criterion) {
10+
let mut group = c.benchmark_group("udp_tracker/connect_once");
11+
group.warm_up_time(Duration::from_millis(500));
12+
group.measurement_time(Duration::from_millis(1000));
13+
14+
group.bench_function("connect_once", |b| {
15+
b.iter(|| sync::connect_once(100));
16+
});
17+
}
18+
19+
criterion_group!(benches, bench_connect_once);
20+
criterion_main!(benches);

0 commit comments

Comments
 (0)