Skip to content

Commit 48ce426

Browse files
committed
dev: bench torrent/repository add sync_asyn variant
1 parent 5c0047a commit 48ce426

File tree

9 files changed

+335
-61
lines changed

9 files changed

+335
-61
lines changed

packages/torrent-repository-benchmarks/src/benches/asyn.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ use std::time::Duration;
44
use clap::Parser;
55
use futures::stream::FuturesUnordered;
66
use torrust_tracker::core::torrent::repository_asyn::{RepositoryAsync, RepositoryTokioRwLock};
7+
use torrust_tracker::core::torrent::UpdateTorrentAsync;
78
use torrust_tracker::shared::bit_torrent::info_hash::InfoHash;
89

910
use crate::args::Args;
1011
use crate::benches::utils::{generate_unique_info_hashes, get_average_and_adjusted_average_from_results, DEFAULT_PEER};
1112

12-
pub async fn async_add_one_torrent<T>(samples: usize) -> (Duration, Duration)
13+
pub async fn add_one_torrent<T>(samples: usize) -> (Duration, Duration)
1314
where
14-
RepositoryTokioRwLock<T>: RepositoryAsync<T>,
15+
RepositoryTokioRwLock<T>: RepositoryAsync<T> + UpdateTorrentAsync,
1516
{
1617
let mut results: Vec<Duration> = Vec::with_capacity(samples);
1718

@@ -35,10 +36,10 @@ where
3536
}
3637

3738
// Add one torrent ten thousand times in parallel (depending on the set worker threads)
38-
pub async fn async_update_one_torrent_in_parallel<T>(runtime: &tokio::runtime::Runtime, samples: usize) -> (Duration, Duration)
39+
pub async fn update_one_torrent_in_parallel<T>(runtime: &tokio::runtime::Runtime, samples: usize) -> (Duration, Duration)
3940
where
4041
T: Send + Sync + 'static,
41-
RepositoryTokioRwLock<T>: RepositoryAsync<T>,
42+
RepositoryTokioRwLock<T>: RepositoryAsync<T> + UpdateTorrentAsync,
4243
{
4344
let args = Args::parse();
4445
let mut results: Vec<Duration> = Vec::with_capacity(samples);
@@ -85,10 +86,10 @@ where
8586
}
8687

8788
// Add ten thousand torrents in parallel (depending on the set worker threads)
88-
pub async fn async_add_multiple_torrents_in_parallel<T>(runtime: &tokio::runtime::Runtime, samples: usize) -> (Duration, Duration)
89+
pub async fn add_multiple_torrents_in_parallel<T>(runtime: &tokio::runtime::Runtime, samples: usize) -> (Duration, Duration)
8990
where
9091
T: Send + Sync + 'static,
91-
RepositoryTokioRwLock<T>: RepositoryAsync<T>,
92+
RepositoryTokioRwLock<T>: RepositoryAsync<T> + UpdateTorrentAsync,
9293
{
9394
let args = Args::parse();
9495
let mut results: Vec<Duration> = Vec::with_capacity(samples);
@@ -130,13 +131,10 @@ where
130131
}
131132

132133
// Async update ten thousand torrents in parallel (depending on the set worker threads)
133-
pub async fn async_update_multiple_torrents_in_parallel<T>(
134-
runtime: &tokio::runtime::Runtime,
135-
samples: usize,
136-
) -> (Duration, Duration)
134+
pub async fn update_multiple_torrents_in_parallel<T>(runtime: &tokio::runtime::Runtime, samples: usize) -> (Duration, Duration)
137135
where
138136
T: Send + Sync + 'static,
139-
RepositoryTokioRwLock<T>: RepositoryAsync<T>,
137+
RepositoryTokioRwLock<T>: RepositoryAsync<T> + UpdateTorrentAsync,
140138
{
141139
let args = Args::parse();
142140
let mut results: Vec<Duration> = Vec::with_capacity(samples);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
pub mod asyn;
22
pub mod sync;
3+
pub mod sync_asyn;
34
pub mod utils;

packages/torrent-repository-benchmarks/src/benches/sync.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::time::Duration;
44
use clap::Parser;
55
use futures::stream::FuturesUnordered;
66
use torrust_tracker::core::torrent::repository_sync::{RepositoryStdRwLock, RepositorySync};
7+
use torrust_tracker::core::torrent::UpdateTorrentSync;
78
use torrust_tracker::shared::bit_torrent::info_hash::InfoHash;
89

910
use crate::args::Args;
@@ -13,7 +14,7 @@ use crate::benches::utils::{generate_unique_info_hashes, get_average_and_adjuste
1314
#[must_use]
1415
pub fn add_one_torrent<T>(samples: usize) -> (Duration, Duration)
1516
where
16-
RepositoryStdRwLock<T>: RepositorySync<T>,
17+
RepositoryStdRwLock<T>: RepositorySync<T> + UpdateTorrentSync,
1718
{
1819
let mut results: Vec<Duration> = Vec::with_capacity(samples);
1920

@@ -38,7 +39,7 @@ where
3839
pub async fn update_one_torrent_in_parallel<T>(runtime: &tokio::runtime::Runtime, samples: usize) -> (Duration, Duration)
3940
where
4041
T: Send + Sync + 'static,
41-
RepositoryStdRwLock<T>: RepositorySync<T>,
42+
RepositoryStdRwLock<T>: RepositorySync<T> + UpdateTorrentSync,
4243
{
4344
let args = Args::parse();
4445
let mut results: Vec<Duration> = Vec::with_capacity(samples);
@@ -84,7 +85,7 @@ where
8485
pub async fn add_multiple_torrents_in_parallel<T>(runtime: &tokio::runtime::Runtime, samples: usize) -> (Duration, Duration)
8586
where
8687
T: Send + Sync + 'static,
87-
RepositoryStdRwLock<T>: RepositorySync<T>,
88+
RepositoryStdRwLock<T>: RepositorySync<T> + UpdateTorrentSync,
8889
{
8990
let args = Args::parse();
9091
let mut results: Vec<Duration> = Vec::with_capacity(samples);
@@ -127,7 +128,7 @@ where
127128
pub async fn update_multiple_torrents_in_parallel<T>(runtime: &tokio::runtime::Runtime, samples: usize) -> (Duration, Duration)
128129
where
129130
T: Send + Sync + 'static,
130-
RepositoryStdRwLock<T>: RepositorySync<T>,
131+
RepositoryStdRwLock<T>: RepositorySync<T> + UpdateTorrentSync,
131132
{
132133
let args = Args::parse();
133134
let mut results: Vec<Duration> = Vec::with_capacity(samples);
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
use std::sync::Arc;
2+
use std::time::Duration;
3+
4+
use clap::Parser;
5+
use futures::stream::FuturesUnordered;
6+
use torrust_tracker::core::torrent::repository_sync::{RepositoryStdRwLock, RepositorySync};
7+
use torrust_tracker::core::torrent::UpdateTorrentAsync;
8+
use torrust_tracker::shared::bit_torrent::info_hash::InfoHash;
9+
10+
use crate::args::Args;
11+
use crate::benches::utils::{generate_unique_info_hashes, get_average_and_adjusted_average_from_results, DEFAULT_PEER};
12+
13+
// Simply add one torrent
14+
#[must_use]
15+
pub async fn add_one_torrent<T>(samples: usize) -> (Duration, Duration)
16+
where
17+
RepositoryStdRwLock<T>: RepositorySync<T> + UpdateTorrentAsync,
18+
{
19+
let mut results: Vec<Duration> = Vec::with_capacity(samples);
20+
21+
for _ in 0..samples {
22+
let torrent_repository = Arc::new(RepositoryStdRwLock::<T>::default());
23+
24+
let info_hash = InfoHash([0; 20]);
25+
26+
let start_time = std::time::Instant::now();
27+
28+
torrent_repository
29+
.update_torrent_with_peer_and_get_stats(&info_hash, &DEFAULT_PEER)
30+
.await;
31+
32+
let result = start_time.elapsed();
33+
34+
results.push(result);
35+
}
36+
37+
get_average_and_adjusted_average_from_results(results)
38+
}
39+
40+
// Add one torrent ten thousand times in parallel (depending on the set worker threads)
41+
pub async fn update_one_torrent_in_parallel<T>(runtime: &tokio::runtime::Runtime, samples: usize) -> (Duration, Duration)
42+
where
43+
T: Send + Sync + 'static,
44+
RepositoryStdRwLock<T>: RepositorySync<T> + UpdateTorrentAsync,
45+
{
46+
let args = Args::parse();
47+
let mut results: Vec<Duration> = Vec::with_capacity(samples);
48+
49+
for _ in 0..samples {
50+
let torrent_repository = Arc::new(RepositoryStdRwLock::<T>::default());
51+
let info_hash: &'static InfoHash = &InfoHash([0; 20]);
52+
let handles = FuturesUnordered::new();
53+
54+
// Add the torrent/peer to the torrent repository
55+
torrent_repository
56+
.update_torrent_with_peer_and_get_stats(info_hash, &DEFAULT_PEER)
57+
.await;
58+
59+
let start_time = std::time::Instant::now();
60+
61+
for _ in 0..10_000 {
62+
let torrent_repository_clone = torrent_repository.clone();
63+
64+
let handle = runtime.spawn(async move {
65+
torrent_repository_clone
66+
.update_torrent_with_peer_and_get_stats(info_hash, &DEFAULT_PEER)
67+
.await;
68+
69+
if let Some(sleep_time) = args.sleep {
70+
let start_time = std::time::Instant::now();
71+
72+
while start_time.elapsed().as_nanos() < u128::from(sleep_time) {}
73+
}
74+
});
75+
76+
handles.push(handle);
77+
}
78+
79+
// Await all tasks
80+
futures::future::join_all(handles).await;
81+
82+
let result = start_time.elapsed();
83+
84+
results.push(result);
85+
}
86+
87+
get_average_and_adjusted_average_from_results(results)
88+
}
89+
90+
// Add ten thousand torrents in parallel (depending on the set worker threads)
91+
pub async fn add_multiple_torrents_in_parallel<T>(runtime: &tokio::runtime::Runtime, samples: usize) -> (Duration, Duration)
92+
where
93+
T: Send + Sync + 'static,
94+
RepositoryStdRwLock<T>: RepositorySync<T> + UpdateTorrentAsync,
95+
{
96+
let args = Args::parse();
97+
let mut results: Vec<Duration> = Vec::with_capacity(samples);
98+
99+
for _ in 0..samples {
100+
let torrent_repository = Arc::new(RepositoryStdRwLock::<T>::default());
101+
let info_hashes = generate_unique_info_hashes(10_000);
102+
let handles = FuturesUnordered::new();
103+
104+
let start_time = std::time::Instant::now();
105+
106+
for info_hash in info_hashes {
107+
let torrent_repository_clone = torrent_repository.clone();
108+
109+
let handle = runtime.spawn(async move {
110+
torrent_repository_clone
111+
.update_torrent_with_peer_and_get_stats(&info_hash, &DEFAULT_PEER)
112+
.await;
113+
114+
if let Some(sleep_time) = args.sleep {
115+
let start_time = std::time::Instant::now();
116+
117+
while start_time.elapsed().as_nanos() < u128::from(sleep_time) {}
118+
}
119+
});
120+
121+
handles.push(handle);
122+
}
123+
124+
// Await all tasks
125+
futures::future::join_all(handles).await;
126+
127+
let result = start_time.elapsed();
128+
129+
results.push(result);
130+
}
131+
132+
get_average_and_adjusted_average_from_results(results)
133+
}
134+
135+
// Update ten thousand torrents in parallel (depending on the set worker threads)
136+
pub async fn update_multiple_torrents_in_parallel<T>(runtime: &tokio::runtime::Runtime, samples: usize) -> (Duration, Duration)
137+
where
138+
T: Send + Sync + 'static,
139+
RepositoryStdRwLock<T>: RepositorySync<T> + UpdateTorrentAsync,
140+
{
141+
let args = Args::parse();
142+
let mut results: Vec<Duration> = Vec::with_capacity(samples);
143+
144+
for _ in 0..samples {
145+
let torrent_repository = Arc::new(RepositoryStdRwLock::<T>::default());
146+
let info_hashes = generate_unique_info_hashes(10_000);
147+
let handles = FuturesUnordered::new();
148+
149+
// Add the torrents/peers to the torrent repository
150+
for info_hash in &info_hashes {
151+
torrent_repository
152+
.update_torrent_with_peer_and_get_stats(info_hash, &DEFAULT_PEER)
153+
.await;
154+
}
155+
156+
let start_time = std::time::Instant::now();
157+
158+
for info_hash in info_hashes {
159+
let torrent_repository_clone = torrent_repository.clone();
160+
161+
let handle = runtime.spawn(async move {
162+
torrent_repository_clone
163+
.update_torrent_with_peer_and_get_stats(&info_hash, &DEFAULT_PEER)
164+
.await;
165+
166+
if let Some(sleep_time) = args.sleep {
167+
let start_time = std::time::Instant::now();
168+
169+
while start_time.elapsed().as_nanos() < u128::from(sleep_time) {}
170+
}
171+
});
172+
173+
handles.push(handle);
174+
}
175+
176+
// Await all tasks
177+
futures::future::join_all(handles).await;
178+
179+
let result = start_time.elapsed();
180+
181+
results.push(result);
182+
}
183+
184+
get_average_and_adjusted_average_from_results(results)
185+
}

0 commit comments

Comments
 (0)