Skip to content

Commit 6bb407f

Browse files
committed
add test for memory usage in worse-case scenario
1 parent c71d16e commit 6bb407f

File tree

6 files changed

+50
-0
lines changed

6 files changed

+50
-0
lines changed

Cargo.lock

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gix-config/tests/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ publish = false
1414
name = "config"
1515
path = "config.rs"
1616

17+
[[test]]
18+
name = "mem"
19+
path = "mem.rs"
20+
1721
[dev-dependencies]
1822
gix-config = { path = ".."}
1923
gix-testtools = { path = "../../tests/tools"}
@@ -23,3 +27,6 @@ gix-path = { path = "../../gix-path" }
2327
gix-sec = { path = "../../gix-sec" }
2428
serial_test = { version = "2.0.0", default-features = false }
2529
bstr = { version = "1.3.0", default-features = false, features = ["std"] }
30+
31+
bytesize = "1.3.0"
32+
cap = { version = "0.1.2", features = ["stats"] }

gix-config/tests/config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
pub use gix_testtools::Result;
22

33
mod file;
4+
mod mem;
45
mod parse;
56
mod source;
67
mod value;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.config -text -eof
Binary file not shown.

gix-config/tests/mem.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
use bytesize::ByteSize;
2+
use cap::Cap;
3+
use std::alloc;
4+
use std::time::Instant;
5+
6+
#[global_allocator]
7+
static ALLOCATOR: Cap<alloc::System> = Cap::new(alloc::System, usize::max_value());
8+
9+
#[test]
10+
fn usage() {
11+
let before = ALLOCATOR.allocated();
12+
let start = Instant::now();
13+
let config = gix_config::File::from_bytes_no_includes(
14+
include_bytes!("fixtures/fuzzed/mem-amplification.config"),
15+
gix_config::file::Metadata::from(gix_config::Source::User),
16+
Default::default(),
17+
)
18+
.unwrap();
19+
let after = ALLOCATOR.allocated();
20+
let used = after - before;
21+
let elapsed = start.elapsed().as_secs_f32();
22+
eprintln!(
23+
"used mem: {}B for {} sections, took {elapsed:.02}s [total-mem: {total}, peak-mem: {peak}]",
24+
ByteSize(used as u64),
25+
config.sections().count(),
26+
total = ByteSize(ALLOCATOR.total_allocated() as u64),
27+
peak = ByteSize(ALLOCATOR.max_allocated() as u64),
28+
);
29+
assert!(
30+
used < 60 * 1024 * 1024,
31+
"we should now start using more memory than anticipated, to keep mem-amplification low"
32+
);
33+
}

0 commit comments

Comments
 (0)