Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
fornwall committed Sep 21, 2023
1 parent f6787d7 commit c1a5810
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "allocation-counter"
version = "0.6.0"
version = "0.7.0"
authors = ["Fredrik Fornwall <fredrik@fornwall.net>"]
categories = ["development-tools", "memory-management"]
description = "Count the number of memory allocation of some code."
Expand Down
25 changes: 22 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*!
This crate provides a method to count and test the number of allocations while running some code.
This crate provides a method to measure memory allocations while running some code.
# Example
# Usage
Add as a dependency - since including the trait replaces the global memory allocator, you most likely want it gated behind a feature.
```toml
Expand All @@ -27,14 +27,33 @@ Tests can be conditional on the feature:
The test code itself could look like:
```
```no_run
# fn code_that_should_not_allocate() {}
# fn code_that_should_allocate_a_little() {}
# fn external_code_that_should_not_be_tested() {}
// Verify that no memory allocations are made:
let info = allocation_counter::measure(|| {
code_that_should_not_allocate();
});
assert_eq!(info.count_total, 0);
// Let's use a case where some allocations are expected.
let info = allocation_counter::measure(|| {
code_that_should_allocate_a_little();
});
// Using a lower bound can help track behaviour over time:
assert!((500..600).contains(&info.count_total));
assert!((10_000..20_000).contains(&info.bytes_total));
// Limit peak memory usage:
assert!((100..200).contains(&info.count_max));
assert!((1_000..2_000).contains(&info.bytes_max));
// We don't want any leaks:
assert_eq!(0, info.count_current);
assert_eq!(0, info.bytes_current);
// It's possible to opt out of counting allocations
// for certain parts of the code flow:
let info = allocation_counter::measure(|| {
Expand Down

0 comments on commit c1a5810

Please sign in to comment.