forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsync.rs
More file actions
166 lines (118 loc) · 5.34 KB
/
Copy pathsync.rs
File metadata and controls
166 lines (118 loc) · 5.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
use std::sync::Arc;
use std::time::Duration;
use clap::Parser;
use futures::stream::FuturesUnordered;
use torrust_tracker::core::torrent::repository::Repository;
use torrust_tracker::shared::bit_torrent::info_hash::InfoHash;
use crate::args::Args;
use crate::benches::utils::{generate_unique_info_hashes, get_average_and_adjusted_average_from_results, DEFAULT_PEER};
// Simply add one torrent
#[must_use]
pub fn add_one_torrent<T: Repository + Send + Sync + 'static>(samples: usize) -> (Duration, Duration) {
let mut results: Vec<Duration> = Vec::with_capacity(samples);
for _ in 0..samples {
let torrent_repository = Arc::new(T::new());
let info_hash = InfoHash([0; 20]);
let start_time = std::time::Instant::now();
torrent_repository.update_torrent_with_peer_and_get_stats(&info_hash, &DEFAULT_PEER);
let result = start_time.elapsed();
results.push(result);
}
get_average_and_adjusted_average_from_results(results)
}
// Add one torrent ten thousand times in parallel (depending on the set worker threads)
pub async fn update_one_torrent_in_parallel<T: Repository + Send + Sync + 'static>(
runtime: &tokio::runtime::Runtime,
samples: usize,
) -> (Duration, Duration) {
let args = Args::parse();
let mut results: Vec<Duration> = Vec::with_capacity(samples);
for _ in 0..samples {
let torrent_repository = Arc::new(T::new());
let info_hash: &'static InfoHash = &InfoHash([0; 20]);
let handles = FuturesUnordered::new();
// Add the torrent/peer to the torrent repository
torrent_repository.update_torrent_with_peer_and_get_stats(info_hash, &DEFAULT_PEER);
let start_time = std::time::Instant::now();
for _ in 0..10_000 {
let torrent_repository_clone = torrent_repository.clone();
let handle = runtime.spawn(async move {
torrent_repository_clone.update_torrent_with_peer_and_get_stats(info_hash, &DEFAULT_PEER);
if let Some(sleep_time) = args.sleep {
let start_time = std::time::Instant::now();
while start_time.elapsed().as_nanos() < u128::from(sleep_time) {}
}
});
handles.push(handle);
}
// Await all tasks
futures::future::join_all(handles).await;
let result = start_time.elapsed();
results.push(result);
}
get_average_and_adjusted_average_from_results(results)
}
// Add ten thousand torrents in parallel (depending on the set worker threads)
pub async fn add_multiple_torrents_in_parallel<T: Repository + Send + Sync + 'static>(
runtime: &tokio::runtime::Runtime,
samples: usize,
) -> (Duration, Duration) {
let args = Args::parse();
let mut results: Vec<Duration> = Vec::with_capacity(samples);
for _ in 0..samples {
let torrent_repository = Arc::new(T::new());
let info_hashes = generate_unique_info_hashes(10_000);
let handles = FuturesUnordered::new();
let start_time = std::time::Instant::now();
for info_hash in info_hashes {
let torrent_repository_clone = torrent_repository.clone();
let handle = runtime.spawn(async move {
torrent_repository_clone.update_torrent_with_peer_and_get_stats(&info_hash, &DEFAULT_PEER);
if let Some(sleep_time) = args.sleep {
let start_time = std::time::Instant::now();
while start_time.elapsed().as_nanos() < u128::from(sleep_time) {}
}
});
handles.push(handle);
}
// Await all tasks
futures::future::join_all(handles).await;
let result = start_time.elapsed();
results.push(result);
}
get_average_and_adjusted_average_from_results(results)
}
// Update ten thousand torrents in parallel (depending on the set worker threads)
pub async fn update_multiple_torrents_in_parallel<T: Repository + Send + Sync + 'static>(
runtime: &tokio::runtime::Runtime,
samples: usize,
) -> (Duration, Duration) {
let args = Args::parse();
let mut results: Vec<Duration> = Vec::with_capacity(samples);
for _ in 0..samples {
let torrent_repository = Arc::new(T::new());
let info_hashes = generate_unique_info_hashes(10_000);
let handles = FuturesUnordered::new();
// Add the torrents/peers to the torrent repository
for info_hash in &info_hashes {
torrent_repository.update_torrent_with_peer_and_get_stats(info_hash, &DEFAULT_PEER);
}
let start_time = std::time::Instant::now();
for info_hash in info_hashes {
let torrent_repository_clone = torrent_repository.clone();
let handle = runtime.spawn(async move {
torrent_repository_clone.update_torrent_with_peer_and_get_stats(&info_hash, &DEFAULT_PEER);
if let Some(sleep_time) = args.sleep {
let start_time = std::time::Instant::now();
while start_time.elapsed().as_nanos() < u128::from(sleep_time) {}
}
});
handles.push(handle);
}
// Await all tasks
futures::future::join_all(handles).await;
let result = start_time.elapsed();
results.push(result);
}
get_average_and_adjusted_average_from_results(results)
}