|
1 | | -use std::time::Duration; |
| 1 | +use std::time::{Duration, Instant}; |
2 | 2 |
|
3 | | -use clap::Parser; |
4 | 3 | use futures::stream::FuturesUnordered; |
5 | 4 | use torrust_tracker_primitives::info_hash::InfoHash; |
6 | 5 | use torrust_tracker_torrent_repository::repository::UpdateTorrentAsync; |
7 | 6 |
|
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}; |
10 | 8 |
|
11 | | -pub async fn add_one_torrent<V>(samples: usize) -> (Duration, Duration) |
| 9 | +pub async fn add_one_torrent<V>(samples: u64) -> Duration |
12 | 10 | where |
13 | 11 | V: UpdateTorrentAsync + Default, |
14 | 12 | { |
15 | | - let mut results: Vec<Duration> = Vec::with_capacity(samples); |
| 13 | + let start = Instant::now(); |
16 | 14 |
|
17 | 15 | for _ in 0..samples { |
18 | 16 | let torrent_repository = V::default(); |
19 | 17 |
|
20 | 18 | let info_hash = InfoHash([0; 20]); |
21 | 19 |
|
22 | | - let start_time = std::time::Instant::now(); |
23 | | - |
24 | 20 | torrent_repository |
25 | 21 | .update_torrent_with_peer_and_get_stats(&info_hash, &DEFAULT_PEER) |
26 | 22 | .await; |
27 | | - |
28 | | - let result = start_time.elapsed(); |
29 | | - |
30 | | - results.push(result); |
31 | 23 | } |
32 | 24 |
|
33 | | - get_average_and_adjusted_average_from_results(results) |
| 25 | + start.elapsed() |
34 | 26 | } |
35 | 27 |
|
36 | 28 | // 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 |
38 | 30 | where |
39 | 31 | V: UpdateTorrentAsync + Default + Clone + Send + Sync + 'static, |
40 | 32 | { |
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(); |
53 | 36 |
|
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; |
55 | 41 |
|
56 | | - for _ in 0..10_000 { |
57 | | - let torrent_repository_clone = torrent_repository.clone(); |
| 42 | + let start = Instant::now(); |
58 | 43 |
|
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(); |
70 | 46 |
|
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; |
73 | 51 |
|
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(); |
76 | 54 |
|
77 | | - let result = start_time.elapsed(); |
| 55 | + while start_time.elapsed().as_nanos() < u128::from(sleep_time) {} |
| 56 | + } |
| 57 | + }); |
78 | 58 |
|
79 | | - results.push(result); |
| 59 | + handles.push(handle); |
80 | 60 | } |
81 | 61 |
|
82 | | - get_average_and_adjusted_average_from_results(results) |
| 62 | + // Await all tasks |
| 63 | + futures::future::join_all(handles).await; |
| 64 | + |
| 65 | + start.elapsed() |
83 | 66 | } |
84 | 67 |
|
85 | 68 | // 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 |
87 | 70 | where |
88 | 71 | V: UpdateTorrentAsync + Default + Clone + Send + Sync + 'static, |
89 | 72 | { |
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(); |
97 | 76 |
|
98 | | - let start_time = std::time::Instant::now(); |
| 77 | + let start = Instant::now(); |
99 | 78 |
|
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(); |
102 | 81 |
|
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; |
117 | 86 |
|
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(); |
120 | 89 |
|
121 | | - let result = start_time.elapsed(); |
| 90 | + while start_time.elapsed().as_nanos() < u128::from(sleep_time) {} |
| 91 | + } |
| 92 | + }); |
122 | 93 |
|
123 | | - results.push(result); |
| 94 | + handles.push(handle); |
124 | 95 | } |
125 | 96 |
|
126 | | - get_average_and_adjusted_average_from_results(results) |
| 97 | + // Await all tasks |
| 98 | + futures::future::join_all(handles).await; |
| 99 | + |
| 100 | + start.elapsed() |
127 | 101 | } |
128 | 102 |
|
129 | 103 | // 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 |
131 | 109 | where |
132 | 110 | V: UpdateTorrentAsync + Default + Clone + Send + Sync + 'static, |
133 | 111 | { |
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(); |
136 | 115 |
|
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 | + } |
158 | 122 |
|
159 | | - if let Some(sleep_time) = args.sleep { |
160 | | - let start_time = std::time::Instant::now(); |
| 123 | + let start = Instant::now(); |
161 | 124 |
|
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(); |
165 | 127 |
|
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; |
168 | 132 |
|
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(); |
171 | 135 |
|
172 | | - let result = start_time.elapsed(); |
| 136 | + while start_time.elapsed().as_nanos() < u128::from(sleep_time) {} |
| 137 | + } |
| 138 | + }); |
173 | 139 |
|
174 | | - results.push(result); |
| 140 | + handles.push(handle); |
175 | 141 | } |
176 | 142 |
|
177 | | - get_average_and_adjusted_average_from_results(results) |
| 143 | + // Await all tasks |
| 144 | + futures::future::join_all(handles).await; |
| 145 | + |
| 146 | + start.elapsed() |
178 | 147 | } |
0 commit comments