Skip to content

Commit

Permalink
dev: create torrent repo trait and extract entry
Browse files Browse the repository at this point in the history
  • Loading branch information
da2ce7 committed Mar 25, 2024
1 parent 48ce426 commit 1025125
Show file tree
Hide file tree
Showing 21 changed files with 1,222 additions and 747 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cSpell.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"Containerfile",
"curr",
"Cyberneering",
"dashmap",
"datagram",
"datetime",
"debuginfo",
Expand Down
19 changes: 10 additions & 9 deletions packages/torrent-repository-benchmarks/src/benches/asyn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ use std::time::Duration;

use clap::Parser;
use futures::stream::FuturesUnordered;
use torrust_tracker::core::torrent::repository_asyn::{RepositoryAsync, RepositoryTokioRwLock};
use torrust_tracker::core::torrent::UpdateTorrentAsync;
use torrust_tracker::core::torrent::repository::tokio_sync::RepositoryTokioRwLock;
use torrust_tracker::core::torrent::repository::UpdateTorrentAsync;
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};

pub async fn add_one_torrent<T>(samples: usize) -> (Duration, Duration)
where
RepositoryTokioRwLock<T>: RepositoryAsync<T> + UpdateTorrentAsync,
T: Default,
RepositoryTokioRwLock<T>: UpdateTorrentAsync + Default,
{
let mut results: Vec<Duration> = Vec::with_capacity(samples);

Expand All @@ -38,8 +39,8 @@ where
// Add one torrent ten thousand times in parallel (depending on the set worker threads)
pub async fn update_one_torrent_in_parallel<T>(runtime: &tokio::runtime::Runtime, samples: usize) -> (Duration, Duration)
where
T: Send + Sync + 'static,
RepositoryTokioRwLock<T>: RepositoryAsync<T> + UpdateTorrentAsync,
T: Default + Send + Sync + 'static,
RepositoryTokioRwLock<T>: UpdateTorrentAsync + Default,
{
let args = Args::parse();
let mut results: Vec<Duration> = Vec::with_capacity(samples);
Expand Down Expand Up @@ -88,8 +89,8 @@ where
// Add ten thousand torrents in parallel (depending on the set worker threads)
pub async fn add_multiple_torrents_in_parallel<T>(runtime: &tokio::runtime::Runtime, samples: usize) -> (Duration, Duration)
where
T: Send + Sync + 'static,
RepositoryTokioRwLock<T>: RepositoryAsync<T> + UpdateTorrentAsync,
T: Default + Send + Sync + 'static,
RepositoryTokioRwLock<T>: UpdateTorrentAsync + Default,
{
let args = Args::parse();
let mut results: Vec<Duration> = Vec::with_capacity(samples);
Expand Down Expand Up @@ -133,8 +134,8 @@ where
// Async update ten thousand torrents in parallel (depending on the set worker threads)
pub async fn update_multiple_torrents_in_parallel<T>(runtime: &tokio::runtime::Runtime, samples: usize) -> (Duration, Duration)
where
T: Send + Sync + 'static,
RepositoryTokioRwLock<T>: RepositoryAsync<T> + UpdateTorrentAsync,
T: Default + Send + Sync + 'static,
RepositoryTokioRwLock<T>: UpdateTorrentAsync + Default,
{
let args = Args::parse();
let mut results: Vec<Duration> = Vec::with_capacity(samples);
Expand Down
12 changes: 6 additions & 6 deletions packages/torrent-repository-benchmarks/src/benches/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::time::Duration;

use clap::Parser;
use futures::stream::FuturesUnordered;
use torrust_tracker::core::torrent::repository_sync::{RepositoryStdRwLock, RepositorySync};
use torrust_tracker::core::torrent::UpdateTorrentSync;
use torrust_tracker::core::torrent::repository::std_sync::RepositoryStdRwLock;
use torrust_tracker::core::torrent::repository::UpdateTorrentSync;
use torrust_tracker::shared::bit_torrent::info_hash::InfoHash;

use crate::args::Args;
Expand All @@ -14,7 +14,7 @@ use crate::benches::utils::{generate_unique_info_hashes, get_average_and_adjuste
#[must_use]
pub fn add_one_torrent<T>(samples: usize) -> (Duration, Duration)
where
RepositoryStdRwLock<T>: RepositorySync<T> + UpdateTorrentSync,
RepositoryStdRwLock<T>: UpdateTorrentSync + Default,
{
let mut results: Vec<Duration> = Vec::with_capacity(samples);

Expand All @@ -39,7 +39,7 @@ where
pub async fn update_one_torrent_in_parallel<T>(runtime: &tokio::runtime::Runtime, samples: usize) -> (Duration, Duration)
where
T: Send + Sync + 'static,
RepositoryStdRwLock<T>: RepositorySync<T> + UpdateTorrentSync,
RepositoryStdRwLock<T>: UpdateTorrentSync + Default,
{
let args = Args::parse();
let mut results: Vec<Duration> = Vec::with_capacity(samples);
Expand Down Expand Up @@ -85,7 +85,7 @@ where
pub async fn add_multiple_torrents_in_parallel<T>(runtime: &tokio::runtime::Runtime, samples: usize) -> (Duration, Duration)
where
T: Send + Sync + 'static,
RepositoryStdRwLock<T>: RepositorySync<T> + UpdateTorrentSync,
RepositoryStdRwLock<T>: UpdateTorrentSync + Default,
{
let args = Args::parse();
let mut results: Vec<Duration> = Vec::with_capacity(samples);
Expand Down Expand Up @@ -128,7 +128,7 @@ where
pub async fn update_multiple_torrents_in_parallel<T>(runtime: &tokio::runtime::Runtime, samples: usize) -> (Duration, Duration)
where
T: Send + Sync + 'static,
RepositoryStdRwLock<T>: RepositorySync<T> + UpdateTorrentSync,
RepositoryStdRwLock<T>: UpdateTorrentSync + Default,
{
let args = Args::parse();
let mut results: Vec<Duration> = Vec::with_capacity(samples);
Expand Down
12 changes: 6 additions & 6 deletions packages/torrent-repository-benchmarks/src/benches/sync_asyn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::time::Duration;

use clap::Parser;
use futures::stream::FuturesUnordered;
use torrust_tracker::core::torrent::repository_sync::{RepositoryStdRwLock, RepositorySync};
use torrust_tracker::core::torrent::UpdateTorrentAsync;
use torrust_tracker::core::torrent::repository::std_sync::RepositoryStdRwLock;
use torrust_tracker::core::torrent::repository::UpdateTorrentAsync;
use torrust_tracker::shared::bit_torrent::info_hash::InfoHash;

use crate::args::Args;
Expand All @@ -14,7 +14,7 @@ use crate::benches::utils::{generate_unique_info_hashes, get_average_and_adjuste
#[must_use]
pub async fn add_one_torrent<T>(samples: usize) -> (Duration, Duration)
where
RepositoryStdRwLock<T>: RepositorySync<T> + UpdateTorrentAsync,
RepositoryStdRwLock<T>: UpdateTorrentAsync + Default,
{
let mut results: Vec<Duration> = Vec::with_capacity(samples);

Expand All @@ -41,7 +41,7 @@ where
pub async fn update_one_torrent_in_parallel<T>(runtime: &tokio::runtime::Runtime, samples: usize) -> (Duration, Duration)
where
T: Send + Sync + 'static,
RepositoryStdRwLock<T>: RepositorySync<T> + UpdateTorrentAsync,
RepositoryStdRwLock<T>: UpdateTorrentAsync + Default,
{
let args = Args::parse();
let mut results: Vec<Duration> = Vec::with_capacity(samples);
Expand Down Expand Up @@ -91,7 +91,7 @@ where
pub async fn add_multiple_torrents_in_parallel<T>(runtime: &tokio::runtime::Runtime, samples: usize) -> (Duration, Duration)
where
T: Send + Sync + 'static,
RepositoryStdRwLock<T>: RepositorySync<T> + UpdateTorrentAsync,
RepositoryStdRwLock<T>: UpdateTorrentAsync + Default,
{
let args = Args::parse();
let mut results: Vec<Duration> = Vec::with_capacity(samples);
Expand Down Expand Up @@ -136,7 +136,7 @@ where
pub async fn update_multiple_torrents_in_parallel<T>(runtime: &tokio::runtime::Runtime, samples: usize) -> (Duration, Duration)
where
T: Send + Sync + 'static,
RepositoryStdRwLock<T>: RepositorySync<T> + UpdateTorrentAsync,
RepositoryStdRwLock<T>: UpdateTorrentAsync + Default,
{
let args = Args::parse();
let mut results: Vec<Duration> = Vec::with_capacity(samples);
Expand Down
34 changes: 17 additions & 17 deletions packages/torrent-repository-benchmarks/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use clap::Parser;
use torrust_torrent_repository_benchmarks::args::Args;
use torrust_torrent_repository_benchmarks::benches::{asyn, sync, sync_asyn};
use torrust_tracker::core::torrent::{Entry, EntryMutexStd, EntryMutexTokio};
use torrust_tracker::core::torrent::entry::{Entry, MutexStd, MutexTokio};

#[allow(clippy::too_many_lines)]
#[allow(clippy::print_literal)]
Expand Down Expand Up @@ -68,22 +68,22 @@ fn main() {
println!(
"{}: Avg/AdjAvg: {:?}",
"add_one_torrent",
sync::add_one_torrent::<EntryMutexStd>(1_000_000)
sync::add_one_torrent::<MutexStd>(1_000_000)
);
println!(
"{}: Avg/AdjAvg: {:?}",
"update_one_torrent_in_parallel",
rt.block_on(sync::update_one_torrent_in_parallel::<EntryMutexStd>(&rt, 10))
rt.block_on(sync::update_one_torrent_in_parallel::<MutexStd>(&rt, 10))
);
println!(
"{}: Avg/AdjAvg: {:?}",
"add_multiple_torrents_in_parallel",
rt.block_on(sync::add_multiple_torrents_in_parallel::<EntryMutexStd>(&rt, 10))
rt.block_on(sync::add_multiple_torrents_in_parallel::<MutexStd>(&rt, 10))
);
println!(
"{}: Avg/AdjAvg: {:?}",
"update_multiple_torrents_in_parallel",
rt.block_on(sync::update_multiple_torrents_in_parallel::<EntryMutexStd>(&rt, 10))
rt.block_on(sync::update_multiple_torrents_in_parallel::<MutexStd>(&rt, 10))
);

println!();
Expand All @@ -92,22 +92,22 @@ fn main() {
println!(
"{}: Avg/AdjAvg: {:?}",
"add_one_torrent",
rt.block_on(sync_asyn::add_one_torrent::<EntryMutexTokio>(1_000_000))
rt.block_on(sync_asyn::add_one_torrent::<MutexTokio>(1_000_000))
);
println!(
"{}: Avg/AdjAvg: {:?}",
"update_one_torrent_in_parallel",
rt.block_on(sync_asyn::update_one_torrent_in_parallel::<EntryMutexTokio>(&rt, 10))
rt.block_on(sync_asyn::update_one_torrent_in_parallel::<MutexTokio>(&rt, 10))
);
println!(
"{}: Avg/AdjAvg: {:?}",
"add_multiple_torrents_in_parallel",
rt.block_on(sync_asyn::add_multiple_torrents_in_parallel::<EntryMutexTokio>(&rt, 10))
rt.block_on(sync_asyn::add_multiple_torrents_in_parallel::<MutexTokio>(&rt, 10))
);
println!(
"{}: Avg/AdjAvg: {:?}",
"update_multiple_torrents_in_parallel",
rt.block_on(sync_asyn::update_multiple_torrents_in_parallel::<EntryMutexTokio>(&rt, 10))
rt.block_on(sync_asyn::update_multiple_torrents_in_parallel::<MutexTokio>(&rt, 10))
);

println!();
Expand All @@ -116,22 +116,22 @@ fn main() {
println!(
"{}: Avg/AdjAvg: {:?}",
"add_one_torrent",
rt.block_on(asyn::add_one_torrent::<EntryMutexStd>(1_000_000))
rt.block_on(asyn::add_one_torrent::<MutexStd>(1_000_000))
);
println!(
"{}: Avg/AdjAvg: {:?}",
"update_one_torrent_in_parallel",
rt.block_on(asyn::update_one_torrent_in_parallel::<EntryMutexStd>(&rt, 10))
rt.block_on(asyn::update_one_torrent_in_parallel::<MutexStd>(&rt, 10))
);
println!(
"{}: Avg/AdjAvg: {:?}",
"add_multiple_torrents_in_parallel",
rt.block_on(asyn::add_multiple_torrents_in_parallel::<EntryMutexStd>(&rt, 10))
rt.block_on(asyn::add_multiple_torrents_in_parallel::<MutexStd>(&rt, 10))
);
println!(
"{}: Avg/AdjAvg: {:?}",
"update_multiple_torrents_in_parallel",
rt.block_on(asyn::update_multiple_torrents_in_parallel::<EntryMutexStd>(&rt, 10))
rt.block_on(asyn::update_multiple_torrents_in_parallel::<MutexStd>(&rt, 10))
);

println!();
Expand All @@ -140,22 +140,22 @@ fn main() {
println!(
"{}: Avg/AdjAvg: {:?}",
"add_one_torrent",
rt.block_on(asyn::add_one_torrent::<EntryMutexTokio>(1_000_000))
rt.block_on(asyn::add_one_torrent::<MutexTokio>(1_000_000))
);
println!(
"{}: Avg/AdjAvg: {:?}",
"update_one_torrent_in_parallel",
rt.block_on(asyn::update_one_torrent_in_parallel::<EntryMutexTokio>(&rt, 10))
rt.block_on(asyn::update_one_torrent_in_parallel::<MutexTokio>(&rt, 10))
);
println!(
"{}: Avg/AdjAvg: {:?}",
"add_multiple_torrents_in_parallel",
rt.block_on(asyn::add_multiple_torrents_in_parallel::<EntryMutexTokio>(&rt, 10))
rt.block_on(asyn::add_multiple_torrents_in_parallel::<MutexTokio>(&rt, 10))
);
println!(
"{}: Avg/AdjAvg: {:?}",
"update_multiple_torrents_in_parallel",
rt.block_on(asyn::update_multiple_torrents_in_parallel::<EntryMutexTokio>(&rt, 10))
rt.block_on(asyn::update_multiple_torrents_in_parallel::<MutexTokio>(&rt, 10))
);
}
}
4 changes: 3 additions & 1 deletion src/core/databases/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ use self::error::Error;
use crate::core::auth::{self, Key};
use crate::shared::bit_torrent::info_hash::InfoHash;

pub type PersistentTorrents = Vec<(InfoHash, u32)>;

struct Builder<T>
where
T: Database,
Expand Down Expand Up @@ -125,7 +127,7 @@ pub trait Database: Sync + Send {
/// # Errors
///
/// Will return `Err` if unable to load.
async fn load_persistent_torrents(&self) -> Result<Vec<(InfoHash, u32)>, Error>;
async fn load_persistent_torrents(&self) -> Result<PersistentTorrents, Error>;

/// It saves the torrent metrics data into the database.
///
Expand Down
Loading

0 comments on commit 1025125

Please sign in to comment.