From 201ed4790143d9ffe0ef3761c1a985adbbcbb94b Mon Sep 17 00:00:00 2001 From: "John T. Wodder II" Date: Thu, 28 Mar 2024 13:37:25 -0400 Subject: [PATCH] Log current memory usage before & after handling each request --- CHANGELOG.md | 1 + Cargo.lock | 11 +++++++++++ Cargo.toml | 1 + src/main.rs | 21 +++++++++++++++++++-- 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 604645d..1669fc8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ In Development -------------- - Set `Access-Control-Allow-Origin: *` header in all responses +- Log current memory usage before & after handling each request v0.3.0 (2024-03-15) ------------------- diff --git a/Cargo.lock b/Cargo.lock index 0f7319a..ab25ae5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -860,6 +860,7 @@ dependencies = [ "humansize", "indoc", "itertools", + "memory-stats", "moka", "percent-encoding", "pretty_assertions", @@ -1511,6 +1512,16 @@ version = "2.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149" +[[package]] +name = "memory-stats" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34f79cf9964c5c9545493acda1263f1912f8d2c56c8a2ffee2606cb960acaacc" +dependencies = [ + "libc", + "winapi", +] + [[package]] name = "mime" version = "0.3.17" diff --git a/Cargo.toml b/Cargo.toml index 3413912..6a0884d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,6 +30,7 @@ futures-util = "0.3.30" humansize = "2.1.3" indoc = "2.0.5" itertools = "0.12.1" +memory-stats = "1.1.0" moka = { version = "0.12.5", features = ["future"] } percent-encoding = "2.3.1" reqwest = { version = "0.11.26", default-features = false, features = ["json", "rustls-tls-native-roots"] } diff --git a/src/main.rs b/src/main.rs index ac0cc3e..0faa5aa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -114,8 +114,13 @@ async fn run() -> anyhow::Result<()> { "/", service_fn(move |req: Request| { let dav = Arc::clone(&dav); - // Box the large future: - async move { Box::pin(dav.handle_request(req)).await } + async move { + log_memory(); + // Box the large future: + let r = Box::pin(dav.handle_request(req)).await; + log_memory(); + r + } }), ) .layer(middleware::from_fn(handle_head)) @@ -147,3 +152,15 @@ async fn handle_head(method: Method, mut request: Request, next: Next) -> next.run(request).await } } + +fn log_memory() { + if let Some(stats) = memory_stats::memory_stats() { + tracing::info!( + "Current memory usage: {} physical, {} virtual", + stats.physical_mem, + stats.virtual_mem, + ); + } else { + tracing::info!("Failed to get current memory usage"); + } +}