From 4823283820dc84813f3ae45bb9640109a0867099 Mon Sep 17 00:00:00 2001 From: Olivier 'reivilibre Date: Sat, 19 Mar 2022 11:18:12 +0000 Subject: [PATCH] Add Tokio benchmark for memory overhead of tasks --- .gitignore | 4 ++++ Cargo.toml | 11 +++++++++ src/bin/tokio_task_overhead.rs | 43 ++++++++++++++++++++++++++++++++++ src/lib.rs | 8 +++++++ 4 files changed, 66 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 src/bin/tokio_task_overhead.rs create mode 100644 src/lib.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7a53b1c --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/target +Cargo.lock + +.idea \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..0a58d1f --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "benches" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +tokio = { version = "1.17.0", features = ["full"] } +probes = "0.4.1" +linreg = "0.2.0" diff --git a/src/bin/tokio_task_overhead.rs b/src/bin/tokio_task_overhead.rs new file mode 100644 index 0000000..03da5e2 --- /dev/null +++ b/src/bin/tokio_task_overhead.rs @@ -0,0 +1,43 @@ +use probes::process_memory::current_rss; +use std::sync::Arc; +use tokio::sync::Semaphore; + +pub const TASKS_PER_DATA_POINT: usize = 1024; +/// The first data point is 0 tasks +pub const DATA_POINTS: usize = 16385; + +/// Calculates the memory overhead of Tokio tasks. +/// +/// Experimental results as of 2022-03-19: +/// Debug mode: 0.188 kiB/task; initial: 3843.6 kiB +/// Release mode: 0.188 kiB/task; initial: 2696.2 kiB +#[tokio::main] +pub async fn main() { + let mut tasks = vec![0f64; DATA_POINTS]; + let mut rsses = vec![0f64; DATA_POINTS]; + let mut num_tasks = 0; + + let semaphore = Arc::new(Semaphore::new(0)); + + for data_point in 0..DATA_POINTS { + if data_point != 0 { + for _ in 0..TASKS_PER_DATA_POINT { + let semaphore = semaphore.clone(); + tokio::spawn(async move { + let permit = semaphore.acquire().await.unwrap(); + drop(permit); + }); + } + + num_tasks += TASKS_PER_DATA_POINT; + } + + rsses[data_point] = current_rss().unwrap() as f64; + tasks[data_point] = num_tasks as f64; + } + + let (slope, intercept) = linreg::linear_regression::<_, _, f64>(&tasks, &rsses).unwrap(); + //eprintln!("{:?}", rsses); + eprintln!("{:.3} kiB/task; initial: {:.1} kiB", slope, intercept); + semaphore.add_permits(16); +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..1b4a90c --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,8 @@ +#[cfg(test)] +mod tests { + #[test] + fn it_works() { + let result = 2 + 2; + assert_eq!(result, 4); + } +}