From f5ec740c359683b77fd60d71b14bb70b437b80a2 Mon Sep 17 00:00:00 2001 From: Daksh Kaushik Date: Mon, 25 Mar 2024 01:34:57 +0530 Subject: [PATCH] resolve the problems --- .../{asynctest => async}/Cargo.toml | 3 +- .../runtime-benchmarks/async/src/main.rs | 47 +++++++++++++++++ .../runtime-benchmarks/asynctest/src/main.rs | 50 ------------------- .../runtime-benchmarks/asynctest/src/poem.txt | 24 --------- .../asynctest/src/poem2.txt | 9 ---- 5 files changed, 49 insertions(+), 84 deletions(-) rename collector/runtime-benchmarks/{asynctest => async}/Cargo.toml (90%) create mode 100644 collector/runtime-benchmarks/async/src/main.rs delete mode 100644 collector/runtime-benchmarks/asynctest/src/main.rs delete mode 100644 collector/runtime-benchmarks/asynctest/src/poem.txt delete mode 100644 collector/runtime-benchmarks/asynctest/src/poem2.txt diff --git a/collector/runtime-benchmarks/asynctest/Cargo.toml b/collector/runtime-benchmarks/async/Cargo.toml similarity index 90% rename from collector/runtime-benchmarks/asynctest/Cargo.toml rename to collector/runtime-benchmarks/async/Cargo.toml index f4e75f4bf..ff49a15f4 100644 --- a/collector/runtime-benchmarks/asynctest/Cargo.toml +++ b/collector/runtime-benchmarks/async/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "asynctest" +name = "async" version = "0.1.0" edition = "2021" @@ -8,5 +8,6 @@ edition = "2021" [dependencies] benchlib = { path = "../../benchlib" } tokio = { version = "1.0", features = ["full"] } +flate2 = "1" [workspace] \ No newline at end of file diff --git a/collector/runtime-benchmarks/async/src/main.rs b/collector/runtime-benchmarks/async/src/main.rs new file mode 100644 index 000000000..22bfea038 --- /dev/null +++ b/collector/runtime-benchmarks/async/src/main.rs @@ -0,0 +1,47 @@ +use benchlib::benchmark::run_benchmark_group; +use std::time::Instant; +use tokio::runtime::Runtime; +use std::fs::File; +use std::io::{self, BufRead, BufReader}; +use flate2::read::GzDecoder; + +async fn total_char_count(reader: BufReader>,x: &mut usize) { + for line in reader.lines() { + *x += line_char_count(line.expect("invalid character")).await; + } +} + +async fn line_char_count(line: String) -> usize { + let line_count = line.chars().count(); + line_count +} + +async fn async_operation() -> (usize, u128) { + let start_time = Instant::now(); + + let file = File::open("./collector/runtime-benchmarks/data/sherlock.txt.gz").expect("can't read a file"); + let decoder = GzDecoder::new(file); + let reader2 = BufReader::new(decoder); + let mut total_char = 0; + total_char_count(reader2, &mut total_char).await; + + let end_time = Instant::now(); + let duration = end_time - start_time; + (total_char,duration.as_millis()) +} + +fn main() { + run_benchmark_group(|group| { + group.register_benchmark("Async", || { + // This closure should prepare data that will be needed for the benchmark (if any), + // and then return a closure that will actually be benchmarked/profiled. + // Create a Tokio runtime + let rt = Runtime::new().unwrap(); + move || { + rt.block_on(async_operation()); + } + }); + }); +} + + diff --git a/collector/runtime-benchmarks/asynctest/src/main.rs b/collector/runtime-benchmarks/asynctest/src/main.rs deleted file mode 100644 index 1838c5405..000000000 --- a/collector/runtime-benchmarks/asynctest/src/main.rs +++ /dev/null @@ -1,50 +0,0 @@ -use benchlib::benchmark::run_benchmark_group; -use std::time::Instant; -use tokio::runtime::Runtime; -use std::fs::File; -use std::io::{self, BufRead, BufReader}; - -async fn read_the_textfile(file_path: &str) -> io::Result { - let file = File::open(file_path)?; - let reader = BufReader::new(file); - let mut total_characters = 0; - for line in reader.lines() { - let line = line?; - total_characters += line.expect("invalid character").chars().count(); - } - - Ok(total_characters) -} - -async fn async_operation() { - let f1 = read_the_textfile("./collector/runtime-benchmarks/Async2/src/poem.txt").await.expect("Can't read file"); - let f2 = read_the_textfile("./collector/runtime-benchmarks/Async2/src/poem2.txt").await.expect("Can't read file"); - let total_char_count =f1+f2; - -} - -fn main() { - run_benchmark_group(|group| { - group.register_benchmark("Async", || { - // This closure should prepare data that will be needed for the benchmark (if any), - // and then return a closure that will actually be benchmarked/profiled. - - // Create a Tokio runtime - let rt = Runtime::new().unwrap(); - let start_time = Instant::now(); - - for _ in 0..1000 { - rt.block_on(async_operation()); - } - - let end_time = Instant::now(); - - let duration = end_time - start_time; - move || { - duration - } - }); - }); -} - - diff --git a/collector/runtime-benchmarks/asynctest/src/poem.txt b/collector/runtime-benchmarks/asynctest/src/poem.txt deleted file mode 100644 index 1a08d214a..000000000 --- a/collector/runtime-benchmarks/asynctest/src/poem.txt +++ /dev/null @@ -1,24 +0,0 @@ -In the realm where code weaves tales bold, -A language emerged, its story untold. -With the hue of iron, sturdy and grand, -Rust stood firm on digital land. - -In the hearts of developers, it found its place, -A tool for crafting with elegance and grace. -With syntax refined, like a poet's pen, -It beckons creators, both women and men. - -In the crucible of time, it was forged with care, -To conquer challenges, it did dare. -With lifetimes borrowed from legends of old, -Rust marches forth, fearless and bold. - -It guards against errors with a vigilant eye, -Through ownership and borrowing, it helps us fly. -No more shall we fear the rust of old, -For Rust, the language, is a sight to behold. - -With concurrency, it dances in harmony's tune, -Asynchronous rhythms, like stars 'neath the moon. -In lifetimes and traits, its essence imbued, -A symphony of traits, in harmony renewed. diff --git a/collector/runtime-benchmarks/asynctest/src/poem2.txt b/collector/runtime-benchmarks/asynctest/src/poem2.txt deleted file mode 100644 index 6c20fa4b8..000000000 --- a/collector/runtime-benchmarks/asynctest/src/poem2.txt +++ /dev/null @@ -1,9 +0,0 @@ -O Rust, thou art a masterpiece divine, -In the kingdom of languages, you brightly shine. -With borrow checkers and lifetimes in hand, -You lead us forward, across the digital land. - -So let us raise a toast to Rust, our guide, -In its shelter, our dreams shall abide. -For in its embrace, we find our worth, -Rust, the language, the pride of Earth. \ No newline at end of file