Skip to content

Commit

Permalink
feat(fs-index): add benchmarks for Resourceindex::update_one
Browse files Browse the repository at this point in the history
Signed-off-by: Tarek <tareknaser360@gmail.com>
  • Loading branch information
tareknaser committed Aug 27, 2024
1 parent 91645e2 commit 6a4d591
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 8 deletions.
1 change: 1 addition & 0 deletions fs-index/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
49 changes: 49 additions & 0 deletions fs-index/benches/resource_index_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Crc32> =
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();
}

Expand Down
12 changes: 4 additions & 8 deletions fs-index/src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,16 @@ type IndexedPaths = HashSet<Timestamped<PathBuf>>;
/// #### 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
/// ID.
/// - [`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
Expand All @@ -97,7 +93,7 @@ type IndexedPaths = HashSet<Timestamped<PathBuf>>;
/// 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<Crc32> = ResourceIndex::build(root_path).expect("Failed to build index");
Expand Down

0 comments on commit 6a4d591

Please sign in to comment.