Skip to content

Commit 65bc3c1

Browse files
committed
dev: more work on the torrent repository package
1 parent fe80703 commit 65bc3c1

22 files changed

+472
-494
lines changed

.vscode/settings.json

+1-5
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,5 @@
3131
"evenBetterToml.formatter.trailingNewline": true,
3232
"evenBetterToml.formatter.reorderKeys": true,
3333
"evenBetterToml.formatter.reorderArrays": true,
34-
"rust-analyzer.linkedProjects": [
35-
"./packages/torrent-repository/Cargo.toml",
36-
"./packages/primitives/Cargo.toml",
37-
"./packages/primitives/Cargo.toml"
38-
],
34+
3935
}

packages/torrent-repository/benches/helpers/asyn.rs

+20-12
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1+
use std::sync::Arc;
12
use std::time::{Duration, Instant};
23

34
use futures::stream::FuturesUnordered;
45
use torrust_tracker_primitives::info_hash::InfoHash;
5-
use torrust_tracker_torrent_repository::repository::UpdateTorrentAsync;
6+
use torrust_tracker_torrent_repository::repository::RepositoryAsync;
67

78
use super::utils::{generate_unique_info_hashes, DEFAULT_PEER};
89

9-
pub async fn add_one_torrent<V>(samples: u64) -> Duration
10+
pub async fn add_one_torrent<V, T>(samples: u64) -> Duration
1011
where
11-
V: UpdateTorrentAsync + Default,
12+
V: RepositoryAsync<T> + Default,
1213
{
1314
let start = Instant::now();
1415

@@ -26,11 +27,12 @@ where
2627
}
2728

2829
// Add one torrent ten thousand times in parallel (depending on the set worker threads)
29-
pub async fn update_one_torrent_in_parallel<V>(runtime: &tokio::runtime::Runtime, samples: u64, sleep: Option<u64>) -> Duration
30+
pub async fn update_one_torrent_in_parallel<V, T>(runtime: &tokio::runtime::Runtime, samples: u64, sleep: Option<u64>) -> Duration
3031
where
31-
V: UpdateTorrentAsync + Default + Clone + Send + Sync + 'static,
32+
V: RepositoryAsync<T> + Default,
33+
Arc<V>: Clone + Send + Sync + 'static,
3234
{
33-
let torrent_repository = V::default();
35+
let torrent_repository = Arc::<V>::default();
3436
let info_hash: &'static InfoHash = &InfoHash([0; 20]);
3537
let handles = FuturesUnordered::new();
3638

@@ -66,11 +68,16 @@ where
6668
}
6769

6870
// Add ten thousand torrents in parallel (depending on the set worker threads)
69-
pub async fn add_multiple_torrents_in_parallel<V>(runtime: &tokio::runtime::Runtime, samples: u64, sleep: Option<u64>) -> Duration
71+
pub async fn add_multiple_torrents_in_parallel<V, T>(
72+
runtime: &tokio::runtime::Runtime,
73+
samples: u64,
74+
sleep: Option<u64>,
75+
) -> Duration
7076
where
71-
V: UpdateTorrentAsync + Default + Clone + Send + Sync + 'static,
77+
V: RepositoryAsync<T> + Default,
78+
Arc<V>: Clone + Send + Sync + 'static,
7279
{
73-
let torrent_repository = V::default();
80+
let torrent_repository = Arc::<V>::default();
7481
let info_hashes = generate_unique_info_hashes(samples.try_into().expect("it should fit in a usize"));
7582
let handles = FuturesUnordered::new();
7683

@@ -101,15 +108,16 @@ where
101108
}
102109

103110
// Async update ten thousand torrents in parallel (depending on the set worker threads)
104-
pub async fn update_multiple_torrents_in_parallel<V>(
111+
pub async fn update_multiple_torrents_in_parallel<V, T>(
105112
runtime: &tokio::runtime::Runtime,
106113
samples: u64,
107114
sleep: Option<u64>,
108115
) -> Duration
109116
where
110-
V: UpdateTorrentAsync + Default + Clone + Send + Sync + 'static,
117+
V: RepositoryAsync<T> + Default,
118+
Arc<V>: Clone + Send + Sync + 'static,
111119
{
112-
let torrent_repository = V::default();
120+
let torrent_repository = Arc::<V>::default();
113121
let info_hashes = generate_unique_info_hashes(samples.try_into().expect("it should fit in usize"));
114122
let handles = FuturesUnordered::new();
115123

packages/torrent-repository/benches/helpers/sync.rs

+20-12
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1+
use std::sync::Arc;
12
use std::time::{Duration, Instant};
23

34
use futures::stream::FuturesUnordered;
45
use torrust_tracker_primitives::info_hash::InfoHash;
5-
use torrust_tracker_torrent_repository::repository::UpdateTorrentSync;
6+
use torrust_tracker_torrent_repository::repository::Repository;
67

78
use super::utils::{generate_unique_info_hashes, DEFAULT_PEER};
89

910
// Simply add one torrent
1011
#[must_use]
11-
pub fn add_one_torrent<V>(samples: u64) -> Duration
12+
pub fn add_one_torrent<V, T>(samples: u64) -> Duration
1213
where
13-
V: UpdateTorrentSync + Default,
14+
V: Repository<T> + Default,
1415
{
1516
let start = Instant::now();
1617

@@ -26,11 +27,12 @@ where
2627
}
2728

2829
// Add one torrent ten thousand times in parallel (depending on the set worker threads)
29-
pub async fn update_one_torrent_in_parallel<V>(runtime: &tokio::runtime::Runtime, samples: u64, sleep: Option<u64>) -> Duration
30+
pub async fn update_one_torrent_in_parallel<V, T>(runtime: &tokio::runtime::Runtime, samples: u64, sleep: Option<u64>) -> Duration
3031
where
31-
V: UpdateTorrentSync + Default + Clone + Send + Sync + 'static,
32+
V: Repository<T> + Default,
33+
Arc<V>: Clone + Send + Sync + 'static,
3234
{
33-
let torrent_repository = V::default();
35+
let torrent_repository = Arc::<V>::default();
3436
let info_hash: &'static InfoHash = &InfoHash([0; 20]);
3537
let handles = FuturesUnordered::new();
3638

@@ -62,11 +64,16 @@ where
6264
}
6365

6466
// Add ten thousand torrents in parallel (depending on the set worker threads)
65-
pub async fn add_multiple_torrents_in_parallel<V>(runtime: &tokio::runtime::Runtime, samples: u64, sleep: Option<u64>) -> Duration
67+
pub async fn add_multiple_torrents_in_parallel<V, T>(
68+
runtime: &tokio::runtime::Runtime,
69+
samples: u64,
70+
sleep: Option<u64>,
71+
) -> Duration
6672
where
67-
V: UpdateTorrentSync + Default + Clone + Send + Sync + 'static,
73+
V: Repository<T> + Default,
74+
Arc<V>: Clone + Send + Sync + 'static,
6875
{
69-
let torrent_repository = V::default();
76+
let torrent_repository = Arc::<V>::default();
7077
let info_hashes = generate_unique_info_hashes(samples.try_into().expect("it should fit in a usize"));
7178
let handles = FuturesUnordered::new();
7279

@@ -95,15 +102,16 @@ where
95102
}
96103

97104
// Update ten thousand torrents in parallel (depending on the set worker threads)
98-
pub async fn update_multiple_torrents_in_parallel<V>(
105+
pub async fn update_multiple_torrents_in_parallel<V, T>(
99106
runtime: &tokio::runtime::Runtime,
100107
samples: u64,
101108
sleep: Option<u64>,
102109
) -> Duration
103110
where
104-
V: UpdateTorrentSync + Default + Clone + Send + Sync + 'static,
111+
V: Repository<T> + Default,
112+
Arc<V>: Clone + Send + Sync + 'static,
105113
{
106-
let torrent_repository = V::default();
114+
let torrent_repository = Arc::<V>::default();
107115
let info_hashes = generate_unique_info_hashes(samples.try_into().expect("it should fit in usize"));
108116
let handles = FuturesUnordered::new();
109117

packages/torrent-repository/benches/repository_benchmark.rs

+26-29
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::sync::Arc;
21
use std::time::Duration;
32

43
mod helpers;
@@ -20,30 +19,30 @@ fn add_one_torrent(c: &mut Criterion) {
2019
group.measurement_time(Duration::from_millis(1000));
2120

2221
group.bench_function("RwLockStd", |b| {
23-
b.iter_custom(sync::add_one_torrent::<Arc<TorrentsRwLockStd>>);
22+
b.iter_custom(sync::add_one_torrent::<TorrentsRwLockStd, _>);
2423
});
2524

2625
group.bench_function("RwLockStdMutexStd", |b| {
27-
b.iter_custom(sync::add_one_torrent::<Arc<TorrentsRwLockStdMutexStd>>);
26+
b.iter_custom(sync::add_one_torrent::<TorrentsRwLockStdMutexStd, _>);
2827
});
2928

3029
group.bench_function("RwLockStdMutexTokio", |b| {
3130
b.to_async(&rt)
32-
.iter_custom(asyn::add_one_torrent::<Arc<TorrentsRwLockStdMutexTokio>>);
31+
.iter_custom(asyn::add_one_torrent::<TorrentsRwLockStdMutexTokio, _>);
3332
});
3433

3534
group.bench_function("RwLockTokio", |b| {
36-
b.to_async(&rt).iter_custom(asyn::add_one_torrent::<Arc<TorrentsRwLockTokio>>);
35+
b.to_async(&rt).iter_custom(asyn::add_one_torrent::<TorrentsRwLockTokio, _>);
3736
});
3837

3938
group.bench_function("RwLockTokioMutexStd", |b| {
4039
b.to_async(&rt)
41-
.iter_custom(asyn::add_one_torrent::<Arc<TorrentsRwLockTokioMutexStd>>);
40+
.iter_custom(asyn::add_one_torrent::<TorrentsRwLockTokioMutexStd, _>);
4241
});
4342

4443
group.bench_function("RwLockTokioMutexTokio", |b| {
4544
b.to_async(&rt)
46-
.iter_custom(asyn::add_one_torrent::<Arc<TorrentsRwLockTokioMutexTokio>>);
45+
.iter_custom(asyn::add_one_torrent::<TorrentsRwLockTokioMutexTokio, _>);
4746
});
4847

4948
group.finish();
@@ -62,32 +61,32 @@ fn add_multiple_torrents_in_parallel(c: &mut Criterion) {
6261

6362
group.bench_function("RwLockStd", |b| {
6463
b.to_async(&rt)
65-
.iter_custom(|iters| sync::add_multiple_torrents_in_parallel::<Arc<TorrentsRwLockStd>>(&rt, iters, None));
64+
.iter_custom(|iters| sync::add_multiple_torrents_in_parallel::<TorrentsRwLockStd, _>(&rt, iters, None));
6665
});
6766

6867
group.bench_function("RwLockStdMutexStd", |b| {
6968
b.to_async(&rt)
70-
.iter_custom(|iters| sync::add_multiple_torrents_in_parallel::<Arc<TorrentsRwLockStdMutexStd>>(&rt, iters, None));
69+
.iter_custom(|iters| sync::add_multiple_torrents_in_parallel::<TorrentsRwLockStdMutexStd, _>(&rt, iters, None));
7170
});
7271

7372
group.bench_function("RwLockStdMutexTokio", |b| {
7473
b.to_async(&rt)
75-
.iter_custom(|iters| asyn::add_multiple_torrents_in_parallel::<Arc<TorrentsRwLockStdMutexTokio>>(&rt, iters, None));
74+
.iter_custom(|iters| asyn::add_multiple_torrents_in_parallel::<TorrentsRwLockStdMutexTokio, _>(&rt, iters, None));
7675
});
7776

7877
group.bench_function("RwLockTokio", |b| {
7978
b.to_async(&rt)
80-
.iter_custom(|iters| asyn::add_multiple_torrents_in_parallel::<Arc<TorrentsRwLockTokio>>(&rt, iters, None));
79+
.iter_custom(|iters| asyn::add_multiple_torrents_in_parallel::<TorrentsRwLockTokio, _>(&rt, iters, None));
8180
});
8281

8382
group.bench_function("RwLockTokioMutexStd", |b| {
8483
b.to_async(&rt)
85-
.iter_custom(|iters| asyn::add_multiple_torrents_in_parallel::<Arc<TorrentsRwLockTokioMutexStd>>(&rt, iters, None));
84+
.iter_custom(|iters| asyn::add_multiple_torrents_in_parallel::<TorrentsRwLockTokioMutexStd, _>(&rt, iters, None));
8685
});
8786

8887
group.bench_function("RwLockTokioMutexTokio", |b| {
8988
b.to_async(&rt)
90-
.iter_custom(|iters| asyn::add_multiple_torrents_in_parallel::<Arc<TorrentsRwLockTokioMutexTokio>>(&rt, iters, None));
89+
.iter_custom(|iters| asyn::add_multiple_torrents_in_parallel::<TorrentsRwLockTokioMutexTokio, _>(&rt, iters, None));
9190
});
9291

9392
group.finish();
@@ -106,32 +105,32 @@ fn update_one_torrent_in_parallel(c: &mut Criterion) {
106105

107106
group.bench_function("RwLockStd", |b| {
108107
b.to_async(&rt)
109-
.iter_custom(|iters| sync::update_one_torrent_in_parallel::<Arc<TorrentsRwLockStd>>(&rt, iters, None));
108+
.iter_custom(|iters| sync::update_one_torrent_in_parallel::<TorrentsRwLockStd, _>(&rt, iters, None));
110109
});
111110

112111
group.bench_function("RwLockStdMutexStd", |b| {
113112
b.to_async(&rt)
114-
.iter_custom(|iters| sync::update_one_torrent_in_parallel::<Arc<TorrentsRwLockStdMutexStd>>(&rt, iters, None));
113+
.iter_custom(|iters| sync::update_one_torrent_in_parallel::<TorrentsRwLockStdMutexStd, _>(&rt, iters, None));
115114
});
116115

117116
group.bench_function("RwLockStdMutexTokio", |b| {
118117
b.to_async(&rt)
119-
.iter_custom(|iters| asyn::update_one_torrent_in_parallel::<Arc<TorrentsRwLockStdMutexTokio>>(&rt, iters, None));
118+
.iter_custom(|iters| asyn::update_one_torrent_in_parallel::<TorrentsRwLockStdMutexTokio, _>(&rt, iters, None));
120119
});
121120

122121
group.bench_function("RwLockTokio", |b| {
123122
b.to_async(&rt)
124-
.iter_custom(|iters| asyn::update_one_torrent_in_parallel::<Arc<TorrentsRwLockTokio>>(&rt, iters, None));
123+
.iter_custom(|iters| asyn::update_one_torrent_in_parallel::<TorrentsRwLockTokio, _>(&rt, iters, None));
125124
});
126125

127126
group.bench_function("RwLockTokioMutexStd", |b| {
128127
b.to_async(&rt)
129-
.iter_custom(|iters| asyn::update_one_torrent_in_parallel::<Arc<TorrentsRwLockTokioMutexStd>>(&rt, iters, None));
128+
.iter_custom(|iters| asyn::update_one_torrent_in_parallel::<TorrentsRwLockTokioMutexStd, _>(&rt, iters, None));
130129
});
131130

132131
group.bench_function("RwLockTokioMutexTokio", |b| {
133132
b.to_async(&rt)
134-
.iter_custom(|iters| asyn::update_one_torrent_in_parallel::<Arc<TorrentsRwLockTokioMutexTokio>>(&rt, iters, None));
133+
.iter_custom(|iters| asyn::update_one_torrent_in_parallel::<TorrentsRwLockTokioMutexTokio, _>(&rt, iters, None));
135134
});
136135

137136
group.finish();
@@ -150,34 +149,32 @@ fn update_multiple_torrents_in_parallel(c: &mut Criterion) {
150149

151150
group.bench_function("RwLockStd", |b| {
152151
b.to_async(&rt)
153-
.iter_custom(|iters| sync::update_multiple_torrents_in_parallel::<Arc<TorrentsRwLockStd>>(&rt, iters, None));
152+
.iter_custom(|iters| sync::update_multiple_torrents_in_parallel::<TorrentsRwLockStd, _>(&rt, iters, None));
154153
});
155154

156155
group.bench_function("RwLockStdMutexStd", |b| {
157156
b.to_async(&rt)
158-
.iter_custom(|iters| sync::update_multiple_torrents_in_parallel::<Arc<TorrentsRwLockStdMutexStd>>(&rt, iters, None));
157+
.iter_custom(|iters| sync::update_multiple_torrents_in_parallel::<TorrentsRwLockStdMutexStd, _>(&rt, iters, None));
159158
});
160159

161160
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-
});
161+
b.to_async(&rt)
162+
.iter_custom(|iters| asyn::update_multiple_torrents_in_parallel::<TorrentsRwLockStdMutexTokio, _>(&rt, iters, None));
165163
});
166164

167165
group.bench_function("RwLockTokio", |b| {
168166
b.to_async(&rt)
169-
.iter_custom(|iters| asyn::update_multiple_torrents_in_parallel::<Arc<TorrentsRwLockTokio>>(&rt, iters, None));
167+
.iter_custom(|iters| asyn::update_multiple_torrents_in_parallel::<TorrentsRwLockTokio, _>(&rt, iters, None));
170168
});
171169

172170
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-
});
171+
b.to_async(&rt)
172+
.iter_custom(|iters| asyn::update_multiple_torrents_in_parallel::<TorrentsRwLockTokioMutexStd, _>(&rt, iters, None));
176173
});
177174

178175
group.bench_function("RwLockTokioMutexTokio", |b| {
179176
b.to_async(&rt).iter_custom(|iters| {
180-
asyn::update_multiple_torrents_in_parallel::<Arc<TorrentsRwLockTokioMutexTokio>>(&rt, iters, None)
177+
asyn::update_multiple_torrents_in_parallel::<TorrentsRwLockTokioMutexTokio, _>(&rt, iters, None)
181178
});
182179
});
183180

0 commit comments

Comments
 (0)