commit 4823283820dc84813f3ae45bb9640109a0867099 Author: Olivier 'reivilibre Date: Sat Mar 19 11:18:12 2022 +0000 Add Tokio benchmark for memory overhead of tasks 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); + } +}