-
Notifications
You must be signed in to change notification settings - Fork 721
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: memory leak on apple native OCR #278
- Loading branch information
1 parent
a957411
commit b0c023f
Showing
14 changed files
with
293 additions
and
191 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
use criterion::{criterion_group, criterion_main, Criterion}; | ||
use image::GenericImageView; | ||
use memory_stats::memory_stats; | ||
use screenpipe_vision::perform_ocr_apple; | ||
use std::path::PathBuf; | ||
|
||
fn bytes_to_mb(bytes: usize) -> f64 { | ||
bytes as f64 / (1024.0 * 1024.0) | ||
} | ||
|
||
fn bytes_to_gb(bytes: usize) -> f64 { | ||
bytes as f64 / (1024.0 * 1024.0 * 1024.0) | ||
} | ||
|
||
fn apple_ocr_benchmark(c: &mut Criterion) { | ||
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); | ||
path.push("tests"); | ||
path.push("testing_OCR.png"); | ||
|
||
let image = image::open(&path).expect("Failed to open image"); | ||
println!("Image dimensions: {:?}", image.dimensions()); | ||
|
||
let mut group = c.benchmark_group("apple_ocr"); | ||
group.sample_size(100); // Increased sample size | ||
group.measurement_time(std::time::Duration::from_secs(60)); // Run for at least 60 seconds | ||
group.bench_function("perform_ocr_apple", |b| { | ||
b.iter_custom(|iters| { | ||
let start = std::time::Instant::now(); | ||
let mut initial_memory = 0; | ||
let mut final_memory = 0; | ||
let mut max_memory = 0; | ||
|
||
for i in 0..iters { | ||
if i == 0 { | ||
if let Some(usage) = memory_stats() { | ||
initial_memory = usage.physical_mem; | ||
max_memory = initial_memory; | ||
} | ||
} | ||
|
||
let result = perform_ocr_apple(&image); | ||
assert!( | ||
result.contains("receiver_count"), | ||
"OCR failed: {:?}", | ||
result | ||
); | ||
|
||
if let Some(usage) = memory_stats() { | ||
final_memory = usage.physical_mem; | ||
max_memory = max_memory.max(final_memory); | ||
} | ||
|
||
if i % 10 == 0 { | ||
println!( | ||
"Iteration {}: Current memory usage: {:.2} MB ({:.3} GB)", | ||
i, | ||
bytes_to_mb(final_memory), | ||
bytes_to_gb(final_memory) | ||
); | ||
} | ||
} | ||
|
||
println!( | ||
"Initial memory usage: {:.2} MB ({:.3} GB)", | ||
bytes_to_mb(initial_memory), | ||
bytes_to_gb(initial_memory) | ||
); | ||
println!( | ||
"Final memory usage: {:.2} MB ({:.3} GB)", | ||
bytes_to_mb(final_memory), | ||
bytes_to_gb(final_memory) | ||
); | ||
println!( | ||
"Max memory usage: {:.2} MB ({:.3} GB)", | ||
bytes_to_mb(max_memory), | ||
bytes_to_gb(max_memory) | ||
); | ||
println!( | ||
"Total memory difference: {:.2} MB ({:.3} GB)", | ||
bytes_to_mb(final_memory - initial_memory), | ||
bytes_to_gb(final_memory - initial_memory) | ||
); | ||
println!( | ||
"Max memory difference: {:.2} MB ({:.3} GB)", | ||
bytes_to_mb(max_memory - initial_memory), | ||
bytes_to_gb(max_memory - initial_memory) | ||
); | ||
|
||
start.elapsed() | ||
}); | ||
}); | ||
group.finish(); | ||
} | ||
|
||
criterion_group!(benches, apple_ocr_benchmark); | ||
criterion_main!(benches); |
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.