|
| 1 | +use std::sync::Arc; |
| 2 | +use std::time::{Duration, Instant}; |
| 3 | + |
| 4 | +use bittorrent_primitives::info_hash::InfoHash; |
| 5 | +use futures::stream::FuturesUnordered; |
| 6 | +use torrust_tracker_torrent_repository_benchmarking::repository::RepositoryAsync; |
| 7 | + |
| 8 | +use super::utils::{generate_unique_info_hashes, DEFAULT_PEER}; |
| 9 | + |
| 10 | +pub async fn add_one_torrent<V, T>(samples: u64) -> Duration |
| 11 | +where |
| 12 | + V: RepositoryAsync<T> + Default, |
| 13 | +{ |
| 14 | + let start = Instant::now(); |
| 15 | + |
| 16 | + for _ in 0..samples { |
| 17 | + let torrent_repository = V::default(); |
| 18 | + |
| 19 | + let info_hash = InfoHash::default(); |
| 20 | + |
| 21 | + torrent_repository.upsert_peer(&info_hash, &DEFAULT_PEER, None).await; |
| 22 | + |
| 23 | + torrent_repository.get_swarm_metadata(&info_hash).await; |
| 24 | + } |
| 25 | + |
| 26 | + start.elapsed() |
| 27 | +} |
| 28 | + |
| 29 | +// Add one torrent ten thousand times in parallel (depending on the set worker threads) |
| 30 | +pub async fn update_one_torrent_in_parallel<V, T>(runtime: &tokio::runtime::Runtime, samples: u64, sleep: Option<u64>) -> Duration |
| 31 | +where |
| 32 | + V: RepositoryAsync<T> + Default, |
| 33 | + Arc<V>: Clone + Send + Sync + 'static, |
| 34 | +{ |
| 35 | + let torrent_repository = Arc::<V>::default(); |
| 36 | + let info_hash = InfoHash::default(); |
| 37 | + let handles = FuturesUnordered::new(); |
| 38 | + |
| 39 | + // Add the torrent/peer to the torrent repository |
| 40 | + torrent_repository.upsert_peer(&info_hash, &DEFAULT_PEER, None).await; |
| 41 | + |
| 42 | + torrent_repository.get_swarm_metadata(&info_hash).await; |
| 43 | + |
| 44 | + let start = Instant::now(); |
| 45 | + |
| 46 | + for _ in 0..samples { |
| 47 | + let torrent_repository_clone = torrent_repository.clone(); |
| 48 | + |
| 49 | + let handle = runtime.spawn(async move { |
| 50 | + torrent_repository_clone.upsert_peer(&info_hash, &DEFAULT_PEER, None).await; |
| 51 | + |
| 52 | + torrent_repository_clone.get_swarm_metadata(&info_hash).await; |
| 53 | + |
| 54 | + if let Some(sleep_time) = sleep { |
| 55 | + let start_time = std::time::Instant::now(); |
| 56 | + |
| 57 | + while start_time.elapsed().as_nanos() < u128::from(sleep_time) {} |
| 58 | + } |
| 59 | + }); |
| 60 | + |
| 61 | + handles.push(handle); |
| 62 | + } |
| 63 | + |
| 64 | + // Await all tasks |
| 65 | + futures::future::join_all(handles).await; |
| 66 | + |
| 67 | + start.elapsed() |
| 68 | +} |
| 69 | + |
| 70 | +// Add ten thousand torrents in parallel (depending on the set worker threads) |
| 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 |
| 76 | +where |
| 77 | + V: RepositoryAsync<T> + Default, |
| 78 | + Arc<V>: Clone + Send + Sync + 'static, |
| 79 | +{ |
| 80 | + let torrent_repository = Arc::<V>::default(); |
| 81 | + let info_hashes = generate_unique_info_hashes(samples.try_into().expect("it should fit in a usize")); |
| 82 | + let handles = FuturesUnordered::new(); |
| 83 | + |
| 84 | + let start = Instant::now(); |
| 85 | + |
| 86 | + for info_hash in info_hashes { |
| 87 | + let torrent_repository_clone = torrent_repository.clone(); |
| 88 | + |
| 89 | + let handle = runtime.spawn(async move { |
| 90 | + torrent_repository_clone.upsert_peer(&info_hash, &DEFAULT_PEER, None).await; |
| 91 | + |
| 92 | + torrent_repository_clone.get_swarm_metadata(&info_hash).await; |
| 93 | + |
| 94 | + if let Some(sleep_time) = sleep { |
| 95 | + let start_time = std::time::Instant::now(); |
| 96 | + |
| 97 | + while start_time.elapsed().as_nanos() < u128::from(sleep_time) {} |
| 98 | + } |
| 99 | + }); |
| 100 | + |
| 101 | + handles.push(handle); |
| 102 | + } |
| 103 | + |
| 104 | + // Await all tasks |
| 105 | + futures::future::join_all(handles).await; |
| 106 | + |
| 107 | + start.elapsed() |
| 108 | +} |
| 109 | + |
| 110 | +// Async update ten thousand torrents in parallel (depending on the set worker threads) |
| 111 | +pub async fn update_multiple_torrents_in_parallel<V, T>( |
| 112 | + runtime: &tokio::runtime::Runtime, |
| 113 | + samples: u64, |
| 114 | + sleep: Option<u64>, |
| 115 | +) -> Duration |
| 116 | +where |
| 117 | + V: RepositoryAsync<T> + Default, |
| 118 | + Arc<V>: Clone + Send + Sync + 'static, |
| 119 | +{ |
| 120 | + let torrent_repository = Arc::<V>::default(); |
| 121 | + let info_hashes = generate_unique_info_hashes(samples.try_into().expect("it should fit in usize")); |
| 122 | + let handles = FuturesUnordered::new(); |
| 123 | + |
| 124 | + // Add the torrents/peers to the torrent repository |
| 125 | + for info_hash in &info_hashes { |
| 126 | + torrent_repository.upsert_peer(info_hash, &DEFAULT_PEER, None).await; |
| 127 | + torrent_repository.get_swarm_metadata(info_hash).await; |
| 128 | + } |
| 129 | + |
| 130 | + let start = Instant::now(); |
| 131 | + |
| 132 | + for info_hash in info_hashes { |
| 133 | + let torrent_repository_clone = torrent_repository.clone(); |
| 134 | + |
| 135 | + let handle = runtime.spawn(async move { |
| 136 | + torrent_repository_clone.upsert_peer(&info_hash, &DEFAULT_PEER, None).await; |
| 137 | + torrent_repository_clone.get_swarm_metadata(&info_hash).await; |
| 138 | + |
| 139 | + if let Some(sleep_time) = sleep { |
| 140 | + let start_time = std::time::Instant::now(); |
| 141 | + |
| 142 | + while start_time.elapsed().as_nanos() < u128::from(sleep_time) {} |
| 143 | + } |
| 144 | + }); |
| 145 | + |
| 146 | + handles.push(handle); |
| 147 | + } |
| 148 | + |
| 149 | + // Await all tasks |
| 150 | + futures::future::join_all(handles).await; |
| 151 | + |
| 152 | + start.elapsed() |
| 153 | +} |
0 commit comments