From 6a4d5918b6dc7ae0340a4878102cb1e3509be810 Mon Sep 17 00:00:00 2001 From: Tarek Date: Tue, 27 Aug 2024 20:29:23 +0300 Subject: [PATCH] feat(fs-index): add benchmarks for Resourceindex::update_one Signed-off-by: Tarek --- fs-index/README.md | 1 + fs-index/benches/resource_index_benchmark.rs | 49 ++++++++++++++++++++ fs-index/src/index.rs | 12 ++--- 3 files changed, 54 insertions(+), 8 deletions(-) diff --git a/fs-index/README.md b/fs-index/README.md index 9253375..e9862d6 100644 --- a/fs-index/README.md +++ b/fs-index/README.md @@ -10,6 +10,7 @@ The most important struct in this crate is `ResourceIndex` which comes with: - **Reactive API** - `update_all`: Method to update the index by rescanning files and returning changes (additions/deletions). + - `update_one`: Method to update the index by rescanning a single file (addition/deletion/modification). - **Snapshot API** - `get_resources_by_id`: Query resources from the index by ID. - `get_resource_by_path`: Query a resource from the index by its path. diff --git a/fs-index/benches/resource_index_benchmark.rs b/fs-index/benches/resource_index_benchmark.rs index f6d3f21..36a8d69 100644 --- a/fs-index/benches/resource_index_benchmark.rs +++ b/fs-index/benches/resource_index_benchmark.rs @@ -84,6 +84,55 @@ fn resource_index_benchmark(c: &mut Criterion) { }); }); + // Benchmark `ResourceIndex::update_one()` + + // First, create a new temp directory specifically for the update_one + // benchmark since we will be creating new files, removing files, and + // modifying files + + let update_one_benchmarks_dir = + TempDir::with_prefix("ark-fs-index-benchmarks-update-one").unwrap(); + let update_one_benchmarks_dir = update_one_benchmarks_dir.path(); + + group.bench_function("index_update_one", |b| { + b.iter(|| { + // Clear the directory + std::fs::remove_dir_all(&update_one_benchmarks_dir).unwrap(); + std::fs::create_dir(&update_one_benchmarks_dir).unwrap(); + + // Create 5000 new files + for i in 0..5000 { + let new_file = + update_one_benchmarks_dir.join(format!("file_{}.txt", i)); + std::fs::File::create(&new_file).unwrap(); + std::fs::write(&new_file, format!("Hello, World! {}", i)) + .unwrap(); + } + let mut index: ResourceIndex = + ResourceIndex::build(black_box(&update_one_benchmarks_dir)) + .unwrap(); + + // Create a new file + let new_file = update_one_benchmarks_dir.join("new_file.txt"); + std::fs::File::create(&new_file).unwrap(); + std::fs::write(&new_file, "Hello, World from new file!").unwrap(); + + // Modify an existing file + let modified_file = update_one_benchmarks_dir.join("file_0.txt"); + std::fs::write(&modified_file, "Hello, World from modified file!") + .unwrap(); + + // Remove an existing file + let removed_file = update_one_benchmarks_dir.join("file_1.txt"); + std::fs::remove_file(&removed_file).unwrap(); + + // Update the index + let _update_result = index.update_one("new_file.txt").unwrap(); + let _update_result = index.update_one("file_0.txt").unwrap(); + let _update_result = index.update_one("file_1.txt").unwrap(); + }); + }); + group.finish(); } diff --git a/fs-index/src/index.rs b/fs-index/src/index.rs index 77f54a8..9989cbd 100644 --- a/fs-index/src/index.rs +++ b/fs-index/src/index.rs @@ -75,6 +75,8 @@ type IndexedPaths = HashSet>; /// #### Reactive API /// - [`ResourceIndex::update_all`]: Method to update the index by rescanning /// files and returning changes (additions/deletions/updates). +/// - [`ResourceIndex::update_one`]: Method to update the index for a single +/// resource. /// /// #### Snapshot API /// - [`ResourceIndex::get_resources_by_id`]: Query resources from the index by @@ -82,13 +84,7 @@ type IndexedPaths = HashSet>; /// - [`ResourceIndex::get_resource_by_path`]: Query a resource from the index /// by its path. /// -/// #### Track API -/// Allows for fine-grained control over tracking changes in the index -/// - [`ResourceIndex::track_addition`]: Track a newly added file (checks if the -/// file exists in the file system). -/// - [`ResourceIndex::track_removal`]: Track the deletion of a file (checks if -/// the file was actually deleted). -/// - [`ResourceIndex::track_modification`]: Track an update on a single file. +/// /// /// ## Examples /// ```no_run @@ -97,7 +93,7 @@ type IndexedPaths = HashSet>; /// use dev_hash::Crc32; /// /// // Define the root path -/// let root_path = Path::new("animals"); +/// let root_path = Path::new("path/to/animals"); /// /// // Build the index /// let index: ResourceIndex = ResourceIndex::build(root_path).expect("Failed to build index");