|
| 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