about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBenedikt Peetz <benedikt.peetz@b-peetz.de>2024-08-22 14:19:42 +0200
committerBenedikt Peetz <benedikt.peetz@b-peetz.de>2024-08-22 14:19:42 +0200
commit72acfb93627918f6fc9c68a5bf5b4ecf34d07a23 (patch)
treee59a493e573b05a4fc5a09f3c6d2702635410bec
parentperf(raw_update.py)!: Don't fetch entries that are already in the database (diff)
downloadyt-72acfb93627918f6fc9c68a5bf5b4ecf34d07a23.tar.gz
yt-72acfb93627918f6fc9c68a5bf5b4ecf34d07a23.zip
test(benches/update): Init
-rw-r--r--.envrc1
-rwxr-xr-xbench/update.sh21
-rw-r--r--src/app.rs8
-rw-r--r--src/cli.rs4
-rw-r--r--src/download/mod.rs24
-rw-r--r--src/main.rs6
6 files changed, 46 insertions, 18 deletions
diff --git a/.envrc b/.envrc
index 4f5b95e..493fa32 100644
--- a/.envrc
+++ b/.envrc
@@ -15,5 +15,6 @@ use flake
 PATH_add ./target/debug
 PATH_add ./target/release
 PATH_add ./target/profiling
+PATH_add ./python_update
 
 export PYO3_PYTHON=python3
diff --git a/bench/update.sh b/bench/update.sh
new file mode 100755
index 0000000..d4799bc
--- /dev/null
+++ b/bench/update.sh
@@ -0,0 +1,21 @@
+#!/usr/bin/env sh
+
+root="$(git rev-parse --show-toplevel)"
+cd "$root" || {
+    echo "There seems to be no root?" 2>&1
+    exit 1
+}
+
+db_dir="$(mktemp -d)"
+db="$db_dir/db.sqlite"
+
+[ "$1" = "--keep" ] || cargo build --profile=profiling
+
+./target/profiling/yt --db-path "$db" subscriptions add --name 'Kurzgesagt – In a Nutshell - Videos' 'https://www.youtube.com/@kurzgesagt/videos/'
+./target/profiling/yt --db-path "$db" update --max-backlog 20 --subscriptions="Kurzgesagt – In a Nutshell - Videos"
+
+hyperfine --show-output --max-runs 5 --min-runs 3 -- "./target/profiling/yt --db-path '$db' update --max-backlog 20 --subscriptions='Kurzgesagt – In a Nutshell - Videos'"
+
+rm "$db"
+rm --directory "$db_dir"
+# vim: ft=sh
diff --git a/src/app.rs b/src/app.rs
index 14b85a3..f956251 100644
--- a/src/app.rs
+++ b/src/app.rs
@@ -8,19 +8,17 @@
 // You should have received a copy of the License along with this program.
 // If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.
 
+use std::path::PathBuf;
+
 use anyhow::{Context, Result};
 use sqlx::{query, sqlite::SqliteConnectOptions, SqlitePool};
 
-use crate::constants;
-
 pub struct App {
     pub database: SqlitePool,
 }
 
 impl App {
-    pub async fn new() -> Result<Self> {
-        let db_name = constants::database()?;
-
+    pub async fn new(db_name: PathBuf) -> Result<Self> {
         let options = SqliteConnectOptions::new()
             .filename(db_name)
             .optimize_on_close(true, None)
diff --git a/src/cli.rs b/src/cli.rs
index 8f9d605..f3f4b7e 100644
--- a/src/cli.rs
+++ b/src/cli.rs
@@ -31,6 +31,10 @@ pub struct CliArgs {
     #[arg(long="verbose", short = 'v', action = ArgAction::Count)]
     pub verbosity: u8,
 
+    /// Set the path to the videos.db. Otherwise use the default location
+    #[arg(long, short)]
+    pub db_path: Option<PathBuf>,
+
     /// Silence all output
     #[arg(long, short = 'q')]
     pub quiet: bool,
diff --git a/src/download/mod.rs b/src/download/mod.rs
index 4431d3e..3785876 100644
--- a/src/download/mod.rs
+++ b/src/download/mod.rs
@@ -8,7 +8,7 @@
 // You should have received a copy of the License along with this program.
 // If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.
 
-use std::time::Duration;
+use std::{sync::Arc, time::Duration};
 
 use crate::{
     app::App,
@@ -34,14 +34,11 @@ pub struct CurrentDownload {
 }
 
 impl CurrentDownload {
-    fn new_from_video(video: Video) -> Self {
+    fn new_from_video(app: Arc<App>, video: Video) -> Self {
         let extractor_hash = video.extractor_hash.clone();
 
         let task_handle = tokio::spawn(async move {
-            // FIXME: Remove this app reconstruction <2024-07-29>
-            let new_app = App::new().await?;
-
-            Downloader::actually_cache_video(&new_app, &video)
+            Downloader::actually_cache_video(&app, &video)
                 .await
                 .with_context(|| format!("Failed to cache video: '{}'", video.title))?;
             Ok(())
@@ -69,7 +66,7 @@ impl Downloader {
     /// This Downloader will periodically check if the database has changed, and then also
     /// change which videos it downloads.
     /// This will run, until the database doesn't contain any watchable videos
-    pub async fn consume(&mut self, app: &App) -> Result<()> {
+    pub async fn consume(&mut self, app: Arc<App>, max_cache_size: u64) -> Result<()> {
         while let Some(next_video) = get_next_uncached_video(app).await? {
             if let Some(_) = &self.current_download {
                 let current_download = self.current_download.take().expect("Is Some");
@@ -82,19 +79,23 @@ impl Downloader {
                 if next_video.extractor_hash != current_download.extractor_hash {
                     info!(
                     "Noticed, that the next video is not the video being downloaded, replacing it ('{}' vs. '{}')!",
-                        next_video.extractor_hash.into_short_hash(app).await?, current_download.extractor_hash.into_short_hash(app).await?
+                        next_video.extractor_hash.into_short_hash(&app).await?, current_download.extractor_hash.into_short_hash(&app).await?
                     );
 
                     // Replace the currently downloading video
                     current_download.task_handle.abort();
 
-                    let new_current_download = CurrentDownload::new_from_video(next_video);
+                    let new_current_download =
+                        CurrentDownload::new_from_video(Arc::clone(&app), next_video);
 
                     self.current_download = Some(new_current_download);
                 } else {
                     debug!(
                         "Currently downloading '{}'",
-                        current_download.extractor_hash.into_short_hash(app).await?
+                        current_download
+                            .extractor_hash
+                            .into_short_hash(&app)
+                            .await?
                     );
                     // Reset the taken value
                     self.current_download = Some(current_download);
@@ -105,7 +106,8 @@ impl Downloader {
                     "No video is being downloaded right now, setting it to '{}'",
                     next_video.title
                 );
-                let new_current_download = CurrentDownload::new_from_video(next_video);
+                let new_current_download =
+                    CurrentDownload::new_from_video(Arc::clone(&app), next_video);
                 self.current_download = Some(new_current_download);
             }
 
diff --git a/src/main.rs b/src/main.rs
index cfd6adc..c223140 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -53,7 +53,7 @@ async fn main() -> Result<()> {
         .init()
         .expect("Let's just hope that this does not panic");
 
-    let app = App::new().await?;
+    let app = App::new(args.db_path.unwrap_or(constants::database()?)).await?;
 
     match args.command.unwrap_or(Command::default()) {
         Command::Download { force } => {
@@ -61,7 +61,9 @@ async fn main() -> Result<()> {
                 invalidate(&app, true).await?;
             }
 
-            download::Downloader::new().consume(&app).await?;
+            download::Downloader::new()
+                .consume(Arc::new(app), max_cache_size)
+                .await?;
         }
         Command::Select { cmd } => {
             let cmd = cmd.unwrap_or(SelectCommand::default());