Skip to content

Commit

Permalink
add test for memory usage in worse-case scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Jan 5, 2024
1 parent c71d16e commit 6bb407f
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Cargo.lock

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

7 changes: 7 additions & 0 deletions gix-config/tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ publish = false
name = "config"
path = "config.rs"

[[test]]
name = "mem"
path = "mem.rs"

[dev-dependencies]
gix-config = { path = ".."}
gix-testtools = { path = "../../tests/tools"}
Expand All @@ -23,3 +27,6 @@ gix-path = { path = "../../gix-path" }
gix-sec = { path = "../../gix-sec" }
serial_test = { version = "2.0.0", default-features = false }
bstr = { version = "1.3.0", default-features = false, features = ["std"] }

bytesize = "1.3.0"
cap = { version = "0.1.2", features = ["stats"] }
1 change: 1 addition & 0 deletions gix-config/tests/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub use gix_testtools::Result;

mod file;
mod mem;
mod parse;
mod source;
mod value;
1 change: 1 addition & 0 deletions gix-config/tests/fixtures/fuzzed/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.config -text -eof
Binary file not shown.
33 changes: 33 additions & 0 deletions gix-config/tests/mem.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use bytesize::ByteSize;
use cap::Cap;
use std::alloc;
use std::time::Instant;

#[global_allocator]
static ALLOCATOR: Cap<alloc::System> = Cap::new(alloc::System, usize::max_value());

#[test]
fn usage() {
let before = ALLOCATOR.allocated();
let start = Instant::now();
let config = gix_config::File::from_bytes_no_includes(
include_bytes!("fixtures/fuzzed/mem-amplification.config"),
gix_config::file::Metadata::from(gix_config::Source::User),
Default::default(),
)
.unwrap();
let after = ALLOCATOR.allocated();
let used = after - before;
let elapsed = start.elapsed().as_secs_f32();
eprintln!(
"used mem: {}B for {} sections, took {elapsed:.02}s [total-mem: {total}, peak-mem: {peak}]",
ByteSize(used as u64),
config.sections().count(),
total = ByteSize(ALLOCATOR.total_allocated() as u64),
peak = ByteSize(ALLOCATOR.max_allocated() as u64),
);
assert!(
used < 60 * 1024 * 1024,
"we should now start using more memory than anticipated, to keep mem-amplification low"
);
}

0 comments on commit 6bb407f

Please sign in to comment.