Skip to content

Commit

Permalink
Overhaul performance measuring
Browse files Browse the repository at this point in the history
  • Loading branch information
diondokter committed Feb 6, 2024
1 parent f17a740 commit 60267b3
Show file tree
Hide file tree
Showing 6 changed files with 326 additions and 91 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ keywords = ["no_std", "embedded", "flash", "storage"]
embedded-storage-async = "0.4.1"
defmt = { version = "0.3", optional = true }
futures = { version = "0.3.30", features = ["executor"], optional = true }
approx = { version = "0.5.1", optional = true }

[dev-dependencies]
approx = "0.5.1"
Expand All @@ -23,4 +24,4 @@ futures-test = "0.3.30"

[features]
defmt = ["dep:defmt"]
_test = ["dep:futures"]
_test = ["dep:futures", "dep:approx"]
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,16 @@ Instead, we can optionally store some state in ram.

These numbers are taken from the test cases in the cache module:

| Name | Map # flash reads | Queue # flash reads |
| -------------: | ----------------: | ------------------: |
| NoCache | 100% | 100% |
| PageStateCache | 77% | 51% |
| Name | Map # flash reads | Map flash bytes read | Queue # flash reads | Queue flash bytes read |
| -------------: | ----------------: | -------------------: | ------------------: | ---------------------: |
| NoCache | 100% | 100% | 100% | 100% |
| PageStateCache | 77% | 97% | 51% | 90% |

***Note:** These are the number of reads, not the amount of bytes.*
#### Takeaways

- PageStateCache
- Mostly tackles number of reads
- Very cheap in RAM, so easy win

## Inner workings

Expand Down
54 changes: 44 additions & 10 deletions src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ mod queue_tests {
use core::ops::Range;

use crate::{
mock_flash::{self, WriteCountCheck},
mock_flash::{self, FlashStatsResult, WriteCountCheck},
queue::{peek, pop, push},
AlignedBuf,
};
Expand All @@ -190,23 +190,40 @@ mod queue_tests {

#[test]
async fn no_cache() {
assert_eq!(run_test(&mut NoCache::new()).await, (594934, 6299, 146));
assert_eq!(
run_test(&mut NoCache::new()).await,
FlashStatsResult {
erases: 146,
reads: 594934,
writes: 6299,
bytes_read: 2766058,
bytes_written: 53299
}
);
}

#[test]
async fn page_state_cache() {
assert_eq!(
run_test(&mut PageStateCache::<NUM_PAGES>::new()).await,
(308740, 6299, 146)
FlashStatsResult {
erases: 146,
reads: 308740,
writes: 6299,
bytes_read: 2479864,
bytes_written: 53299
}
);
}

async fn run_test(mut cache: impl CacheImpl) -> (u32, u32, u32) {
async fn run_test(mut cache: impl CacheImpl) -> FlashStatsResult {
let mut flash =
mock_flash::MockFlashBase::<NUM_PAGES, 1, 256>::new(WriteCountCheck::Twice, None, true);
const FLASH_RANGE: Range<u32> = 0x00..0x400;
let mut data_buffer = AlignedBuf([0; 1024]);

let start_snapshot = flash.stats_snapshot();

for i in 0..LOOP_COUNT {
println!("{i}");
let data = vec![i as u8; i % 20 + 1];
Expand Down Expand Up @@ -243,7 +260,7 @@ mod queue_tests {
println!("DONE");
}

(flash.reads, flash.writes, flash.erases)
start_snapshot.compare_to(flash.stats_snapshot())
}
}

Expand All @@ -253,7 +270,7 @@ mod map_tests {

use crate::{
map::{fetch_item, store_item, StorageItem},
mock_flash::{self, WriteCountCheck},
mock_flash::{self, FlashStatsResult, WriteCountCheck},
AlignedBuf,
};

Expand All @@ -264,14 +281,29 @@ mod map_tests {

#[test]
async fn no_cache() {
assert_eq!(run_test(&mut NoCache::new()).await, (224161, 5201, 198));
assert_eq!(
run_test(&mut NoCache::new()).await,
FlashStatsResult {
erases: 198,
reads: 224161,
writes: 5201,
bytes_read: 1770974,
bytes_written: 50401
}
);
}

#[test]
async fn page_state_cache() {
assert_eq!(
run_test(&mut PageStateCache::<NUM_PAGES>::new()).await,
(172831, 5201, 198)
FlashStatsResult {
erases: 198,
reads: 172831,
writes: 5201,
bytes_read: 1719644,
bytes_written: 50401
}
);
}

Expand Down Expand Up @@ -343,7 +375,7 @@ mod map_tests {
}
}

async fn run_test(mut cache: impl CacheImpl) -> (u32, u32, u32) {
async fn run_test(mut cache: impl CacheImpl) -> FlashStatsResult {
let mut cache = cache.inner();

let mut flash =
Expand All @@ -355,6 +387,8 @@ mod map_tests {
11, 13, 6, 13, 13, 10, 2, 3, 5, 36, 1, 65, 4, 6, 1, 15, 10, 7, 3, 15, 9, 3, 4, 5,
];

let start_snapshot = flash.stats_snapshot();

for _ in 0..100 {
for i in 0..24 {
let item = MockStorageItem {
Expand Down Expand Up @@ -385,6 +419,6 @@ mod map_tests {
}
}

(flash.reads, flash.writes, flash.erases)
start_snapshot.compare_to(flash.stats_snapshot())
}
}
7 changes: 3 additions & 4 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,8 @@ mod tests {

let mut data_buffer = AlignedBuf([0; 128]);

let start_snapshot = flash.stats_snapshot();

let item = fetch_item::<MockStorageItem, _>(
&mut flash,
flash_range.clone(),
Expand Down Expand Up @@ -900,10 +902,7 @@ mod tests {
assert_eq!(item.value, vec![(i % 10) as u8 * 2; (i % 10) as usize]);
}

println!(
"Erases: {}, reads: {}, writes: {}",
flash.erases, flash.reads, flash.writes
);
println!("{:?}", start_snapshot.compare_to(flash.stats_snapshot()),);
}

#[test]
Expand Down
Loading

0 comments on commit 60267b3

Please sign in to comment.