Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 840af5b

Browse files
committedMar 2, 2024··
dev: repository benchmark uses criterion
1 parent 72d0678 commit 840af5b

File tree

29 files changed

+369
-478
lines changed

29 files changed

+369
-478
lines changed
 

‎Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ anyhow = "1.0.79"
7979
hex-literal = "0.4.1"
8080

8181
[dev-dependencies]
82-
criterion = { version = "0.5.1", features = ["async_tokio"] }
82+
criterion = { version = "0", features = ["async_tokio"] }
8383
local-ip-address = "0"
8484
mockall = "0"
8585
once_cell = "1.18.0"

‎contrib/bencode/src/mutable/encode.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::iter::Extend;
2-
31
use crate::access::bencode::{BRefAccess, RefKind};
42
use crate::access::dict::BDictAccess;
53
use crate::access::list::BListAccess;

‎contrib/bencode/src/reference/bencode_ref.rs

-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ impl<'a> BRefAccessExt<'a> for BencodeRef<'a> {
125125

126126
#[cfg(test)]
127127
mod tests {
128-
use std::default::Default;
129128

130129
use crate::access::bencode::BRefAccess;
131130
use crate::reference::bencode_ref::BencodeRef;

‎contrib/bencode/src/reference/decode.rs

-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,6 @@ fn peek_byte(bytes: &[u8], pos: usize) -> BencodeParseResult<u8> {
177177

178178
#[cfg(test)]
179179
mod tests {
180-
use std::default::Default;
181180

182181
use crate::access::bencode::BRefAccess;
183182
use crate::reference::bencode_ref::BencodeRef;

‎contrib/bencode/src/reference/decode_opt.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::default::Default;
2-
31
const DEFAULT_MAX_RECURSION: usize = 50;
42
const DEFAULT_CHECK_KEY_SORT: bool = false;
53
const DEFAULT_ENFORCE_FULL_DECODE: bool = true;

‎packages/configuration/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ pub struct Configuration {
482482
/// peers from the torrent peer list.
483483
pub inactive_peer_cleanup_interval: u64,
484484
/// If enabled, the tracker will remove torrents that have no peers.
485-
/// THe clean up torrent job runs every `inactive_peer_cleanup_interval`
485+
/// The clean up torrent job runs every `inactive_peer_cleanup_interval`
486486
/// seconds and it removes inactive peers. Eventually, the peer list of a
487487
/// torrent could be empty and the torrent will be removed if this option is
488488
/// enabled.

‎packages/primitives/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ pub enum DatabaseDriver {
5454
// TODO: Move to the database crate once that gets its own crate.
5555
/// The Sqlite3 database driver.
5656
Sqlite3,
57-
/// The MySQL database driver.
57+
/// The `MySQL` database driver.
5858
MySQL,
5959
}
6060

‎packages/primitives/src/peer.rs

-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
use std::net::{IpAddr, SocketAddr};
2525
use std::sync::Arc;
2626

27-
use serde;
2827
use serde::Serialize;
2928

3029
use crate::announce_event::AnnounceEvent;

‎packages/torrent-repository/Cargo.toml

+8-1
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,11 @@ torrust-tracker-primitives = { version = "3.0.0-alpha.12-develop", path = "../pr
2323
torrust-tracker-configuration = { version = "3.0.0-alpha.12-develop", path = "../configuration" }
2424
serde = { version = "1", features = ["derive"] }
2525
tdyne-peer-id = "1"
26-
tdyne-peer-id-registry = "0"
26+
tdyne-peer-id-registry = "0"
27+
28+
[dev-dependencies]
29+
criterion = { version = "0", features = ["async_tokio"] }
30+
31+
[[bench]]
32+
harness = false
33+
name = "repository_benchmark"

‎packages/torrent-repository/benches/helpers/args.rs

-15
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,178 +1,147 @@
1-
use std::time::Duration;
1+
use std::time::{Duration, Instant};
22

3-
use clap::Parser;
43
use futures::stream::FuturesUnordered;
54
use torrust_tracker_primitives::info_hash::InfoHash;
65
use torrust_tracker_torrent_repository::repository::UpdateTorrentAsync;
76

8-
use super::args::Args;
9-
use super::utils::{generate_unique_info_hashes, get_average_and_adjusted_average_from_results, DEFAULT_PEER};
7+
use super::utils::{generate_unique_info_hashes, DEFAULT_PEER};
108

11-
pub async fn add_one_torrent<V>(samples: usize) -> (Duration, Duration)
9+
pub async fn add_one_torrent<V>(samples: u64) -> Duration
1210
where
1311
V: UpdateTorrentAsync + Default,
1412
{
15-
let mut results: Vec<Duration> = Vec::with_capacity(samples);
13+
let start = Instant::now();
1614

1715
for _ in 0..samples {
1816
let torrent_repository = V::default();
1917

2018
let info_hash = InfoHash([0; 20]);
2119

22-
let start_time = std::time::Instant::now();
23-
2420
torrent_repository
2521
.update_torrent_with_peer_and_get_stats(&info_hash, &DEFAULT_PEER)
2622
.await;
27-
28-
let result = start_time.elapsed();
29-
30-
results.push(result);
3123
}
3224

33-
get_average_and_adjusted_average_from_results(results)
25+
start.elapsed()
3426
}
3527

3628
// Add one torrent ten thousand times in parallel (depending on the set worker threads)
37-
pub async fn update_one_torrent_in_parallel<V>(runtime: &tokio::runtime::Runtime, samples: usize) -> (Duration, Duration)
29+
pub async fn update_one_torrent_in_parallel<V>(runtime: &tokio::runtime::Runtime, samples: u64, sleep: Option<u64>) -> Duration
3830
where
3931
V: UpdateTorrentAsync + Default + Clone + Send + Sync + 'static,
4032
{
41-
let args = Args::parse();
42-
let mut results: Vec<Duration> = Vec::with_capacity(samples);
43-
44-
for _ in 0..samples {
45-
let torrent_repository = V::default();
46-
let info_hash: &'static InfoHash = &InfoHash([0; 20]);
47-
let handles = FuturesUnordered::new();
48-
49-
// Add the torrent/peer to the torrent repository
50-
torrent_repository
51-
.update_torrent_with_peer_and_get_stats(info_hash, &DEFAULT_PEER)
52-
.await;
33+
let torrent_repository = V::default();
34+
let info_hash: &'static InfoHash = &InfoHash([0; 20]);
35+
let handles = FuturesUnordered::new();
5336

54-
let start_time = std::time::Instant::now();
37+
// Add the torrent/peer to the torrent repository
38+
torrent_repository
39+
.update_torrent_with_peer_and_get_stats(info_hash, &DEFAULT_PEER)
40+
.await;
5541

56-
for _ in 0..10_000 {
57-
let torrent_repository_clone = torrent_repository.clone();
42+
let start = Instant::now();
5843

59-
let handle = runtime.spawn(async move {
60-
torrent_repository_clone
61-
.update_torrent_with_peer_and_get_stats(info_hash, &DEFAULT_PEER)
62-
.await;
63-
64-
if let Some(sleep_time) = args.sleep {
65-
let start_time = std::time::Instant::now();
66-
67-
while start_time.elapsed().as_nanos() < u128::from(sleep_time) {}
68-
}
69-
});
44+
for _ in 0..samples {
45+
let torrent_repository_clone = torrent_repository.clone();
7046

71-
handles.push(handle);
72-
}
47+
let handle = runtime.spawn(async move {
48+
torrent_repository_clone
49+
.update_torrent_with_peer_and_get_stats(info_hash, &DEFAULT_PEER)
50+
.await;
7351

74-
// Await all tasks
75-
futures::future::join_all(handles).await;
52+
if let Some(sleep_time) = sleep {
53+
let start_time = std::time::Instant::now();
7654

77-
let result = start_time.elapsed();
55+
while start_time.elapsed().as_nanos() < u128::from(sleep_time) {}
56+
}
57+
});
7858

79-
results.push(result);
59+
handles.push(handle);
8060
}
8161

82-
get_average_and_adjusted_average_from_results(results)
62+
// Await all tasks
63+
futures::future::join_all(handles).await;
64+
65+
start.elapsed()
8366
}
8467

8568
// Add ten thousand torrents in parallel (depending on the set worker threads)
86-
pub async fn add_multiple_torrents_in_parallel<V>(runtime: &tokio::runtime::Runtime, samples: usize) -> (Duration, Duration)
69+
pub async fn add_multiple_torrents_in_parallel<V>(runtime: &tokio::runtime::Runtime, samples: u64, sleep: Option<u64>) -> Duration
8770
where
8871
V: UpdateTorrentAsync + Default + Clone + Send + Sync + 'static,
8972
{
90-
let args = Args::parse();
91-
let mut results: Vec<Duration> = Vec::with_capacity(samples);
92-
93-
for _ in 0..samples {
94-
let torrent_repository = V::default();
95-
let info_hashes = generate_unique_info_hashes(10_000);
96-
let handles = FuturesUnordered::new();
73+
let torrent_repository = V::default();
74+
let info_hashes = generate_unique_info_hashes(samples.try_into().expect("it should fit in a usize"));
75+
let handles = FuturesUnordered::new();
9776

98-
let start_time = std::time::Instant::now();
77+
let start = Instant::now();
9978

100-
for info_hash in info_hashes {
101-
let torrent_repository_clone = torrent_repository.clone();
79+
for info_hash in info_hashes {
80+
let torrent_repository_clone = torrent_repository.clone();
10281

103-
let handle = runtime.spawn(async move {
104-
torrent_repository_clone
105-
.update_torrent_with_peer_and_get_stats(&info_hash, &DEFAULT_PEER)
106-
.await;
107-
108-
if let Some(sleep_time) = args.sleep {
109-
let start_time = std::time::Instant::now();
110-
111-
while start_time.elapsed().as_nanos() < u128::from(sleep_time) {}
112-
}
113-
});
114-
115-
handles.push(handle);
116-
}
82+
let handle = runtime.spawn(async move {
83+
torrent_repository_clone
84+
.update_torrent_with_peer_and_get_stats(&info_hash, &DEFAULT_PEER)
85+
.await;
11786

118-
// Await all tasks
119-
futures::future::join_all(handles).await;
87+
if let Some(sleep_time) = sleep {
88+
let start_time = std::time::Instant::now();
12089

121-
let result = start_time.elapsed();
90+
while start_time.elapsed().as_nanos() < u128::from(sleep_time) {}
91+
}
92+
});
12293

123-
results.push(result);
94+
handles.push(handle);
12495
}
12596

126-
get_average_and_adjusted_average_from_results(results)
97+
// Await all tasks
98+
futures::future::join_all(handles).await;
99+
100+
start.elapsed()
127101
}
128102

129103
// Async update ten thousand torrents in parallel (depending on the set worker threads)
130-
pub async fn update_multiple_torrents_in_parallel<V>(runtime: &tokio::runtime::Runtime, samples: usize) -> (Duration, Duration)
104+
pub async fn update_multiple_torrents_in_parallel<V>(
105+
runtime: &tokio::runtime::Runtime,
106+
samples: u64,
107+
sleep: Option<u64>,
108+
) -> Duration
131109
where
132110
V: UpdateTorrentAsync + Default + Clone + Send + Sync + 'static,
133111
{
134-
let args = Args::parse();
135-
let mut results: Vec<Duration> = Vec::with_capacity(samples);
112+
let torrent_repository = V::default();
113+
let info_hashes = generate_unique_info_hashes(samples.try_into().expect("it should fit in usize"));
114+
let handles = FuturesUnordered::new();
136115

137-
for _ in 0..samples {
138-
let torrent_repository = V::default();
139-
let info_hashes = generate_unique_info_hashes(10_000);
140-
let handles = FuturesUnordered::new();
141-
142-
// Add the torrents/peers to the torrent repository
143-
for info_hash in &info_hashes {
144-
torrent_repository
145-
.update_torrent_with_peer_and_get_stats(info_hash, &DEFAULT_PEER)
146-
.await;
147-
}
148-
149-
let start_time = std::time::Instant::now();
150-
151-
for info_hash in info_hashes {
152-
let torrent_repository_clone = torrent_repository.clone();
153-
154-
let handle = runtime.spawn(async move {
155-
torrent_repository_clone
156-
.update_torrent_with_peer_and_get_stats(&info_hash, &DEFAULT_PEER)
157-
.await;
116+
// Add the torrents/peers to the torrent repository
117+
for info_hash in &info_hashes {
118+
torrent_repository
119+
.update_torrent_with_peer_and_get_stats(info_hash, &DEFAULT_PEER)
120+
.await;
121+
}
158122

159-
if let Some(sleep_time) = args.sleep {
160-
let start_time = std::time::Instant::now();
123+
let start = Instant::now();
161124

162-
while start_time.elapsed().as_nanos() < u128::from(sleep_time) {}
163-
}
164-
});
125+
for info_hash in info_hashes {
126+
let torrent_repository_clone = torrent_repository.clone();
165127

166-
handles.push(handle);
167-
}
128+
let handle = runtime.spawn(async move {
129+
torrent_repository_clone
130+
.update_torrent_with_peer_and_get_stats(&info_hash, &DEFAULT_PEER)
131+
.await;
168132

169-
// Await all tasks
170-
futures::future::join_all(handles).await;
133+
if let Some(sleep_time) = sleep {
134+
let start_time = std::time::Instant::now();
171135

172-
let result = start_time.elapsed();
136+
while start_time.elapsed().as_nanos() < u128::from(sleep_time) {}
137+
}
138+
});
173139

174-
results.push(result);
140+
handles.push(handle);
175141
}
176142

177-
get_average_and_adjusted_average_from_results(results)
143+
// Await all tasks
144+
futures::future::join_all(handles).await;
145+
146+
start.elapsed()
178147
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
pub mod args;
21
pub mod asyn;
32
pub mod sync;
43
pub mod utils;
Original file line numberDiff line numberDiff line change
@@ -1,168 +1,137 @@
1-
use std::time::Duration;
1+
use std::time::{Duration, Instant};
22

3-
use clap::Parser;
43
use futures::stream::FuturesUnordered;
54
use torrust_tracker_primitives::info_hash::InfoHash;
65
use torrust_tracker_torrent_repository::repository::UpdateTorrentSync;
76

8-
use super::args::Args;
9-
use super::utils::{generate_unique_info_hashes, get_average_and_adjusted_average_from_results, DEFAULT_PEER};
7+
use super::utils::{generate_unique_info_hashes, DEFAULT_PEER};
108

119
// Simply add one torrent
1210
#[must_use]
13-
pub fn add_one_torrent<V>(samples: usize) -> (Duration, Duration)
11+
pub fn add_one_torrent<V>(samples: u64) -> Duration
1412
where
1513
V: UpdateTorrentSync + Default,
1614
{
17-
let mut results: Vec<Duration> = Vec::with_capacity(samples);
15+
let start = Instant::now();
1816

1917
for _ in 0..samples {
2018
let torrent_repository = V::default();
2119

2220
let info_hash = InfoHash([0; 20]);
2321

24-
let start_time = std::time::Instant::now();
25-
2622
torrent_repository.update_torrent_with_peer_and_get_stats(&info_hash, &DEFAULT_PEER);
27-
28-
let result = start_time.elapsed();
29-
30-
results.push(result);
3123
}
3224

33-
get_average_and_adjusted_average_from_results(results)
25+
start.elapsed()
3426
}
3527

3628
// Add one torrent ten thousand times in parallel (depending on the set worker threads)
37-
pub async fn update_one_torrent_in_parallel<V>(runtime: &tokio::runtime::Runtime, samples: usize) -> (Duration, Duration)
29+
pub async fn update_one_torrent_in_parallel<V>(runtime: &tokio::runtime::Runtime, samples: u64, sleep: Option<u64>) -> Duration
3830
where
3931
V: UpdateTorrentSync + Default + Clone + Send + Sync + 'static,
4032
{
41-
let args = Args::parse();
42-
let mut results: Vec<Duration> = Vec::with_capacity(samples);
43-
44-
for _ in 0..samples {
45-
let torrent_repository = V::default();
46-
let info_hash: &'static InfoHash = &InfoHash([0; 20]);
47-
let handles = FuturesUnordered::new();
48-
49-
// Add the torrent/peer to the torrent repository
50-
torrent_repository.update_torrent_with_peer_and_get_stats(info_hash, &DEFAULT_PEER);
51-
52-
let start_time = std::time::Instant::now();
33+
let torrent_repository = V::default();
34+
let info_hash: &'static InfoHash = &InfoHash([0; 20]);
35+
let handles = FuturesUnordered::new();
5336

54-
for _ in 0..10_000 {
55-
let torrent_repository_clone = torrent_repository.clone();
37+
// Add the torrent/peer to the torrent repository
38+
torrent_repository.update_torrent_with_peer_and_get_stats(info_hash, &DEFAULT_PEER);
5639

57-
let handle = runtime.spawn(async move {
58-
torrent_repository_clone.update_torrent_with_peer_and_get_stats(info_hash, &DEFAULT_PEER);
40+
let start = Instant::now();
5941

60-
if let Some(sleep_time) = args.sleep {
61-
let start_time = std::time::Instant::now();
62-
63-
while start_time.elapsed().as_nanos() < u128::from(sleep_time) {}
64-
}
65-
});
42+
for _ in 0..samples {
43+
let torrent_repository_clone = torrent_repository.clone();
6644

67-
handles.push(handle);
68-
}
45+
let handle = runtime.spawn(async move {
46+
torrent_repository_clone.update_torrent_with_peer_and_get_stats(info_hash, &DEFAULT_PEER);
6947

70-
// Await all tasks
71-
futures::future::join_all(handles).await;
48+
if let Some(sleep_time) = sleep {
49+
let start_time = std::time::Instant::now();
7250

73-
let result = start_time.elapsed();
51+
while start_time.elapsed().as_nanos() < u128::from(sleep_time) {}
52+
}
53+
});
7454

75-
results.push(result);
55+
handles.push(handle);
7656
}
7757

78-
get_average_and_adjusted_average_from_results(results)
58+
// Await all tasks
59+
futures::future::join_all(handles).await;
60+
61+
start.elapsed()
7962
}
8063

8164
// Add ten thousand torrents in parallel (depending on the set worker threads)
82-
pub async fn add_multiple_torrents_in_parallel<V>(runtime: &tokio::runtime::Runtime, samples: usize) -> (Duration, Duration)
65+
pub async fn add_multiple_torrents_in_parallel<V>(runtime: &tokio::runtime::Runtime, samples: u64, sleep: Option<u64>) -> Duration
8366
where
8467
V: UpdateTorrentSync + Default + Clone + Send + Sync + 'static,
8568
{
86-
let args = Args::parse();
87-
let mut results: Vec<Duration> = Vec::with_capacity(samples);
88-
89-
for _ in 0..samples {
90-
let torrent_repository = V::default();
91-
let info_hashes = generate_unique_info_hashes(10_000);
92-
let handles = FuturesUnordered::new();
93-
94-
let start_time = std::time::Instant::now();
95-
96-
for info_hash in info_hashes {
97-
let torrent_repository_clone = torrent_repository.clone();
98-
99-
let handle = runtime.spawn(async move {
100-
torrent_repository_clone.update_torrent_with_peer_and_get_stats(&info_hash, &DEFAULT_PEER);
69+
let torrent_repository = V::default();
70+
let info_hashes = generate_unique_info_hashes(samples.try_into().expect("it should fit in a usize"));
71+
let handles = FuturesUnordered::new();
10172

102-
if let Some(sleep_time) = args.sleep {
103-
let start_time = std::time::Instant::now();
73+
let start = Instant::now();
10474

105-
while start_time.elapsed().as_nanos() < u128::from(sleep_time) {}
106-
}
107-
});
75+
for info_hash in info_hashes {
76+
let torrent_repository_clone = torrent_repository.clone();
10877

109-
handles.push(handle);
110-
}
78+
let handle = runtime.spawn(async move {
79+
torrent_repository_clone.update_torrent_with_peer_and_get_stats(&info_hash, &DEFAULT_PEER);
11180

112-
// Await all tasks
113-
futures::future::join_all(handles).await;
81+
if let Some(sleep_time) = sleep {
82+
let start_time = std::time::Instant::now();
11483

115-
let result = start_time.elapsed();
84+
while start_time.elapsed().as_nanos() < u128::from(sleep_time) {}
85+
}
86+
});
11687

117-
results.push(result);
88+
handles.push(handle);
11889
}
11990

120-
get_average_and_adjusted_average_from_results(results)
91+
// Await all tasks
92+
futures::future::join_all(handles).await;
93+
94+
start.elapsed()
12195
}
12296

12397
// Update ten thousand torrents in parallel (depending on the set worker threads)
124-
pub async fn update_multiple_torrents_in_parallel<V>(runtime: &tokio::runtime::Runtime, samples: usize) -> (Duration, Duration)
98+
pub async fn update_multiple_torrents_in_parallel<V>(
99+
runtime: &tokio::runtime::Runtime,
100+
samples: u64,
101+
sleep: Option<u64>,
102+
) -> Duration
125103
where
126104
V: UpdateTorrentSync + Default + Clone + Send + Sync + 'static,
127105
{
128-
let args = Args::parse();
129-
let mut results: Vec<Duration> = Vec::with_capacity(samples);
130-
131-
for _ in 0..samples {
132-
let torrent_repository = V::default();
133-
let info_hashes = generate_unique_info_hashes(10_000);
134-
let handles = FuturesUnordered::new();
135-
136-
// Add the torrents/peers to the torrent repository
137-
for info_hash in &info_hashes {
138-
torrent_repository.update_torrent_with_peer_and_get_stats(info_hash, &DEFAULT_PEER);
139-
}
140-
141-
let start_time = std::time::Instant::now();
142-
143-
for info_hash in info_hashes {
144-
let torrent_repository_clone = torrent_repository.clone();
106+
let torrent_repository = V::default();
107+
let info_hashes = generate_unique_info_hashes(samples.try_into().expect("it should fit in usize"));
108+
let handles = FuturesUnordered::new();
145109

146-
let handle = runtime.spawn(async move {
147-
torrent_repository_clone.update_torrent_with_peer_and_get_stats(&info_hash, &DEFAULT_PEER);
110+
// Add the torrents/peers to the torrent repository
111+
for info_hash in &info_hashes {
112+
torrent_repository.update_torrent_with_peer_and_get_stats(info_hash, &DEFAULT_PEER);
113+
}
148114

149-
if let Some(sleep_time) = args.sleep {
150-
let start_time = std::time::Instant::now();
115+
let start = Instant::now();
151116

152-
while start_time.elapsed().as_nanos() < u128::from(sleep_time) {}
153-
}
154-
});
117+
for info_hash in info_hashes {
118+
let torrent_repository_clone = torrent_repository.clone();
155119

156-
handles.push(handle);
157-
}
120+
let handle = runtime.spawn(async move {
121+
torrent_repository_clone.update_torrent_with_peer_and_get_stats(&info_hash, &DEFAULT_PEER);
158122

159-
// Await all tasks
160-
futures::future::join_all(handles).await;
123+
if let Some(sleep_time) = sleep {
124+
let start_time = std::time::Instant::now();
161125

162-
let result = start_time.elapsed();
126+
while start_time.elapsed().as_nanos() < u128::from(sleep_time) {}
127+
}
128+
});
163129

164-
results.push(result);
130+
handles.push(handle);
165131
}
166132

167-
get_average_and_adjusted_average_from_results(results)
133+
// Await all tasks
134+
futures::future::join_all(handles).await;
135+
136+
start.elapsed()
168137
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::collections::HashSet;
22
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
3-
use std::time::Duration;
43

54
use torrust_tracker_primitives::announce_event::AnnounceEvent;
65
use torrust_tracker_primitives::info_hash::InfoHash;
@@ -39,35 +38,3 @@ pub fn generate_unique_info_hashes(size: usize) -> Vec<InfoHash> {
3938

4039
result.into_iter().collect()
4140
}
42-
43-
#[must_use]
44-
pub fn within_acceptable_range(test: &Duration, norm: &Duration) -> bool {
45-
let test_secs = test.as_secs_f64();
46-
let norm_secs = norm.as_secs_f64();
47-
48-
// Calculate the upper and lower bounds for the 10% tolerance
49-
let tolerance = norm_secs * 0.1;
50-
51-
// Calculate the upper and lower limits
52-
let upper_limit = norm_secs + tolerance;
53-
let lower_limit = norm_secs - tolerance;
54-
55-
test_secs < upper_limit && test_secs > lower_limit
56-
}
57-
58-
#[must_use]
59-
pub fn get_average_and_adjusted_average_from_results(mut results: Vec<Duration>) -> (Duration, Duration) {
60-
#[allow(clippy::cast_possible_truncation)]
61-
let average = results.iter().sum::<Duration>() / results.len() as u32;
62-
63-
results.retain(|result| within_acceptable_range(result, &average));
64-
65-
let mut adjusted_average = Duration::from_nanos(0);
66-
67-
#[allow(clippy::cast_possible_truncation)]
68-
if results.len() > 1 {
69-
adjusted_average = results.iter().sum::<Duration>() / results.len() as u32;
70-
}
71-
72-
(average, adjusted_average)
73-
}

‎packages/torrent-repository/benches/repository-benchmark.rs

-189
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
use std::sync::Arc;
2+
use std::time::Duration;
3+
4+
mod helpers;
5+
6+
use criterion::{criterion_group, criterion_main, Criterion};
7+
use torrust_tracker_torrent_repository::{
8+
TorrentsRwLockStd, TorrentsRwLockStdMutexStd, TorrentsRwLockStdMutexTokio, TorrentsRwLockTokio, TorrentsRwLockTokioMutexStd,
9+
TorrentsRwLockTokioMutexTokio,
10+
};
11+
12+
use crate::helpers::{asyn, sync};
13+
14+
fn add_one_torrent(c: &mut Criterion) {
15+
let rt = tokio::runtime::Builder::new_multi_thread().worker_threads(4).build().unwrap();
16+
17+
let mut group = c.benchmark_group("add_one_torrent");
18+
19+
group.warm_up_time(Duration::from_millis(500));
20+
group.measurement_time(Duration::from_millis(1000));
21+
22+
group.bench_function("RwLockStd", |b| {
23+
b.iter_custom(sync::add_one_torrent::<Arc<TorrentsRwLockStd>>);
24+
});
25+
26+
group.bench_function("RwLockStdMutexStd", |b| {
27+
b.iter_custom(sync::add_one_torrent::<Arc<TorrentsRwLockStdMutexStd>>);
28+
});
29+
30+
group.bench_function("RwLockStdMutexTokio", |b| {
31+
b.to_async(&rt)
32+
.iter_custom(asyn::add_one_torrent::<Arc<TorrentsRwLockStdMutexTokio>>);
33+
});
34+
35+
group.bench_function("RwLockTokio", |b| {
36+
b.to_async(&rt).iter_custom(asyn::add_one_torrent::<Arc<TorrentsRwLockTokio>>);
37+
});
38+
39+
group.bench_function("RwLockTokioMutexStd", |b| {
40+
b.to_async(&rt)
41+
.iter_custom(asyn::add_one_torrent::<Arc<TorrentsRwLockTokioMutexStd>>);
42+
});
43+
44+
group.bench_function("RwLockTokioMutexTokio", |b| {
45+
b.to_async(&rt)
46+
.iter_custom(asyn::add_one_torrent::<Arc<TorrentsRwLockTokioMutexTokio>>);
47+
});
48+
49+
group.finish();
50+
}
51+
52+
fn add_multiple_torrents_in_parallel(c: &mut Criterion) {
53+
let rt = tokio::runtime::Builder::new_multi_thread().worker_threads(4).build().unwrap();
54+
55+
let mut group = c.benchmark_group("add_multiple_torrents_in_parallel");
56+
57+
//group.sampling_mode(criterion::SamplingMode::Flat);
58+
//group.sample_size(10);
59+
60+
group.warm_up_time(Duration::from_millis(500));
61+
group.measurement_time(Duration::from_millis(1000));
62+
63+
group.bench_function("RwLockStd", |b| {
64+
b.to_async(&rt)
65+
.iter_custom(|iters| sync::add_multiple_torrents_in_parallel::<Arc<TorrentsRwLockStd>>(&rt, iters, None));
66+
});
67+
68+
group.bench_function("RwLockStdMutexStd", |b| {
69+
b.to_async(&rt)
70+
.iter_custom(|iters| sync::add_multiple_torrents_in_parallel::<Arc<TorrentsRwLockStdMutexStd>>(&rt, iters, None));
71+
});
72+
73+
group.bench_function("RwLockStdMutexTokio", |b| {
74+
b.to_async(&rt)
75+
.iter_custom(|iters| asyn::add_multiple_torrents_in_parallel::<Arc<TorrentsRwLockStdMutexTokio>>(&rt, iters, None));
76+
});
77+
78+
group.bench_function("RwLockTokio", |b| {
79+
b.to_async(&rt)
80+
.iter_custom(|iters| asyn::add_multiple_torrents_in_parallel::<Arc<TorrentsRwLockTokio>>(&rt, iters, None));
81+
});
82+
83+
group.bench_function("RwLockTokioMutexStd", |b| {
84+
b.to_async(&rt)
85+
.iter_custom(|iters| asyn::add_multiple_torrents_in_parallel::<Arc<TorrentsRwLockTokioMutexStd>>(&rt, iters, None));
86+
});
87+
88+
group.bench_function("RwLockTokioMutexTokio", |b| {
89+
b.to_async(&rt)
90+
.iter_custom(|iters| asyn::add_multiple_torrents_in_parallel::<Arc<TorrentsRwLockTokioMutexTokio>>(&rt, iters, None));
91+
});
92+
93+
group.finish();
94+
}
95+
96+
fn update_one_torrent_in_parallel(c: &mut Criterion) {
97+
let rt = tokio::runtime::Builder::new_multi_thread().worker_threads(4).build().unwrap();
98+
99+
let mut group = c.benchmark_group("update_one_torrent_in_parallel");
100+
101+
//group.sampling_mode(criterion::SamplingMode::Flat);
102+
//group.sample_size(10);
103+
104+
group.warm_up_time(Duration::from_millis(500));
105+
group.measurement_time(Duration::from_millis(1000));
106+
107+
group.bench_function("RwLockStd", |b| {
108+
b.to_async(&rt)
109+
.iter_custom(|iters| sync::update_one_torrent_in_parallel::<Arc<TorrentsRwLockStd>>(&rt, iters, None));
110+
});
111+
112+
group.bench_function("RwLockStdMutexStd", |b| {
113+
b.to_async(&rt)
114+
.iter_custom(|iters| sync::update_one_torrent_in_parallel::<Arc<TorrentsRwLockStdMutexStd>>(&rt, iters, None));
115+
});
116+
117+
group.bench_function("RwLockStdMutexTokio", |b| {
118+
b.to_async(&rt)
119+
.iter_custom(|iters| asyn::update_one_torrent_in_parallel::<Arc<TorrentsRwLockStdMutexTokio>>(&rt, iters, None));
120+
});
121+
122+
group.bench_function("RwLockTokio", |b| {
123+
b.to_async(&rt)
124+
.iter_custom(|iters| asyn::update_one_torrent_in_parallel::<Arc<TorrentsRwLockTokio>>(&rt, iters, None));
125+
});
126+
127+
group.bench_function("RwLockTokioMutexStd", |b| {
128+
b.to_async(&rt)
129+
.iter_custom(|iters| asyn::update_one_torrent_in_parallel::<Arc<TorrentsRwLockTokioMutexStd>>(&rt, iters, None));
130+
});
131+
132+
group.bench_function("RwLockTokioMutexTokio", |b| {
133+
b.to_async(&rt)
134+
.iter_custom(|iters| asyn::update_one_torrent_in_parallel::<Arc<TorrentsRwLockTokioMutexTokio>>(&rt, iters, None));
135+
});
136+
137+
group.finish();
138+
}
139+
140+
fn update_multiple_torrents_in_parallel(c: &mut Criterion) {
141+
let rt = tokio::runtime::Builder::new_multi_thread().worker_threads(4).build().unwrap();
142+
143+
let mut group = c.benchmark_group("update_multiple_torrents_in_parallel");
144+
145+
//group.sampling_mode(criterion::SamplingMode::Flat);
146+
//group.sample_size(10);
147+
148+
group.warm_up_time(Duration::from_millis(500));
149+
group.measurement_time(Duration::from_millis(1000));
150+
151+
group.bench_function("RwLockStd", |b| {
152+
b.to_async(&rt)
153+
.iter_custom(|iters| sync::update_multiple_torrents_in_parallel::<Arc<TorrentsRwLockStd>>(&rt, iters, None));
154+
});
155+
156+
group.bench_function("RwLockStdMutexStd", |b| {
157+
b.to_async(&rt)
158+
.iter_custom(|iters| sync::update_multiple_torrents_in_parallel::<Arc<TorrentsRwLockStdMutexStd>>(&rt, iters, None));
159+
});
160+
161+
group.bench_function("RwLockStdMutexTokio", |b| {
162+
b.to_async(&rt).iter_custom(|iters| {
163+
asyn::update_multiple_torrents_in_parallel::<Arc<TorrentsRwLockStdMutexTokio>>(&rt, iters, None)
164+
});
165+
});
166+
167+
group.bench_function("RwLockTokio", |b| {
168+
b.to_async(&rt)
169+
.iter_custom(|iters| asyn::update_multiple_torrents_in_parallel::<Arc<TorrentsRwLockTokio>>(&rt, iters, None));
170+
});
171+
172+
group.bench_function("RwLockTokioMutexStd", |b| {
173+
b.to_async(&rt).iter_custom(|iters| {
174+
asyn::update_multiple_torrents_in_parallel::<Arc<TorrentsRwLockTokioMutexStd>>(&rt, iters, None)
175+
});
176+
});
177+
178+
group.bench_function("RwLockTokioMutexTokio", |b| {
179+
b.to_async(&rt).iter_custom(|iters| {
180+
asyn::update_multiple_torrents_in_parallel::<Arc<TorrentsRwLockTokioMutexTokio>>(&rt, iters, None)
181+
});
182+
});
183+
184+
group.finish();
185+
}
186+
187+
criterion_group!(
188+
benches,
189+
add_one_torrent,
190+
add_multiple_torrents_in_parallel,
191+
update_one_torrent_in_parallel,
192+
update_multiple_torrents_in_parallel
193+
);
194+
criterion_main!(benches);

‎src/console/clients/checker/config.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use std::net::SocketAddr;
44

55
use reqwest::Url as ServiceUrl;
66
use serde::Deserialize;
7-
use url;
87

98
/// It parses the configuration from a JSON format.
109
///
@@ -88,7 +87,7 @@ impl TryFrom<PlainConfiguration> for Configuration {
8887

8988
#[cfg(test)]
9089
mod tests {
91-
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
90+
use std::net::{IpAddr, Ipv4Addr};
9291

9392
use super::*;
9493

‎src/servers/apis/v1/context/auth_key/resources.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//! API resources for the [`auth_key`](crate::servers::apis::v1::context::auth_key) API context.
2-
use std::convert::From;
32
43
use serde::{Deserialize, Serialize};
54

‎src/servers/http/v1/responses/error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
//! code.
1414
use axum::http::StatusCode;
1515
use axum::response::{IntoResponse, Response};
16-
use serde::{self, Serialize};
16+
use serde::Serialize;
1717

1818
/// `Error` response for the [`HTTP tracker`](crate::servers::http).
1919
#[derive(Serialize, Debug, PartialEq)]

‎src/servers/udp/error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub enum Error {
1313
source: LocatedError<'static, dyn std::error::Error + Send + Sync>,
1414
},
1515

16-
/// Error returned from a third-party library (aquatic_udp_protocol).
16+
/// Error returned from a third-party library (`aquatic_udp_protocol`).
1717
#[error("internal server error: {message}, {location}")]
1818
InternalServer {
1919
location: &'static Location<'static>,

‎src/shared/bit_torrent/tracker/http/client/requests/scrape.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::convert::TryFrom;
21
use std::error::Error;
32
use std::fmt::{self};
43
use std::str::FromStr;

‎src/shared/bit_torrent/tracker/http/client/responses/announce.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
22

3-
use serde::{self, Deserialize, Serialize};
3+
use serde::{Deserialize, Serialize};
44
use torrust_tracker_primitives::peer;
55

66
#[derive(Serialize, Deserialize, Debug, PartialEq)]

‎src/shared/bit_torrent/tracker/http/client/responses/error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use serde::{self, Deserialize, Serialize};
1+
use serde::{Deserialize, Serialize};
22

33
#[derive(Serialize, Deserialize, Debug, PartialEq)]
44
pub struct Error {

‎src/shared/bit_torrent/tracker/http/client/responses/scrape.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::fmt::Write;
33
use std::str;
44

55
use serde::ser::SerializeMap;
6-
use serde::{self, Deserialize, Serialize, Serializer};
6+
use serde::{Deserialize, Serialize, Serializer};
77
use serde_bencode::value::Value;
88

99
use crate::shared::bit_torrent::tracker::http::{ByteArray20, InfoHash};

‎src/shared/crypto/keys.rs

-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ pub mod seeds {
8686

8787
#[cfg(test)]
8888
mod tests {
89-
use std::convert::TryInto;
9089

9190
use crate::shared::crypto::ephemeral_instance_keys::RANDOM_SEED;
9291
use crate::shared::crypto::keys::seeds::detail::ZEROED_TEST_SEED;

‎tests/servers/http/responses/announce.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
22

3-
use serde::{self, Deserialize, Serialize};
3+
use serde::{Deserialize, Serialize};
44
use torrust_tracker_primitives::peer;
55

66
#[derive(Serialize, Deserialize, Debug, PartialEq)]

‎tests/servers/http/responses/error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use serde::{self, Deserialize, Serialize};
1+
use serde::{Deserialize, Serialize};
22

33
#[derive(Serialize, Deserialize, Debug, PartialEq)]
44
pub struct Error {

‎tests/servers/http/responses/scrape.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::collections::HashMap;
22
use std::str;
33

4-
use serde::{self, Deserialize, Serialize};
4+
use serde::{Deserialize, Serialize};
55
use serde_bencode::value::Value;
66

77
use crate::servers::http::{ByteArray20, InfoHash};

0 commit comments

Comments
 (0)
Please sign in to comment.